ploeh blog 2019-05-20T06:13:46+00:00 Mark Seemann danish software design https://blog.ploeh.dk Maybe catamorphism https://blog.ploeh.dk/2019/05/20/maybe-catamorphism 2019-05-20T06:04:00+00:00 Mark Seemann <div id="post"> <p> <em>The catamorphism for Maybe is just a simplification of its fold.</em> </p> <p> This article is part of an <a href="/2019/04/29/catamorphisms">article series about catamorphisms</a>. A catamorphism is a <a href="/2017/10/04/from-design-patterns-to-category-theory">universal abstraction</a> that describes how to digest a data structure into a potentially more compact value. </p> <p> This article presents the catamorphism for <a href="/2018/03/26/the-maybe-functor">Maybe</a>, as well as how to identify it. The beginning of this article presents the catamorphism in C#, with examples. The rest of the article describes how to deduce the catamorphism. This part of the article presents my work in <a href="https://www.haskell.org">Haskell</a>. Readers not comfortable with Haskell can just read the first part, and consider the rest of the article as an optional appendix. </p> <p> <em>Maybe</em> is a <a href="https://bartoszmilewski.com/2014/01/14/functors-are-containers">data container</a> that models the absence or presence of a value. <a href="/2015/11/13/null-has-no-type-but-maybe-has">Contrary to null, Maybe has a type</a>, so offers a sane and reasonable way to model that situation. </p> <h3 id="1feeee3382ff44d182e0a28a33f8f80a"> C# catamorphism <a href="#1feeee3382ff44d182e0a28a33f8f80a" title="permalink">#</a> </h3> <p> This article uses <a href="/2018/06/04/church-encoded-maybe">Church-encoded Maybe</a>. Other, <a href="/2018/03/26/the-maybe-functor">alternative implementations of Maybe are possible</a>. The catamorphism for Maybe is the <code>Match</code> method: </p> <p> <pre><span style="color:#2b91af;">TResult</span>&nbsp;Match&lt;<span style="color:#2b91af;">TResult</span>&gt;(<span style="color:#2b91af;">TResult</span>&nbsp;nothing,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;just);</pre> </p> <p> Like the <a href="/2019/05/13/peano-catamorphism">Peano catamorphism</a>, the Maybe catamorphism is a pair of a value and a function. The <code>nothing</code> value corresponds to the absence of data, whereas the <code>just</code> function handles the presence of data. </p> <p> Given, for example, a Maybe containing a number, you can use <code>Match</code> to <a href="/2019/02/04/how-to-get-the-value-out-of-the-monad">get the value out of the Maybe</a>: </p> <p> <pre>&gt; <span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;maybe&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Just</span>&lt;<span style="color:blue;">int</span>&gt;(42); &gt; maybe.Match(0,&nbsp;x&nbsp;=&gt;&nbsp;x) 42 &gt; <span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;maybe&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Nothing</span>&lt;<span style="color:blue;">int</span>&gt;(); &gt; maybe.Match(0,&nbsp;x&nbsp;=&gt;&nbsp;x) 0</pre> </p> <p> The functionality is, however, more useful than a simple <em>get-value-or-default</em> operation. Often, you don't have a good default value for the type potentially wrapped in a Maybe object. In the core of your application architecture, it may not be clear how to deal with, say, the absence of a <code>Reservation</code> object, whereas at the boundary of your system, it's evident how to convert both absence and presence of data into a unifying type, such as an HTTP response: </p> <p> <pre><span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;maybe&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:blue;">return</span>&nbsp;maybe &nbsp;&nbsp;&nbsp;&nbsp;.Select(r&nbsp;=&gt;&nbsp;Repository.Create(r)) &nbsp;&nbsp;&nbsp;&nbsp;.Match&lt;<span style="color:#2b91af;">IHttpActionResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nothing:&nbsp;Content( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">HttpStatusCode</span>.InternalServerError, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HttpError</span>(<span style="color:#a31515;">&quot;Couldn&#39;t&nbsp;accept.&quot;</span>)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;just:&nbsp;id&nbsp;=&gt;&nbsp;Ok(id));</pre> </p> <p> This enables you to avoid special cases, such as null <code>Reservation</code> objects, or magic numbers like <code>-1</code> to indicate the absence of <code>id</code> values. At the boundary of an HTTP-based application, you know that you must return an HTTP response. That's the unifying type, so you can return <code>200 OK</code> with a reservation ID in the response body when data is present, and <code>500 Internal Server Error</code> when data is absent. </p> <h3 id="87d91da8944f4eb5b8b24e9ea20d3e1b"> Maybe F-Algebra <a href="#87d91da8944f4eb5b8b24e9ea20d3e1b" title="permalink">#</a> </h3> <p> As in the <a href="/2019/05/13/peano-catamorphism">previous article</a>, I'll use <code>Fix</code> and <code>cata</code> as explained in <a href="https://bartoszmilewski.com">Bartosz Milewski</a>'s excellent <a href="https://bartoszmilewski.com/2017/02/28/f-algebras/">article on F-Algebras</a>. </p> <p> While F-Algebras and fixed points are mostly used for recursive data structures, you can also define an F-Algebra for a non-recursive data structure. You already saw an example of that in the article about <a href="/2019/05/06/boolean-catamorphism">Boolean catamorphism</a>. The difference between Boolean values and Maybe is that the <em>just</em> case of Maybe carries a value. You can model this as a <code>Functor</code> with both a carrier type and a type argument for the data that Maybe may contain: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;MaybeF&nbsp;a&nbsp;c&nbsp;=&nbsp;NothingF&nbsp;|&nbsp;JustF&nbsp;a&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Read</span>) <span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Functor</span>&nbsp;(<span style="color:blue;">MaybeF</span>&nbsp;a)&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;<span style="color:blue;">fmap</span>&nbsp;_&nbsp;&nbsp;NothingF&nbsp;=&nbsp;NothingF &nbsp;&nbsp;<span style="color:blue;">fmap</span>&nbsp;_&nbsp;(JustF&nbsp;x)&nbsp;=&nbsp;JustF&nbsp;x</pre> </p> <p> I chose to call the 'data type' <code>a</code> and the carrier type <code>c</code> (for <em>carrier</em>). As was also the case with <code>BoolF</code>, the <code>Functor</code> instance ignores the map function because the carrier type is missing from both the <code>NothingF</code> case and the <code>JustF</code> case. Like the <code>Functor</code> instance for <code>BoolF</code>, it'd seem that nothing happens, but at the type level, this is still a translation from <code>MaybeF a c</code> to <code>MaybeF a c1</code>. Not much of a function, perhaps, but definitely an <em>endofunctor</em>. </p> <p> In the previous articles, it was possible to work directly with the fixed points of both functors; i.e. <code>Fix BoolF</code> and <code>Fix NatF</code>. Haskell isn't happy about attempts to define various instances for <code>Fix (MaybeF a)</code>, so in order to make this easier, you can define a <code>newtype</code> wrapper: </p> <p> <pre><span style="color:blue;">newtype</span>&nbsp;MaybeFix&nbsp;a&nbsp;= &nbsp;&nbsp;MaybeFix&nbsp;{&nbsp;unMaybeFix&nbsp;::&nbsp;Fix&nbsp;(MaybeF&nbsp;a)&nbsp;}&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Read</span>)</pre> </p> <p> In order to make it easier to work with <code>MaybeFix</code> you can add helper functions to create values: </p> <p> <pre><span style="color:#2b91af;">nothingF</span>&nbsp;::&nbsp;<span style="color:blue;">MaybeFix</span>&nbsp;a nothingF&nbsp;=&nbsp;MaybeFix&nbsp;$&nbsp;Fix&nbsp;NothingF <span style="color:#2b91af;">justF</span>&nbsp;::&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">MaybeFix</span>&nbsp;a justF&nbsp;=&nbsp;MaybeFix&nbsp;.&nbsp;Fix&nbsp;.&nbsp;JustF</pre> </p> <p> You can now create <code>MaybeFix</code> values to your heart's content: </p> <p> <pre>Prelude Fix Maybe&gt; justF 42 MaybeFix {unMaybeFix = Fix (JustF 42)} Prelude Fix Maybe&gt; nothingF MaybeFix {unMaybeFix = Fix NothingF}</pre> </p> <p> That's all you need to identify the catamorphism. </p> <h3 id="24db4c715d1f4540bd8f87604819952f"> Haskell catamorphism <a href="#24db4c715d1f4540bd8f87604819952f" title="permalink">#</a> </h3> <p> At this point, you have two out of three elements of an F-Algebra. You have an endofunctor (<code>MaybeF</code>), and an object <code>a</code>, but you still need to find a morphism <code>MaybeF a c -&gt; c</code>. Notice that the algebra you have to find is the function that reduces the functor to its <em>carrier type</em> <code>c</code>, not the 'data type' <code>a</code>. This takes some time to get used to, but that's how catamorphisms work. This doesn't mean, however, that you get to ignore <code>a</code>, as you'll see. </p> <p> As in the previous article, start by writing a function that will become the catamorphism, based on <code>cata</code>: </p> <p> <pre>maybeF&nbsp;=&nbsp;cata&nbsp;alg&nbsp;.&nbsp;unMaybeFix &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;alg&nbsp;&nbsp;NothingF&nbsp;=&nbsp;<span style="color:blue;">undefined</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alg&nbsp;(JustF&nbsp;x)&nbsp;=&nbsp;<span style="color:blue;">undefined</span></pre> </p> <p> While this compiles, with its <code>undefined</code> implementations, it obviously doesn't do anything useful. I find, however, that it helps me think. How can you return a value of the type <code>c</code> from the <code>NothingF</code> case? You could pass an argument to the <code>maybeF</code> function: </p> <p> <pre>maybeF&nbsp;n&nbsp;=&nbsp;cata&nbsp;alg&nbsp;.&nbsp;unMaybeFix &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;alg&nbsp;&nbsp;NothingF&nbsp;=&nbsp;n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alg&nbsp;(JustF&nbsp;x)&nbsp;=&nbsp;<span style="color:blue;">undefined</span></pre> </p> <p> The <code>JustF</code> case, contrary to <code>NothingF</code>, already contains a value, and it'd be incorrect to ignore it. On the other hand, <code>x</code> is a value of type <code>a</code>, and you need to return a value of type <code>c</code>. You'll need a function to perform the conversion, so pass such a function as an argument to <code>maybeF</code> as well: </p> <p> <pre><span style="color:#2b91af;">maybeF</span>&nbsp;::&nbsp;c&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;(a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">MaybeFix</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c maybeF&nbsp;n&nbsp;f&nbsp;=&nbsp;cata&nbsp;alg&nbsp;.&nbsp;unMaybeFix &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;alg&nbsp;&nbsp;NothingF&nbsp;=&nbsp;n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alg&nbsp;(JustF&nbsp;x)&nbsp;=&nbsp;f&nbsp;x</pre> </p> <p> This works. Since <code>cata</code> has the type <code>Functor f =&gt; (f a -&gt; a) -&gt; Fix f -&gt; a</code>, that means that <code>alg</code> has the type <code>f a -&gt; a</code>. In the case of <code>MaybeF</code>, the compiler infers that the <code>alg</code> function has the type <code>MaybeF a c -&gt; c</code>, which is just what you need! </p> <p> You can now see what the carrier type <code>c</code> is for. It's the type that the algebra extracts, and thus the type that the catamorphism returns. </p> <p> Notice that <code>maybeF</code>, like the above C# <code>Match</code> method, takes as arguments a pair of a value and a function (together with the Maybe value itself). Those are two representations of the same idea. Furthermore, this is nearly identical to the <code>maybe</code> function in Haskell's <code>Data.Maybe</code> module. I found if fitting, therefore, to name the function <code>maybeF</code>. </p> <h3 id="d8a0eed800de48a994085c419b7b5379"> Basis <a href="#d8a0eed800de48a994085c419b7b5379" title="permalink">#</a> </h3> <p> You can implement most other useful functionality with <code>maybeF</code>. Here's the <code>Functor</code> instance: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Functor</span>&nbsp;<span style="color:blue;">MaybeFix</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;<span style="color:blue;">fmap</span>&nbsp;f&nbsp;=&nbsp;maybeF&nbsp;nothingF&nbsp;(justF&nbsp;.&nbsp;f)</pre> </p> <p> Since <code>fmap</code> should be a structure-preserving map, you'll have to map the <em>nothing</em> case to the <em>nothing</em> case, and <em>just</em> to <em>just</em>. When calling <code>maybeF</code>, you must supply a value for the <em>nothing</em> case and a function to deal with the <em>just</em> case. The <em>nothing</em> case is easy to handle: just use <code>nothingF</code>. </p> <p> In the <em>just</em> case, first apply the function <code>f</code> to map from <code>a</code> to <code>b</code>, and then use <code>justF</code> to wrap the <code>b</code> value in a new <code>MaybeFix</code> container to get <code>MaybeFix b</code>. </p> <p> <code>Applicative</code> is a little harder, but not much: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Applicative</span>&nbsp;<span style="color:blue;">MaybeFix</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;pure&nbsp;=&nbsp;justF &nbsp;&nbsp;f&nbsp;&lt;*&gt;&nbsp;x&nbsp;=&nbsp;maybeF&nbsp;nothingF&nbsp;(&lt;$&gt;&nbsp;x)&nbsp;f</pre> </p> <p> The <code>pure</code> function is just <em>justF</em> (pun intended). The <em>apply</em> operator <code>&lt;*&gt;</code> is more complex. </p> <p> Both <code>f</code> and <code>x</code> surrounding <code>&lt;*&gt;</code> are <code>MaybeFix</code> values: <code>f</code> is <code>MaybeFix (a -&gt; b)</code>, and <code>x</code> is <code>MaybeFix a</code>. While it's becoming increasingly clear that you can use a catamorphism like <code>maybeF</code> to implement most other functionality, to which <code>MaybeFix</code> value should you apply it? To <code>f</code> or <code>x</code>? </p> <p> Both are possible, but the code looks (in my opinion) more readable if you apply it to <code>f</code>. Again, when <code>f</code> is <em>nothing</em>, return <code>nothingF</code>. When <code>f</code> is <em>just</em>, use the functor instance to map <code>x</code> (using the infix <code>fmap</code> alias <code>&lt;$&gt;</code>). </p> <p> The <code>Monad</code> instance, on the other hand, is almost trivial: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Monad</span>&nbsp;<span style="color:blue;">MaybeFix</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;x&nbsp;&gt;&gt;=&nbsp;f&nbsp;=&nbsp;maybeF&nbsp;nothingF&nbsp;f&nbsp;x</pre> </p> <p> As usual, map <em>nothing</em> to <em>nothing</em> by supplying <code>nothingF</code>. <code>f</code> is already a function that returns a <code>MaybeFix b</code> value, so just use that. </p> <p> The <code>Foldable</code> instance is likewise trivial (although, as you'll see below, you can make it even more trivial): </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Foldable</span>&nbsp;<span style="color:blue;">MaybeFix</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;foldMap&nbsp;=&nbsp;maybeF&nbsp;mempty</pre> </p> <p> The <code>foldMap</code> function must return a <code>Monoid</code> instance, so for the <em>nothing</em> case, simply return the identity, <em>mempty</em>. Furthermore, <code>foldMap</code> takes a function <code>a -&gt; m</code>, but since the <code>foldMap</code> implementation is <a href="https://en.wikipedia.org/wiki/Tacit_programming">point-free</a>, you can't 'see' that function as an argument. </p> <p> Finally, for the sake of completeness, here's the <code>Traversable</code> instance: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Traversable</span>&nbsp;<span style="color:blue;">MaybeFix</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;sequenceA&nbsp;=&nbsp;maybeF&nbsp;(pure&nbsp;nothingF)&nbsp;(justF&nbsp;&lt;$&gt;)</pre> </p> <p> In the <em>nothing</em> case, you can put <code>nothingF</code> into the desired <code>Applicative</code> with <code>pure</code>. In the <em>just</em> case you can take advantage of the desired <code>Applicative</code> being also a <code>Functor</code> by simply mapping the inner value(s) with <code>justF</code>. </p> <p> Since the <code>Applicative</code> instance for <code>MaybeFix</code> equals <code>pure</code> to <code>justF</code>, you could alternatively write the <code>Traversable</code> instance like this: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Traversable</span>&nbsp;<span style="color:blue;">MaybeFix</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;sequenceA&nbsp;=&nbsp;maybeF&nbsp;(pure&nbsp;nothingF)&nbsp;(pure&nbsp;&lt;$&gt;)</pre> </p> <p> I like this alternative less, since I find it confusing. The two appearances of <code>pure</code> relate to two different types. The <code>pure</code> in <code>pure nothingF</code> has the type <code>MaybeFix a -&gt; f (MaybeFix a)</code>, while the <code>pure</code> in <code>pure&nbsp;&lt;$&gt;</code> has the type <code>a -&gt; MaybeFix a</code>! </p> <p> Both implementations work the same, though: </p> <p> <pre>Prelude Fix Maybe&gt; sequenceA (justF ("foo", 42)) ("foo",MaybeFix {unMaybeFix = Fix (JustF 42)})</pre> </p> <p> Here, I'm using the <code>Applicative</code> instance of <code>(,) String</code>. </p> <p> Finally, you can implement conversions to and from the standard <code>Maybe</code> type, using <code>ana</code> as the dual of <code>cata</code>: </p> <p> <pre><span style="color:#2b91af;">toMaybe</span>&nbsp;::&nbsp;<span style="color:blue;">MaybeFix</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&nbsp;a toMaybe&nbsp;=&nbsp;maybeF&nbsp;Nothing&nbsp;<span style="color:blue;">return</span> <span style="color:#2b91af;">fromMaybe</span>&nbsp;::&nbsp;<span style="color:#2b91af;">Maybe</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">MaybeFix</span>&nbsp;a fromMaybe&nbsp;=&nbsp;MaybeFix&nbsp;.&nbsp;ana&nbsp;coalg &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;coalg&nbsp;&nbsp;Nothing&nbsp;=&nbsp;NothingF &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;coalg&nbsp;(Just&nbsp;x)&nbsp;=&nbsp;JustF&nbsp;x</pre> </p> <p> This demonstrates that <code>MaybeFix</code> is isomorphic to <code>Maybe</code>, which again establishes that <code>maybeF</code> and <code>maybe</code> are equivalent. </p> <h3 id="2ec047e5122b4750a10cbe2012285524"> Alternatives <a href="#2ec047e5122b4750a10cbe2012285524" title="permalink">#</a> </h3> <p> As usual, the above <code>maybeF</code> isn't the only possible catamorphism. A trivial variation is to flip its arguments, but other variations exist. </p> <p> It's a recurring observation that a catamorphism is just a generalisation of a <em>fold</em>. In the above code, the <code>Foldable</code> instance already looked as simple as anyone could desire, but another variation of a catamorphism for Maybe is this gratuitously embellished definition: </p> <p> <pre><span style="color:#2b91af;">maybeF</span>&nbsp;::&nbsp;(a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">MaybeFix</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c maybeF&nbsp;f&nbsp;n&nbsp;=&nbsp;cata&nbsp;alg&nbsp;.&nbsp;unMaybeFix &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;alg&nbsp;&nbsp;NothingF&nbsp;=&nbsp;n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alg&nbsp;(JustF&nbsp;x)&nbsp;=&nbsp;f&nbsp;x&nbsp;n</pre> </p> <p> This variation redundantly passes <code>n</code> as an argument to <code>f</code>, thereby changing the type of <code>f</code> to <code>a -&gt; c -&gt; c</code>. There's no particular motivation for doing this, apart from establishing that this catamorphism is exactly the same as the fold: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Foldable</span>&nbsp;<span style="color:blue;">MaybeFix</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;<span style="color:blue;">foldr</span>&nbsp;=&nbsp;maybeF</pre> </p> <p> You can still implement the other instances as well, but the rest of the code suffers in clarity. Here's a few examples: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Functor</span>&nbsp;<span style="color:blue;">MaybeFix</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;<span style="color:blue;">fmap</span>&nbsp;f&nbsp;=&nbsp;maybeF&nbsp;(<span style="color:blue;">const</span>&nbsp;.&nbsp;justF&nbsp;.&nbsp;f)&nbsp;nothingF <span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Applicative</span>&nbsp;<span style="color:blue;">MaybeFix</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;pure&nbsp;=&nbsp;justF &nbsp;&nbsp;f&nbsp;&lt;*&gt;&nbsp;x&nbsp;=&nbsp;maybeF&nbsp;(<span style="color:blue;">const</span>&nbsp;.&nbsp;(&lt;$&gt;&nbsp;x))&nbsp;nothingF&nbsp;f <span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Monad</span>&nbsp;<span style="color:blue;">MaybeFix</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;x&nbsp;&gt;&gt;=&nbsp;f&nbsp;=&nbsp;maybeF&nbsp;(<span style="color:blue;">const</span>&nbsp;.&nbsp;f)&nbsp;nothingF&nbsp;x</pre> </p> <p> I find that the need to compose with <code>const</code> does nothing to improve the readability of the code, so this variation is mostly, I think, of academic interest. It does show, though, that the catamorphism of Maybe is isomorphic to its fold, as the diagram in the overview article indicated: </p> <p> <img src="/content/binary/catamorphism-and-fold-relations.png" alt="Catamorphisms and folds as sets, for various sum types."> </p> <p> You can demonstrate that this variation, too, is isomorphic to <code>Maybe</code> with a set of conversion: </p> <p> <pre><span style="color:#2b91af;">toMaybe</span>&nbsp;::&nbsp;<span style="color:blue;">MaybeFix</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&nbsp;a toMaybe&nbsp;=&nbsp;maybeF&nbsp;(<span style="color:blue;">const</span>&nbsp;.&nbsp;<span style="color:blue;">return</span>)&nbsp;Nothing <span style="color:#2b91af;">fromMaybe</span>&nbsp;::&nbsp;<span style="color:#2b91af;">Maybe</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">MaybeFix</span>&nbsp;a fromMaybe&nbsp;=&nbsp;MaybeFix&nbsp;.&nbsp;ana&nbsp;coalg &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;coalg&nbsp;&nbsp;Nothing&nbsp;=&nbsp;NothingF &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;coalg&nbsp;(Just&nbsp;x)&nbsp;=&nbsp;JustF&nbsp;x</pre> </p> <p> Only <code>toMaybe</code> has changed, compared to above; the <code>fromMaybe</code> function remains the same. The only change to <code>toMaybe</code> is that the arguments have been flipped, and <code>return</code> is now composed with <code>const</code>. </p> <p> Since (according to <a href="http://amzn.to/13tGJ0f">Conceptual Mathematics</a>) isomorphisms are transitive this means that the two variations of <code>maybeF</code> are isomorphic. The latter, more complex, variation of <code>maybeF</code> is identical <code>foldr</code>, so we can consider the simpler, more frequently encountered variation a simplification of <em>fold</em>. </p> <h3 id="f88757d425a04e97956d89270b32c0c0"> Summary <a href="#f88757d425a04e97956d89270b32c0c0" title="permalink">#</a> </h3> <p> The catamorphism for Maybe is the same as its Church encoding: a pair made from a default value and a function. In Haskell's base library, this is simply called <code>maybe</code>. In the above C# code, it's called <code>Match</code>. </p> <p> This function is total, and you can implement any other functionality you need with it. I therefore consider it the canonical representation of Maybe, which is also why it annoys me that most Maybe implementations come equipped with partial functions like <code>fromJust</code>, or F#'s <code>Option.get</code>. Those functions shouldn't be part of a sane and reasonable Maybe API. You shouldn't need them. </p> <p> In this series of articles about catamorphisms, you've now seen the first example of catamorphism and fold coinciding. In the next article, you'll see another such example - probably the most well-known catamorphism example of them all. </p> <p> <strong>Next:</strong> List catamorphism. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Peano catamorphism https://blog.ploeh.dk/2019/05/13/peano-catamorphism 2019-05-13T05:10:00+00:00 Mark Seemann <div id="post"> <p> <em>The catamorphism for Peano numbers involves a base value and a successor function.</em> </p> <p> This article is part of an <a href="/2019/04/29/catamorphisms">article series about catamorphisms</a>. A catamorphism is a <a href="/2017/10/04/from-design-patterns-to-category-theory">universal abstraction</a> that describes how to digest a data structure into a potentially more compact value. </p> <p> This article presents the catamorphism for <a href="https://en.wikipedia.org/wiki/Natural_number">natural numbers</a>, as well as how to identify it. The beginning of the article presents the catamorphism in C#, with examples. The rest of the article describes how I deduced the catamorphism. This part of the article presents my work in <a href="https://www.haskell.org">Haskell</a>. Readers not comfortable with Haskell can just read the first part, and consider the rest of the article as an optional appendix. </p> <h3 id="742f4b9c0c014152a933961c10f98b66"> C# catamorphism <a href="#742f4b9c0c014152a933961c10f98b66" title="permalink">#</a> </h3> <p> In this article, I model natural numbers using <a href="https://en.wikipedia.org/wiki/Peano_axioms">Peano's model</a>, and I'll reuse the <a href="/2018/05/28/church-encoded-natural-numbers">Church-encoded implementation you've seen before</a>. The catamorphism for <code>INaturalNumber</code> is: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Cata&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;n,&nbsp;<span style="color:#2b91af;">T</span>&nbsp;zero,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;succ) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;n.Match(zero,&nbsp;p&nbsp;=&gt;&nbsp;p.Cata(succ(zero),&nbsp;succ)); }</pre> </p> <p> Notice that this is an extension method on <code>INaturalNumber</code>, taking two other arguments: a <code>zero</code> argument which will be returned when the number is <em>zero</em>, and a successor function to return the 'next' value based on a previous value. </p> <p> The <code>zero</code> argument is the easiest to understand. It's simply passed to <code>Match</code> so that this is the value that <code>Cata</code> returns when <code>n</code> is <em>zero</em>. </p> <p> The other argument to <code>Match</code> must be a <code>Func&lt;INaturalNumber, T&gt;</code>; that is, a function that takes an <code>INaturalNumber</code> as input and returns a value of the type <code>T</code>. You can supply such a function by using a lambda expression. This expression receives a predecessor <code>p</code> as input, and has to return a value of the type <code>T</code>. The only function available in this context, however, is <code>succ</code>, which has the type <code>Func&lt;T, T&gt;</code>. How can you make that work? </p> <p> As is often the case when programming with generics, it pays to <em>follow the types</em>. A <code>Func&lt;T, T&gt;</code> requires an input of the type <code>T</code>. Do you have any <code>T</code> objects around? </p> <p> The only available <code>T</code> object is <code>zero</code>, so you could call <code>succ(zero)</code> to produce another <code>T</code> value. While you could return that immediately, that'd ignore the predecessor <code>p</code>, so that's not going to work. Another option, which is the one that works, is to recursively call <code>Cata</code> with <code>succ(zero)</code> as the <code>zero</code> value, and <code>succ</code> as the second argument. </p> <p> What this accomplishes is that <code>Cata</code> keeps recursively calling itself until <code>n</code> is <em>zero</em>. The <code>zero</code> object, however, will be the result of repeated applications of <code>succ(zero)</code>. In other words, <code>succ</code> will be called as many times as the natural number. If <code>n</code> is 7, then <code>succ</code> will be called seven times, the first time with the original <code>zero</code> value, the next time with the result of <code>succ(zero)</code>, the third time with the result of <code>succ(succ(zero))</code>, and so on. If the number is 42, <code>succ</code> will be called 42 times. </p> <h3 id="633dae2048cd45ebaa17962710048c67"> Arithmetic <a href="#633dae2048cd45ebaa17962710048c67" title="permalink">#</a> </h3> <p> You can implement all the functionality you saw in the article on Church-encoded natural numbers. You can start gently by converting a Peano number into a normal C# <code>int</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Count(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;n) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;n.Cata(0,&nbsp;x&nbsp;=&gt;&nbsp;1&nbsp;+&nbsp;x); }</pre> </p> <p> You can play with the functionality in <em>C# Interactive</em> to get a feel for how it works: </p> <p> <pre>&gt; <span style="color:#2b91af;">NaturalNumber</span>.Eight.Count() 8 &gt; <span style="color:#2b91af;">NaturalNumber</span>.Five.Count() 5</pre> </p> <p> The <code>Count</code> extension method uses <code>Cata</code> to count the level of recursion. The <code>zero</code> value is, not surprisingly, <code>0</code>, and the successor function simply adds one to the previous number. Since the successor function runs as many times as encoded by the Peano number, and since the initial value is <code>0</code>, you get the integer value of the number when <code>Cata</code> exits. </p> <p> A useful building block you can write using <code>Cata</code> is a function to increment a natural number by one: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;Increment(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;n) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;n.Cata(One,&nbsp;p&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Successor</span>(p)); }</pre> </p> <p> This, again, works as you'd expect: </p> <p> <pre>&gt; <span style="color:#2b91af;">NaturalNumber</span>.Zero.Increment().Count() 1 &gt; <span style="color:#2b91af;">NaturalNumber</span>.Eight.Increment().Count() 9</pre> </p> <p> With the <code>Increment</code> method and <code>Cata</code>, you can easily implement addition: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;Add(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;x,&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;x.Cata(y,&nbsp;p&nbsp;=&gt;&nbsp;p.Increment()); }</pre> </p> <p> The trick here is to use <code>y</code> as the <code>zero</code> case for <code>x</code>. In other words, if <code>x</code> is <em>zero</em>, then <code>Add</code> should return <code>y</code>. If <code>x</code> isn't <em>zero</em>, then <code>Increment</code> it as many times as the number encodes, but starting at <code>y</code>. In other words, start with <code>y</code> and <code>Increment</code> <code>x</code> times. </p> <p> The catamorphism makes it much easier to implement arithmetic operation. Just consider multiplication, which wasn't the simplest implementation in the previous article. Now, it's as simple as this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;Multiply(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;x,&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;x.Cata(Zero,&nbsp;p&nbsp;=&gt;&nbsp;p.Add(y)); }</pre> </p> <p> Start at <code>0</code> and simply <code>Add(y)</code> <code>x</code> times. </p> <p> <pre>&gt; <span style="color:#2b91af;">NaturalNumber</span>.Nine.Multiply(<span style="color:#2b91af;">NaturalNumber</span>.Four).Count() 36</pre> </p> <p> Finally, you can also implement some common predicates: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IChurchBoolean</span>&nbsp;IsZero(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;n) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;n.Cata&lt;<span style="color:#2b91af;">IChurchBoolean</span>&gt;(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchTrue</span>(),&nbsp;_&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchFalse</span>()); } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IChurchBoolean</span>&nbsp;IsEven(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;n) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;n.Cata&lt;<span style="color:#2b91af;">IChurchBoolean</span>&gt;(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchTrue</span>(),&nbsp;b&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchNot</span>(b)); } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IChurchBoolean</span>&nbsp;IsOdd(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">INaturalNumber</span>&nbsp;n) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ChurchNot</span>(n.IsEven()); }</pre> </p> <p> Particularly <code>IsEven</code> is elegant: It considers <code>zero</code> even, so simply uses a <code>new ChurchTrue()</code> object for that case. In all other cases, it alternates between <em>false</em> and <em>true</em> by negating the predecessor. </p> <p> <pre>&gt; <span style="color:#2b91af;">NaturalNumber</span>.Three.IsEven().ToBool() false</pre> </p> <p> It seems convincing that we can use <code>Cata</code> to implement all the other functionality we need. That seems to be a characteristic of a catamorphism. Still, how do we know that <code>Cata</code> is, in fact, the catamorphism for natural numbers? </p> <h3 id="05dbca489b8a49be830df87e13bfcae3"> Peano F-Algebra <a href="#05dbca489b8a49be830df87e13bfcae3" title="permalink">#</a> </h3> <p> As in the <a href="/2019/05/06/boolean-catamorphism">previous article</a>, I'll use <code>Fix</code> and <code>cata</code> as explained in <a href="https://bartoszmilewski.com">Bartosz Milewski</a>'s excellent <a href="https://bartoszmilewski.com/2017/02/28/f-algebras/">article on F-Algebras</a>. The <code>NatF</code> type comes from his article as well: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;NatF&nbsp;a&nbsp;=&nbsp;ZeroF&nbsp;|&nbsp;SuccF&nbsp;a&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Read</span>) <span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Functor</span>&nbsp;<span style="color:blue;">NatF</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;<span style="color:blue;">fmap</span>&nbsp;_&nbsp;ZeroF&nbsp;=&nbsp;ZeroF &nbsp;&nbsp;<span style="color:blue;">fmap</span>&nbsp;f&nbsp;(SuccF&nbsp;x)&nbsp;=&nbsp;SuccF&nbsp;$&nbsp;f&nbsp;x</pre> </p> <p> You can use the fixed point of this functor to define numbers with a shared type. Here's just the first ten: </p> <p> <pre><span style="color:#2b91af;">zeroF</span>,&nbsp;<span style="color:#2b91af;">oneF</span>,&nbsp;<span style="color:#2b91af;">twoF</span>,&nbsp;<span style="color:#2b91af;">threeF</span>,&nbsp;<span style="color:#2b91af;">fourF</span>,&nbsp;<span style="color:#2b91af;">fiveF</span>,&nbsp;<span style="color:#2b91af;">sixF</span>,&nbsp;<span style="color:#2b91af;">sevenF</span>,&nbsp;<span style="color:#2b91af;">eightF</span>,&nbsp;<span style="color:#2b91af;">nineF</span>&nbsp;::&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">NatF</span> zeroF&nbsp;&nbsp;=&nbsp;Fix&nbsp;ZeroF oneF&nbsp;&nbsp;&nbsp;=&nbsp;Fix&nbsp;$&nbsp;SuccF&nbsp;zeroF twoF&nbsp;&nbsp;&nbsp;=&nbsp;Fix&nbsp;$&nbsp;SuccF&nbsp;oneF threeF&nbsp;=&nbsp;Fix&nbsp;$&nbsp;SuccF&nbsp;twoF fourF&nbsp;&nbsp;=&nbsp;Fix&nbsp;$&nbsp;SuccF&nbsp;threeF fiveF&nbsp;&nbsp;=&nbsp;Fix&nbsp;$&nbsp;SuccF&nbsp;fourF sixF&nbsp;&nbsp;&nbsp;=&nbsp;Fix&nbsp;$&nbsp;SuccF&nbsp;fiveF sevenF&nbsp;=&nbsp;Fix&nbsp;$&nbsp;SuccF&nbsp;sixF eightF&nbsp;=&nbsp;Fix&nbsp;$&nbsp;SuccF&nbsp;sevenF nineF&nbsp;&nbsp;=&nbsp;Fix&nbsp;$&nbsp;SuccF&nbsp;eightF</pre> </p> <p> That's all you need to identify the catamorphism. </p> <h3 id="f0e66c873a034830a4b069229971299a"> Haskell catamorphism <a href="#f0e66c873a034830a4b069229971299a" title="permalink">#</a> </h3> <p> At this point, you have two out of three elements of an F-Algebra. You have an endofunctor (<code>NatF</code>), and an object <code>a</code>, but you still need to find a morphism <code>NatF a -&gt; a</code>. </p> <p> As in the previous article, start by writing a function that will become the catamorphism, based on <code>cata</code>: </p> <p> <pre>natF&nbsp;=&nbsp;cata&nbsp;alg &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;alg&nbsp;ZeroF&nbsp;=&nbsp;<span style="color:blue;">undefined</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alg&nbsp;(SuccF&nbsp;predecessor)&nbsp;=&nbsp;<span style="color:blue;">undefined</span></pre> </p> <p> While this compiles, with its <code>undefined</code> implementations, it obviously doesn't do anything useful. I find, however, that it helps me think. How can you return a value of the type <code>a</code> from the <code>ZeroF</code> case? You could pass an argument to the <code>natF</code> function: </p> <p> <pre>natF&nbsp;z&nbsp;=&nbsp;cata&nbsp;alg &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;alg&nbsp;ZeroF&nbsp;=&nbsp;z &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alg&nbsp;(SuccF&nbsp;predecessor)&nbsp;=&nbsp;<span style="color:blue;">undefined</span></pre> </p> <p> In the <code>SuccF</code> case, <code>predecessor</code> is already of the polymorphic type <code>a</code>, so instead of returning a constant value, you can supply a function as an argument to <code>natF</code> and use it in that case: </p> <p> <pre><span style="color:#2b91af;">natF</span>&nbsp;::&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;(a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">NatF</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a natF&nbsp;z&nbsp;next&nbsp;=&nbsp;cata&nbsp;alg &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;alg&nbsp;ZeroF&nbsp;=&nbsp;z &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alg&nbsp;(SuccF&nbsp;predecessor)&nbsp;=&nbsp;next&nbsp;predecessor</pre> </p> <p> This works. Since <code>cata</code> has the type <code>Functor f =&gt; (f a -&gt; a) -&gt; Fix f -&gt; a</code>, that means that <code>alg</code> has the type <code>f a -&gt; a</code>. In the case of <code>NatF</code>, the compiler infers that the <code>alg</code> function has the type <code>NatF a -&gt; a</code>, which is just what you need! </p> <p> For good measure, I should point out that, as usual, the above <code>natF</code> function isn't the only possible catamorphism. Trivially, you can flip the order of the arguments, and this would also be a catamorphism. These two alternatives are isomorphic. </p> <p> The <code>natF</code> function identifies the Peano number catamorphism, which is equivalent to the C# representation in the beginning of the article. I called the function <code>natF</code>, because there's a tendency in Haskell to name the 'case analysis' or catamorphism after the type, just with a lower-case initial letter. </p> <h3 id="e78ae79059e14f4b92f525393cc74861"> Basis <a href="#e78ae79059e14f4b92f525393cc74861" title="permalink">#</a> </h3> <p> A catamorphism can be used to implement most (if not all) other useful functionality, like all of the above C# functionality. In fact, I wrote the Haskell code first, and then translated my implementations into the above C# extension methods. This means that the following functions apply the same reasoning: </p> <p> <pre><span style="color:#2b91af;">evenF</span>&nbsp;::&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">NatF</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">BoolF</span> evenF&nbsp;=&nbsp;natF&nbsp;trueF&nbsp;notF <span style="color:#2b91af;">oddF</span>&nbsp;::&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">NatF</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">BoolF</span> oddF&nbsp;=&nbsp;notF&nbsp;.&nbsp;evenF <span style="color:#2b91af;">incF</span>&nbsp;::&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">NatF</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">NatF</span> incF&nbsp;=&nbsp;natF&nbsp;oneF&nbsp;$&nbsp;Fix&nbsp;.&nbsp;SuccF <span style="color:#2b91af;">addF</span>&nbsp;::&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">NatF</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">NatF</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">NatF</span> addF&nbsp;x&nbsp;y&nbsp;=&nbsp;natF&nbsp;y&nbsp;incF&nbsp;x <span style="color:#2b91af;">multiplyF</span>&nbsp;::&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">NatF</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">NatF</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">NatF</span> multiplyF&nbsp;x&nbsp;y&nbsp;=&nbsp;natF&nbsp;zeroF&nbsp;(addF&nbsp;y)&nbsp;x</pre> </p> <p> Here are some GHCi usage examples: </p> <p> <pre>Prelude Boolean Nat&gt; evenF eightF Fix TrueF Prelude Boolean Nat&gt; toNum $ multiplyF sevenF sixF 42</pre> </p> <p> The <code>toNum</code> function corresponds to the above <code>Count</code> C# method. It is, again, based on <code>cata</code>. You can use <code>ana</code> to convert the other way: </p> <p> <pre><span style="color:#2b91af;">toNum</span>&nbsp;::&nbsp;<span style="color:blue;">Num</span>&nbsp;a&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">NatF</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a toNum&nbsp;=&nbsp;natF&nbsp;0&nbsp;(+&nbsp;1) <span style="color:#2b91af;">fromNum</span>&nbsp;::&nbsp;(<span style="color:blue;">Eq</span>&nbsp;a,&nbsp;<span style="color:blue;">Num</span>&nbsp;a)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">NatF</span> fromNum&nbsp;=&nbsp;ana&nbsp;coalg &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;coalg&nbsp;0&nbsp;=&nbsp;ZeroF &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;coalg&nbsp;x&nbsp;=&nbsp;SuccF&nbsp;$&nbsp;x&nbsp;-&nbsp;1</pre> </p> <p> This demonstrates that <code>Fix NatF</code> is isomorphic to <code>Num</code> instances, such as <code>Integer</code>. </p> <h3 id="d09e79446af14875a42f66869e10f33a"> Summary <a href="#d09e79446af14875a42f66869e10f33a" title="permalink">#</a> </h3> <p> The catamorphism for Peano numbers is a pair consisting of a zero value and a successor function. The most common description of catamorphisms that I've found emphasise how a catamorphism is like a <em>fold;</em> an operation you can use to reduce a data structure like a list or a tree to a single value. This is what happens here, but even so, the <code>Fix NatF</code> type isn't a <code>Foldable</code> instance. The reason is that while <code>NatF</code> is a polymorphic type, its fixed point <code>Fix NatF</code> isn't. Haskell's <code>Foldable</code> type class requires foldable containers to be polymorphic (what C# programmers would call 'generic'). </p> <p> When I first ran into the concept of a <em>catamorphism</em>, it was invariably described as a 'generalisation of fold'. The examples shown were always how the catamorphism for linked list is the same as its <em>fold</em>. I found such explanations unhelpful, because I couldn't understand how those two concepts differ. </p> <p> The purpose with this article series is to show just how much more general the abstraction of a catamorphism is. In this article you saw how an infinitely recursive data structure like Peano numbers have a catamorphism, even though it isn't a parametrically polymorphic type. In the next article, though, you'll see the first example of a polymorphic type where the catamorphism coincides with the fold. </p> <p> <strong>Next:</strong> <a href="/2019/05/20/maybe-catamorphism">Maybe catamorphism</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Boolean catamorphism https://blog.ploeh.dk/2019/05/06/boolean-catamorphism 2019-05-06T12:30:00+00:00 Mark Seemann <div id="post"> <p> <em>The catamorphism for Boolean values is just the common ternary operator.</em> </p> <p> This article is part of an <a href="/2019/04/29/catamorphisms">article series about catamorphisms</a>. A catamorphism is a <a href="/2017/10/04/from-design-patterns-to-category-theory">universal abstraction</a> that describes how to digest a data structure into a potentially more compact value. </p> <p> This article presents the catamorphism for Boolean values, as well as how you identify it. The beginning of this article presents the catamorphism in C#, with a simple example. The rest of the article describes how I deduced the catamorphism. That part of the article presents my work in <a href="https://www.haskell.org">Haskell</a>. Readers not comfortable with Haskell can just read the first part, and consider the rest of the article as an optional appendix. </p> <h3 id="35155b758274445cbe57f75d730a4eb6"> C# catamorphism <a href="#35155b758274445cbe57f75d730a4eb6" title="permalink">#</a> </h3> <p> The catamorphism for Boolean values is the familiar <a href="https://en.wikipedia.org/wiki/%3F:">ternary conditional operator</a>: </p> <p> <pre>&gt; <span style="color:#2b91af;">DateTime</span>.Now.Day&nbsp;%&nbsp;2&nbsp;==&nbsp;0&nbsp;?&nbsp;<span style="color:#a31515;">&quot;Even&nbsp;date&quot;</span>&nbsp;:&nbsp;<span style="color:#a31515;">&quot;Odd&nbsp;date&quot;</span> "Odd date"</pre> </p> <p> Given a Boolean expression, you basically provide two values: one to use in case the Boolean expression is <em>true</em>, and one to use in case it's <em>false</em>. </p> <p> For <a href="/2018/05/24/church-encoded-boolean-values">Church-encoded Boolean values</a>, the catamorphism looks like this: </p> <p> <pre><span style="color:#2b91af;">T</span>&nbsp;Match&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">T</span>&nbsp;trueCase,&nbsp;<span style="color:#2b91af;">T</span>&nbsp;falseCase);</pre> </p> <p> This is an instance method where you must, again, supply two alternatives. When the instance represents <em>true</em>, you'll get the left-most value <code>trueCase</code>; otherwise, when the instance represents <em>false</em>, you'll get the right-most value <code>falseCase</code>. </p> <p> The catamorphism turns out to be the same as the <a href="/2018/05/22/church-encoding">Church encoding</a>. This seems to be a recurring pattern. </p> <h3 id="cd81cb92ed2d42f8bc0ad5adbde4b014"> Alternatives <a href="#cd81cb92ed2d42f8bc0ad5adbde4b014" title="permalink">#</a> </h3> <p> To be accurate, there's more than one catamorphism for Boolean values. It's only by convention that the value corresponding to <em>true</em> goes on the left, and the <em>false</em> value goes to the right. You could flip the arguments, and it would still be a catamorphism. This is, in fact, what Haskell's <code>Data.Bool</code> module does: </p> <p> <pre>Prelude Data.Bool&gt; bool "Odd date" "Even date" $ even date "Odd date"</pre> </p> <p> The <a href="http://hackage.haskell.org/package/base/docs/Data-Bool.html">module documentation</a> calls this the <em>"Case analysis for the <code>Bool</code> type"</em>, instead of a catamorphism, but the two representations are isomorphic: <blockquote> "This is equivalent to <code>if p then y else x</code>; that is, one can think of it as an if-then-else construct with its arguments reordered." </blockquote> This is another recurring result. There's typically more than one catamorphism, but the alternatives are isomorphic. In this article series, I'll mostly present the alternative that strikes me as the one you'll encounter most frequently. </p> <h3 id="60235fb428d14785a5aeea440c05cce5"> Fix <a href="#60235fb428d14785a5aeea440c05cce5" title="permalink">#</a> </h3> <p> In this and future articles, I'll derive the catamorphism from an F-Algebra. For an introduction to F-Algebras and fixed points, I'll refer you to <a href="https://bartoszmilewski.com">Bartosz Milewski</a>'s excellent <a href="https://bartoszmilewski.com/2017/02/28/f-algebras/">article on the topic</a>. In it, he presents a generic data type for a fixed point, as well as polymorphic functions for catamorphisms and anamorphisms. While they're available in his article, I'll repeat them here for good measure: </p> <p> <pre><span style="color:blue;">newtype</span>&nbsp;Fix&nbsp;f&nbsp;=&nbsp;Fix&nbsp;{&nbsp;unFix&nbsp;::&nbsp;f&nbsp;(Fix&nbsp;f)&nbsp;} <span style="color:#2b91af;">cata</span>&nbsp;::&nbsp;<span style="color:blue;">Functor</span>&nbsp;f&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;(f&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Fix</span>&nbsp;f&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a cata&nbsp;alg&nbsp;=&nbsp;alg&nbsp;.&nbsp;<span style="color:blue;">fmap</span>&nbsp;(cata&nbsp;alg)&nbsp;.&nbsp;unFix <span style="color:#2b91af;">ana</span>&nbsp;::&nbsp;<span style="color:blue;">Functor</span>&nbsp;f&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;(a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;f&nbsp;a)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Fix</span>&nbsp;f ana&nbsp;coalg&nbsp;=&nbsp;Fix&nbsp;.&nbsp;<span style="color:blue;">fmap</span>&nbsp;(ana&nbsp;coalg)&nbsp;.&nbsp;coalg</pre> </p> <p> This should be recognisable from Bartosz Milewski's article. With one small exception, this is just a copy of the code shown there. </p> <h3 id="6b0fdc2b04e540d19322f0e30c00e86c"> Boolean F-Algebra <a href="#6b0fdc2b04e540d19322f0e30c00e86c" title="permalink">#</a> </h3> <p> While F-Algebras and fixed points are mostly used for recursive data structures, you can also define an F-Algebra for a non-recursive data structure. As data types go, they don't get much simpler than Boolean values, which are just two mutually exclusive cases. In order to make a <code>Functor</code> out of the definition, though, you can equip it with a <em>carrier type:</em> </p> <p> <pre><span style="color:blue;">data</span>&nbsp;BoolF&nbsp;a&nbsp;=&nbsp;TrueF&nbsp;|&nbsp;FalseF&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Eq</span>,&nbsp;<span style="color:#2b91af;">Read</span>) <span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Functor</span>&nbsp;<span style="color:blue;">BoolF</span>&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;<span style="color:blue;">fmap</span>&nbsp;_&nbsp;&nbsp;TrueF&nbsp;=&nbsp;&nbsp;TrueF &nbsp;&nbsp;<span style="color:blue;">fmap</span>&nbsp;_&nbsp;FalseF&nbsp;=&nbsp;FalseF</pre> </p> <p> The <code>Functor</code> instance simply ignores the carrier type and just returns <code>TrueF</code> and <code>FalseF</code>, respectively. It'd seem that nothing happens, but at the type level, this is still a translation from <code>BoolF a</code> to <code>BoolF b</code>. Not much of a function, perhaps, but definitely an <em>endofunctor</em>. </p> <p> Another note that may be in order here, as well as for all future articles in this series, is that you'll notice that most types and custom functions come with the <code>F</code> suffix. This is simply a suffix I've added to avoid conflicts with built-in types, values, and functions, such as <code>Bool</code>, <code>True</code>, <code>and</code>, and so on. The <code>F</code> is for <em>F-Algebra</em>. </p> <p> You can lift these values into <code>Fix</code> in order to make it fit with the <code>cata</code> function: </p> <p> <pre><span style="color:#2b91af;">trueF</span>,&nbsp;<span style="color:#2b91af;">falseF</span>&nbsp;::&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">BoolF</span> trueF&nbsp;&nbsp;=&nbsp;Fix&nbsp;&nbsp;TrueF falseF&nbsp;=&nbsp;Fix&nbsp;FalseF</pre> </p> <p> That's all you need to identify the catamorphism. </p> <h3 id="a28e972fc7eb45038427cff258c0c8f2"> Haskell catamorphism <a href="#a28e972fc7eb45038427cff258c0c8f2" title="permalink">#</a> </h3> <p> At this point, you have two out of three elements of an F-Algebra. You have an endofunctor (<code>BoolF</code>), and an object <code>a</code>, but you still need to find a morphism <code>BoolF a -&gt; a</code>. At first glance, this seems impossible, because neither <code>TrueF</code> nor <code>FalseF</code> actually contain a value of the type <code>a</code>. How, then, can you conjure an <code>a</code> value out of thin air? </p> <p> The <code>cata</code> function has the answer. </p> <p> What you can do is to start writing the function that will become the catamorphism, basing it on <code>cata</code>: </p> <p> <pre>boolF&nbsp;=&nbsp;cata&nbsp;alg &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;alg&nbsp;&nbsp;TrueF&nbsp;=&nbsp;<span style="color:blue;">undefined</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alg&nbsp;FalseF&nbsp;=&nbsp;<span style="color:blue;">undefined</span></pre> </p> <p> While this compiles, with its <code>undefined</code> implementations, it obviously doesn't do anything useful. I find, however, that it helps me think. How can you return a value of the type <code>a</code> from the <code>TrueF</code> case? You could pass an argument to the <code>boolF</code> function: </p> <p> <pre>boolF&nbsp;x&nbsp;=&nbsp;cata&nbsp;alg &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;alg&nbsp;&nbsp;TrueF&nbsp;=&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alg&nbsp;FalseF&nbsp;=&nbsp;<span style="color:blue;">undefined</span></pre> </p> <p> That seems promising, so do that for the <code>FalseF</code> case as well: </p> <p> <pre><span style="color:#2b91af;">boolF</span>&nbsp;::&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">BoolF</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a boolF&nbsp;x&nbsp;y&nbsp;=&nbsp;cata&nbsp;alg &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;alg&nbsp;&nbsp;TrueF&nbsp;=&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alg&nbsp;FalseF&nbsp;=&nbsp;y</pre> </p> <p> This works. Since <code>cata</code> has the type <code>Functor f =&gt; (f a -&gt; a) -&gt; Fix f -&gt; a</code>, that means that <code>alg</code> has the type <code>f a -&gt; a</code>. In the case of <code>BoolF</code>, the compiler infers that the <code>alg</code> function has the type <code>BoolF a -&gt; a</code>, which is just what you need! </p> <p> The <code>boolF</code> function identifies the Boolean catamorphism, which is equivalent to representations in the beginning of the article. I called the function <code>boolF</code>, because there's a tendency in Haskell to name the 'case analysis' or catamorphism after the type, just with a lower-case initial letter. </p> <p> You can use the <code>boolF</code> function just like the above ternary operator: </p> <p> <pre>Prelude Boolean Nat&gt; boolF "Even date" "Odd date" $ evenF dateF "Odd date"</pre> </p> <p> Here, I've also used <code>evenF</code> from the <code>Nat</code> module shown in the next article in the series. </p> <p> From the above definition of <code>boolF</code>, it should also be clear that you can arrive at the alternative catamorphism defined by <code>Data.Bool.bool</code> by simply flipping <code>x</code> and <code>y</code>. </p> <h3 id="54886f1be8684dd4a5909e4d50b7d5dc"> Basis <a href="#54886f1be8684dd4a5909e4d50b7d5dc" title="permalink">#</a> </h3> <p> A catamorphism can be used to implement most (if not all) other useful functionality. For Boolean values, that would be the standard Boolean operations <em>and</em>, <em>or</em>, and <em>not:</em> </p> <p> <pre><span style="color:#2b91af;">andF</span>&nbsp;::&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">BoolF</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">BoolF</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">BoolF</span> andF&nbsp;x&nbsp;y&nbsp;=&nbsp;boolF&nbsp;y&nbsp;falseF&nbsp;x <span style="color:#2b91af;">orF</span>&nbsp;::&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">BoolF</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">BoolF</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">BoolF</span> orF&nbsp;x&nbsp;y&nbsp;=&nbsp;boolF&nbsp;trueF&nbsp;y&nbsp;x <span style="color:#2b91af;">notF</span>&nbsp;::&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">BoolF</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">BoolF</span> notF&nbsp;=&nbsp;boolF&nbsp;falseF&nbsp;trueF</pre> </p> <p> They work as you'd expect them to work: </p> <p> <pre>Prelude Boolean&gt; andF trueF falseF Fix FalseF Prelude Boolean&gt; orF trueF falseF Fix TrueF Prelude Boolean&gt; orF (notF trueF) falseF Fix FalseF</pre> </p> <p> You can also implement conversion to and from the built-in <code>Bool</code> type: </p> <p> <pre><span style="color:#2b91af;">toBool</span>&nbsp;::&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">BoolF</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">Bool</span> toBool&nbsp;=&nbsp;boolF&nbsp;True&nbsp;False <span style="color:#2b91af;">fromBool</span>&nbsp;::&nbsp;<span style="color:#2b91af;">Bool</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Fix</span>&nbsp;<span style="color:blue;">BoolF</span> fromBool&nbsp;=&nbsp;ana&nbsp;coalg &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;coalg&nbsp;&nbsp;True&nbsp;=&nbsp;TrueF &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;coalg&nbsp;False&nbsp;=&nbsp;FalseF</pre> </p> <p> This demonstrates that <code>Fix BoolF</code> is isomorphic to <code>Bool</code>. </p> <h3 id="327411e72cea46bbb1f6fe167738c7b2"> Summary <a href="#327411e72cea46bbb1f6fe167738c7b2" title="permalink">#</a> </h3> <p> The catamorphism for Boolean values is a function, method, or operator akin to the familiar ternary conditional operator. The most common descriptions of catamorphisms that I've found emphasise how a catamorphism is like a <em>fold;</em> an operation you can use to reduce a data structure like a list or a tree to a single value. In that light, it may be surprising that something as simple as Boolean values have an associated catamorphism. </p> <p> Since <code>Fix BoolF</code> is isomorphic to <code>Bool</code>, you may wonder what the point is. Why define this data type, and implement functions like <code>andF</code>, <code>orF</code>, and <code>notF</code>? </p> <p> The code presented here is nothing but an analysis tool. It's a way to identify the catamorphism for Boolean values. </p> <p> <strong>Next:</strong> <a href="/2019/05/13/peano-catamorphism">Peano catamorphism</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Catamorphisms https://blog.ploeh.dk/2019/04/29/catamorphisms 2019-04-29T18:31:00+00:00 Mark Seemann <div id="post"> <p> <em>A catamorphism is a general abstraction that enables you to handle multiple values, for example in order to reduce them to a single value.</em> </p> <p> This article series is part of <a href="/2017/10/04/from-design-patterns-to-category-theory">an even larger series of articles about the relationship between design patterns and category theory</a>. In another article series in this big series of articles, you learned about <a href="/2018/03/19/functors-applicatives-and-friends">functors, applicatives, and other types of data containers</a>. </p> <p> You may have heard about <em>map-reduce</em> architectures. Much software can be designed around two general types of operations: those that <em>map</em> data, and those that <em>reduce</em> data. A <a href="https://bartoszmilewski.com/2014/01/14/functors-are-containers">functor is a container of data</a> that supports structure-preserving maps. Thus, you can think of <a href="/2018/03/22/functors">functors</a> as the general abstraction for map operations (also sometimes called <em>projections</em>). Does a similar universal abstraction exist for operations that reduce data? </p> <p> Yes, that abstraction is called a <em>catamorphism</em>. </p> <h3 id="bb64d005b16b49f892c00824ef803997"> Aggregation <a href="#bb64d005b16b49f892c00824ef803997" title="permalink">#</a> </h3> <p> <em>Catamorphism</em> is an intimidating word, so let's start with an example. You often have a collection of values that you'd like to reduce to a single value. Such a collection can contain arbitrarily complex objects, but I'll keep it simple and start with a collection of numbers: </p> <p> <pre><span style="color:blue;">new</span>[]&nbsp;{&nbsp;42,&nbsp;1337,&nbsp;2112,&nbsp;90125,&nbsp;5040,&nbsp;7,&nbsp;1984&nbsp;};</pre> </p> <p> This particular list of numbers is an array, but that's not important. What comes next works for any <code>IEnumerable&lt;T&gt;</code>, including arrays. I only chose an array because the C# syntax for array creation is more compact than for other collection types. </p> <p> How do you reduce those seven numbers to a single number? That depends on what you want that number to tell you. One option is to add the numbers together. There's a specific, built-in function for that: </p> <p> <pre>&gt; <span style="color:blue;">new</span>[]&nbsp;{&nbsp;42,&nbsp;1337,&nbsp;2112,&nbsp;90125,&nbsp;5040,&nbsp;7,&nbsp;1984&nbsp;}.Sum(); 100647</pre> </p> <p> The <a href="https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.sum">Sum</a> extension method is a one of many built-in functions that enable you to reduce a list of numbers to a single number: <a href="https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.average">Average</a>, <a href="https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.max">Max</a>, <a href="https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.count">Count</a>, and so on. </p> <p> What do you do, though, if you need to reduce many values to one, and there's no existing function for that? What if, for example, you need to add all the numbers using <a href="/2018/07/16/angular-addition-monoid">modulo 360 addition</a>? </p> <p> In that case, you use <a href="https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.aggregate">Aggregate</a>: </p> <p> <pre>&gt; <span style="color:blue;">new</span>[]&nbsp;{&nbsp;42,&nbsp;1337,&nbsp;2112,&nbsp;90125,&nbsp;5040,&nbsp;7,&nbsp;1984&nbsp;}.Aggregate((x,&nbsp;y)&nbsp;=&gt;&nbsp;(x&nbsp;+&nbsp;y)&nbsp;%&nbsp;360) 207</pre> </p> <p> The way to interpret this result is that the initial array represents a sequence of rotations (measured in degrees), and the result is the final angle after all the rotations have completed. </p> <p> In other (functional) languages, such a 'reduce' operation is called a <em>fold</em>. The metaphor, I suppose, is that you fold multiple values together, two by two. </p> <p> A <em>fold</em> is a catamorphism, but a catamorphism is a more general abstraction. For some data structures, the catamorphism is more powerful than the fold, but for collections, there's no difference. </p> <p> There's one edge case we need to be aware of, though. What if the collection is empty? </p> <h3 id="65483950f21d453ebe4e8949eac5751f"> Aggregation of empty containers <a href="#65483950f21d453ebe4e8949eac5751f" title="permalink">#</a> </h3> <p> What happens if you attempt to aggregate an empty collection? </p> <p> <pre>&gt; <span style="color:blue;">new</span>&nbsp;<span style="color:blue;">int</span>[0].Aggregate((x,&nbsp;y)&nbsp;=&gt;&nbsp;(x&nbsp;+&nbsp;y)&nbsp;%&nbsp;360) <span style="color:red;">Sequence contains no elements + System.Linq.Enumerable.Aggregate&lt;TSource&gt;(IEnumerable&lt;TSource&gt;, Func&lt;TSource, TSource, TSource&gt;)</span></pre> </p> <p> The <code>Aggregate</code> method throws an exception because it doesn't know how to deal with empty collections. The lambda expression you supply tells the <code>Aggregate</code> method how to combine two values into one. This is, for instance, how <a href="/2017/12/11/semigroups-accumulate">semigroups accumulate</a>. </p> <p> The lambda expression handles all cases where you have two or more values. If you have only a single value, then that's no problem either: </p> <p> <pre>&gt; <span style="color:blue;">new</span>[]&nbsp;{&nbsp;1337&nbsp;}.Aggregate((x,&nbsp;y)&nbsp;=&gt;&nbsp;(x&nbsp;+&nbsp;y)&nbsp;%&nbsp;360) 1337</pre> </p> <p> In that case, the lambda expression isn't involved at all, because the single value is simply returned without modification. In this example, this could even be interpreted as being incorrect, since you'd expect the result to be 257 (<code>1337 % 360</code>). </p> <p> It's safer to use the <code>Aggregate</code> overload that takes a <em>seed</em> value: </p> <p> <pre>&gt; <span style="color:blue;">new</span>&nbsp;<span style="color:blue;">int</span>[0].Aggregate(0,&nbsp;(x,&nbsp;y)&nbsp;=&gt;&nbsp;(x&nbsp;+&nbsp;y)&nbsp;%&nbsp;360) 0</pre> </p> <p> Not only does that gracefully handle empty collections, it also gives you a 'better' result for a single value: </p> <p> <pre>&gt; <span style="color:blue;">new</span>[]&nbsp;{&nbsp;1337&nbsp;}.Aggregate(0,&nbsp;(x,&nbsp;y)&nbsp;=&gt;&nbsp;(x&nbsp;+&nbsp;y)&nbsp;%&nbsp;360) 257</pre> </p> <p> This works better because the method always starts with the <em>seed</em> value, which means that even if there's only a single value (<code>1337</code>), the lambda expression still runs (<code>(0 + 1337) % 360</code>). </p> <p> This overload of <code>Aggregate</code> has a different type, though: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">TAccumulate</span>&nbsp;Aggregate&lt;<span style="color:#2b91af;">TSource</span>,&nbsp;<span style="color:#2b91af;">TAccumulate</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">TSource</span>&gt;&nbsp;source, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">TAccumulate</span>&nbsp;seed, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">TAccumulate</span>,&nbsp;<span style="color:#2b91af;">TSource</span>,&nbsp;<span style="color:#2b91af;">TAccumulate</span>&gt;&nbsp;func);</pre> </p> <p> Notice that the <code>func</code> doesn't require the accumulator to have the same type as elements from the <code>source</code> collection. This enables you to translate on the fly, so to speak. You can still use binary operations like the above modulo 360 addition, because that just implies that both <code>TSource</code> and <code>TAccumulate</code> are <code>int</code>. </p> <p> With this overload, you could, for example, use <a href="/2018/07/16/angular-addition-monoid">the Angle class</a> to perform the work: </p> <p> <pre>&gt; <span style="color:blue;">new</span>[]&nbsp;{&nbsp;42,&nbsp;1337,&nbsp;2112,&nbsp;90125,&nbsp;5040,&nbsp;7,&nbsp;1984&nbsp;} . .Aggregate(<span style="color:#2b91af;">Angle</span>.Identity,&nbsp;(a,&nbsp;i)&nbsp;=&gt;&nbsp;a.Add(<span style="color:#2b91af;">Angle</span>.FromDegrees(i))) [{ Angle = 207° }]</pre> </p> <p> Now the <code>seed</code> argument is <code>Angle.Identity</code>, which implies that <code>TAccumulate</code> is <code>Angle</code>. The <code>source</code> is still a collection of numbers, so <code>TSource</code> is <code>int</code>. Hence, I called the angle <code>a</code> and the integer <code>i</code> in the lambda expression. The output is an <code>Angle</code> object that represents 207°. </p> <p> That <code>Aggregate</code> overload is the catamorphism for collections. It reduces a collection to an object. </p> <h3 id="5a964dee9b1f4cdd8427ce0a0806d65d"> Catamorphisms and folds <a href="#5a964dee9b1f4cdd8427ce0a0806d65d" title="permalink">#</a> </h3> <p> Is <em>catamorphism</em> just an intimidating word for <em>aggregate</em>, <em>accumulate</em>, <em>fold</em>, or <em>reduce?</em> </p> <p> It took me a long time to be able to tell the difference, because in many cases, it seems that there's no difference. The purpose of this article series is to make the distinction clearer. In short, a catamorphism is a more general concept. </p> <p> <img src="/content/binary/catamorphism-and-fold-relations.png" alt="Catamorphisms and folds as sets, for various sum types."> </p> <p> For some data structures, such as <a href="/2018/05/24/church-encoded-boolean-values">Boolean values</a>, or <a href="/2018/05/28/church-encoded-natural-numbers">Peano numbers</a>, the catamorphism is all there is; no fold exists. For other data structures, such as <a href="/2018/06/04/church-encoded-maybe">Maybe</a> or collections, the catamorphism and the fold coincide. Still other data structures, such as <a href="/2018/06/11/church-encoded-either">Either</a> and <a href="/2018/08/06/a-tree-functor">trees</a>, support folding, but the fold is based on the catamorphism. For those types, there are operations you can do with the catamorphism that are impossible to implement with the <em>fold</em> function. One example is that a tree's catamorphism enables you to count its leaves; you can't do that with its <em>fold</em> function. </p> <p> You'll see plenty of examples in this article series: </p> <p> <ul> <li><a href="/2019/05/06/boolean-catamorphism">Boolean catamorphism</a></li> <li><a href="/2019/05/13/peano-catamorphism">Peano catamorphism</a></li> <li><a href="/2019/05/20/maybe-catamorphism">Maybe catamorphism</a></li> <li>List catamorphism</li> <li>Either catamorphism</li> <li>Tree catamorphism</li> <li>Full binary tree catamorphism</li> <li>Payment types catamorphism</li> </ul> </p> <p> Each of these articles will contain a fair amount of <a href="https://www.haskell.org">Haskell</a> code, but even if you're an object-oriented programmer who doesn't read Haskell, you should still scan them, as I'll start each with some C# examples. The Haskell code, by the way, is <a href="https://github.com/ploeh/FAlgebras">available on GitHub</a>. </p> <h3 id="cc687d1bebed47229cbdeffdf98fd666"> Greek <a href="#cc687d1bebed47229cbdeffdf98fd666" title="permalink">#</a> </h3> <p> When encountering a word like <em>catamorphism</em>, your reaction might be: <blockquote> "Catamorphism?! What does that even mean? It's all Greek to me." </blockquote> Indeed, it's Greek, as is so much of mathematical terminology. The <em>cata</em> prefix means 'down'; lots of words start with <em>cata</em>, like <em>catastrophe</em>, <em>catalogue</em>, <em>catatonia</em>, <em>catacomb</em>, etc. </p> <p> The <em>morph</em> suffix generally means 'shape'. While the <em>cata</em> prefix appears in common words like <em>catastrophe</em>, the <em>morph</em> suffix mostly appears in more academic contexts. Programmers will probably have encountered <em>polymorphism</em> and <em>skeuomorphism</em>, not to mention <a href="/2018/01/08/software-design-isomorphisms">isomorphism</a>. While <em>morphism</em> is heavily used in mathematics, other sciences use the suffix too, like <em>dimorphism</em> in biology. </p> <p> In category theory, a <em>morphism</em> is basically just an arrow that points from one object to another. Think of it as a function. </p> <p> If a morphism is just a function, why don't we just call it that, then? Is it really necessary with this intimidating terminology? Yes and no. </p> <p> If someone had originally figured all of this out in the context of mainstream programming, he or she would probably have used friendlier names, like <em>condense</em>, <em>reduce</em>, <em>fold</em>, and so on. This would have been more encouraging, although <a href="/2017/10/05/monoids-semigroups-and-friends">I'm not sure it would have been better</a>. </p> <p> In software architecture we use many overloaded terms. For example, what's a <em>service</em>, or a <em>client?</em> What does <em>tier</em> mean? Is it the same as a <em>layer</em>, or is it something different? What's the <a href="http://tomasp.net/blog/2015/library-frameworks">difference between a library and a framework</a>? </p> <p> At least a word like <em>catamorphism</em> is concise. It's not in common use, so isn't overloaded and vague. </p> <p> Another, more pragmatic, concern is that whether you like it or not, the terminology is already established. Mathematicians decided to name the concept <em>catamorphism</em>. While the name may seem intimidating, I prefer to teach concepts like these using established terminology. This means that if my articles are unclear, you can do further research with other resources. That's the benefit of established terminology, whether you like the specific words or not. </p> <h3 id="c179dad7693c48c2ae592d33cb2be792"> Summary <a href="#c179dad7693c48c2ae592d33cb2be792" title="permalink">#</a> </h3> <p> You can compose entire applications based on the abstractions of <em>map</em> and <em>reduce</em>. You can see one example of such a system in my <a href="https://blog.ploeh.dk/functional-architecture-with-fsharp">A Functional Architecture with F#</a> Pluralsight course. </p> <p> The terms <em>map</em> and <em>reduce</em> may, however, not be helpful, because it may not be clear exactly what types of data you can map, and what types you can reduce. One of the most important goals of this overall article series about universal abstractions is to help you identify when such software architectures apply. This is more often that you think. </p> <p> What sort of data can you map? You can map <em>functors</em>. While hardly finite, there's a catalogue of well-known functors, of which I've covered some, but not all. That catalogue contains data containers like <a href="/2018/03/26/the-maybe-functor">Maybe</a>, <a href="/2018/08/06/a-tree-functor">Tree</a>, <a href="/2018/09/10/the-lazy-functor">lazy computations</a>, <a href="/2018/09/24/asynchronous-functors">tasks</a>, and perhaps a score more. The catalogue of (actually useful) functors has, in my experience, a manageable size. </p> <p> Likewise you could ask: What sort of data can you reduce? How do you implement that reduction? Again, there's a compact set of well-known catamorphisms. How do you reduce a collection? You use its catamorphism (which is equal to a fold). How do you reduce a tree? You use its catamorphism. How do you reduce an Either object? You use its catamorphism. </p> <p> When we learn new programming languages, new libraries, new frameworks, we gladly invest time in learning hundreds, if not thousands, of keywords, APIs, extensibility points, and so on. May I offer, for your consideration, that your mental resources are better spent learning only a handful of universal abstractions? </p> <p> <strong>Next:</strong> <a href="/2019/05/06/boolean-catamorphism">Boolean catamorphism</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Applicative monoids https://blog.ploeh.dk/2019/04/22/applicative-monoids 2019-04-22T05:36:00+00:00 Mark Seemann <div id="post"> <p> <em>An applicative functor containing monoidal values itself forms a monoid.</em> </p> <p> This article is an instalment in <a href="/2018/10/01/applicative-functors">an article series about applicative functors</a>. An applicative functor is a <a href="https://bartoszmilewski.com/2014/01/14/functors-are-containers">data container</a> that supports combinations. If an applicative functor contains values of a type that gives rise to a <a href="/2017/10/06/monoids">monoid</a>, then the <a href="/2018/03/22/functors">functor</a> itself forms a monoid. </p> <p> In a previous article you learned that <a href="/2019/04/15/lazy-monoids">lazy computations of monoids remain monoids</a>. Furthermore, <a href="/2018/12/17/the-lazy-applicative-functor">a lazy computation is an applicative functor</a>, and it turns out that the result generalises. The result regarding lazy computation is just a special case. </p> <h3 id="3c6acb0da15b4ae8b78f5d8879b7efe3"> Monap <a href="#3c6acb0da15b4ae8b78f5d8879b7efe3" title="permalink">#</a> </h3> <p> Since version 4.11 of <a href="https://www.haskell.org">Haskell</a>'s <em>base</em> library, <code>Monoid</code> is a subset of <code>Semigroup</code>, so in order to create a <code>Monoid</code> instance, you must first define a <code>Semigroup</code> instance. </p> <p> In order to escape the need for flexible contexts, you'll have to define a wrapper <code>newtype</code> that'll be the instance. What should you call it? It's going to be an applicative functor of monoids, so perhaps something like <em>ApplicativeMonoid?</em> Nah, that's too long. <em>AppMon</em>, then? Sure, but how about flipping the terms: <em>MonApp?</em> That's better. Let's drop the last <em>p</em> and dispense with the <a href="https://en.wikipedia.org/wiki/Camel_case">Pascal case</a>: <em>Monap</em>. </p> <p> <em>Monap</em> almost looks like <em>Monad</em>, only with the last letter rotated half a revolution. This should allow for maximum confusion. </p> <p> To be clear, I normally don't advocate for droll word play when writing production code, but I occasionally do it in articles and presentations. The <em>Monap</em> in this article exists only to illustrate a point. It's not intended to be used. Furthermore, this article doesn't discuss monads at all, so the risk of confusion should, hopefully, be minimised. I may, however, regret this decision... </p> <h3 id="fb2568655a314bba8e417d06317b2690"> Applicative semigroup <a href="#fb2568655a314bba8e417d06317b2690" title="permalink">#</a> </h3> <p> First, introduce the wrapper <code>newtype</code>: </p> <p> <pre><span style="color:blue;">newtype</span>&nbsp;Monap&nbsp;f&nbsp;a&nbsp;=&nbsp;Monap&nbsp;{&nbsp;runMonap&nbsp;::&nbsp;f&nbsp;a&nbsp;}&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Eq</span>)</pre> </p> <p> This only states that there's a type called <code>Monap</code> that wraps some higher-kinded type <code>f a</code>; that is, a container <code>f</code> of values of the type <code>a</code>. The intent is that <code>f</code> is an applicative functor, hence the use of the letter <em>f</em>, but the type itself doesn't constrain <code>f</code> to any type class. </p> <p> The <code>Semigroup</code> instance does, though: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;(<span style="color:blue;">Applicative</span>&nbsp;f,&nbsp;<span style="color:blue;">Semigroup</span>&nbsp;a)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;<span style="color:blue;">Semigroup</span>&nbsp;(<span style="color:blue;">Monap</span>&nbsp;f&nbsp;a)&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;(Monap&nbsp;x)&nbsp;&lt;&gt;&nbsp;(Monap&nbsp;y)&nbsp;=&nbsp;Monap&nbsp;$&nbsp;liftA2&nbsp;<span style="color:#2b91af;">(&lt;&gt;)</span>&nbsp;x&nbsp;y </pre> </p> <p> This states that when <code>f</code> is a <code>Applicative</code> instance, and <code>a</code> is a <code>Semigroup</code> instance, then <code>Monap f a</code> is also a <code>Semigroup</code> instance. </p> <p> Here's an example of combining two applicative <a href="/2017/11/27/semigroups">semigroups</a>: </p> <p> <pre>λ&gt; Monap (Just (Max 42)) &lt;&gt; Monap (Just (Max 1337)) Monap {runMonap = Just (Max {getMax = 1337})}</pre> </p> <p> This example uses the <code>Max</code> semigroup container, and <code>Maybe</code> as the applicative functor. For <code>Max</code>, the <code>&lt;&gt;</code> operator returns the value that contains the highest value, which in this case is 1337. </p> <p> It even works when the applicative functor in question is <code>IO</code>: </p> <p> <pre>λ&gt; runMonap $ Monap (Sum &lt;$&gt; randomIO @Word8) &lt;&gt; Monap (Sum &lt;$&gt; randomIO @Word8) Sum {getSum = 165}</pre> </p> <p> This example uses <code>randomIO</code> to generate two random values. It uses the <code>TypeApplications</code> GHC extension to make <code>randomIO</code> generate <code>Word8</code> values. Each random number is projected into the <code>Sum</code> container, which means that <code>&lt;&gt;</code> will add the numbers together. In the above example, the result is 165, but if you evaluate the expression a second time, you'll most likely get another result: </p> <p> <pre>λ&gt; runMonap $ Monap (Sum &lt;$&gt; randomIO @Word8) &lt;&gt; Monap (Sum &lt;$&gt; randomIO @Word8) Sum {getSum = 246}</pre> </p> <p> You can also use linked list (<code>[]</code>) as the applicative functor. In this case, the result may be surprising (depending on what you expect): </p> <p> <pre>λ&gt; Monap [Product 2, Product 3] &lt;&gt; Monap [Product 4, Product 5, Product 6] Monap {runMonap = [Product {getProduct = 8},Product {getProduct = 10},Product {getProduct = 12}, Product {getProduct = 12},Product {getProduct = 15},Product {getProduct = 18}]}</pre> </p> <p> Notice that we get all the combinations of products: <em>2</em> multiplied with each element in the second list, followed by <em>3</em> multiplied by each of the elements in the second list. This shouldn't be that startling, though, since you've already, previously in this article series, seen several examples of how an applicative functor implies combinations. </p> <h3 id="33d4ff4f0fbf4e3cbf4d802b0f287f63"> Applicative monoid <a href="#33d4ff4f0fbf4e3cbf4d802b0f287f63" title="permalink">#</a> </h3> <p> With the <code>Semigroup</code> instance in place, you can now add the <code>Monoid</code> instance: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;(<span style="color:blue;">Applicative</span>&nbsp;f,&nbsp;<span style="color:blue;">Monoid</span>&nbsp;a)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;<span style="color:blue;">Monoid</span>&nbsp;(<span style="color:blue;">Monap</span>&nbsp;f&nbsp;a)&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;mempty&nbsp;=&nbsp;Monap&nbsp;$&nbsp;pure&nbsp;$&nbsp;mempty </pre> </p> <p> This is straightforward: you take the identity (<code>mempty</code>) of the monoid <code>a</code>, promote it to the applicative functor <code>f</code> with <code>pure</code>, and finally put that value into the <code>Monap</code> wrapper. </p> <p> This works fine as well: </p> <p> <pre>λ&gt; mempty :: Monap Maybe (Sum Integer) Monap {runMonap = Just (Sum {getSum = 0})} λ&gt; mempty :: Monap [] (Product Word8) Monap {runMonap = [Product {getProduct = 1}]}</pre> </p> <p> The identity laws also seem to hold: </p> <p> <pre>λ&gt; Monap (Right mempty) &lt;&gt; Monap (Right (Sum 2112)) Monap {runMonap = Right (Sum {getSum = 2112})} λ&gt; Monap ("foo", All False) &lt;&gt; Monap mempty Monap {runMonap = ("foo",All {getAll = False})}</pre> </p> <p> The last, right-identity example is interesting, because the applicative functor in question is a tuple. Tuples are <code>Applicative</code> instances when the first, or left, element is a <code>Monoid</code> instance. In other words, <code>f</code> is, in this case, <code>(,) String</code>. The <code>Monoid</code> instance that <code>Monap</code> sees as <code>a</code>, on the other hand, is <code>All</code>. </p> <p> Since <a href="/2017/10/30/tuple-monoids">tuples of monoids are themselves monoids</a>, however, I can get away with writing <code>Monap mempty</code> on the right-hand side, instead of the more elaborate template the other examples use: </p> <p> <pre>λ&gt; Monap ("foo", All False) &lt;&gt; Monap ("", mempty) Monap {runMonap = ("foo",All {getAll = False})}</pre> </p> <p> or perhaps even: </p> <p> <pre>λ&gt; Monap ("foo", All False) &lt;&gt; Monap (mempty, mempty) Monap {runMonap = ("foo",All {getAll = False})}</pre> </p> <p> Ultimately, all three alternatives mean the same. </p> <h3 id="6b484b60d19b4ef4a3716ceaa693cb8b"> Associativity <a href="#6b484b60d19b4ef4a3716ceaa693cb8b" title="permalink">#</a> </h3> <p> As usual, I'm not going to do the work of formally proving that the monoid laws hold for the <code>Monap</code> instances, but I'd like to share some QuickCheck properties that indicate that they do, starting with a property that verifies associativity: </p> <p> <pre><span style="color:#2b91af;">assocLaw</span>&nbsp;::&nbsp;(<span style="color:blue;">Eq</span>&nbsp;a,&nbsp;<span style="color:blue;">Show</span>&nbsp;a,&nbsp;<span style="color:blue;">Semigroup</span>&nbsp;a)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Property</span> assocLaw&nbsp;x&nbsp;y&nbsp;z&nbsp;=&nbsp;(x&nbsp;&lt;&gt;&nbsp;y)&nbsp;&lt;&gt;&nbsp;z&nbsp;===&nbsp;x&nbsp;&lt;&gt;&nbsp;(y&nbsp;&lt;&gt;&nbsp;z)</pre> </p> <p> This property is entirely generic. It'll verify associativity for any <code>Semigroup a</code>, not only for <code>Monap</code>. You can, however, run it for various <code>Monap</code> types, as well. You'll see how this is done a little later. </p> <h3 id="51973eba155f418e8903d37f6d3938d2"> Identity <a href="#51973eba155f418e8903d37f6d3938d2" title="permalink">#</a> </h3> <p> Likewise, you can write two properties that check left and right identity, respectively. </p> <p> <pre><span style="color:#2b91af;">leftIdLaw</span>&nbsp;::&nbsp;(<span style="color:blue;">Eq</span>&nbsp;a,&nbsp;<span style="color:blue;">Show</span>&nbsp;a,&nbsp;<span style="color:blue;">Monoid</span>&nbsp;a)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Property</span> leftIdLaw&nbsp;x&nbsp;=&nbsp;x&nbsp;===&nbsp;mempty&nbsp;&lt;&gt;&nbsp;x <span style="color:#2b91af;">rightIdLaw</span>&nbsp;::&nbsp;(<span style="color:blue;">Eq</span>&nbsp;a,&nbsp;<span style="color:blue;">Show</span>&nbsp;a,&nbsp;<span style="color:blue;">Monoid</span>&nbsp;a)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Property</span> rightIdLaw&nbsp;x&nbsp;=&nbsp;x&nbsp;===&nbsp;x&nbsp;&lt;&gt;&nbsp;mempty </pre> </p> <p> Again, this is entirely generic. These properties can be used to test the identity laws for any monoid, including <code>Monap</code>. </p> <h3 id="0cc5968e519c48a0816574e1dc1667fc"> Properties <a href="#0cc5968e519c48a0816574e1dc1667fc" title="permalink">#</a> </h3> <p> You can run each of these properties multiple time, for various different functors and monoids. As <code>Applicative</code> instances, I've used <code>Maybe</code>, <code>[]</code>, <code>(,) Any</code>, and <code>Identity</code>. As <code>Monoid</code> instances, I've used <code>String</code>, <code>Sum Integer</code>, <code>Max Int16</code>, and <code>[Float]</code>. Notice that a list (<code>[]</code>) is both an applicative functor as well as a monoid. In this test set, I've used it in both roles. </p> <p> <pre>tests&nbsp;= &nbsp;&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;testGroup&nbsp;<span style="color:#a31515;">&quot;Properties&quot;</span>&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;testProperty&nbsp;<span style="color:#a31515;">&quot;Associativity&nbsp;law,&nbsp;Maybe&nbsp;String&quot;</span>&nbsp;(assocLaw&nbsp;@(Monap&nbsp;Maybe&nbsp;String)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;testProperty&nbsp;<span style="color:#a31515;">&quot;Left&nbsp;identity&nbsp;law,&nbsp;Maybe&nbsp;String&quot;</span>&nbsp;(leftIdLaw&nbsp;@(Monap&nbsp;Maybe&nbsp;String)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;testProperty&nbsp;<span style="color:#a31515;">&quot;Right&nbsp;identity&nbsp;law,&nbsp;Maybe&nbsp;String&quot;</span>&nbsp;(rightIdLaw&nbsp;@(Monap&nbsp;Maybe&nbsp;String)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;testProperty&nbsp;<span style="color:#a31515;">&quot;Associativity&nbsp;law,&nbsp;[Sum&nbsp;Integer]&quot;</span>&nbsp;(assocLaw&nbsp;@(Monap&nbsp;<span style="color:blue;">[]</span>&nbsp;(Sum&nbsp;Integer))), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;testProperty&nbsp;<span style="color:#a31515;">&quot;Left&nbsp;identity&nbsp;law,&nbsp;[Sum&nbsp;Integer]&quot;</span>&nbsp;(leftIdLaw&nbsp;@(Monap&nbsp;<span style="color:blue;">[]</span>&nbsp;(Sum&nbsp;Integer))), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;testProperty&nbsp;<span style="color:#a31515;">&quot;Right&nbsp;identity&nbsp;law,&nbsp;[Sum&nbsp;Integer]&quot;</span>&nbsp;(rightIdLaw&nbsp;@(Monap&nbsp;<span style="color:blue;">[]</span>&nbsp;(Sum&nbsp;Integer))), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;testProperty&nbsp;<span style="color:#a31515;">&quot;Associativity&nbsp;law,&nbsp;(Any,&nbsp;Max&nbsp;Int8)&quot;</span>&nbsp;(assocLaw&nbsp;@(Monap&nbsp;(<span style="color:#2b91af;">(,)</span>&nbsp;Any)&nbsp;(Max&nbsp;Int8))), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;testProperty&nbsp;<span style="color:#a31515;">&quot;Left&nbsp;identity&nbsp;law,&nbsp;(Any,&nbsp;Max&nbsp;Int8)&quot;</span>&nbsp;(leftIdLaw&nbsp;@(Monap&nbsp;(<span style="color:#2b91af;">(,)</span>&nbsp;Any)&nbsp;(Max&nbsp;Int8))), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;testProperty&nbsp;<span style="color:#a31515;">&quot;Right&nbsp;identity&nbsp;law,&nbsp;(Any,&nbsp;Max&nbsp;Int8)&quot;</span>&nbsp;(rightIdLaw&nbsp;@(Monap&nbsp;(<span style="color:#2b91af;">(,)</span>&nbsp;Any)&nbsp;(Max&nbsp;Int8))), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;testProperty&nbsp;<span style="color:#a31515;">&quot;Associativity&nbsp;law,&nbsp;Identity&nbsp;[Float]&quot;</span>&nbsp;(assocLaw&nbsp;@(Monap&nbsp;Identity&nbsp;[Float])), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;testProperty&nbsp;<span style="color:#a31515;">&quot;Left&nbsp;identity&nbsp;law,&nbsp;Identity&nbsp;[Float]&quot;</span>&nbsp;(leftIdLaw&nbsp;@(Monap&nbsp;Identity&nbsp;[Float])), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;testProperty&nbsp;<span style="color:#a31515;">&quot;Right&nbsp;identity&nbsp;law,&nbsp;Identity&nbsp;[Float]&quot;</span>&nbsp;(rightIdLaw&nbsp;@(Monap&nbsp;Identity&nbsp;[Float])) &nbsp;&nbsp;&nbsp;&nbsp;] &nbsp;&nbsp;] </pre> </p> <p> All of these properties pass. </p> <h3 id="af35f2986a734a16be80590c86d0432d"> Summary <a href="#af35f2986a734a16be80590c86d0432d" title="permalink">#</a> </h3> <p> It seems that any applicative functor that contains monoidal values itself forms a monoid. The <code>Monap</code> type presented in this article only exists to demonstrate this conjecture; it's not intended to be <em>used</em>. </p> <p> If it holds, I think it's an interesting result, because it further enables you to reason about the properties of complex systems, based on the properties of simpler systems. </p> <p> <strong>Next: </strong> <a href="/2018/12/24/bifunctors">Bifunctors</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="9a3acf3cf4174e178dc9349e11fee488"> <div class="comment-author">Tyson Williams</div> <div class="comment-content"> <blockquote> It seems that any applicative functor that contains monoidal values itself forms a monoid. </blockquote> <p> Is it necessary for the functor to be applicative? Do know of a functor that contains monoidal values for which itself does <em>not</em> form a monoid? </p> </div> <div class="comment-date">2019-05-13 11:28 UTC</div> </div> <div class="comment" id="a164909adb884cd78b309a81029a2dd8"> <div class="comment-author"><a href="/">Mark Seemann</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. Yes, it's necessary for the functor to be applicative, because you need the applicative combination operator <code>&lt;*&gt;</code> in order to implement the combination. In C#, you'd need an <code>Apply</code> method <a href="/2018/10/01/applicative-functors/#cef395ee19644f30bfd1ad7a84b6f912">as shown here</a>. </p> <p> Technically, the monoidal <code>&lt;&gt;</code> operator for <code>Monap</code> is, as you can see, implemented with a call to <code>liftA2</code>. In Haskell, you can implement an instance of <code>Applicative</code> by implementing either <code>liftA2</code> or <code>&lt;*&gt;</code>, as well as <code>pure</code>. You usually see <code>Applicative</code> described by <code>&lt;*&gt;</code>, which is what I've done in <a href="/2018/10/01/applicative-functors">my article series on applicative functors</a>. If you do that, you can define <code>liftA2</code> by a combination of <code>&lt;*&gt;</code> and <code>fmap</code> (the <code>Select</code> method that defines functors). </p> <p> If you want to put this in C# terms, you need both <code>Select</code> and <code>Apply</code> in order to be able to lift a monoid into a functor. </p> <p> Is there a functor that contains monoidal values that itself doesn't form a monoid? </p> <p> Yes, indeed. In order to answer that question, we 'just' need to identify a functor that's <em>not</em> an applicative functor. Tuples are good examples. </p> <p> A <a href="https://blog.ploeh.dk/2018/12/31/tuple-bifunctor/#d918d0271c33406ba3047ef162212100">tuple forms a functor</a>, but in general nothing more than that. Consider a tuple where the first element is a <code>Guid</code>. It's a functor, but can you implement the following function? </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Guid</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;Apply&lt;<span style="color:#2b91af;">TResult</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Guid</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;&nbsp;selector, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Guid</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;source) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">NotImplementedException</span>(<span style="color:#a31515;">&quot;What&nbsp;would&nbsp;you&nbsp;write&nbsp;here?&quot;</span>); }</pre> </p> <p> You can pull the <code>T</code> value out of <code>source</code> and project it to a <code>TResult</code> value with <code>selector</code>, but you'll need to put it back in a <code>Tuple&lt;Guid, TResult&gt;</code>. Which <code>Guid</code> value are you going to use for that tuple? </p> <p> There's no clear answer to that question. </p> <p> More specifically, consider <code>Tuple&lt;Guid, int&gt;</code>. This is a functor that contains monoidal values. Let's say that we want to use the addition monoid over integers. How would you implement the following method? </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Guid</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;Add(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Guid</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;x,&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Guid</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">NotImplementedException</span>(<span style="color:#a31515;">&quot;What&nbsp;would&nbsp;you&nbsp;write&nbsp;here?&quot;</span>); }</pre> </p> <p> Again, you run into the issue that while you can pull the integers out of the tuples and add them together, there's no clear way to figure out which <code>Guid</code> value to put into the tuple that contains the sum. </p> <p> The issue particularly with tuples is that there's no general way to combine the leftmost values of the tuples. If there is - that is, if leftmost values form a monoid - then the tuple is also an applicative functor. For example, <code>Tuple&lt;string, int&gt;</code> is applicative and forms a monoid over addition: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;Apply&lt;<span style="color:#2b91af;">TResult</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;&nbsp;selector, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;source) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">Tuple</span>.Create( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;selector.Item1&nbsp;+&nbsp;source.Item1, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;selector.Item2(source.Item2)); } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;Add(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;x,&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">Tuple</span>.Create(x.Item1&nbsp;+&nbsp;y.Item1,&nbsp;x.Item2&nbsp;+&nbsp;y.Item2); }</pre> </p> <p> You can also implement <code>Add</code> with <code>Apply</code>, but you're going to need two <code>Apply</code> overloads to make it work. </p> <p> Incidentally, unbeknownst to me, the <code>Ap</code> wrapper was added to Haskell's <code>Data.Monoid</code> module 12 days before I wrote this article. In all but name, it's equivalent to the <code>Monap</code> wrapper presented here. </p> </div> <div class="comment-date">2019-05-14 20:44 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Lazy monoids https://blog.ploeh.dk/2019/04/15/lazy-monoids 2019-04-15T13:54:00+00:00 Mark Seemann <div id="post"> <p> <em>Lazy monoids are monoids. An article for object-oriented programmers.</em> </p> <p> This article is part of a <a href="/2017/10/06/monoids">series about monoids</a>. In short, a <em>monoid</em> is an associative binary operation with a neutral element (also known as <em>identity</em>). Previous articles have shown how more complex monoids arise from simpler monoids, such as <a href="/2017/10/30/tuple-monoids">tuple monoids</a>, <a href="/2017/11/06/function-monoids">function monoids</a>, and <a href="/2018/04/03/maybe-monoids">Maybe monoids</a>. This article shows another such result: how lazy computations of monoids itself form monoids. </p> <p> You'll see how simple this is through a series of examples. Specifically, you'll revisit several of the examples you've already seen in this article series. </p> <h3 id="a715cff45376401db9863a095a5e156d"> Lazy addition <a href="#a715cff45376401db9863a095a5e156d" title="permalink">#</a> </h3> <p> Perhaps the most intuitive monoid is <em>addition</em>. Lazy addition forms a monoid as well. In C#, you can implement this with a simple extension method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;Add(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;x,&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;(()&nbsp;=&gt;&nbsp;x.Value&nbsp;+&nbsp;y.Value); }</pre> </p> <p> This <code>Add</code> method simply adds two lazy integers together in a lazy computation. You use it like any other extension method: </p> <p> <pre><span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;x&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;y&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;sum&nbsp;=&nbsp;x.Add(y);</pre> </p> <p> I'll spare you the tedious listing of <a href="https://fscheck.github.io/FsCheck">FsCheck</a>-based properties that demonstrate that the monoid laws hold. We'll look at an example of such a set of properties later in this article, for one of the other monoids. </p> <h3 id="eda5d39029904d70995ebee84570cf60"> Lazy multiplication <a href="#eda5d39029904d70995ebee84570cf60" title="permalink">#</a> </h3> <p> Not surprisingly, I hope, you can implement multiplication over lazy numbers in the same way: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;Multiply(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;x,&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;(()&nbsp;=&gt;&nbsp;x.Value&nbsp;*&nbsp;y.Value); }</pre> </p> <p> Usage is similar to lazy addition: </p> <p> <pre><span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;x&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;y&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;product&nbsp;=&nbsp;x.Multiply(y);</pre> </p> <p> As is the case with lazy addition, this <code>Multiply</code> method currently only works with lazy <code>int</code> values. If you also want it to work with <code>long</code>, <code>short</code>, or other types of numbers, you'll have to add method overloads. </p> <h3 id="93b85bb3c55045dabdb16fd11167dad7"> Lazy Boolean monoids <a href="#93b85bb3c55045dabdb16fd11167dad7" title="permalink">#</a> </h3> <p> There are four monoids over Boolean values, although I've customarily only shown two of them: <em>and</em> and <em>or</em>. These also, trivially, work with lazy Boolean values: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">bool</span>&gt;&nbsp;And(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">bool</span>&gt;&nbsp;x,&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">bool</span>&gt;&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">bool</span>&gt;(()&nbsp;=&gt;&nbsp;x.Value&nbsp;&amp;&amp;&nbsp;y.Value); } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">bool</span>&gt;&nbsp;Or(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">bool</span>&gt;&nbsp;x,&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">bool</span>&gt;&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">bool</span>&gt;(()&nbsp;=&gt;&nbsp;x.Value&nbsp;||&nbsp;y.Value); }</pre> </p> <p> Given the previous examples, you'll hardly be surprised to see how you can use one of these extension methods: </p> <p> <pre><span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">bool</span>&gt;&nbsp;x&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">bool</span>&gt;&nbsp;y&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">bool</span>&gt;&nbsp;b&nbsp;=&nbsp;x.And(y);</pre> </p> <p> Have you noticed a pattern in how the lazy binary operations <code>Add</code>, <code>Multiply</code>, <code>And</code>, and <code>Or</code> are implemented? Could this be generalised? </p> <h3 id="12b7d0077481413cab6acf8f6bf696cf"> Lazy angular addition <a href="#12b7d0077481413cab6acf8f6bf696cf" title="permalink">#</a> </h3> <p> In a previous article you saw how <a href="/2018/07/16/angular-addition-monoid">angular addition forms a monoid</a>. Lazy angular addition forms a monoid as well, which you can implement with another extension method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Angle</span>&gt;&nbsp;Add(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Angle</span>&gt;&nbsp;x,&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Angle</span>&gt;&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Angle</span>&gt;(()&nbsp;=&gt;&nbsp;x.Value.Add(y.Value)); }</pre> </p> <p> Until now, you may have noticed that all the extension methods seemed to follow a common pattern that looks like this: </p> <p> <pre><span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Foo</span>&gt;(()&nbsp;=&gt;&nbsp;x.Value&nbsp;&diamond;&nbsp;y.Value);</pre> </p> <p> I've here used the diamond operator <code>&diamond;</code> as a place-holder for any sort of binary operation. My choice of that particular character is strongly influenced by <a href="https://www.haskell.org">Haskell</a>, where <a href="/2017/11/27/semigroups">semigroups</a> and monoids polymorphically are modelled with (among other options) the <code>&lt;&gt;</code> operator. </p> <p> The lazy angular addition implementation looks a bit different, though. This is because the original example uses an instance method to model the binary operation, instead of an infix operator such as <code>+</code>, <code>&&</code>, and so on. Given that the implementation of a lazy binary operation can also look like this, can you still imagine a generalisation? </p> <h3 id="669710b096144c6d8aaaca4426f8f795"> Lazy string concatenation <a href="#669710b096144c6d8aaaca4426f8f795" title="permalink">#</a> </h3> <p> If we follow the rough ordering of examples introduced in this article series about monoids, we've now reached <a href="/2017/10/10/strings-lists-and-sequences-as-a-monoid">concatenation as a monoid</a>. While various lists, arrays, and other sorts of collections also form a monoid over concatenation, in .NET, <code>IEnumerable&lt;T&gt;</code> already enables lazy evaluation, so I think it's more interesting to consider lazy string concatenation. </p> <p> The implementation, however, holds few surprises: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;Concat(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;x,&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">string</span>&gt;(()&nbsp;=&gt;&nbsp;x.Value&nbsp;+&nbsp;y.Value); }</pre> </p> <p> The overall result, so far, seems encouraging. All the basic monoids we've covered are also monoids when lazily computed. </p> <h3 id="29df873ebae145e2b903f6562825b69e"> Lazy money addition <a href="#29df873ebae145e2b903f6562825b69e" title="permalink">#</a> </h3> <p> The portfolio example from Kent Beck's book <a href="http://bit.ly/tddbe">Test-Driven Development By Example</a> also <a href="/2017/10/16/money-monoid">forms a monoid</a>. You can, again, implement an extension method that enables you to add lazy expressions together: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">IExpression</span>&gt;&nbsp;Plus( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">IExpression</span>&gt;&nbsp;source, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">IExpression</span>&gt;&nbsp;addend) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">IExpression</span>&gt;(()&nbsp;=&gt;&nbsp;source.Value.Plus(addend.Value)); }</pre> </p> <p> So far, you've seen several examples of implementations, but are they really monoids? All are clearly binary operations, but are they associative? Do identities exist? In other words, do the lazy binary operations obey the monoid laws? </p> <p> As usual, I'm not going to prove that they do, but I do want to share a set of FsCheck properties that demonstrate that the monoid laws hold. As an example, I'll share the properties for this lazy <code>Plus</code> method, but you can write similar properties for all of the above methods as well. </p> <p> You can verify the associativity law like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;LazyPlusIsAssociative( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">IExpression</span>&gt;&nbsp;x, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">IExpression</span>&gt;&nbsp;y, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">IExpression</span>&gt;&nbsp;z) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.Plus(y).Plus(z), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.Plus(y.Plus(z)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Compare</span>.UsingBank); }</pre> </p> <p> Here, <code>Compare.UsingBank</code> is just a <a href="http://xunitpatterns.com/Test%20Utility%20Method.html">test utility API</a> to make the code more readable: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Compare</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">ExpressionEqualityComparer</span>&nbsp;UsingBank&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ExpressionEqualityComparer</span>(); }</pre> </p> <p> This takes advantage of the overloads for <a href="https://xunit.github.io">xUnit.net</a>'s <code>Assert</code> methods that take custom equality comparers as an extra, optional argument. <code>ExpressionEqualityComparer</code> is implemented in the test code base: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ExpressionEqualityComparer</span>&nbsp;: &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IEqualityComparer</span>&lt;<span style="color:#2b91af;">IExpression</span>&gt;,&nbsp;<span style="color:#2b91af;">IEqualityComparer</span>&lt;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">IExpression</span>&gt;&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">Bank</span>&nbsp;bank; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;ExpressionEqualityComparer() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bank&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Bank</span>(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bank.AddRate(<span style="color:#a31515;">&quot;CHF&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;USD&quot;</span>,&nbsp;2); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;Equals(<span style="color:#2b91af;">IExpression</span>&nbsp;x,&nbsp;<span style="color:#2b91af;">IExpression</span>&nbsp;y) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;xm&nbsp;=&nbsp;bank.Reduce(x,&nbsp;<span style="color:#a31515;">&quot;USD&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;ym&nbsp;=&nbsp;bank.Reduce(y,&nbsp;<span style="color:#a31515;">&quot;USD&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">object</span>.Equals(xm,&nbsp;ym); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;GetHashCode(<span style="color:#2b91af;">IExpression</span>&nbsp;obj) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;bank.Reduce(obj,&nbsp;<span style="color:#a31515;">&quot;USD&quot;</span>).GetHashCode(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;Equals(<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">IExpression</span>&gt;&nbsp;x,&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">IExpression</span>&gt;&nbsp;y) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Equals(x.Value,&nbsp;y.Value); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;GetHashCode(<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">IExpression</span>&gt;&nbsp;obj) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;GetHashCode(obj.Value); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> If you think that the exchange rate between Dollars and Swiss Francs looks ridiculous, it's because I'm using the rate that Kent Beck used in his book, which is from 2002 (but otherwise timeless). </p> <p> The above property passes for hundreds of randomly generated input values, as is the case for this property, which verifies the left and right identity: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;LazyPlusHasIdentity(<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">IExpression</span>&gt;&nbsp;x) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(x,&nbsp;x.Plus(<span style="color:#2b91af;">Plus</span>.Identity.ToLazy()),&nbsp;<span style="color:#2b91af;">Compare</span>.UsingBank); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(x,&nbsp;<span style="color:#2b91af;">Plus</span>.Identity.ToLazy().Plus(x),&nbsp;<span style="color:#2b91af;">Compare</span>.UsingBank); }</pre> </p> <p> These properties are just examples, not proofs. Still, they give confidence that lazy computations of monoids are themselves monoids. </p> <h3 id="6b5aa8f3a0b34d4c9d145eac3e59fe9b"> Lazy Roster combinations <a href="#6b5aa8f3a0b34d4c9d145eac3e59fe9b" title="permalink">#</a> </h3> <p> The last example you'll get in this article is the <code>Roster</code> example from the article on <a href="/2017/10/30/tuple-monoids">tuple monoids</a>. Here's yet another extension method that enables you to combine to lazy rosters: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Roster</span>&gt;&nbsp;Combine(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Roster</span>&gt;&nbsp;x,&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Roster</span>&gt;&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Roster</span>&gt;(()&nbsp;=&gt;&nbsp;x.Value.Combine(y.Value)); }</pre> </p> <p> At this point it should be clear that there's essentially two variations in how the above extension methods are implemented. One variation is when the binary operation is implemented with an infix operator (like <code>+</code>, <code>||</code>, and so on), and another variation is when it's modelled as an instance method. How do these implementations generalise? </p> <p> I'm sure you could come up with an ad-hoc higher-order function, abstract base class, or interface to model such a generalisation, but I'm not motivated by <a href="/2018/09/17/typing-is-not-a-programming-bottleneck">saving keystrokes</a>. What I'm trying to uncover with this <a href="/2017/10/04/from-design-patterns-to-category-theory">overall article series</a> is how universal abstractions apply to programming. </p> <p> Which universal abstraction is in play here? </p> <h3 id="6d247327cf214fa698dc9ec29693bd6f"> Lazy monoids as applicative operations <a href="#6d247327cf214fa698dc9ec29693bd6f" title="permalink">#</a> </h3> <p> <a href="/2018/12/17/the-lazy-applicative-functor">Lazy&lt;T&gt; forms an applicative functor</a>. Using appropriate <code>Apply</code> overloads, you can rewrite all the above implementations in applicative style. Here's lazy addition: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;Add(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;x,&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;f&nbsp;=&nbsp;(i,&nbsp;j)&nbsp;=&gt;&nbsp;i&nbsp;+&nbsp;j; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;f.Apply(x).Apply(y); }</pre> </p> <p> This first declares a <code>Func</code> value <code>f</code> that invokes the non-lazy binary operation, in this case <code>+</code>. Next, you can leverage the applicative nature of <code>Lazy&lt;T&gt;</code> to apply <code>f</code> to the two lazy values <code>x</code> and <code>y</code>. </p> <p> Multiplication, as well as the Boolean operations, follow the exact same template: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;Multiply(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;x,&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;f&nbsp;=&nbsp;(i,&nbsp;j)&nbsp;=&gt;&nbsp;i&nbsp;*&nbsp;j; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;f.Apply(x).Apply(y); } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">bool</span>&gt;&nbsp;And(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">bool</span>&gt;&nbsp;x,&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">bool</span>&gt;&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">bool</span>,&nbsp;<span style="color:blue;">bool</span>,&nbsp;<span style="color:blue;">bool</span>&gt;&nbsp;f&nbsp;=&nbsp;(b1,&nbsp;b2)&nbsp;=&gt;&nbsp;b1&nbsp;&amp;&amp;&nbsp;b2; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;f.Apply(x).Apply(y); } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">bool</span>&gt;&nbsp;Or(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">bool</span>&gt;&nbsp;x,&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">bool</span>&gt;&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">bool</span>,&nbsp;<span style="color:blue;">bool</span>,&nbsp;<span style="color:blue;">bool</span>&gt;&nbsp;f&nbsp;=&nbsp;(b1,&nbsp;b2)&nbsp;=&gt;&nbsp;b1&nbsp;||&nbsp;b2; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;f.Apply(x).Apply(y); }</pre> </p> <p> Notice that in all four implementations, the second line of code is verbatim the same: <code>return f.Apply(x).Apply(y);</code> </p> <p> Does this generalisation also hold when the underlying, non-lazy binary operation is modelled as an instance method, as is the case of e.g. angular addition? Yes, indeed: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Angle</span>&gt;&nbsp;Add(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Angle</span>&gt;&nbsp;x,&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Angle</span>&gt;&nbsp;y) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Angle</span>,&nbsp;<span style="color:#2b91af;">Angle</span>,&nbsp;<span style="color:#2b91af;">Angle</span>&gt;&nbsp;f&nbsp;=&nbsp;(i,&nbsp;j)&nbsp;=&gt;&nbsp;i.Add(j); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;f.Apply(x).Apply(y); }</pre> </p> <p> You can implement the <code>Combine</code> method for lazy <code>Roster</code> objects in the same way, as well as <code>Plus</code> for lazy monetary expressions. The latter is worth revisiting. </p> <h3 id="445af7622459483ab64f4ff31ceb1c2e"> Using the Lazy functor over portfolio expressions <a href="#445af7622459483ab64f4ff31ceb1c2e" title="permalink">#</a> </h3> <p> The lazy <code>Plus</code> implementation looks like all of the above <code>Apply</code>-based implementations: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">IExpression</span>&gt;&nbsp;Plus( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">IExpression</span>&gt;&nbsp;source,&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">IExpression</span>&gt;&nbsp;addend) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">IExpression</span>,&nbsp;<span style="color:#2b91af;">IExpression</span>,&nbsp;<span style="color:#2b91af;">IExpression</span>&gt;&nbsp;f&nbsp;=&nbsp;(x,&nbsp;y)&nbsp;=&gt;&nbsp;x.Plus(y); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;f.Apply(source).Apply(addend); }</pre> </p> <p> In my article, however, you saw how, when <code>Plus</code> is a monoid, you can implement <code>Times</code> as an extension method. Can you implement a lazy version of <code>Times</code> as well? Must it be another extension method? </p> <p> Yes, but instead of an ad-hoc implementation, you can take advantage of the functor nature of Lazy: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">IExpression</span>&gt;&nbsp;Times(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">IExpression</span>&gt;&nbsp;exp,&nbsp;<span style="color:blue;">int</span>&nbsp;multiplier) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;exp.Select(x&nbsp;=&gt;&nbsp;x.Times(multiplier)); }</pre> </p> <p> Notice that instead of explicitly reaching into the lazy <code>Value</code>, you can simply call <code>Select</code> on <code>exp</code>. This lazily projects the <code>Times</code> operation, while preserving the invariants of <code>Lazy&lt;T&gt;</code> (i.e. that the computation is deferred until you ultimately access the <code>Value</code> property). </p> <p> You can implement a lazy version of <code>Reduce</code> in the same way: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Money</span>&gt;&nbsp;Reduce(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">IExpression</span>&gt;&nbsp;exp,&nbsp;<span style="color:#2b91af;">Bank</span>&nbsp;bank,&nbsp;<span style="color:blue;">string</span>&nbsp;to) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;exp.Select(x&nbsp;=&gt;&nbsp;x.Reduce(bank,&nbsp;to)); }</pre> </p> <p> The question is, however, is it even worthwhile? Do you need to create all these overloads, or could you just leverage <code>Select</code> when you have a lazy value? </p> <p> For example, if the above <code>Reduce</code> overload didn't exist, you'd still be able to work with the portfolio API like this: </p> <p> <pre><span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">IExpression</span>&gt;&nbsp;portfolio&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Money</span>&gt;&nbsp;result&nbsp;=&nbsp;portfolio.Select(x&nbsp;=&gt;&nbsp;x.Reduce(bank,&nbsp;<span style="color:#a31515;">&quot;USD&quot;</span>));</pre> </p> <p> If you only occasionally use <code>Reduce</code>, then perhaps this is good enough. If you frequently call <code>Reduce</code>, however, it might be worth to add the above overload, in which case you could then instead write: </p> <p> <pre><span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">IExpression</span>&gt;&nbsp;portfolio&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Money</span>&gt;&nbsp;result&nbsp;=&nbsp;portfolio.Reduce(bank,&nbsp;<span style="color:#a31515;">&quot;USD&quot;</span>);</pre> </p> <p> In both cases, however, I think that you'd be putting the concept of an applicative functor to good use. </p> <h3 id="a5942465683c459a92457ea13e3bd21d"> Towards generalisation <a href="#a5942465683c459a92457ea13e3bd21d" title="permalink">#</a> </h3> <p> Is the applicative style better than the initial ad-hoc implementations? That depends on how you evaluate 'better'. If you count lines of code, then the applicative style is twice as verbose as the ad-hoc implementations. In other words, this: </p> <p> <pre><span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;(()&nbsp;=&gt;&nbsp;x.Value&nbsp;+&nbsp;y.Value);</pre> </p> <p> seems simpler than this: </p> <p> <pre><span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;f&nbsp;=&nbsp;(i,&nbsp;j)&nbsp;=&gt;&nbsp;i&nbsp;+&nbsp;j; <span style="color:blue;">return</span>&nbsp;f.Apply(x).Apply(y);</pre> </p> <p> This is, however, mostly because C# is too weak to express such abstractions in an elegant way. In <a href="https://fsharp.org">F#</a>, using the custom <code>&lt;*&gt;</code> operator from <a href="/2018/12/17/the-lazy-applicative-functor">the article on the Lazy applicative functor</a>, you could express the lazy addition as simply as: </p> <p> <pre><span style="color:blue;">lazy</span>&nbsp;(+)&nbsp;&lt;*&gt;&nbsp;x&nbsp;&lt;*&gt;&nbsp;y</pre> </p> <p> In <a href="https://www.haskell.org">Haskell</a> (if we, once more, pretend that <code>Identity</code> is equivalent to <code>Lazy</code>), you can simplify even further to: </p> <p> <pre>(+) &lt;$&gt; x &lt;*&gt; y</pre> </p> <p> Or rather, if you want it in <a href="https://en.wikipedia.org/wiki/Tacit_programming">point-free style</a>, <code>liftA2 (+)</code>. </p> <h3 id="f418f44f772b44c287365c7054d51aa1"> Summary <a href="#f418f44f772b44c287365c7054d51aa1" title="permalink">#</a> </h3> <p> The point of this article series isn't to save keystrokes, but to identify universal laws of computing, even as they relate to object-oriented programming. The pattern seems clear enough that I dare propose the following: </p> <p> <em>All monoids remain monoids under lazy computation.</em> </p> <p> In a future article I'll offer further support for that proposition. </p> <p> <strong>Next:</strong> <a href="/2017/11/20/monoids-accumulate">Monoids accumulate</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. A pure Test Spy https://blog.ploeh.dk/2019/04/08/a-pure-test-spy 2019-04-08T06:02:00+00:00 Mark Seemann <div id="post"> <p> <em>Ad-hoc Test Spies can be implemented in Haskell using the Writer monad.</em> </p> <p> In a previous article on <a href="/2019/03/11/an-example-of-state-based-testing-in-haskell">state-based testing in Haskell</a>, I made the following throw-away statement: <blockquote> "you <em>could</em> write an ad-hoc Mock using, for example, the Writer monad" </blockquote> In that article, I didn't pursue that thought, since the theme was another. Instead, I'll expand on it here. </p> <h3 id="552a47e82a99466ebb8fe9d840b80a2f"> Test Double continuum <a href="#552a47e82a99466ebb8fe9d840b80a2f" title="permalink">#</a> </h3> <p> More than a decade ago, I wrote an MSDN Magazine article called <em>Exploring The Continuum Of Test Doubles</em>. It was in the September 2007 issue, and since then, the Magazine restructured so that the article is no longer available online. You can still <a href="https://msdn.microsoft.com/en-us/magazine/msdn-magazine-issues.aspx">download the entire issue as a single file</a> and read the article offline, should you want to. </p> <p> In the article, I made the argument that the classification of <a href="http://xunitpatterns.com/Test%20Double.html">Test Doubles</a> presented in the excellent <a href="http://bit.ly/xunitpatterns">xUnit Test Patterns</a> should be thought of more as a continuum with vague and fuzzy transitions, rather than discrete categories. </p> <p> <img src="/content/binary/msdn-test-double-continuum.png" alt="Spectrum of Test Doubles."> </p> <p> This figure appeared in the original article. Given that the entire MSDN Magazine issue is available for free, and that I'm the original author of the article, I consider it fair use to repeat it here. </p> <p> The point is that it's not always clear whether a Test Double is, say, a Mock, or a Spy. What I'll show you in this article is closer to a Test Spy than to a Mock, but since the distinction is blurred anyway, I think that I can get away with it. </p> <h3 id="200ba4d9157442fe9caef2429371f7c6"> Test Spy <a href="#200ba4d9157442fe9caef2429371f7c6" title="permalink">#</a> </h3> <p> <em>xUnit Test Patterns</em> defines a Test Spy as a Test Double that captures "the indirect output calls made to another component by the SUT [System Under Test] for later verification by the test." When, as shown in <a href="/2019/02/25/an-example-of-interaction-based-testing-in-c">a previous article</a>, you use <code>Mock&lt;T&gt;.Verify</code> to assert than an interaction took place, you're using the Test Double more as a Spy than a Mock: </p> <p> <pre>repoTD.Verify(r&nbsp;=&gt;&nbsp;r.Update(user));</pre> </p> <p> Strictly speaking, a Mock is a Test Double that <em>immediately</em> fails the test if any unexpected interaction takes place. People often call those <em>Strict Mocks</em>, but according to the book, that's a Mock. If the Test Double only records what happens, so that you can later query it to verify whether some interaction took place, it's closer to being a Test Spy. </p> <p> Whether you call it a Mock or a Spy, you can implement verification similar to the above <code>Verify</code> method in functional programming using the Writer monad. </p> <h3 id="cf14cbf8e65a4dd6a47f879c1b850de5"> Writer-based Spy <a href="#cf14cbf8e65a4dd6a47f879c1b850de5" title="permalink">#</a> </h3> <p> I'll show you a single example in <a href="https://www.haskell.org">Haskell</a>. In <a href="http://blog.ploeh.dk/2018/07/30/flattening-arrow-code-using-a-stack-of-monads">a previous article</a>, you saw a simplified function to implement a restaurant reservation feature, repeated here for your convenience: </p> <p> <pre><span style="color:#2b91af;">tryAccept</span>&nbsp;::&nbsp;<span style="color:#2b91af;">Int</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Reservation</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">MaybeT</span>&nbsp;<span style="color:blue;">ReservationsProgram</span>&nbsp;<span style="color:#2b91af;">Int</span> tryAccept&nbsp;capacity&nbsp;reservation&nbsp;=&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;guard&nbsp;=&lt;&lt;&nbsp;isReservationInFuture&nbsp;reservation &nbsp;&nbsp;reservations&nbsp;&lt;-&nbsp;readReservations&nbsp;$&nbsp;reservationDate&nbsp;reservation &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;reservedSeats&nbsp;=&nbsp;<span style="color:blue;">sum</span>&nbsp;$&nbsp;reservationQuantity&nbsp;&lt;$&gt;&nbsp;reservations &nbsp;&nbsp;guard&nbsp;$&nbsp;reservedSeats&nbsp;+&nbsp;reservationQuantity&nbsp;reservation&nbsp;&lt;=&nbsp;capacity &nbsp;&nbsp;create&nbsp;$&nbsp;reservation&nbsp;{&nbsp;reservationIsAccepted&nbsp;=&nbsp;True&nbsp;} </pre> </p> <p> This function runs in the <code>MaybeT</code> monad, so the two <code>guard</code> functions could easily prevent if from running 'to completion'. In the happy path, though, execution should reach 'the end' of the function and call the <code>create</code> function. </p> <p> In order to test this happy path, you'll need to not only run a test-specific interpreter over the <code>ReservationsProgram</code> free monad, you should also verify that <code>reservationIsAccepted</code> is <code>True</code>. </p> <p> You can do this using the <code>Writer</code> monad to implement a Test Spy: </p> <p> <pre>testProperty&nbsp;<span style="color:#a31515;">&quot;tryAccept,&nbsp;happy&nbsp;path&quot;</span>&nbsp;$&nbsp;\ &nbsp;&nbsp;(NonNegative&nbsp;i) &nbsp;&nbsp;(<span style="color:blue;">fmap</span>&nbsp;getReservation&nbsp;-&gt;&nbsp;reservations) &nbsp;&nbsp;(ArbReservation&nbsp;reservation) &nbsp;&nbsp;expected &nbsp;&nbsp;-&gt; &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;spy&nbsp;(IsReservationInFuture&nbsp;_&nbsp;next)&nbsp;=&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;next&nbsp;True &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;spy&nbsp;(ReadReservations&nbsp;_&nbsp;next)&nbsp;=&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;next&nbsp;reservations &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;spy&nbsp;(Create&nbsp;r&nbsp;next)&nbsp;=&nbsp;tell&nbsp;[r]&nbsp;&gt;&gt;&nbsp;<span style="color:blue;">return</span>&nbsp;(next&nbsp;expected) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reservedSeats&nbsp;=&nbsp;<span style="color:blue;">sum</span>&nbsp;$&nbsp;reservationQuantity&nbsp;&lt;$&gt;&nbsp;reservations &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;capacity&nbsp;=&nbsp;reservedSeats&nbsp;+&nbsp;reservationQuantity&nbsp;reservation&nbsp;+&nbsp;i &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(actual,&nbsp;observedReservations)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;runWriter&nbsp;$&nbsp;foldFreeT&nbsp;spy&nbsp;$&nbsp;runMaybeT&nbsp;$&nbsp;tryAccept&nbsp;capacity&nbsp;reservation &nbsp;&nbsp;<span style="color:blue;">in</span>&nbsp;&nbsp;Just&nbsp;expected&nbsp;==&nbsp;actual&nbsp;&amp;&amp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[True]&nbsp;==&nbsp;(reservationIsAccepted&nbsp;&lt;$&gt;&nbsp;observedReservations)</pre> </p> <p> This test is an <a href="http://blog.ploeh.dk/2018/05/07/inlined-hunit-test-lists">inlined</a> QuickCheck-based property. The entire <a href="https://github.com/ploeh/dependency-injection-revisited">source code is available on GitHub</a>. </p> <p> Notice the <code>spy</code> function. As the name implies, it's the Test Spy for the test. Its full type is: </p> <p> <pre>spy&nbsp;::&nbsp;Monad&nbsp;m&nbsp;=&gt;&nbsp;ReservationsInstruction&nbsp;a&nbsp;-&gt;&nbsp;WriterT&nbsp;[Reservation]&nbsp;m&nbsp;a</pre> </p> <p> This is a function that, for a given <code>ReservationsInstruction</code> value returns a <code>WriterT</code> value where the type of data being written is <code>[Reservation]</code>. The function only writes to the writer context in one of the three cases: the <code>Create</code> case. The <code>Create</code> case carries with it a <code>Reservation</code> value here named <code>r</code>. Before returning the <code>next</code> step in interpreting the free monad, the <code>spy</code> function calls <code>tell</code>, thereby writing a singleton list of <code>[r]</code> to the writer context. </p> <p> In the Act phase of the test, it calls the <code>tryAccept</code> function and proceeds to interpret the result, which is a <code>MaybeT ReservationsProgram Int</code> value. Calling <code>runMaybeT</code> produces a <code>ReservationsProgram (Maybe Int)</code>, which you can then interpret with <code>foldFreeT spy</code>. This returns a <code>Writer [Reservation] (Maybe Int)</code>, which you can finally run with <code>runWriter</code> to get a <code>(Maybe Int, [Reservation])</code> tuple. Thus, <code>actual</code> is a <code>Maybe Int</code> value, and <code>observedReservations</code> is a <code>[Reservation]</code> value - the reservation that was written by <code>spy</code> using <code>tell</code>. </p> <p> The Assert phase of the test is a Boolean expression that checks that <code>actual</code> is as expected, and that <code>reservationIsAccepted</code> of the observed reservation is <code>True</code>. </p> <p> It takes a little while to make the pieces of the puzzle fit, but it's basically just standard Haskell library functions clicked together. </p> <h3 id="fd3478dba1a241a7bb95d9a97140ebac"> Summary <a href="#fd3478dba1a241a7bb95d9a97140ebac" title="permalink">#</a> </h3> <p> People sometimes ask me: <em>How do Mocks and Stubs work in functional programming?</em> </p> <p> In general, my answer is that you don't need Mocks and Stubs because when functions are <a href="https://en.wikipedia.org/wiki/Pure_function">pure</a>, you don't need to to test interactions. Sooner or later, though, you may run into higher-level interactions, even if they're <a href="http://blog.ploeh.dk/2017/07/10/pure-interactions">pure interactions</a>, and you'll likely want to unit test those. </p> <p> In a previous article you saw how to apply <a href="/2019/03/11/an-example-of-state-based-testing-in-haskell">state-based testing in Haskell, using the State monad</a>. In this article you've seen how you can create ad-hoc Mocks or Spies with the Writer monad. No auto-magical test-specific 'isolation frameworks' are required. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. An example of state-based testing in C# https://blog.ploeh.dk/2019/04/01/an-example-of-state-based-testing-in-c 2019-04-01T05:50:00+00:00 Mark Seemann <div id="post"> <p> <em>An example of avoiding Mocks and Stubs in C# unit testing.</em> </p> <p> This article is an instalment in an article series about how to move <a href="/2019/02/18/from-interaction-based-to-state-based-testing">from interaction-based testing to state-based testing</a>. In the previous article, you saw <a href="/2019/03/25/an-example-of-state-based-testing-in-f">an example of a pragmatic state-based test in F#</a>. You can now take your new-found knowledge and apply it to the <a href="/2019/02/25/an-example-of-interaction-based-testing-in-c">original C# example</a>. </p> <p> In the spirit of <a href="http://bit.ly/xunitpatterns">xUnit Test Patterns</a>, in this article you'll see how to refactor the tests while keeping the implementation code constant. </p> <p> The code shown in this article is <a href="https://github.com/ploeh/UserManagement">available on GitHub</a>. </p> <h3 id="da7f393fdef24bae9e72c4bcad7e8373"> Connect two users <a href="#da7f393fdef24bae9e72c4bcad7e8373" title="permalink">#</a> </h3> <p> The <a href="/2019/02/25/an-example-of-interaction-based-testing-in-c">previous article</a> provides more details on the System Under Test (SUT), but here it is, repeated, for your convenience: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ConnectionsController</span>&nbsp;:&nbsp;<span style="color:#2b91af;">ApiController</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;ConnectionsController( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IUserReader</span>&nbsp;userReader, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IUserRepository</span>&nbsp;userRepository) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UserReader&nbsp;=&nbsp;userReader; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UserRepository&nbsp;=&nbsp;userRepository; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IUserReader</span>&nbsp;UserReader&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IUserRepository</span>&nbsp;UserRepository&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IHttpActionResult</span>&nbsp;Post(<span style="color:blue;">string</span>&nbsp;userId,&nbsp;<span style="color:blue;">string</span>&nbsp;otherUserId) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;userRes&nbsp;=&nbsp;UserReader.Lookup(userId).SelectError( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;error&nbsp;=&gt;&nbsp;error.Accept(<span style="color:#2b91af;">UserLookupError</span>.Switch( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onInvalidId:&nbsp;<span style="color:#a31515;">&quot;Invalid&nbsp;user&nbsp;ID.&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onNotFound:&nbsp;&nbsp;<span style="color:#a31515;">&quot;User&nbsp;not&nbsp;found.&quot;</span>))); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;otherUserRes&nbsp;=&nbsp;UserReader.Lookup(otherUserId).SelectError( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;error&nbsp;=&gt;&nbsp;error.Accept(<span style="color:#2b91af;">UserLookupError</span>.Switch( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onInvalidId:&nbsp;<span style="color:#a31515;">&quot;Invalid&nbsp;ID&nbsp;for&nbsp;other&nbsp;user.&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onNotFound:&nbsp;&nbsp;<span style="color:#a31515;">&quot;Other&nbsp;user&nbsp;not&nbsp;found.&quot;</span>))); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;connect&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;user&nbsp;<span style="color:blue;">in</span>&nbsp;userRes &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;otherUser&nbsp;<span style="color:blue;">in</span>&nbsp;otherUserRes &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;Connect(user,&nbsp;otherUser); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;connect.SelectBoth(Ok,&nbsp;BadRequest).Bifold(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">User</span>&nbsp;Connect(<span style="color:#2b91af;">User</span>&nbsp;user,&nbsp;<span style="color:#2b91af;">User</span>&nbsp;otherUser) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user.Connect(otherUser); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UserRepository.Update(user); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;otherUser; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This implementation code is a simplification of the code example that serves as an example running through my two <a href="https://cleancoders.com">Clean Coders</a> videos, <a href="https://cleancoders.com/episode/humane-code-real-episode-4/show">Church Visitor</a> and <a href="https://cleancoders.com/episode/humane-code-real-episode-5/show">Preserved in translation</a>. </p> <h3 id="4e6812ee83ad4cda9b8deedef99ab4e1"> A Fake database <a href="#4e6812ee83ad4cda9b8deedef99ab4e1" title="permalink">#</a> </h3> <p> As in the previous article, you can define a test-specific <a href="http://xunitpatterns.com/Fake%20Object.html">Fake</a> database: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">FakeDB</span>&nbsp;:&nbsp;<span style="color:#2b91af;">Collection</span>&lt;<span style="color:#2b91af;">User</span>&gt;,&nbsp;<span style="color:#2b91af;">IUserReader</span>,&nbsp;<span style="color:#2b91af;">IUserRepository</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IResult</span>&lt;<span style="color:#2b91af;">User</span>,&nbsp;<span style="color:#2b91af;">IUserLookupError</span>&gt;&nbsp;Lookup(<span style="color:blue;">string</span>&nbsp;id) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!(<span style="color:blue;">int</span>.TryParse(id,&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="color:blue;">int</span>&nbsp;i))) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">Result</span>.Error&lt;<span style="color:#2b91af;">User</span>,&nbsp;<span style="color:#2b91af;">IUserLookupError</span>&gt;(<span style="color:#2b91af;">UserLookupError</span>.InvalidId); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;user&nbsp;=&nbsp;<span style="color:blue;">this</span>.FirstOrDefault(u&nbsp;=&gt;&nbsp;u.Id&nbsp;==&nbsp;i); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(user&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">Result</span>.Error&lt;<span style="color:#2b91af;">User</span>,&nbsp;<span style="color:#2b91af;">IUserLookupError</span>&gt;(<span style="color:#2b91af;">UserLookupError</span>.NotFound); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">Result</span>.Success&lt;<span style="color:#2b91af;">User</span>,&nbsp;<span style="color:#2b91af;">IUserLookupError</span>&gt;(user); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;IsDirty&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Update(<span style="color:#2b91af;">User</span>&nbsp;user) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IsDirty&nbsp;=&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(!Contains(user)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Add(user); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This is one of the few cases where I find inheritance more convenient than composition. By deriving from <code>Collection&lt;User&gt;</code>, you don't have to explicitly write code to expose a <em>Retrieval Interface</em>. The entirety of a standard collection API is already available via the base class. Had this class been part of a public API, I'd be concerned that inheritance could introduce future breaking changes, but as part of a suite of unit tests, I hope that I've made the right decision. </p> <p> Although you can derive a Fake database from a base class, you can still implement required interfaces - in this case <code>IUserReader</code> and <code>IUserRepository</code>. The <code>Update</code> method is the easiest one to implement, since it simply sets the <code>IsDirty</code> flag to <code>true</code> and adds the <code>user</code> if it's not already part of the collection. </p> <p> The <code>IsDirty</code> flag is the only custom Retrieval Interface added to the <code>FakeDB</code> class. As the previous article explains, this flag provides a convenient was to verify whether or not the database has changed. </p> <p> The <code>Lookup</code> method is a bit more involved, since it has to support all three outcomes implied by the protocol: <ul> <li>If the <code>id</code> is invalid, a result to that effect is returned.</li> <li>If the user isn't found, a result to that effect is returned.</li> <li>If the user with the requested <code>id</code> is found, then that user is returned.</li> </ul> This is a typical quality of a Fake: it contains <em>some</em> production-like behaviour, while still taking shortcuts compared to a full production implementation. In this case, it properly adheres to the protocol implied by the interface and protects its invariants. It still doesn't implement persistent storage, though. </p> <h3 id="92480e7451ad4c02a524f9fcd1ef1c8d"> Happy path test case <a href="#92480e7451ad4c02a524f9fcd1ef1c8d" title="permalink">#</a> </h3> <p> This is all you need in terms of <a href="http://xunitpatterns.com/Test%20Double.html">Test Doubles</a>. You now have a test-specific <code>IUserReader</code> and <code>IUserRepository</code> implementation that you can pass to the <code>Post</code> method. Notice that a single class implements multiple interfaces. This is often key to be able to implement a Fake object in the first place. </p> <p> Like in the previous article, you can start by exercising the happy path where a user successfully connects with another user: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">UserManagementTestConventions</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;UsersSuccessfullyConnect( &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">Frozen</span>(<span style="color:#2b91af;">Matching</span>.ImplementedInterfaces)]<span style="color:#2b91af;">FakeDB</span>&nbsp;db, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">User</span>&nbsp;user, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">User</span>&nbsp;otherUser, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">ConnectionsController</span>&nbsp;sut) { &nbsp;&nbsp;&nbsp;&nbsp;db.Add(user); &nbsp;&nbsp;&nbsp;&nbsp;db.Add(otherUser); &nbsp;&nbsp;&nbsp;&nbsp;db.IsDirty&nbsp;=&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;sut.Post(user.Id.ToString(),&nbsp;otherUser.Id.ToString()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;ok&nbsp;=&nbsp;<span style="color:#2b91af;">Assert</span>.IsAssignableFrom&lt;<span style="color:#2b91af;">OkNegotiatedContentResult</span>&lt;<span style="color:#2b91af;">User</span>&gt;&gt;(actual); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(otherUser,&nbsp;ok.Content); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.True(db.IsDirty); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Contains(otherUser.Id,&nbsp;user.Connections); }</pre> </p> <p> This, and all other tests in this article use <a href="https://xunit.github.io">xUnit.net</a> 2.3.1 and <a href="https://github.com/AutoFixture/AutoFixture">AutoFixture</a> 4.1.0. </p> <p> The test is organised according to my standard <a href="http://blog.ploeh.dk/2013/06/24/a-heuristic-for-formatting-code-according-to-the-aaa-pattern">heuristic for formatting tests according to the Arrange Act Assert pattern</a>. In the Arrange phase, it adds the two valid <code>User</code> objects to the Fake <code>db</code> and sets the <code>IsDirty</code> flag to false. </p> <p> Setting the flag is necessary because this is object-oriented code, where objects have mutable state. In the previous articles with examples in <a href="https://fsharp.org">F#</a> and <a href="https://www.haskell.org">Haskell</a>, the <code>User</code> types were immutable. Connecting two users didn't mutate one of the users, but rather returned a new <code>User</code> value, as this F# example demonstrates: </p> <p> <pre><span style="color:green;">//&nbsp;User&nbsp;-&gt;&nbsp;User&nbsp;-&gt;&nbsp;User</span> <span style="color:blue;">let</span>&nbsp;addConnection&nbsp;user&nbsp;otherUser&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;user&nbsp;<span style="color:blue;">with</span>&nbsp;ConnectedUsers&nbsp;=&nbsp;otherUser&nbsp;::&nbsp;user.ConnectedUsers&nbsp;}</pre> </p> <p> In the current object-oriented code base, however, connecting one user to another is an instance method on the <code>User</code> class that mutates its state: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Connect(<span style="color:#2b91af;">User</span>&nbsp;otherUser) { &nbsp;&nbsp;&nbsp;&nbsp;connections.Add(otherUser.Id); }</pre> </p> <p> As a consequence, the <code>Post</code> method could, if someone made a mistake in its implementation, call <code>user.Connect</code>, but forget to invoke <code>UserRepository.Update</code>. Even if that happened, then all the other assertions would pass. This is the reason that you need the <code>Assert.True(db.IsDirty)</code> assertion in the Assert phase of the test. </p> <p> While we can apply to object-oriented code what we've learned from functional programming, the latter remains simpler. </p> <h3 id="011dc9e733ad44c9a656c07b2d420e1f"> Error test cases <a href="#011dc9e733ad44c9a656c07b2d420e1f" title="permalink">#</a> </h3> <p> While there's one happy path, there's four distinct error paths that you ought to cover. You can use the Fake database for that as well: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">UserManagementTestConventions</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;UsersFailToConnectWhenUserIdIsInvalid( &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">Frozen</span>(<span style="color:#2b91af;">Matching</span>.ImplementedInterfaces)]<span style="color:#2b91af;">FakeDB</span>&nbsp;db, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;userId, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">User</span>&nbsp;otherUser, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">ConnectionsController</span>&nbsp;sut) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.False(<span style="color:blue;">int</span>.TryParse(userId,&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="color:blue;">var</span>&nbsp;_)); &nbsp;&nbsp;&nbsp;&nbsp;db.Add(otherUser); &nbsp;&nbsp;&nbsp;&nbsp;db.IsDirty&nbsp;=&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;sut.Post(userId,&nbsp;otherUser.Id.ToString()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;err&nbsp;=&nbsp;<span style="color:#2b91af;">Assert</span>.IsAssignableFrom&lt;<span style="color:#2b91af;">BadRequestErrorMessageResult</span>&gt;(actual); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(<span style="color:#a31515;">&quot;Invalid&nbsp;user&nbsp;ID.&quot;</span>,&nbsp;err.Message); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.False(db.IsDirty); } [<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">UserManagementTestConventions</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;UsersFailToConnectWhenOtherUserIdIsInvalid( &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">Frozen</span>(<span style="color:#2b91af;">Matching</span>.ImplementedInterfaces)]<span style="color:#2b91af;">FakeDB</span>&nbsp;db, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">User</span>&nbsp;user, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;otherUserId, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">ConnectionsController</span>&nbsp;sut) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.False(<span style="color:blue;">int</span>.TryParse(otherUserId,&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="color:blue;">var</span>&nbsp;_)); &nbsp;&nbsp;&nbsp;&nbsp;db.Add(user); &nbsp;&nbsp;&nbsp;&nbsp;db.IsDirty&nbsp;=&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;sut.Post(user.Id.ToString(),&nbsp;otherUserId); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;err&nbsp;=&nbsp;<span style="color:#2b91af;">Assert</span>.IsAssignableFrom&lt;<span style="color:#2b91af;">BadRequestErrorMessageResult</span>&gt;(actual); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(<span style="color:#a31515;">&quot;Invalid&nbsp;ID&nbsp;for&nbsp;other&nbsp;user.&quot;</span>,&nbsp;err.Message); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.False(db.IsDirty); } [<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">UserManagementTestConventions</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;UsersDoNotConnectWhenUserDoesNotExist( &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">Frozen</span>(<span style="color:#2b91af;">Matching</span>.ImplementedInterfaces)]<span style="color:#2b91af;">FakeDB</span>&nbsp;db, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;userId, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">User</span>&nbsp;otherUser, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">ConnectionsController</span>&nbsp;sut) { &nbsp;&nbsp;&nbsp;&nbsp;db.Add(otherUser); &nbsp;&nbsp;&nbsp;&nbsp;db.IsDirty&nbsp;=&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;sut.Post(userId.ToString(),&nbsp;otherUser.Id.ToString()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;err&nbsp;=&nbsp;<span style="color:#2b91af;">Assert</span>.IsAssignableFrom&lt;<span style="color:#2b91af;">BadRequestErrorMessageResult</span>&gt;(actual); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(<span style="color:#a31515;">&quot;User&nbsp;not&nbsp;found.&quot;</span>,&nbsp;err.Message); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.False(db.IsDirty); } [<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">UserManagementTestConventions</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;UsersDoNotConnectWhenOtherUserDoesNotExist( &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">Frozen</span>(<span style="color:#2b91af;">Matching</span>.ImplementedInterfaces)]<span style="color:#2b91af;">FakeDB</span>&nbsp;db, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">User</span>&nbsp;user, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;otherUserId, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">ConnectionsController</span>&nbsp;sut) { &nbsp;&nbsp;&nbsp;&nbsp;db.Add(user); &nbsp;&nbsp;&nbsp;&nbsp;db.IsDirty&nbsp;=&nbsp;<span style="color:blue;">false</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;sut.Post(user.Id.ToString(),&nbsp;otherUserId.ToString()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;err&nbsp;=&nbsp;<span style="color:#2b91af;">Assert</span>.IsAssignableFrom&lt;<span style="color:#2b91af;">BadRequestErrorMessageResult</span>&gt;(actual); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(<span style="color:#a31515;">&quot;Other&nbsp;user&nbsp;not&nbsp;found.&quot;</span>,&nbsp;err.Message); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.False(db.IsDirty); }</pre> </p> <p> There's little to say about these tests that hasn't already been said in at least one of the previous articles. All tests inspect the state of the Fake database after calling the <code>Post</code> method. The exact interactions between <code>Post</code> and <code>db</code> aren't specified. Instead, these tests rely on setting up the initial state, exercising the SUT, and verifying the final state. These are all state-based tests that avoid over-specifying the interactions. </p> <p> Specifically, none of these tests use Mocks and Stubs. In fact, at this incarnation of the test code, I was able to entirely remove the reference to <a href="https://github.com/moq/moq4">Moq</a>. </p> <h3 id="2e73c70d74cf40dfbb3e258e4293a5cf"> Summary <a href="#2e73c70d74cf40dfbb3e258e4293a5cf" title="permalink">#</a> </h3> <p> The premise of <a href="http://amzn.to/YPdQDf">Refactoring</a> is that in order to be able to refactor, the "precondition is [...] solid tests". In reality, many development organisations have the opposite experience. When programmers attempt to make changes to how their code is organised, tests break. In <a href="http://bit.ly/xunitpatterns">xUnit Test Patterns</a> this problem is called <em>Fragile Tests</em>, and the cause is often <em>Overspecified Software</em>. This means that tests are tightly coupled to implementation details of the SUT. </p> <p> It's easy to inadvertently fall into this trap when you use Mocks and Stubs, even when you follow the rule of using <a href="http://blog.ploeh.dk/2013/10/23/mocks-for-commands-stubs-for-queries">Mocks for Commands and Stubs for Queries</a>. Refactoring tests towards state-based testing with Fake objects, instead of interaction-based testing, could make test suites more robust to changes. </p> <p> It's intriguing, though, that state-based testing is simpler in functional programming. In Haskell, you can simply write your tests in the State monad and compare the expected outcome to the actual outcome. Since state in Haskell is immutable, it's trivial to compare the expected with the actual state. </p> <p> As soon as you introduce mutable state, structural equality is no longer safe, and instead you have to rely on other inspection mechanisms, such as the <code>IsDirty</code> flag seen in this, and the previous, article. This makes the tests slightly more brittle, because it tends to pull towards interaction-based testing. </p> <p> While you can implement the State monad in both F# and C#, it's probably more pragmatic to express state-based tests using mutable state and the occasional <code>IsDirty</code> flag. As always, there's no panacea. </p> <p> While this article concludes the series on moving towards state-based testing, I think that an appendix on Test Spies is in order. </p> <p> <strong>Next:</strong> <a href="/2019/04/08/a-pure-test-spy">A pure Test Spy</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="b66a6b327b8949b69db335722c9501e3"> <div class="comment-author">ladeak</div> <div class="comment-content"> <p> If we had checked the FakeDB contains to user (by retrieving, similar as in the F# case), and assert Connections property on the retrieved objects, would we still need the IsDirty flag? I think it would be good to create a couple of cases which demonstrates refactoring, and how overspecified tests break with the interaction based tests, while works nicely here. </p> </div> <div class="comment-date">2019-04-05 17:20 UTC</div> </div> <div class="comment" id="39fa5e0bfdf24153854a260addf32d0e"> <div class="comment-author"><a href="/">Mark Seemann</a></div> <div class="comment-content"> <p> ladeak, thank you for writing. The <code>IsDirty</code> flag is essentially a hack to work around the mutable nature of the <code>FakeDB</code>. As the <a href="/2019/03/25/an-example-of-state-based-testing-in-f#dd0384ed4c2d478f8374dbd55c86b197">previous article describes</a>: <blockquote> "In the previous article, the Fake database was simply an immutable dictionary. This meant that tests could easily compare expected and actual values, since they were immutable. When you use a mutable object, like the above dictionary, this is harder. Instead, what I chose to do here was to introduce an <code>IsDirty</code> flag. This enables easy verification of whether or not the database changed." </blockquote> The <a href="/2019/03/11/an-example-of-state-based-testing-in-haskell">Haskell example</a> demonstrates how no <code>IsDirty</code> flag is required, because you can simply compare the state before and after the SUT was exercised. </p> <p> You could do something similar in C# or F#, but that would require you to take an immutable snapshot of the Fake database before exercising the SUT, and then compare that snapshot with the state of the Fake database after the SUT was exercised. This is definitely also doable (as the Haskell example demonstrates), but a bit more work, which is the (unprincipled, pragmatic) reason I instead chose to use an <code>IsDirty</code> flag. </p> <p> Regarding more examples, I originally wrote another sample code base to support <a href="https://vimeo.com/296652563">this talk</a>. That sample code base contains examples that demonstrate how overspecified tests break even when you make small internal changes. I haven't yet, however, found a good home for that code base. </p> </div> <div class="comment-date">2019-04-06 10:25 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. An example of state based-testing in F# https://blog.ploeh.dk/2019/03/25/an-example-of-state-based-testing-in-f 2019-03-25T06:34:00+00:00 Mark Seemann <div id="post"> <p> <em>While F# is a functional-first language, it's okay to occasionally be pragmatic and use mutable state, for example to easily write some sustainable state-based tests.</em> </p> <p> This article is an instalment in an article series about how to move <a href="/2019/02/18/from-interaction-based-to-state-based-testing">from interaction-based testing to state-based testing</a>. In the previous article, you saw how to write <a href="/2019/03/11/an-example-of-state-based-testing-in-haskell">state-based tests in Haskell</a>. In this article, you'll see how to apply what you've learned in <a href="https://fsharp.org">F#</a>. </p> <p> The code shown in this article is <a href="https://github.com/ploeh/UserManagement">available on GitHub</a>. </p> <h3 id="57929a79c0a740c5986df12fc6e4b1c1"> A function to connect two users <a href="#57929a79c0a740c5986df12fc6e4b1c1" title="permalink">#</a> </h3> <p> This article, like the others in this series, implements an operation to connect two users. I explain the example in details in my two <a href="https://cleancoders.com">Clean Coders</a> videos, <a href="https://cleancoders.com/episode/humane-code-real-episode-4/show">Church Visitor</a> and <a href="https://cleancoders.com/episode/humane-code-real-episode-5/show">Preserved in translation</a>. </p> <p> Like in the previous <a href="https://www.haskell.org">Haskell</a> example, in this article we'll start with the implementation, and then see how to unit test it. </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;Result&lt;User,UserLookupError&gt;)&nbsp;-&gt;&nbsp;(User&nbsp;-&gt;&nbsp;unit)&nbsp;-&gt;&nbsp;&#39;a&nbsp;-&gt;&nbsp;&#39;a&nbsp;-&gt;&nbsp;HttpResponse&lt;User&gt;</span> <span style="color:blue;">let</span>&nbsp;post&nbsp;lookupUser&nbsp;updateUser&nbsp;userId&nbsp;otherUserId&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;userRes&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lookupUser&nbsp;userId&nbsp;|&gt;&nbsp;Result.mapError&nbsp;(<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;InvalidId&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#a31515;">&quot;Invalid&nbsp;user&nbsp;ID.&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;NotFound&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#a31515;">&quot;User&nbsp;not&nbsp;found.&quot;</span>) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;otherUserRes&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lookupUser&nbsp;otherUserId&nbsp;|&gt;&nbsp;Result.mapError&nbsp;(<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;InvalidId&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#a31515;">&quot;Invalid&nbsp;ID&nbsp;for&nbsp;other&nbsp;user.&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;NotFound&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#a31515;">&quot;Other&nbsp;user&nbsp;not&nbsp;found.&quot;</span>) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;connect&nbsp;=&nbsp;result&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;user&nbsp;=&nbsp;userRes &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;otherUser&nbsp;=&nbsp;otherUserRes &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;addConnection&nbsp;user&nbsp;otherUser&nbsp;|&gt;&nbsp;updateUser &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;otherUser&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;connect&nbsp;<span style="color:blue;">with</span>&nbsp;Ok&nbsp;u&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;OK&nbsp;u&nbsp;|&nbsp;Error&nbsp;msg&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;BadRequest&nbsp;msg</pre> </p> <p> While <a href="/2019/02/25/an-example-of-interaction-based-testing-in-c">the original C# example</a> used Constructor Injection, the above <code>post</code> function uses <a href="/2017/01/30/partial-application-is-dependency-injection">partial application for Dependency Injection</a>. The two function arguments <code>lookupUser</code> and <code>updateUser</code> represent interactions with a database. Since functions are polymorphic, however, it's possible to replace them with <a href="http://xunitpatterns.com/Test%20Double.html">Test Doubles</a>. </p> <h3 id="dd0384ed4c2d478f8374dbd55c86b197"> A Fake database <a href="#dd0384ed4c2d478f8374dbd55c86b197" title="permalink">#</a> </h3> <p> Like in the Haskell example, you can implement a <a href="http://xunitpatterns.com/Fake%20Object.html">Fake</a> database in F#. It's also possible to implement the State monad in F#, but there's less need for it. F# is a functional-first language, but you can also write mutable code if need be. You could, then, choose to be pragmatic and base your Fake database on mutable state. </p> <p> <pre><span style="color:blue;">type</span>&nbsp;FakeDB&nbsp;()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;users&nbsp;=&nbsp;Dictionary&lt;int,&nbsp;User&gt;&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;<span style="color:blue;">val</span>&nbsp;IsDirty&nbsp;=&nbsp;<span style="color:blue;">false</span>&nbsp;<span style="color:blue;">with</span>&nbsp;get,&nbsp;set &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.AddUser&nbsp;user&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.IsDirty&nbsp;<span style="color:blue;">&lt;-</span>&nbsp;<span style="color:blue;">true</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;users.Add&nbsp;(user.UserId,&nbsp;user) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.TryFind&nbsp;i&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;users.TryGetValue&nbsp;i&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:blue;">false</span>,&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;None &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:blue;">true</span>,&nbsp;&nbsp;u&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Some&nbsp;u &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.LookupUser&nbsp;s&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;Int32.TryParse&nbsp;s&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:blue;">false</span>,&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Error&nbsp;InvalidId &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:blue;">true</span>,&nbsp;i&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;users.TryGetValue&nbsp;i&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:blue;">false</span>,&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Error&nbsp;NotFound &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:blue;">true</span>,&nbsp;u&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;Ok&nbsp;u &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">member</span>&nbsp;this.UpdateUser&nbsp;u&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.IsDirty&nbsp;<span style="color:blue;">&lt;-</span>&nbsp;<span style="color:blue;">true</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;users.[u.UserId]&nbsp;<span style="color:blue;">&lt;-</span>&nbsp;u</pre> </p> <p> This <code>FakeDB</code> type is <em>a class</em> that wraps a mutable dictionary. While it 'implements' <code>LookupUser</code> and <code>UpdateUser</code>, it also exposes what <a href="http://bit.ly/xunitpatterns">xUnit Test Patterns</a> calls a <em>Retrieval Interface:</em> an API that tests can use to examine the state of the object. </p> <p> Immutable values normally have structural equality. This means that two values are considered equal if they contain the same constituent values, and have the same structure. Mutable objects, on the other hand, typically have reference equality. This makes it harder to compare two objects, which is, however, what almost all unit testing is about. You compare expected state with actual state. </p> <p> In the previous article, the Fake database was simply an immutable dictionary. This meant that tests could easily compare expected and actual values, since they were immutable. When you use a mutable object, like the above dictionary, this is harder. Instead, what I chose to do here was to introduce an <code>IsDirty</code> flag. This enables easy verification of whether or not the database changed. </p> <h3 id="88dbfa0dd8f34f4ba91f2b7acc6173b1"> Happy path test case <a href="#88dbfa0dd8f34f4ba91f2b7acc6173b1" title="permalink">#</a> </h3> <p> This is all you need in terms of Test Doubles. You now have test-specific <code>LookupUser</code> and <code>UpdateUser</code> methods that you can pass to the <code>post</code> function. </p> <p> Like in the previous article, you can start by exercising the happy path where a user successfully connects with another user: </p> <p> <pre>[&lt;Fact&gt;] <span style="color:blue;">let</span>&nbsp;``Users&nbsp;successfully&nbsp;connect``&nbsp;()&nbsp;=&nbsp;Property.check&nbsp;&lt;|&nbsp;property&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;user&nbsp;=&nbsp;Gen.user &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;otherUser&nbsp;=&nbsp;Gen.withOtherId&nbsp;user &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;db&nbsp;=&nbsp;FakeDB&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;db.AddUser&nbsp;user &nbsp;&nbsp;&nbsp;&nbsp;db.AddUser&nbsp;otherUser &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;post&nbsp;db.LookupUser&nbsp;db.UpdateUser&nbsp;(string&nbsp;user.UserId)&nbsp;(string&nbsp;otherUser.UserId) &nbsp;&nbsp;&nbsp;&nbsp;test&nbsp;&lt;@&nbsp;db.TryFind&nbsp;user.UserId &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Option.exists &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:blue;">fun</span>&nbsp;u&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;u.ConnectedUsers&nbsp;|&gt;&nbsp;List.contains&nbsp;otherUser)&nbsp;@&gt; &nbsp;&nbsp;&nbsp;&nbsp;test&nbsp;&lt;@&nbsp;isOK&nbsp;actual&nbsp;@&gt;&nbsp;}</pre> </p> <p> All tests in this article use <a href="https://xunit.github.io">xUnit.net</a> 2.3.1, <a href="https://github.com/SwensenSoftware/unquote">Unquote</a> 4.0.0, and <a href="https://github.com/hedgehogqa/fsharp-hedgehog">Hedgehog</a> 0.7.0.0. </p> <p> This test first adds two valid users to the Fake database <code>db</code>. It then calls the <code>post</code> function, passing the <code>db.LookupUser</code> and <code>db.UpdateUser</code> methods as arguments. Finally, it verifies that the 'first' user's <code>ConnectedUsers</code> now contains the <code>otherUser</code>. It also verifies that <code>actual</code> represents a <code>200 OK</code> HTTP response. </p> <h3 id="750f435b6e854db898e0da44119ee4f6"> Missing user test case <a href="#750f435b6e854db898e0da44119ee4f6" title="permalink">#</a> </h3> <p> While there's one happy-path test case, there's four other test cases left. One of these is when the first user doesn't exist: </p> <p> <pre>[&lt;Fact&gt;] <span style="color:blue;">let</span>&nbsp;``Users&nbsp;don&#39;t&nbsp;connect&nbsp;when&nbsp;user&nbsp;doesn&#39;t&nbsp;exist``&nbsp;()&nbsp;=&nbsp;Property.check&nbsp;&lt;|&nbsp;property&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;i&nbsp;=&nbsp;Range.linear&nbsp;1&nbsp;1_000_000&nbsp;|&gt;&nbsp;Gen.int &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;otherUser&nbsp;=&nbsp;Gen.user &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;db&nbsp;=&nbsp;FakeDB&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;db.AddUser&nbsp;otherUser &nbsp;&nbsp;&nbsp;&nbsp;db.IsDirty&nbsp;<span style="color:blue;">&lt;-</span>&nbsp;<span style="color:blue;">false</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;uniqueUserId&nbsp;=&nbsp;string&nbsp;(otherUser.UserId&nbsp;+&nbsp;i) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;post&nbsp;db.LookupUser&nbsp;db.UpdateUser&nbsp;uniqueUserId&nbsp;(string&nbsp;otherUser.UserId) &nbsp;&nbsp;&nbsp;&nbsp;test&nbsp;&lt;@&nbsp;not&nbsp;db.IsDirty&nbsp;@&gt; &nbsp;&nbsp;&nbsp;&nbsp;test&nbsp;&lt;@&nbsp;isBadRequest&nbsp;actual&nbsp;@&gt;&nbsp;}</pre> </p> <p> This test adds one valid user to the Fake database. Once it's done with configuring the database, it sets <code>IsDirty</code> to <code>false</code>. The <code>AddUser</code> method sets <code>IsDirty</code> to <code>true</code>, so it's important to reset the flag before the <em>act</em> phase of the test. You could consider this a bit of a hack, but I think it makes the intent of the test clear. This is, however, a position I'm ready to reassess should the tests evolve to make this design awkward. </p> <p> As explained in the previous article, this test case requires an ID of a user that doesn't exist. Since this is a property-based test, there's a risk that Hedgehog might generate a number <code>i</code> equal to <code>otherUser.UserId</code>. One way to get around that problem is to add the two numbers together. Since <code>i</code> is generated from the range <em>1 - 1,000,000</em>, <code>uniqueUserId</code> is guaranteed to be different from <code>otherUser.UserId</code>. </p> <p> The test verifies that the state of the database didn't change (that <code>IsDirty</code> is still <code>false</code>), and that <code>actual</code> represents a <code>400 Bad Request</code> HTTP response. </p> <h3 id="4dbe21251f8440b1a594192d07b53c9f"> Remaining test cases <a href="#4dbe21251f8440b1a594192d07b53c9f" title="permalink">#</a> </h3> <p> You can write the remaining three test cases in the same vein: </p> <p> <pre>[&lt;Fact&gt;] <span style="color:blue;">let</span>&nbsp;``Users&nbsp;don&#39;t&nbsp;connect&nbsp;when&nbsp;other&nbsp;user&nbsp;doesn&#39;t&nbsp;exist``&nbsp;()&nbsp;=&nbsp;Property.check&nbsp;&lt;|&nbsp;property&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;i&nbsp;=&nbsp;Range.linear&nbsp;1&nbsp;1_000_000&nbsp;|&gt;&nbsp;Gen.int &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;user&nbsp;=&nbsp;Gen.user &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;db&nbsp;=&nbsp;FakeDB&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;db.AddUser&nbsp;user &nbsp;&nbsp;&nbsp;&nbsp;db.IsDirty&nbsp;<span style="color:blue;">&lt;-</span>&nbsp;<span style="color:blue;">false</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;uniqueOtherUserId&nbsp;=&nbsp;string&nbsp;(user.UserId&nbsp;+&nbsp;i) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;post&nbsp;db.LookupUser&nbsp;db.UpdateUser&nbsp;(string&nbsp;user.UserId)&nbsp;uniqueOtherUserId&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;test&nbsp;&lt;@&nbsp;not&nbsp;db.IsDirty&nbsp;@&gt; &nbsp;&nbsp;&nbsp;&nbsp;test&nbsp;&lt;@&nbsp;isBadRequest&nbsp;actual&nbsp;@&gt;&nbsp;} [&lt;Fact&gt;] <span style="color:blue;">let</span>&nbsp;``Users&nbsp;don&#39;t&nbsp;connect&nbsp;when&nbsp;user&nbsp;Id&nbsp;is&nbsp;invalid``&nbsp;()&nbsp;=&nbsp;Property.check&nbsp;&lt;|&nbsp;property&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;s&nbsp;=&nbsp;Gen.alphaNum&nbsp;|&gt;&nbsp;Gen.string&nbsp;(Range.linear&nbsp;0&nbsp;100)&nbsp;|&gt;&nbsp;Gen.filter&nbsp;isIdInvalid &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;otherUser&nbsp;=&nbsp;Gen.user &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;db&nbsp;=&nbsp;FakeDB&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;db.AddUser&nbsp;otherUser &nbsp;&nbsp;&nbsp;&nbsp;db.IsDirty&nbsp;<span style="color:blue;">&lt;-</span>&nbsp;<span style="color:blue;">false</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;post&nbsp;db.LookupUser&nbsp;db.UpdateUser&nbsp;s&nbsp;(string&nbsp;otherUser.UserId) &nbsp;&nbsp;&nbsp;&nbsp;test&nbsp;&lt;@&nbsp;not&nbsp;db.IsDirty&nbsp;@&gt; &nbsp;&nbsp;&nbsp;&nbsp;test&nbsp;&lt;@&nbsp;isBadRequest&nbsp;actual&nbsp;@&gt;&nbsp;} [&lt;Fact&gt;] <span style="color:blue;">let</span>&nbsp;``Users&nbsp;don&#39;t&nbsp;connect&nbsp;when&nbsp;other&nbsp;user&nbsp;Id&nbsp;is&nbsp;invalid``&nbsp;()&nbsp;=&nbsp;Property.check&nbsp;&lt;|&nbsp;property&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;s&nbsp;=&nbsp;Gen.alphaNum&nbsp;|&gt;&nbsp;Gen.string&nbsp;(Range.linear&nbsp;0&nbsp;100)&nbsp;|&gt;&nbsp;Gen.filter&nbsp;isIdInvalid &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;user&nbsp;=&nbsp;Gen.user &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;db&nbsp;=&nbsp;FakeDB&nbsp;() &nbsp;&nbsp;&nbsp;&nbsp;db.AddUser&nbsp;user &nbsp;&nbsp;&nbsp;&nbsp;db.IsDirty&nbsp;<span style="color:blue;">&lt;-</span>&nbsp;<span style="color:blue;">false</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;post&nbsp;db.LookupUser&nbsp;db.UpdateUser&nbsp;(string&nbsp;user.UserId)&nbsp;s &nbsp;&nbsp;&nbsp;&nbsp;test&nbsp;&lt;@&nbsp;not&nbsp;db.IsDirty&nbsp;@&gt; &nbsp;&nbsp;&nbsp;&nbsp;test&nbsp;&lt;@&nbsp;isBadRequest&nbsp;actual&nbsp;@&gt;&nbsp;}</pre> </p> <p> All tests inspect the state of the Fake database after the calling the <code>post</code> function. The exact interactions between <code>post</code> and <code>db</code> aren't specified. Instead, these tests rely on setting up the initial state, exercising the System Under Test, and verifying the final state. These are all state-based tests that avoid over-specifying the interactions. </p> <h3 id="102ff7e3a9b247b4914f113b06afd6df"> Summary <a href="#102ff7e3a9b247b4914f113b06afd6df" title="permalink">#</a> </h3> <p> While the previous Haskell example demonstrated that it's possible to write state-based unit tests in a functional style, when using F#, it sometimes make sense to leverage the object-oriented features already available in the .NET framework, such as mutable dictionaries. It would have been possible to write purely functional state-based tests in F# as well, by porting the Haskell examples, but here, I wanted to demonstrate that this isn't required. </p> <p> I tend to be of the opinion that it's only possible to be pragmatic if you know how to be dogmatic, but now that we know how to write state-based tests in a strictly functional style, I think it's fine to be pragmatic and use a bit of mutable state in F#. The benefit of this is that it now seems clear how to apply what we've learned to the original C# example. </p> <p> <strong>Next: </strong> <a href="/2019/04/01/an-example-of-state-based-testing-in-c">An example of state-based testing in C#</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The programmer as decision maker https://blog.ploeh.dk/2019/03/18/the-programmer-as-decision-maker 2019-03-18T07:44:00+00:00 Mark Seemann <div id="post"> <p> <em>As a programmer, your job is to make technical decisions. Make some more.</em> </p> <p> When <a href="/schedule">I speak at conferences</a>, people often come and talk to me. (I welcome that, BTW.) Among all the conversations I've had over the years, there's a pattern to some of them. The attendee will start by telling me how inspired (s)he is by the talk I just gave, or something I've written. That's gratifying, and a good way to start a conversation, but is often followed up like this: </p> <p> <strong>Attendee:</strong> "I just wish that we could do something like that in our organisation..." </p> <p> Let's just say that here we're talking about test-driven development, or perhaps just unit testing. Nothing too controversial. I'd typically respond, </p> <p> <strong>Me:</strong> "Why can't you?" </p> <p> <strong>Attendee:</strong> "Our boss won't let us..." </p> <p> That's unfortunate. If your boss has explicitly forbidden you to write and run unit tests, then there's not much you can do. Let me make this absolutely clear: I'm not going on record saying that you should actively disobey a direct order (unless it's unethical, that is). I do wonder, however: </p> <p> <em>Why is the boss even involved in that decision?</em> </p> <p> It seems to me that programmers often defer too much authority to their managers. </p> <h3 id="510367ded1ca47e7922b540d2fff50b0"> A note on culture <a href="#510367ded1ca47e7922b540d2fff50b0" title="permalink">#</a> </h3> <p> I'd like to preface the rest of this article with my own context. I've spent most of my programming career in Danish organisations. Even when I worked for Microsoft, I worked for Danish subsidiaries, with Danish managers. </p> <p> The power distance in Denmark is (in)famously short. It's not unheard of for individual contributors to question their superiors' decisions; sometimes to their face, and sometimes even when other people witness this. When done respectfully (which it often is), this can be extremely efficient. Managers are as fallible as the rest of us, and often their subordinates know of details that could impact a decision that a manager is about to make. Immediately discussing such details can help ensure that good decisions are made, and bad decisions are cancelled. </p> <p> This helps managers make better decisions, so enlightened managers welcome feedback. </p> <p> In general, Danish employees also tend to have a fair degree of autonomy. What I'll suggest in this article is unlikely to get you fired in Denmark. Please use your own judgement if you consider transplanting the following to your own culture. </p> <h3 id="eaa896ed8a6240729301c54fed859ab7"> Technical decisions <a href="#eaa896ed8a6240729301c54fed859ab7" title="permalink">#</a> </h3> <p> If your job is <em>programmer</em>, <em>software developer</em>, or similar, the value you add to the team is that you bring <em>technical expertise</em>. Maybe some of your colleagues are programmers as well, but together, you are the people with the technical expertise. </p> <p> Even if the project manager or other superiors used to program, unless they're also writing code for the current code base, they only have general technical expertise, but not specific expertise related to the code base you're working with. The people with most technical expertise are you and your colleagues. </p> <p> You are decision makers. </p> <p> Whenever you interact with your code base, you make technical decisions. </p> <p> In order to handle incoming HTTP requests to a <code>/reservations</code> resource, you may first decide to create a <a href="/2019/02/11/asynchronous-injection">new file called <code>ReservationsController.cs</code></a>. You'd most likely also decide to open that file and start adding code to it. </p> <p> Perhaps you add a method called <code>Post</code> that takes a <code>Reservation</code> argument. Perhaps you decide to inject an <code>IMaîtreD</code> dependency. </p> <p> At various steps along the way, you may decide to compile the code. </p> <p> Once you think that you've made enough changes to address your current work item, you may decide to run the program to see if it works. For a web-based piece of software, that typically involves starting up a browser and somehow interacting with the service. If your program is a web site, you may start at the front page, log in, click around, and fill in some forms. If your program is a REST API, you may interact with it via Fiddler or Postman (I prefer curl or <a href="https://github.com/ploeh/Furl">Furl</a>, but most people I've met still prefer something they can click on, it seems). </p> <p> What often happens is that your changes don't work the first time around, so you'll have to troubleshoot. Perhaps you decide to use a debugger. </p> <p> How many decisions are that? </p> <p> I just described seven or eight types of the sort of decisions you make as a programmer. You make such decisions all the time. Do you ask your managers permission before you start a debugging session? Before you create a new file? Before you name a variable? </p> <p> Of course you don't. You're the technical expert. There's no-one better equipped than you or your team members to make those decisions. </p> <h3 id="bed169525ccf4a90aaa9c0e33fcbf8d0"> Decide to add unit tests <a href="#bed169525ccf4a90aaa9c0e33fcbf8d0" title="permalink">#</a> </h3> <p> If you want to add unit tests, why don't you just decide to add them? If you want to apply test-driven development, why don't you just do so? </p> <p> A unit test is one or more code files. You're already authorised to make decisions about adding files. </p> <p> You can run a test suite instead of launching the software every time you want to interact with it. It's likely to be faster, even. </p> <p> Why should you ask permission to do that? </p> <h3 id="e42aac76e52141e68a0bc08fefa56a9b"> Decide to refactor <a href="#e42aac76e52141e68a0bc08fefa56a9b" title="permalink">#</a> </h3> <p> Another complaint I hear is that people aren't allowed to refactor. </p> <p> Why are you even asking permission to refactor? </p> <p> <a href="http://amzn.to/YPdQDf">Refactoring</a> means reorganising the code without changing the behaviour of the system. Another word for that is <em>editing</em> the code. It's okay. You're already permitted to edit code. It's part of your job description. </p> <p> I think I know what the underlying problem is, though... </p> <h3 id="f8c290ef29bd437daf2a81b3e06e131c"> Make technical decisions in the small <a href="#f8c290ef29bd437daf2a81b3e06e131c" title="permalink">#</a> </h3> <p> As an individual contributor, you're empowered to make small-scale technical decisions. These are decisions that are unlikely to impact schedules or allocation of programmers, including new hires. Big decisions probably should involve your manager. </p> <p> I have an inkling of why people feel that they need permission to refactor. It's because the refactoring they have in mind is going to take weeks. Weeks in which nothing else can be done. Weeks where perhaps the code doesn't even compile. </p> <p> Many years ago (but not as many as I'd like it to be), my colleague and I had what Eric Evans in <a href="http://amzn.to/WBCwx7">DDD</a> calls a <em>breakthrough</em>. We wanted to refactor towards deeper insight. What prompted the insight was a new feature that we had to add, and we'd been throwing design ideas back and forth for some time before the new insight arrived. </p> <p> We could implement the new feature if we changed one of the core abstractions in our domain model, but it required substantial changes to the existing code base. We informed our manager of our new insight and our plan, estimating that it would take less than a week to make the changes and implement the new feature. Our manager agreed with the plan. </p> <p> Two weeks later our code hadn't been in a compilable state for a week. Our manager pulled me away to tell me, quietly and equitably, that he was not happy with our lack of progress. I could only concur. </p> <p> After more heroic work, we finally managed to complete the changes and implement the new feature. Nonetheless, blocking all other development for two-three weeks in order to make a change isn't acceptable. </p> <p> That sort of change is a big decision because it impacts other team members, schedules, and perhaps overall business plans. Don't make those kinds of decisions without consulting with stakeholders. </p> <p> This still leaves, I believe, lots of room for individual decision-making in the small. What I learned from the experience I just recounted was not to engage in big changes to a code base. Learn how to make multiple incremental changes instead. In case that's completely impossible, add the new model side-by-side with the old model, and incrementally change over. That's what I should have done those many years ago. </p> <h3 id="298ca23885544e5f906379c1a6d15586"> Don't be sneaky <a href="#298ca23885544e5f906379c1a6d15586" title="permalink">#</a> </h3> <p> When I give talks about the blessings of functional programming, I sometimes get into another type of discussion. </p> <p> <strong>Attendee:</strong> It's so inspiring how beautiful and simple complex domain models become in <a href="https://fsharp.org">F#</a>. How can we do the same in C#? </p> <p> <strong>Me:</strong> You can't. If you're already using C#, you should strongly consider F# if you wish to do functional programming. Since it's also a .NET language, you can gradually introduce F# code and mix the compiled code with your existing C# code. </p> <p> <strong>Attendee:</strong> Yes... [already getting impatient with me] But we can't do that... </p> <p> <strong>Me:</strong> Why not? </p> <p> <strong>Attendee:</strong> Because our manager will not allow it. </p> <p> Based on the suggestions I've already made here, you may expect me to say that that's another technical decision that you should make without asking permission. Like the previous example about blocking refactorings, however, this is another large-scale decision. </p> <p> Your manager may be concerned that it'd be hard to find new employees if the code base is written in some niche language. <a href="/2015/12/03/the-rules-of-attraction-language">I tend to disagree with that position</a>, but I do understand why a manager would take that position. While I think it suboptimal to restrict an entire development organisation to a single language (whether it's C#, Java, C++, Ruby, etc.), I'll readily accept that language choice is a strategic decision. </p> <p> If every programmer got to choose the programming language they prefer the most that day, you'd have code bases written in dozens of different languages. While you can train bright new hires to learn a new language or two, it's unrealistic that a new employee will be able to learn thirty different languages in a short while. </p> <p> I find it reasonable that a manager has the final word on the choice of language, even when I often disagree with the decisions. </p> <p> The outcome usually is that people are stuck with C# (or Java, or...). Hence the question: <em>How can we do functional programming in C#?</em> </p> <p> I'll give the answer that I often give here on the blog: <a href="https://en.wikipedia.org/wiki/Mu_(negative)">mu</a> (<em>unask the question</em>). You can, in fact, translate functional concepts to C#, but <a href="/2018/07/24/dependency-injection-revisited">the result is so non-idiomatic</a> that only the syntax remains of C#: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IReservationsInstruction</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;Select&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IReservationsInstruction</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;source, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;selector) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;source.Match&lt;<span style="color:#2b91af;">IReservationsInstruction</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;isReservationInFuture:&nbsp;t&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">IsReservationInFuture</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Reservation</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">bool</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.Item1, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b&nbsp;=&gt;&nbsp;selector(t.Item2(b)))), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;readReservations:&nbsp;t&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ReadReservations</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">DateTimeOffset</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.Item1, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d&nbsp;=&gt;&nbsp;selector(t.Item2(d)))), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;create:&nbsp;t&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Create</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">Reservation</span>,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.Item1, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r&nbsp;=&gt;&nbsp;selector(t.Item2(r))))); }</pre> </p> <p> Keep in mind the manager's motivation for standardising on C#. It's often related to concerns about being able to hire new employees, or move employees from project to project. </p> <p> If you write 'functional' C#, you'll end up with code like the above, or the following real-life example: </p> <p> <pre><span style="color:blue;">return</span>&nbsp;<span style="color:blue;">await</span>&nbsp;sendRequest( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">ApiMethodNames</span>.InitRegistration, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">GSObject</span>()) &nbsp;&nbsp;&nbsp;&nbsp;.Map(r&nbsp;=&gt;&nbsp;<span style="color:#2b91af;">ValidateResponse</span>.Validate(r) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.MapFailure(_&nbsp;=&gt;&nbsp;<span style="color:#2b91af;">ErrorResponse</span>.RegisterErrorResponse())) &nbsp;&nbsp;&nbsp;&nbsp;.Bind(r&nbsp;=&gt;&nbsp;r.RetrieveField(<span style="color:#a31515;">&quot;regToken&quot;</span>)) &nbsp;&nbsp;&nbsp;&nbsp;.BindAsync(token&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sendRequest( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">ApiMethodNames</span>.RegisterAccount, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CreateRegisterRequest( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mailAddress, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;password, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;token)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Map(<span style="color:#2b91af;">ValidateResponse</span>.Validate) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Bind(response&nbsp;=&gt;&nbsp;getIdentity(response) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ToResult(<span style="color:#2b91af;">ErrorResponse</span>.ExternalServiceResponseInvalid))) &nbsp;&nbsp;&nbsp;&nbsp;.Map(id&nbsp;=&gt;&nbsp;<span style="color:#2b91af;">GigyaIdentity</span>.CreateNewSiteUser(id.UserId,&nbsp;mailAddress));</pre> </p> <p> (I'm indebted to <a href="https://twitter.com/runeibsen">Rune Ibsen</a> for this example.) </p> <p> A new hire can have ten years of C# experience and still have no chance in a code base like that. You'll first have to teach him or her functional programming. If you can do that, you might as well also teach a new language, like F#. </p> <p> It's my experience that learning the syntax of a new language is easy, and usually doesn't take much time. The hard part is learning a new way to think. </p> <p> Writing 'functional' C# makes it doubly hard on new team members. Not only do they have to learn a new paradigm (functional programming), but they have to learn it in a language unsuited for that paradigm. </p> <p> That's why I think you should unask the question. If your manager doesn't want to allow F#, then writing 'functional' C# is just being sneaky. That'd be obeying the letter of the law while breaking the spirit of it. That is, in my opinion, immoral. Don't be sneaky. </p> <h3 id="2f1b38953bbc4814994ee471caf4c5ac"> Summary <a href="#2f1b38953bbc4814994ee471caf4c5ac" title="permalink">#</a> </h3> <p> As a professional programmer, your job is to be a technical expert. In normal circumstances (at least the ones I know from my own career), you have agency. In order to get anything done, you make small decisions all the time, such as editing code. That's not only okay, but expected of you. </p> <p> Some decision, on the other hand, can have substantial ramifications. Choosing to write code in an unsanctioned language tends to fall on the side where a manager should be involved in the decision. </p> <p> In between is a grey area. </p> <p> <img src="/content/binary/small-vs-big-decisions-gradient.png" alt="A spectrum of decisions from small to the left to big to the right."> </p> <p> I don't even consider adding unit tests to be in the grey area, but some refactorings may be. <blockquote> <p>"It's easier to ask forgiveness than it is to get permission."</p> <footer><cite>Grace Hopper</cite></footer> </blockquote> </p> <p> To navigate grey areas you need a moral compass. </p> <p> I'll let you be the final judge of what you can get away with, but I consider it both appropriate and ethical to make the decision to add unit tests, and to continually improve code bases. You shouldn't have to ask permission to do that. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="dd2ab8d5dc6e4c5f9e49d7d7f35a8759"> <div class="comment-author"><a href="https://github.com/chicocode">Francisco Berrocal</a></div> <div class="comment-content"> <p>Before all, I'd just like to thank all the content you share, they all make me think in a good way!</p> <p> Now regarding to this post, while I tend to agree that a developer can take the decision to add (or not) unit tests by himself, there is no great value comming out of it, if that's not an approach of the whole development team, right? I believe we need the entire team on board to maximize the values of unit tests. There are changes we need to consider, from changes in the mindset of how you develop to actually running them on continuour integration pipelines. Doesn't all of that push simple decisions like "add unit test" from green area towards orange area? </p> </div> <div class="comment-date">2019-03-18 13:14 UTC</div> </div> <div class="comment" id="2bc69a5123d8499ca40631b9ce946919"> <div class="comment-author"><a href="/">Mark Seemann</a></div> <div class="comment-content"> <p> Francisco, thank you for writing. If you have a team of developers, then I agree that unit tests are going to be most valuable if the team decides to use them. </p> <p> This is still something that you ought to be competent to decide as a self-organising team of developers. Do you need to ask a manager's permission? </p> <p> I'm not trying to pretend that this is easy. I realise that it can be difficult. </p> <p> I've heard about teams where other developers are hostile to the idea of unit testing. In that situation, I can offer no easy fixes. What a lone developer can try to do in that situation is to add and run unit tests locally, on his or her own machine. This will incur some friction, because other team members will be oblivious to the tests, so they'll change code that will cause those unit tests to break. </p> <p> This might teach the lone developer to write tests so that they're as robust to trivial changes as possible. That's a valuable skill in any case. There's still going to be some overhead of maintaining the unit tests in a scenario like that, but if that overhead is smaller than the productivity gained, then in might still be worthwhile. </p> <p> What might then happen could be that other developers who are on the fence see that the lone unit tester is more effective than they are. Perhaps they'll get curious about unit tests after all, once they can see the contours of advantages. </p> <p> The next scenario, then, is a team with a few developers writing unit tests, and other who don't. At some number, you'll have achieved enough critical mass that, at least, you get to check in the unit tests together with the source code. Soon after, you may be able to institute a policy that while not everyone writes unit tests, it's not okay to break existing tests. </p> <p> The next thing you can do, then, is to set up a test run as part of continuous integration and declare that a failing test run means that the build broke. You still have team members who don't write tests, but at least you get to do it, and the tests add value to the whole team. </p> <p> Perhaps the sceptics will slowly start to write unit tests over time. Some die-hards probably never will. </p> <p> You may be able to progress through such stages without asking a manager, but I do understand that there's much variation in organisation and team dynamics. If you can use any of the above sketches as inspiration, then that's great. If you (or other readers) have other success stories to tell, then please share them. </p> <p> The point I was trying to make with this article is that programmers have agency. This isn't a licence to do whatever you please. You still have to navigate the dynamics of whatever organisation you're in. You may not, however, need to ask your manager about every little thing that you're competent to decide yourselves. </p> </div> <div class="comment-date">2019-03-19 7:57 UTC</div> </div> <div class="comment"> <div class="comment-author"><a href="https://hettomei.github.io/">Timothée GAUTHIER</a></div> <div class="comment-content"> <p> Thank you A LOT for putting words on all these thought. You'll be my reference whenever I want to introduce unit test. </p> <p> My usual example is "a surgeon doesn't need to ask to the manager if he can wash his hand. Whashing his hand is part of his job". (Not mine, but I can't remember where it comes from) </p> </div> <div class="comment-date">2019-03-19 20:15 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. An example of state-based testing in Haskell https://blog.ploeh.dk/2019/03/11/an-example-of-state-based-testing-in-haskell 2019-03-11T07:55:00+00:00 Mark Seemann <div id="post"> <p> <em>How do you do state-based testing when state is immutable? You use the State monad.</em> </p> <p> This article is an instalment in an article series about how to move <a href="/2019/02/18/from-interaction-based-to-state-based-testing">from interaction-based testing to state-based testing</a>. In the previous article, you saw <a href="/2019/02/25/an-example-of-interaction-based-testing-in-c">an example of an interaction-based unit test</a> written in C#. The problem that this article series attempts to address is that interaction-based testing can lead to what <a href="http://bit.ly/xunitpatterns">xUnit Test Patterns</a> calls <em>Fragile Tests</em>, because the tests get coupled to implementation details, instead of overall behaviour. </p> <p> My experience is that functional programming is better aligned with unit testing because <a href="/2015/05/07/functional-design-is-intrinsically-testable">functional design is intrinsically testable</a>. While I believe that functional programming is no panacea, it still seems to me that we can learn many valuable lessons about programming from it. </p> <p> People often ask me about <a href="https://fsharp.org">F#</a> programming: <em>How do I know that my F# code is functional?</em> </p> <p> I sometimes wonder that myself, about my own F# code. One can certainly choose to ignore such a question as irrelevant, and I sometimes do, as well. Still, in my experience, asking such questions can create learning opportunities. </p> <p> The best answer that I've found is: <em>Port the F# code to <a href="https://www.haskell.org">Haskell</a>.</em> </p> <p> Haskell enforces <a href="https://en.wikipedia.org/wiki/Referential_transparency">referential transparency</a> via its compiler. If Haskell code compiles, it's functional. In this article, then, I take the problem from the previous article and port it to Haskell. </p> <p> The code shown in this article is <a href="https://github.com/ploeh/UserManagement">available on GitHub</a>. </p> <h3 id="ae68546c3d9f4810a18ea99c2bcc873c"> A function to connect two users <a href="#ae68546c3d9f4810a18ea99c2bcc873c" title="permalink">#</a> </h3> <p> In the previous article, you saw implementation and test coverage of a piece of software functionality to connect two users with each other. This was a simplification of the example running through my two <a href="https://cleancoders.com">Clean Coders</a> videos, <a href="https://cleancoders.com/episode/humane-code-real-episode-4/show">Church Visitor</a> and <a href="https://cleancoders.com/episode/humane-code-real-episode-5/show">Preserved in translation</a>. </p> <p> In contrast to the previous article, we'll start with the implementation of the System Under Test (SUT). </p> <p> <pre><span style="color:#2b91af;">post</span>&nbsp;::&nbsp;<span style="color:blue;">Monad</span>&nbsp;m&nbsp;<span style="color:blue;">=&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(a&nbsp;-&gt;&nbsp;m&nbsp;(Either&nbsp;UserLookupError&nbsp;User))&nbsp;-&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(User&nbsp;-&gt;&nbsp;m&nbsp;<span style="color:blue;">()</span>)&nbsp;-&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a&nbsp;-&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a&nbsp;-&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m&nbsp;(HttpResponse&nbsp;User) post&nbsp;lookupUser&nbsp;updateUser&nbsp;userId&nbsp;otherUserId&nbsp;=&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;userRes&nbsp;&lt;-&nbsp;first&nbsp;(\<span style="color:blue;">case</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;InvalidId&nbsp;-&gt;&nbsp;<span style="color:#a31515;">&quot;Invalid&nbsp;user&nbsp;ID.&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NotFound&nbsp;&nbsp;-&gt;&nbsp;<span style="color:#a31515;">&quot;User&nbsp;not&nbsp;found.&quot;</span>) &nbsp;&nbsp;&nbsp;&nbsp;&lt;$&gt;&nbsp;lookupUser&nbsp;userId &nbsp;&nbsp;otherUserRes&nbsp;&lt;-&nbsp;first&nbsp;(\<span style="color:blue;">case</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;InvalidId&nbsp;-&gt;&nbsp;<span style="color:#a31515;">&quot;Invalid&nbsp;ID&nbsp;for&nbsp;other&nbsp;user.&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NotFound&nbsp;&nbsp;-&gt;&nbsp;<span style="color:#a31515;">&quot;Other&nbsp;user&nbsp;not&nbsp;found.&quot;</span>) &nbsp;&nbsp;&nbsp;&nbsp;&lt;$&gt;&nbsp;lookupUser&nbsp;otherUserId &nbsp;&nbsp;connect&nbsp;&lt;-&nbsp;runExceptT&nbsp;$&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user&nbsp;&lt;-&nbsp;ExceptT&nbsp;$&nbsp;<span style="color:blue;">return</span>&nbsp;userRes &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;otherUser&nbsp;&lt;-&nbsp;ExceptT&nbsp;$&nbsp;<span style="color:blue;">return</span>&nbsp;otherUserRes &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lift&nbsp;$&nbsp;updateUser&nbsp;$&nbsp;addConnection&nbsp;user&nbsp;otherUser &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;otherUser &nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;either&nbsp;BadRequest&nbsp;OK&nbsp;connect </pre> </p> <p> This is as direct a translation of the C# code as makes sense. If I'd only been implementing the desired functionality in Haskell, without having to port existing code, I'd designed the code differently. </p> <p> This <code>post</code> function uses <a href="/2017/01/30/partial-application-is-dependency-injection">partial application as an analogy to dependency injection</a>, but in order to enable potentially impure operations to take place, everything must happen inside of some monad. While the production code must ultimately run in the <code>IO</code> monad in order to interact with a database, tests can choose to run in another monad. </p> <p> In the C# example, two dependencies are injected into the class that defines the <code>Post</code> method. In the above Haskell function, these two dependencies are instead passed as function arguments. Notice that both functions return values in the monad <code>m</code>. </p> <p> The intent of the <code>lookupUser</code> argument is that it'll query a database with a user ID. It'll return the user if present, but it could also return a <code>UserLookupError</code>, which is a simple <a href="https://en.wikipedia.org/wiki/Tagged_union">sum type:</a> </p> <p> <pre><span style="color:blue;">data</span>&nbsp;UserLookupError&nbsp;=&nbsp;InvalidId&nbsp;|&nbsp;NotFound&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#2b91af;">Show</span>,&nbsp;<span style="color:#2b91af;">Eq</span>)</pre> </p> <p> If both users are found, the function connects the users and calls the <code>updateUser</code> function argument. The intent of this 'dependency' is that it updates the database. This is recognisably a <a href="https://en.wikipedia.org/wiki/Command%E2%80%93query_separation">Command</a>, since its return type is <code>m ()</code> - <a href="/2018/01/15/unit-isomorphisms"><em>unit</em> (<code>()</code>) is equivalent to <code>void</code></a>. </p> <h3 id="6855a96abefe47c3a924b6d7d94e7bc8"> State-based testing <a href="#6855a96abefe47c3a924b6d7d94e7bc8" title="permalink">#</a> </h3> <p> How do you unit test such a function? How do you use Mocks and Stubs in Haskell? You don't; you don't have to. While the <code>post</code> method <em>can</em> be impure (when <code>m</code> is <code>IO</code>), it doesn't have to be. Functional design is intrinsically testable, but that proposition depends on purity. Thus, it's worth figuring out how to keep the <code>post</code> function <a href="https://en.wikipedia.org/wiki/Pure_function">pure</a> in the context of unit testing. </p> <p> While <code>IO</code> implies impurity, most common monads are pure. Which one should you choose? You could attempt to entirely 'erase' the monadic quality of the <code>post</code> function with the <code>Identity</code> monad, but if you do that, you can't verify whether or not <code>updateUser</code> was invoked. </p> <p> While you <em>could</em> write an ad-hoc Mock using, for example, the <code>Writer</code> monad, it might be a better choice to investigate if something closer to state-based testing would be possible. </p> <p> In an object-oriented context, state-based testing implies that you exercise the SUT, which mutates some state, and then you verify that the (mutated) state matches your expectations. You can't do that when you test a pure function, but you can examine the state of the function's return value. The <code>State</code> monad is an obvious choice, then. </p> <h3 id="6d9ce24e5e374c5d9987ac481b1fbd37"> A Fake database <a href="#6d9ce24e5e374c5d9987ac481b1fbd37" title="permalink">#</a> </h3> <p> Haskell's <code>State</code> monad is parametrised on the state type as well as the normal 'value type', so in order to be able to test the <code>post</code> function, you'll have to figure out what type of state to use. The interactions implied by the <code>post</code> function's <code>lookupUser</code> and <code>updateUser</code> arguments are those of database interactions. A <a href="http://xunitpatterns.com/Fake%20Object.html">Fake</a> database seems an obvious choice. </p> <p> For the purposes of testing the <code>post</code> function, an in-memory database implemented using a <code>Map</code> is appropriate: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;DB&nbsp;=&nbsp;Map&nbsp;Integer&nbsp;User</pre> </p> <p> This is simply a dictionary keyed by <code>Integer</code> values and containing <code>User</code> values. You can implement compatible <code>lookupUser</code> and <code>updateUser</code> functions with <code>State DB</code> as the <code>Monad</code>. The <code>updateUser</code> function is the easiest one to implement: </p> <p> <pre><span style="color:#2b91af;">updateUser</span>&nbsp;::&nbsp;<span style="color:blue;">User</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">State</span>&nbsp;<span style="color:blue;">DB</span>&nbsp;() updateUser&nbsp;user&nbsp;=&nbsp;modify&nbsp;$&nbsp;Map.insert&nbsp;(userId&nbsp;user)&nbsp;user</pre> </p> <p> This simply inserts the <code>user</code> into the database, using the <code>userId</code> as the key. The type of the function is compatible with the general requirement of <code>User -&gt; m ()</code>, since here, <code>m</code> is <code>State DB</code>. </p> <p> The <code>lookupUser</code> Fake implementation is a bit more involved: </p> <p> <pre><span style="color:#2b91af;">lookupUser</span>&nbsp;::&nbsp;<span style="color:#2b91af;">String</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">State</span>&nbsp;<span style="color:blue;">DB</span>&nbsp;(<span style="color:#2b91af;">Either</span>&nbsp;<span style="color:blue;">UserLookupError</span>&nbsp;<span style="color:blue;">User</span>) lookupUser&nbsp;s&nbsp;=&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;maybeInt&nbsp;=&nbsp;readMaybe&nbsp;s&nbsp;::&nbsp;Maybe&nbsp;Integer &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;eitherInt&nbsp;=&nbsp;<span style="color:blue;">maybe</span>&nbsp;(Left&nbsp;InvalidId)&nbsp;Right&nbsp;maybeInt &nbsp;&nbsp;db&nbsp;&lt;-&nbsp;get &nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;eitherInt&nbsp;&gt;&gt;=&nbsp;<span style="color:blue;">maybe</span>&nbsp;(Left&nbsp;NotFound)&nbsp;Right&nbsp;.&nbsp;<span style="color:blue;">flip</span>&nbsp;Map.<span style="color:blue;">lookup</span>&nbsp;db</pre> </p> <p> First, consider the type. The function takes a <code>String</code> value as an argument and returns a <code>State DB (Either UserLookupError User)</code>. The requirement is a function compatible with the type <code>a -&gt; m (Either UserLookupError User)</code>. This works when <code>a</code> is <code>String</code> and <code>m</code> is, again, <code>State DB</code>. </p> <p> The entire function is written in <code>do</code> notation, where the inferred <code>Monad</code> is, indeed, <code>State DB</code>. The first line attempts to parse the <code>String</code> into an <code>Integer</code>. Since the built-in <code>readMaybe</code> function returns a <code>Maybe Integer</code>, the next line uses the <code>maybe</code> function to handle the two possible cases, converting the <code>Nothing</code> case into the <code>Left InvalidId</code> value, and the <code>Just</code> case into a <code>Right</code> value. </p> <p> It then uses the <code>State</code> module's <code>get</code> function to access the database <code>db</code>, and finally attempt a <code>lookup</code> against that <code>Map</code>. Again, <code>maybe</code> is used to convert the <code>Maybe</code> value returned by <code>Map.lookup</code> into an <code>Either</code> value. </p> <h3 id="280bfa2e191e42d095f6f762e0a94a55"> Happy path test case <a href="#280bfa2e191e42d095f6f762e0a94a55" title="permalink">#</a> </h3> <p> This is all you need in terms of <a href="http://xunitpatterns.com/Test%20Double.html">Test Doubles</a>. You now have test-specific <code>lookupUser</code> and <code>updateUser</code> functions that you can pass to the <code>post</code> function. </p> <p> Like in the previous article, you can start by exercising the happy path where a user successfully connects with another user: </p> <p> <pre>testProperty&nbsp;<span style="color:#a31515;">&quot;Users&nbsp;successfully&nbsp;connect&quot;</span>&nbsp;$&nbsp;\ &nbsp;&nbsp;user&nbsp;otherUser&nbsp;-&gt;&nbsp;runStateTest&nbsp;$&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;put&nbsp;$&nbsp;Map.fromList&nbsp;[toDBEntry&nbsp;user,&nbsp;toDBEntry&nbsp;otherUser] &nbsp;&nbsp;actual&nbsp;&lt;-&nbsp;post&nbsp;lookupUser&nbsp;updateUser&nbsp;(<span style="color:blue;">show</span>&nbsp;$&nbsp;userId&nbsp;user)&nbsp;(<span style="color:blue;">show</span>&nbsp;$&nbsp;userId&nbsp;otherUser) &nbsp;&nbsp;db&nbsp;&lt;-&nbsp;get &nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;$ &nbsp;&nbsp;&nbsp;&nbsp;isOK&nbsp;actual&nbsp;&amp;&amp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">any</span>&nbsp;(<span style="color:blue;">elem</span>&nbsp;otherUser&nbsp;.&nbsp;connectedUsers)&nbsp;(Map.<span style="color:blue;">lookup</span>&nbsp;(userId&nbsp;user)&nbsp;db)</pre> </p> <p> Here I'm <a href="/2018/05/07/inlined-hunit-test-lists">inlining test cases as anonymous functions</a> - this time expressing the tests as QuickCheck properties. I'll later return to the <code>runStateTest</code> helper function, but first I want to focus on the test body itself. It's written in <code>do</code> notation, and specifically, it runs in the <code>State DB</code> monad. </p> <p> <code>user</code> and <code>otherUser</code> are input arguments to the property. These are both <code>User</code> values, since the test also defines <code>Arbitrary</code> instances for that type (not shown in this article; see the source code repository for details). </p> <p> The first step in the test is to 'save' both users in the Fake database. This is easily done by converting each <code>User</code> value to a database entry: </p> <p> <pre><span style="color:#2b91af;">toDBEntry</span>&nbsp;::&nbsp;<span style="color:blue;">User</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;(<span style="color:#2b91af;">Integer</span>,&nbsp;<span style="color:blue;">User</span>) toDBEntry&nbsp;=&nbsp;userId&nbsp;&amp;&amp;&amp;&nbsp;<span style="color:blue;">id</span></pre> </p> <p> Recall that the Fake database is nothing but an alias over <code>Map Integer User</code>, so the only operation required to turn a <code>User</code> into a database entry is to extract the key. </p> <p> The next step in the test is to exercise the SUT, passing the test-specific <code>lookupUser</code> and <code>updateUser</code> Test Doubles to the <code>post</code> function, together with the user IDs converted to <code>String</code> values. </p> <p> In the <em>assert</em> phase of the test, it first extracts the current state of the database, using the <code>State</code> library's built-in <code>get</code> function. It then verifies that <code>actual</code> represents a <code>200 OK</code> value, and that the <code>user</code> entry in the database now contains <code>otherUser</code> as a connected user. </p> <h3 id="050ecaa9962a487c9381da982ab264e7"> Missing user test case <a href="#050ecaa9962a487c9381da982ab264e7" title="permalink">#</a> </h3> <p> While there's one happy-path test case, there's four other test cases left. One of these is when the first user doesn't exist: </p> <p> <pre>testProperty&nbsp;<span style="color:#a31515;">&quot;Users&nbsp;don&#39;t&nbsp;connect&nbsp;when&nbsp;user&nbsp;doesn&#39;t&nbsp;exist&quot;</span>&nbsp;$&nbsp;\ &nbsp;&nbsp;(Positive&nbsp;i)&nbsp;otherUser&nbsp;-&gt;&nbsp;runStateTest&nbsp;$&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;db&nbsp;=&nbsp;Map.fromList&nbsp;[toDBEntry&nbsp;otherUser] &nbsp;&nbsp;put&nbsp;db &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;uniqueUserId&nbsp;=&nbsp;<span style="color:blue;">show</span>&nbsp;$&nbsp;userId&nbsp;otherUser&nbsp;+&nbsp;i &nbsp;&nbsp;actual&nbsp;&lt;-&nbsp;post&nbsp;lookupUser&nbsp;updateUser&nbsp;uniqueUserId&nbsp;(<span style="color:blue;">show</span>&nbsp;$&nbsp;userId&nbsp;otherUser) &nbsp;&nbsp;assertPostFailure&nbsp;db&nbsp;actual</pre> </p> <p> What ought to trigger this test case is that the 'first' user doesn't exist, even if the <code>otherUser</code> does exist. For this reason, the test inserts the <code>otherUser</code> into the Fake database. </p> <p> Since the test is a QuickCheck property, <code>i</code> could be <em>any</em> positive <code>Integer</code> value - <em>including</em> the <code>userId</code> of <code>otherUser</code>. In order to properly exercise the test case, however, you'll need to call the <code>post</code> function with a <code>uniqueUserId</code> - thas it: an ID which is guaranteed to not be equal to the <code>userId</code> of <code>otherUser</code>. There's several options for achieving this guarantee (including, as you'll see soon, the <code>==&gt;</code> operator), but a simple way is to add a non-zero number to the number you need to avoid. </p> <p> You then exercise the <code>post</code> function and, as a verification, call a reusable <code>assertPostFailure</code> function: </p> <p> <pre><span style="color:#2b91af;">assertPostFailure</span>&nbsp;::&nbsp;(<span style="color:blue;">Eq</span>&nbsp;s,&nbsp;<span style="color:blue;">Monad</span>&nbsp;m)&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;s&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">HttpResponse</span>&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">StateT</span>&nbsp;s&nbsp;m&nbsp;<span style="color:#2b91af;">Bool</span> assertPostFailure&nbsp;stateBefore&nbsp;resp&nbsp;=&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;stateAfter&nbsp;&lt;-&nbsp;get &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;stateDidNotChange&nbsp;=&nbsp;stateBefore&nbsp;==&nbsp;stateAfter &nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;$&nbsp;stateDidNotChange&nbsp;&amp;&amp;&nbsp;isBadRequest&nbsp;resp</pre> </p> <p> This function verifies that the state of the database didn't change, and that the response value represents a <code>400 Bad Request</code> HTTP response. This verification doesn't actually verify that the error message associated with the <code>BadRequest</code> case is the expected message, like in the previous article. This would, however, involve a fairly trivial change to the code. </p> <h3 id="dbd934d3e4f7431d95d78dfb0c1d7f4e"> Missing other user test case <a href="#dbd934d3e4f7431d95d78dfb0c1d7f4e" title="permalink">#</a> </h3> <p> Similar to the above test case, users will also fail to connect if the 'other user' doesn't exist. The property is almost identical: </p> <p> <pre>testProperty&nbsp;<span style="color:#a31515;">&quot;Users&nbsp;don&#39;t&nbsp;connect&nbsp;when&nbsp;other&nbsp;user&nbsp;doesn&#39;t&nbsp;exist&quot;</span>&nbsp;$&nbsp;\ &nbsp;&nbsp;(Positive&nbsp;i)&nbsp;user&nbsp;-&gt;&nbsp;runStateTest&nbsp;$&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp; &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;db&nbsp;=&nbsp;Map.fromList&nbsp;[toDBEntry&nbsp;user] &nbsp;&nbsp;put&nbsp;db &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;uniqueOtherUserId&nbsp;=&nbsp;<span style="color:blue;">show</span>&nbsp;$&nbsp;userId&nbsp;user&nbsp;+&nbsp;i &nbsp;&nbsp;actual&nbsp;&lt;-&nbsp;post&nbsp;lookupUser&nbsp;updateUser&nbsp;(<span style="color:blue;">show</span>&nbsp;$&nbsp;userId&nbsp;user)&nbsp;uniqueOtherUserId &nbsp;&nbsp;assertPostFailure&nbsp;db&nbsp;actual</pre> </p> <p> Since this test body is so similar to the previous test, I'm not going to give you a detailed walkthrough. I did, however, promise to describe the <code>runStateTest</code> helper function: </p> <p> <pre><span style="color:#2b91af;">runStateTest</span>&nbsp;::&nbsp;<span style="color:blue;">State</span>&nbsp;(<span style="color:blue;">Map</span>&nbsp;k&nbsp;a)&nbsp;b&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;b runStateTest&nbsp;=&nbsp;<span style="color:blue;">flip</span>&nbsp;evalState&nbsp;Map.empty</pre> </p> <p> Since this is a one-liner, you could also write all the tests by simply in-lining that little expression, but I thought that it made the tests more readable to give this function an explicit name. </p> <p> It takes any <code>State (Map k a) b</code> and runs it with an empty map. Thus, all <code>State</code>-valued functions, like the tests, must explicitly put data into the state. This is also what the tests do. </p> <p> Notice that all the tests return <code>State</code> values. For example, the <code>assertPostFailure</code> function returns <code>StateT s m Bool</code>, of which <code>State s Bool</code> is an alias. This fits <code>State (Map k a) b</code> when <code>s</code> is <code>Map k a</code>, which again is aliased to <code>DB</code>. Reducing all of this, the tests are simply functions that return <code>Bool</code>. </p> <h3 id="bc67a5ebaf7347ffa4906b209c31d156"> Invalid user ID test cases <a href="#bc67a5ebaf7347ffa4906b209c31d156" title="permalink">#</a> </h3> <p> Finally, you can also cover the two test cases where one of the user IDs is invalid: </p> <p> <pre>testProperty&nbsp;<span style="color:#a31515;">&quot;Users&nbsp;don&#39;t&nbsp;connect&nbsp;when&nbsp;user&nbsp;Id&nbsp;is&nbsp;invalid&quot;</span>&nbsp;$&nbsp;\ &nbsp;&nbsp;s&nbsp;otherUser&nbsp;-&gt;&nbsp;isIdInvalid&nbsp;s&nbsp;==&gt;&nbsp;runStateTest&nbsp;$&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;db&nbsp;=&nbsp;Map.fromList&nbsp;[toDBEntry&nbsp;otherUser] &nbsp;&nbsp;put&nbsp;db &nbsp;&nbsp;actual&nbsp;&lt;-&nbsp;post&nbsp;lookupUser&nbsp;updateUser&nbsp;s&nbsp;(<span style="color:blue;">show</span>&nbsp;$&nbsp;userId&nbsp;otherUser) &nbsp;&nbsp;assertPostFailure&nbsp;db&nbsp;actual , testProperty&nbsp;<span style="color:#a31515;">&quot;Users&nbsp;don&#39;t&nbsp;connect&nbsp;when&nbsp;other&nbsp;user&nbsp;Id&nbsp;is&nbsp;invalid&quot;</span>&nbsp;$&nbsp;\ &nbsp;&nbsp;s&nbsp;user&nbsp;-&gt;&nbsp;isIdInvalid&nbsp;s&nbsp;==&gt;&nbsp;runStateTest&nbsp;$&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;db&nbsp;=&nbsp;Map.fromList&nbsp;[toDBEntry&nbsp;user] &nbsp;&nbsp;put&nbsp;db &nbsp;&nbsp;actual&nbsp;&lt;-&nbsp;post&nbsp;lookupUser&nbsp;updateUser&nbsp;(<span style="color:blue;">show</span>&nbsp;$&nbsp;userId&nbsp;user)&nbsp;s &nbsp;&nbsp;assertPostFailure&nbsp;db&nbsp;actual</pre> </p> <p> Both of these properties take a <code>String</code> value <code>s</code> as input. When QuickCheck generates a <code>String</code>, that could be any <code>String</code> value. Both tests require that the value is an invalid user ID. Specifically, it mustn't be possible to parse the string into an <code>Integer</code>. If you don't constrain QuickCheck, it'll generate various strings, including e.g. <code>"8"</code> and other strings that can be parsed as numbers. </p> <p> In the above <code>"Users don't connect when user doesn't exist"</code> test, you saw how one way to explicitly model constraints on data is to project a seed value in such a way that the constraint always holds. Another way is to use QuickCheck's built-in <code>==&gt;</code> operator to filter out undesired values. In this example, both tests employ the <code>isIdInvalid</code> function: </p> <p> <pre><span style="color:#2b91af;">isIdInvalid</span>&nbsp;::&nbsp;<span style="color:#2b91af;">String</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#2b91af;">Bool</span> isIdInvalid&nbsp;s&nbsp;= &nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;userInt&nbsp;=&nbsp;readMaybe&nbsp;s&nbsp;::&nbsp;Maybe&nbsp;Integer &nbsp;&nbsp;<span style="color:blue;">in</span>&nbsp;isNothing&nbsp;userInt</pre> </p> <p> Using <code>isIdInvalid</code> with the <code>==&gt;</code> operator guarantees that <code>s</code> is an invalid ID. </p> <h3 id="5d89ffd490454c0890dedff39c2f0852"> Summary <a href="#5d89ffd490454c0890dedff39c2f0852" title="permalink">#</a> </h3> <p> While state-based testing may, at first, sound incompatible with strictly functional programming, it's not only possible with the State monad, but even, with good language support, easily done. </p> <p> The tests shown in this article aren't concerned with the interactions between the SUT and its dependencies. Instead, they compare the initial state with the state after exercising the SUT. Comparing values, even complex data structures such as maps, tends to be trivial in functional programming. Immutable values typically have built-in structural equality (in Haskell signified by the automatic <code>Eq</code> type class), which makes comparing them trivial. </p> <p> Now that we know that state-based testing is possible even with Haskell's enforced purity, it should be clear that we can repeat the feat in F#. </p> <p> <strong>Next:</strong> <a href="/2019/03/25/an-example-of-state-based-testing-in-f">An example of state based-testing in F#</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Code quality isn't software quality https://blog.ploeh.dk/2019/03/04/code-quality-is-not-software-quality 2019-03-04T07:38:00+00:00 Mark Seemann <div id="post"> <p> <em>A trivial observation made explicit.</em> </p> <p> You'd think that it's evident that code quality and software quality are two different things. Yet, I often see or hear arguments about one or the other that indicates to me that some people don't make that distinction. I wonder why; I do. </p> <h3 id="56d219ddf746496c94b28d22def9a182"> Software quality <a href="#56d219ddf746496c94b28d22def9a182" title="permalink">#</a> </h3> <p> There's a school of thought leaders who advocate that, ultimately, we write code to solve problems, or to improve life, for people. I have nothing against that line of reasoning; it's just not one that I pursue much. Why should I use my energy on this message when someone like <a href="https://dannorth.net">Dan North</a> does it so much better than I could? </p> <p> Dan North is far from the only person making the point that our employers, or clients, or end-users don't care about the code; he is, in my opinion, one of the best communicators in that field. It makes sense that, with that perspective on software development, you'd invent something like <a href="https://en.wikipedia.org/wiki/Behavior-driven_development">behaviour-driven development</a>. </p> <p> The evaluation criterion used in this discourse is one of utility. Does the software serve a purpose? Does it do it well? </p> <p> In that light, <em>quality software</em> is software that serves its purpose beyond expectation. It rarely, if ever, crashes. It's easy to use. It's sufficiently responsive. It's pretty. It works both on-line and off-line. Attributes like that are externally observable qualities. </p> <p> You can write quality software in many different languages, using various styles. When you evaluate the externally observable qualities of software, the code is invisible. It's not part of the evaluation. </p> <p> It seems to me that some people try to make an erroneous conclusion from this premise. They'd say that since no employer, client, or end user evaluates the software based on the code that produced it, then no one cares about the code. </p> <h3 id="6bfca38c59a543b485fb2658ec86a615"> Code quality <a href="#6bfca38c59a543b485fb2658ec86a615" title="permalink">#</a> </h3> <p> It's easy to refute that argument. All you have to do is to come up with a counter-example. You just have to find <em>one</em> person who cares about the code. That's easy. </p> <p> <em>You</em> care about the code. </p> <p> Perhaps you react negatively to that assertion. Perhaps you say: <em>"No! I'm not one of those effete aesthetes who <a href="http://www.sandraandwoo.com/2015/12/24/0747-melodys-guide-to-programming-languages">only program in Plankalkül</a>."</em> Fine. Maybe you're not the type who likes to polish the code; maybe you're the practical, down-to-earth type who just likes to get stuff done, so that your employer/client/end-user is happy. </p> <p> Even so, I think that you still care about the code. Have you ever looked with bewilderment at a piece of code and thought: <em>"Who the hell wrote this piece of shit!?"</em> How many <a href="https://www.osnews.com/story/19266/wtfsm/">WTFs/m</a> is your code? </p> <p> I think every programmer cares about their code bases; if not in an active manner, then at least in a passive way. Bad code can seriously impede progress. I've seen more than one organisation effectively go out of business because of bad legacy code. </p> <p> Code quality is when you care about the readability and malleability of the code. It's when you care about the code's ability to <em>sustain</em> the business, not only today, but also in the future. </p> <h3 id="19f49a7c758947d9b9a8b4c52e2f8e8d"> Sustainable code <a href="#19f49a7c758947d9b9a8b4c52e2f8e8d" title="permalink">#</a> </h3> <p> I often get the impression that some people look at code quality and software quality as a (false) dichotomy. </p> <p> <img src="/content/binary/software-vs-code-quality-false-dichotomy.png" alt="Software quality versus code quality as a false dichotomy."> </p> <p> Such arguments often seem to imply that you can't have one without sacrificing the other. You must choose. </p> <p> The reality is, of course, that you can do both. </p> <p> <img src="/content/binary/software-code-quality-venn.png" alt="Software and code quality Venn diagram."> </p> <p> At the intersection between software and code quality the code sustains the business both now, and in the future. </p> <p> Yes, you should write code such that it produces software that provides value here and now, but you should also do your best to enable it to provide value in the future. This is <em>sustainable code</em>. It's code that can sustain the organisation during its lifetime. </p> <h3 id="1b88784a7a8a4ce5b44334a5ef474a85"> No gold-plating <a href="#1b88784a7a8a4ce5b44334a5ef474a85" title="permalink">#</a> </h3> <p> To be clear: this is not a call for <a href="https://en.wikipedia.org/wiki/Gold_plating_(project_management)">gold plating</a> or <a href="http://wiki.c2.com/?SpeculativeGenerality">speculative generality</a>. You probably can't predict the future needs of the stake-holders. </p> <p> Quality code doesn't have to be able to perfectly address all future requirements. In order to be sustainable, though, it should be easy to modify in the future, or perhaps just easy to throw away and rewrite. I think a good start is to write <a href="https://cleancoders.com/episode/humane-code-real-episode-1/show">humane code</a>; code that fits in your brain. </p> <p> At least, do your best to avoid writing legacy code. </p> <h3 id="74a27b6da02840919eed541b1c93f92f"> Summary <a href="#74a27b6da02840919eed541b1c93f92f" title="permalink">#</a> </h3> <p> Software quality and code quality can co-exist. You can write quality code that compiles to quality software, but one doesn't imply the other. These are two independent quality dimensions. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. An example of interaction-based testing in C# https://blog.ploeh.dk/2019/02/25/an-example-of-interaction-based-testing-in-c 2019-02-25T05:42:00+00:00 Mark Seemann <div id="post"> <p> <em>An example of using Mocks and Stubs for unit testing in C#.</em> </p> <p> This article is an instalment in an article series about how to move <a href="/2019/02/18/from-interaction-based-to-state-based-testing">from interaction-based testing to state-based testing</a>. In this series, you'll be presented with some alternatives to interaction-based testing with Mocks and Stubs. Before we reach the alternatives, however, we need to establish an example of interaction-based testing, so that you have something against which you can compare those alternatives. In this article, I'll present a simple example, in the form of C# code. </p> <p> The code shown in this article is <a href="https://github.com/ploeh/UserManagement">available on GitHub</a>. </p> <h3 id="7cc38b5dd1bc44c6aacb5077ae65c288"> Connect two users <a href="#7cc38b5dd1bc44c6aacb5077ae65c288" title="permalink">#</a> </h3> <p> For the example, I'll use a simplified version of the example that runs through my two <a href="https://cleancoders.com">Clean Coders</a> videos, <a href="https://cleancoders.com/episode/humane-code-real-episode-4/show">Church Visitor</a> and <a href="https://cleancoders.com/episode/humane-code-real-episode-5/show">Preserved in translation</a>. </p> <p> The desired functionality is simple: implement a REST API that enables one user to connect to another user. You could imagine some sort of social media platform, or essentially any sort of online service where users might be interested in connecting with, or following, other users. </p> <p> In essence, you could imagine that a user interface makes an HTTP POST request against our REST API: </p> <p> <pre>POST /connections/42 HTTP/1.1 Content-Type: application/json { "otherUserId": 1337 }</pre> </p> <p> Let's further imagine that we implement the desired functionality with a C# method with this signature: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IHttpActionResult</span>&nbsp;Post(<span style="color:blue;">string</span>&nbsp;userId,&nbsp;<span style="color:blue;">string</span>&nbsp;otherUserId)</pre> </p> <p> We'll return to the implementation later, but I want to point out a few things. </p> <p> First, notice that both <code>userId</code> and <code>otherUserId</code> are <code>string</code> arguments. While the above example encodes both IDs as numbers, essentially, both URLs and JSON are text-based. Following <a href="https://en.wikipedia.org/wiki/Robustness_principle">Postel's law</a>, the method should also accept JSON like <code>{ "otherUserId": "1337" }</code>. That's the reason the <code>Post</code> method takes <code>string</code> arguments instead of <code>int</code> arguments. </p> <p> Second, the return type is <code>IHttpActionResult</code>. Don't worry if you don't know that interface. It's just a way to model HTTP responses, such as <code>200 OK</code> or <code>400 Bad Request</code>. </p> <p> Depending on the input values, and the state of the application, several outcomes are possible: <table> <col> <col> <colgroup span="3"></colgroup> <thead> <tr> <td colspan="2" rowspan="2"></td> <th colspan="3" scope="colgroup">Other user</th> </tr> <tr> <th scope="col">Found</th> <th scope="col">Not found</th> <th scope="col">Invalid</th> </tr> </thead> <tbody> <tr> <th rowspan="3" scope="rowgroup">User</th> <th scope="row">Found</th> <td><em>Other user</em></td> <td><code>"Other user not found."</code></td> <td><code>"Invalid ID for other user."</code></td> </tr> <tr> <th scope="row">Not found</th> <td><code>"User not found."</code></td> <td><code>"User not found."</code></td> <td><code>"User not found."</code></td> </tr> <tr> <th scope="row">Invalid</th> <td><code>"Invalid user ID."</code></td> <td><code>"Invalid user ID."</code></td> <td><code>"Invalid user ID."</code></td> </tr> </tbody> </table> You'll notice that although this is a 3x3 matrix, there's only five distinct outcomes. This is just an implementation decision. If the first user ID is invalid (e.g. if it's a string like <code>"foo"</code> that doesn't represent a number), then it doesn't matter if the other user exists. Likewise, even if the first user ID is well-formed, it might still be the case that no user with that ID exists in the database. </p> <p> The assumption here is that the underlying user database uses integers as row IDs. </p> <p> When both users are found, the other user should be returned in the HTTP response, like this: </p> <p> <pre>HTTP/1.1 200 OK Content-Type: application/json { "id": 1337, "name": "ploeh", "connections": [{ "id": 42, "name": "fnaah" }, { "id": 2112, "name": "ndøh" }] }</pre> </p> <p> The intent is that when the first user (e.g. the one with the <code>42</code> ID) successfully connects to user <em>1337</em>, a user interface can show the full details of the other user, including the other user's connections. </p> <h3 id="2f013c5e86ef4abda25de01c06a90f25"> Happy path test case <a href="#2f013c5e86ef4abda25de01c06a90f25" title="permalink">#</a> </h3> <p> Since there's five distinct outcomes, you ought to write at least five test cases. You could start with the happy-path case, where both user IDs are well-formed and the users exist. </p> <p> All tests in this article use <a href="https://xunit.github.io">xUnit.net</a> 2.3.1, <a href="https://github.com/moq/moq4">Moq</a> 4.8.1, and <a href="https://github.com/AutoFixture/AutoFixture">AutoFixture</a> 4.1.0. </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">UserManagementTestConventions</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;UsersSuccessfullyConnect( &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">Frozen</span>]<span style="color:#2b91af;">Mock</span>&lt;<span style="color:#2b91af;">IUserReader</span>&gt;&nbsp;readerTD, &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">Frozen</span>]<span style="color:#2b91af;">Mock</span>&lt;<span style="color:#2b91af;">IUserRepository</span>&gt;&nbsp;repoTD, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">User</span>&nbsp;user, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">User</span>&nbsp;otherUser, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">ConnectionsController</span>&nbsp;sut) { &nbsp;&nbsp;&nbsp;&nbsp;readerTD &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Setup(r&nbsp;=&gt;&nbsp;r.Lookup(user.Id.ToString())) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Returns(<span style="color:#2b91af;">Result</span>.Success&lt;<span style="color:#2b91af;">User</span>,&nbsp;<span style="color:#2b91af;">IUserLookupError</span>&gt;(user)); &nbsp;&nbsp;&nbsp;&nbsp;readerTD &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Setup(r&nbsp;=&gt;&nbsp;r.Lookup(otherUser.Id.ToString())) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Returns(<span style="color:#2b91af;">Result</span>.Success&lt;<span style="color:#2b91af;">User</span>,&nbsp;<span style="color:#2b91af;">IUserLookupError</span>&gt;(otherUser)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;sut.Post(user.Id.ToString(),&nbsp;otherUser.Id.ToString()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;ok&nbsp;=&nbsp;<span style="color:#2b91af;">Assert</span>.IsAssignableFrom&lt;<span style="color:#2b91af;">OkNegotiatedContentResult</span>&lt;<span style="color:#2b91af;">User</span>&gt;&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;actual); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(otherUser,&nbsp;ok.Content); &nbsp;&nbsp;&nbsp;&nbsp;repoTD.Verify(r&nbsp;=&gt;&nbsp;r.Update(user)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Contains(otherUser.Id,&nbsp;user.Connections); }</pre> </p> <p> To be clear, as far as Overspecified Software goes, this isn't a bad test. It only has two Test Doubles, <code>readerTD</code> and <code>repoTD</code>. My current habit is to name any Test Double with the <em>TD</em> suffix (for <em>Test Double</em>), instead of explicitly naming them <code>readerStub</code> and <code>repoMock</code>. The latter would have been more correct, though, since the <code>Mock&lt;IUserReader&gt;</code> object is consistently used as a Stub, whereas the <code>Mock&lt;IUserRepository&gt;</code> object is used only as a Mock. This is as it should be, because it follows the rule that you should use <a href="/2013/10/23/mocks-for-commands-stubs-for-queries">Mocks for Commands, Stubs for Queries</a>. </p> <p> <code>IUserRepository.Update</code> is, indeed a Command: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IUserRepository</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">void</span>&nbsp;Update(<span style="color:#2b91af;">User</span>&nbsp;user); }</pre> </p> <p> Since the method returns <code>void</code>, unless it doesn't do anything at all, the only thing it can do is to somehow change the state of the system. The test verifies that <code>IUserRepository.Update</code> was invoked with the appropriate input argument. </p> <p> This is fine. </p> <p> I'd like to emphasise that this isn't the biggest problem with this test. A Mock like this verifies that a desired interaction took place. If <code>IUserRepository.Update</code> isn't called in this test case, it would constitute a defect. The software wouldn't have the desired behaviour, so the test ought to fail. </p> <p> The signature of <code>IUserReader.Lookup</code>, on the other hand, implies that it's a Query: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IUserReader</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IResult</span>&lt;<span style="color:#2b91af;">User</span>,&nbsp;<span style="color:#2b91af;">IUserLookupError</span>&gt;&nbsp;Lookup(<span style="color:blue;">string</span>&nbsp;id); }</pre> </p> <p> In C# and most other languages, you can't be sure that implementations of the <code>Lookup</code> method have no side effects. If, however, we assume that the code base in question obeys the <a href="http://en.wikipedia.org/wiki/Command%E2%80%93query_separation">Command Query Separation</a> principle, then, by elimination, this must be a Query (since it's not a Command, because the return type isn't <code>void</code>). </p> <p> For a detailed walkthrough of the <code>IResult&lt;S, E&gt;</code> interface, see my <a href="https://cleancoders.com/episode/humane-code-real-episode-5/show">Preserved in translation</a> video. It's just an <a href="/2018/06/11/church-encoded-either">Either</a> with different terminology, though. <code>Right</code> is equivalent to <code>SuccessResult</code>, and <code>Left</code> corresponds to <code>ErrorResult</code>. </p> <p> The test configures the <code>IUserReader</code> Stub twice. It's necessary to give the Stub some behaviour, but unfortunately you can't just use Moq's <code>It.IsAny&lt;string&gt;()</code> for configuration, because in order to model the test case, the reader should return two different objects for two different inputs. </p> <p> This starts to look like Overspecified Software. </p> <p> Ideally, a Stub should just be present to 'make happy noises' in case the SUT decides to interact with the dependency, but with these two <code>Setup</code> calls, the interaction is overspecified. The test is tightly coupled to how the SUT is implemented. If you change the interaction implemented in the <code>Post</code> method, you could break the test. </p> <p> In any case, what the test does specify is that when you query the <code>UserReader</code>, it returns a <code>Success</code> object for both user lookups, a <code>200 OK</code> result is returned, and the <code>Update</code> method was called with <code>user</code>. </p> <h3 id="e60081720b2e4468883e3b35b9555c47"> Invalid user ID test case <a href="#e60081720b2e4468883e3b35b9555c47" title="permalink">#</a> </h3> <p> If the first user ID is invalid (i.e. not an integer) then the return value should represent <code>400 Bad Request</code> and the message body should indicate as much. This test verifies that this is the case: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">UserManagementTestConventions</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;UsersFailToConnectWhenUserIdIsInvalid( &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">Frozen</span>]<span style="color:#2b91af;">Mock</span>&lt;<span style="color:#2b91af;">IUserReader</span>&gt;&nbsp;readerTD, &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">Frozen</span>]<span style="color:#2b91af;">Mock</span>&lt;<span style="color:#2b91af;">IUserRepository</span>&gt;&nbsp;repoTD, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;userId, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">User</span>&nbsp;otherUser, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">ConnectionsController</span>&nbsp;sut) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.False(<span style="color:blue;">int</span>.TryParse(userId,&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="color:blue;">var</span>&nbsp;_)); &nbsp;&nbsp;&nbsp;&nbsp;readerTD &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Setup(r&nbsp;=&gt;&nbsp;r.Lookup(userId)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Returns(<span style="color:#2b91af;">Result</span>.Error&lt;<span style="color:#2b91af;">User</span>,&nbsp;<span style="color:#2b91af;">IUserLookupError</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">UserLookupError</span>.InvalidId)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;sut.Post(userId,&nbsp;otherUser.Id.ToString()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;err&nbsp;=&nbsp;<span style="color:#2b91af;">Assert</span>.IsAssignableFrom&lt;<span style="color:#2b91af;">BadRequestErrorMessageResult</span>&gt;(actual); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(<span style="color:#a31515;">&quot;Invalid&nbsp;user&nbsp;ID.&quot;</span>,&nbsp;err.Message); &nbsp;&nbsp;&nbsp;&nbsp;repoTD.Verify(r&nbsp;=&gt;&nbsp;r.Update(<span style="color:#2b91af;">It</span>.IsAny&lt;<span style="color:#2b91af;">User</span>&gt;()),&nbsp;<span style="color:#2b91af;">Times</span>.Never()); }</pre> </p> <p> This test starts with a Guard Assertion that <code>userId</code> isn't an integer. This is mostly an artefact of using AutoFixture. Had you used specific example values, then this wouldn't have been necessary. On the other hand, had you written the test case as a property-based test, it would have been even more important to <a href="/2016/01/18/make-pre-conditions-explicit-in-property-based-tests">explicitly encode such a constraint</a>. </p> <p> Perhaps a better design would have been to use a domain-specific method to check for the validity of the ID, but there's always room for improvement. </p> <p> This test is more brittle than it looks. It only defines what should happen when <code>IUserReader.Lookup</code> is called with the invalid <code>userId</code>. What happens if <code>IUserReader.Lookup</code> is called with the <code>Id</code> associated with <code>otherUser</code>? </p> <p> This currently doesn't matter, so the test passes. </p> <p> The test relies, however, on an implementation detail. This test implicitly assumes that the implementation short-circuits as soon as it discovers that <code>userId</code> is invalid. What if, however, you'd made some performance measurements, and you'd discovered that in most cases, the software would run faster if you <code>Lookup</code> both users in parallel? </p> <p> Such an innocuous performance optimisation could break the test, because the behaviour of <code>readerTD</code> is unspecified for all other cases than for <code>userId</code>. </p> <h3 id="d691fe8c85fa42b7baa3ad565c3e6f10"> Invalid ID for other user test case <a href="#d691fe8c85fa42b7baa3ad565c3e6f10" title="permalink">#</a> </h3> <p> What happens if the other user ID is invalid? This unit test exercises that test case: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">UserManagementTestConventions</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;UsersFailToConnectWhenOtherUserIdIsInvalid( &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">Frozen</span>]<span style="color:#2b91af;">Mock</span>&lt;<span style="color:#2b91af;">IUserReader</span>&gt;&nbsp;readerTD, &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">Frozen</span>]<span style="color:#2b91af;">Mock</span>&lt;<span style="color:#2b91af;">IUserRepository</span>&gt;&nbsp;repoTD, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">User</span>&nbsp;user, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;otherUserId, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">ConnectionsController</span>&nbsp;sut) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.False(<span style="color:blue;">int</span>.TryParse(otherUserId,&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="color:blue;">var</span>&nbsp;_)); &nbsp;&nbsp;&nbsp;&nbsp;readerTD &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Setup(r&nbsp;=&gt;&nbsp;r.Lookup(user.Id.ToString())) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Returns(<span style="color:#2b91af;">Result</span>.Success&lt;<span style="color:#2b91af;">User</span>,&nbsp;<span style="color:#2b91af;">IUserLookupError</span>&gt;(user)); &nbsp;&nbsp;&nbsp;&nbsp;readerTD &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Setup(r&nbsp;=&gt;&nbsp;r.Lookup(otherUserId)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Returns(<span style="color:#2b91af;">Result</span>.Error&lt;<span style="color:#2b91af;">User</span>,&nbsp;<span style="color:#2b91af;">IUserLookupError</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">UserLookupError</span>.InvalidId)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;sut.Post(user.Id.ToString(),&nbsp;otherUserId); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;err&nbsp;=&nbsp;<span style="color:#2b91af;">Assert</span>.IsAssignableFrom&lt;<span style="color:#2b91af;">BadRequestErrorMessageResult</span>&gt;(actual); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(<span style="color:#a31515;">&quot;Invalid&nbsp;ID&nbsp;for&nbsp;other&nbsp;user.&quot;</span>,&nbsp;err.Message); &nbsp;&nbsp;&nbsp;&nbsp;repoTD.Verify(r&nbsp;=&gt;&nbsp;r.Update(<span style="color:#2b91af;">It</span>.IsAny&lt;<span style="color:#2b91af;">User</span>&gt;()),&nbsp;<span style="color:#2b91af;">Times</span>.Never()); }</pre> </p> <p> Notice how the test configures <code>readerTD</code> twice: once for the <code>Id</code> associated with <code>user</code>, and once for <code>otherUserId</code>. Why does this test look different from the previous test? </p> <p> Why is the first <code>Setup</code> required? Couldn't the <em>arrange</em> phase of the test just look like the following? </p> <p> <pre><span style="color:#2b91af;">Assert</span>.False(<span style="color:blue;">int</span>.TryParse(otherUserId,&nbsp;<span style="color:blue;">out</span>&nbsp;<span style="color:blue;">var</span>&nbsp;_)); readerTD &nbsp;&nbsp;&nbsp;&nbsp;.Setup(r&nbsp;=&gt;&nbsp;r.Lookup(otherUserId)) &nbsp;&nbsp;&nbsp;&nbsp;.Returns(<span style="color:#2b91af;">Result</span>.Error&lt;<span style="color:#2b91af;">User</span>,&nbsp;<span style="color:#2b91af;">IUserLookupError</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">UserLookupError</span>.InvalidId));</pre> </p> <p> If you wrote the test like that, it would resemble the previous test (<code>UsersFailToConnectWhenUserIdIsInvalid</code>). The problem, though, is that if you remove the <code>Setup</code> for the valid user, the test fails. </p> <p> This is another example of how the use of interaction-based testing makes the tests brittle. The tests are tightly coupled to the implementation. </p> <h3 id="1d8f30969e8345f394af8f3e1bda37ae"> Missing users test cases <a href="#1d8f30969e8345f394af8f3e1bda37ae" title="permalink">#</a> </h3> <p> I don't want to belabour the point, so here's the two remaining tests: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">UserManagementTestConventions</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;UsersDoNotConnectWhenUserDoesNotExist( &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">Frozen</span>]<span style="color:#2b91af;">Mock</span>&lt;<span style="color:#2b91af;">IUserReader</span>&gt;&nbsp;readerTD, &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">Frozen</span>]<span style="color:#2b91af;">Mock</span>&lt;<span style="color:#2b91af;">IUserRepository</span>&gt;&nbsp;repoTD, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;userId, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">User</span>&nbsp;otherUser, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">ConnectionsController</span>&nbsp;sut) { &nbsp;&nbsp;&nbsp;&nbsp;readerTD &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Setup(r&nbsp;=&gt;&nbsp;r.Lookup(userId)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Returns(<span style="color:#2b91af;">Result</span>.Error&lt;<span style="color:#2b91af;">User</span>,&nbsp;<span style="color:#2b91af;">IUserLookupError</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">UserLookupError</span>.NotFound)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;sut.Post(userId,&nbsp;otherUser.Id.ToString()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;err&nbsp;=&nbsp;<span style="color:#2b91af;">Assert</span>.IsAssignableFrom&lt;<span style="color:#2b91af;">BadRequestErrorMessageResult</span>&gt;(actual); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(<span style="color:#a31515;">&quot;User&nbsp;not&nbsp;found.&quot;</span>,&nbsp;err.Message); &nbsp;&nbsp;&nbsp;&nbsp;repoTD.Verify(r&nbsp;=&gt;&nbsp;r.Update(<span style="color:#2b91af;">It</span>.IsAny&lt;<span style="color:#2b91af;">User</span>&gt;()),&nbsp;<span style="color:#2b91af;">Times</span>.Never()); } [<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">UserManagementTestConventions</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;UsersDoNotConnectWhenOtherUserDoesNotExist( &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">Frozen</span>]<span style="color:#2b91af;">Mock</span>&lt;<span style="color:#2b91af;">IUserReader</span>&gt;&nbsp;readerTD, &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#2b91af;">Frozen</span>]<span style="color:#2b91af;">Mock</span>&lt;<span style="color:#2b91af;">IUserRepository</span>&gt;&nbsp;repoTD, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">User</span>&nbsp;user, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;otherUserId, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">ConnectionsController</span>&nbsp;sut) { &nbsp;&nbsp;&nbsp;&nbsp;readerTD &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Setup(r&nbsp;=&gt;&nbsp;r.Lookup(user.Id.ToString())) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Returns(<span style="color:#2b91af;">Result</span>.Success&lt;<span style="color:#2b91af;">User</span>,&nbsp;<span style="color:#2b91af;">IUserLookupError</span>&gt;(user)); &nbsp;&nbsp;&nbsp;&nbsp;readerTD &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Setup(r&nbsp;=&gt;&nbsp;r.Lookup(otherUserId.ToString())) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Returns(<span style="color:#2b91af;">Result</span>.Error&lt;<span style="color:#2b91af;">User</span>,&nbsp;<span style="color:#2b91af;">IUserLookupError</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">UserLookupError</span>.NotFound)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;sut.Post(user.Id.ToString(),&nbsp;otherUserId.ToString()); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;err&nbsp;=&nbsp;<span style="color:#2b91af;">Assert</span>.IsAssignableFrom&lt;<span style="color:#2b91af;">BadRequestErrorMessageResult</span>&gt;(actual); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(<span style="color:#a31515;">&quot;Other&nbsp;user&nbsp;not&nbsp;found.&quot;</span>,&nbsp;err.Message); &nbsp;&nbsp;&nbsp;&nbsp;repoTD.Verify(r&nbsp;=&gt;&nbsp;r.Update(<span style="color:#2b91af;">It</span>.IsAny&lt;<span style="color:#2b91af;">User</span>&gt;()),&nbsp;<span style="color:#2b91af;">Times</span>.Never()); }</pre> </p> <p> Again, notice the asymmetry of these two tests. The top one passes with only one <code>Setup</code> of <code>readerTD</code>, whereas the bottom test requires two in order to pass. </p> <p> You can add a second <code>Setup</code> to the top test to make the two tests equivalent, but people often forget to take such precautions. The result is Fragile Tests. </p> <h3 id="ece9e2b8532f4e7c9e445e9840789121"> Post implementation <a href="#ece9e2b8532f4e7c9e445e9840789121" title="permalink">#</a> </h3> <p> In the spirit of test-driven development, I've shown you the tests before the implementation. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ConnectionsController</span>&nbsp;:&nbsp;<span style="color:#2b91af;">ApiController</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;ConnectionsController( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IUserReader</span>&nbsp;userReader, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IUserRepository</span>&nbsp;userRepository) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UserReader&nbsp;=&nbsp;userReader; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UserRepository&nbsp;=&nbsp;userRepository; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IUserReader</span>&nbsp;UserReader&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IUserRepository</span>&nbsp;UserRepository&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IHttpActionResult</span>&nbsp;Post(<span style="color:blue;">string</span>&nbsp;userId,&nbsp;<span style="color:blue;">string</span>&nbsp;otherUserId) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;userRes&nbsp;=&nbsp;UserReader.Lookup(userId).SelectError( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;error&nbsp;=&gt;&nbsp;error.Accept(<span style="color:#2b91af;">UserLookupError</span>.Switch( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onInvalidId:&nbsp;<span style="color:#a31515;">&quot;Invalid&nbsp;user&nbsp;ID.&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onNotFound:&nbsp;&nbsp;<span style="color:#a31515;">&quot;User&nbsp;not&nbsp;found.&quot;</span>))); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;otherUserRes&nbsp;=&nbsp;UserReader.Lookup(otherUserId).SelectError( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;error&nbsp;=&gt;&nbsp;error.Accept(<span style="color:#2b91af;">UserLookupError</span>.Switch( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onInvalidId:&nbsp;<span style="color:#a31515;">&quot;Invalid&nbsp;ID&nbsp;for&nbsp;other&nbsp;user.&quot;</span>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onNotFound:&nbsp;&nbsp;<span style="color:#a31515;">&quot;Other&nbsp;user&nbsp;not&nbsp;found.&quot;</span>))); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;connect&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;user&nbsp;<span style="color:blue;">in</span>&nbsp;userRes &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;otherUser&nbsp;<span style="color:blue;">in</span>&nbsp;otherUserRes &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;Connect(user,&nbsp;otherUser); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;connect.SelectBoth(Ok,&nbsp;BadRequest).Bifold(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:#2b91af;">User</span>&nbsp;Connect(<span style="color:#2b91af;">User</span>&nbsp;user,&nbsp;<span style="color:#2b91af;">User</span>&nbsp;otherUser) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user.Connect(otherUser); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UserRepository.Update(user); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;otherUser; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This is a simplified version of the code shown towards the end of my <a href="https://cleancoders.com/episode/humane-code-real-episode-5/show">Preserved in translation</a> video, so I'll refer you there for a detailed explanation. </p> <h3 id="ed29f1f1efa14245a29e33010c4dd4d1"> Summary <a href="#ed29f1f1efa14245a29e33010c4dd4d1" title="permalink">#</a> </h3> <p> The premise of <a href="http://amzn.to/YPdQDf">Refactoring</a> is that in order to be able to refactor, the "precondition is [...] solid tests". In reality, many development organisations have the opposite experience. When programmers attempt to make changes to how their code is organised, tests break. In <a href="http://bit.ly/xunitpatterns">xUnit Test Patterns</a> this problem is called <em>Fragile Tests</em>, and the cause is often <em>Overspecified Software</em>. This means that tests are tightly coupled to implementation details of the System Under Test (SUT). </p> <p> It's easy to inadvertently fall into this trap when you use Mocks and Stubs, even when you follow the rule of using Mocks for Commands and Stubs for Queries. In my experience, it's often the explicit configuration of Stubs that tend to make tests brittle. A Command represents an intentional side effect, and you want to verify that such a side effect takes place. A Query, on the other hand, has no side effect, so a black-box test shouldn't be concerned with any interactions involving Queries. </p> <p> Yet, using an 'isolation framework' such as Moq, <a href="https://fakeiteasy.github.io/">FakeItEasy</a>, <a href="http://nsubstitute.github.io/">NSubstitute</a>, and so on, will pull you towards overspecifying the interactions the SUT has with its Query dependencies. </p> <p> How can we improve? One strategy is to move towards a more functional design, which is <a href="/2015/05/07/functional-design-is-intrinsically-testable">intrinsically testable</a>. In the next article, you'll see how to rewrite both tests and implementation in <a href="https://www.haskell.org">Haskell</a>. </p> <p> <strong>Next:</strong> <a href="/2019/03/11/an-example-of-state-based-testing-in-haskell">An example of state-based testing in Haskell</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment"> <div class="comment-author"><a href="https://remibou.github.io/">Rémi Bourgarel</a></div> <div class="comment-content"> <p> Hi Mark, </p> <p> I think I came to the same conclusion (maybe not the same solution), meaning you can't write solid tests when mocking all the dependencies interaction : all these dependencies interaction are implementation details (even the database system you chose). For writing solid tests I chose to write my tests like this : start all the services I can in test environment (database, queue ...), mock only things I have no choice (external PSP or Google Captcha), issue command (using MediatR) and check the result with a query. You can find some of my work <a href="https://github.com/RemiBou/Toss.Blazor/blob/master/Toss.Tests/Server/Models/Tosses/LastTossQueryHandlerTest.cs"> here </a>. The work is not done on all the tests but this is the way I want to go. Let me know what you think about it. </p> <p> I could have launched the tests at the Controller level but I chose Command and Query handler.</p> <p> Can't wait to see your solution </p> </div> <div class="comment-date">2019-02-25 07:53 UTC</div> </div> <div class="comment" id="7b60769bf7eb4be3969623d4819d5c0e"> <div class="comment-author"><a href="/">Mark Seemann</a></div> <div class="comment-content"> <p> Rémi, thank you for writing. Hosting services as part of a test run can be a valuable addition to an overall testing or release pipeline. It's reminiscent of the approach taken in <a href="http://bit.ly/growingoos">GOOS</a>. I've also touched on this option in my Pluralsight course <a href="https://blog.ploeh.dk/outside-in-tdd">Outside-In Test-Driven Development</a>. This is, however, a set of tests I would identify as belonging towards the top of a <a href="https://martinfowler.com/bliki/TestPyramid.html">Test Pyramid</a>. In my experience, such tests tend to run (an order of magnitude) slower than unit tests. </p> <p> That doesn't preclude their use. Depending on circumstances, I still prefer having tests like that. I think that I've written a few applications where tests like that constituted the main body of unit tests. </p> <p> I do, however, also find this style of testing too limiting in many situation. I tend to prefer 'real' unit tests, since they tend to be easier to write, and they execute faster. </p> <p> Apart from performance and maintainability concerns, one problem that I often see with integration tests is that <a href="https://www.infoq.com/presentations/integration-tests-scam">it's practically impossible to cover all edge cases</a>. This tends to lead to either bug-ridden software, or unmaintainable test suites. </p> <p> Still, I think that, ultimately, having enough experience with different styles of testing enables one to make an informed choice. That's my purpose with these articles: to point out that alternatives exist. </p> </div> <div class="comment-date">2019-03-01 9:31 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. From interaction-based to state-based testing https://blog.ploeh.dk/2019/02/18/from-interaction-based-to-state-based-testing 2019-02-18T08:19:00+00:00 Mark Seemann <div id="post"> <p> <em>Indiscriminate use of Mocks and Stubs can lead to brittle test suites. A more functional design can make state-based testing easier, leading to more robust test suites.</em> </p> <p> The original premise of <a href="http://amzn.to/YPdQDf">Refactoring</a> was that in order to refactor, you must have a trustworthy suite of unit tests, so that you can be confident that you didn't break any functionality. <blockquote> <p>"to refactor, the essential precondition is [...] solid tests"</p> <footer><cite>Martin Fowler, <a href="http://amzn.to/YPdQDf">Refactoring</a></cite></footer> </blockquote> The idea is that you can change how the code is organised, and as long as you don't break any tests, all is good. The experience that most people seem to have, though, is that when they change something in the code, tests break. </p> <p> This is a well-known test smell. In <a href="http://bit.ly/xunitpatterns">xUnit Test Patterns</a> this is called <em>Fragile Test</em>, and it's often caused by <em>Overspecified Software</em>. Even if you follow the proper practice of using <a href="/2013/10/23/mocks-for-commands-stubs-for-queries">Mocks for Commands, Stubs for Queries</a>, you can still end up with a code base where the tests are highly coupled to implementation details of the software. </p> <p> The cause is often that when relying on Mocks and Stubs, test verification hinges on how the System Under Test (SUT) interacts with its dependencies. For that reason, we can call such tests <em>interaction-based tests</em>. For more information, watch my Pluralsight course <a href="https://blog.ploeh.dk/advanced-unit-testing">Advanced Unit Testing</a>. </p> <h3 id="fb4f2eb1191943c09450c7281a6c8cb0"> Lessons from functional programming <a href="#fb4f2eb1191943c09450c7281a6c8cb0" title="permalink">#</a> </h3> <p> Another way to verify the outcome of a test is to inspect the state of the system after exercising the SUT. We can, quite naturally, call this <em>state-based testing</em>. In object-oriented design, this can lead to other problems. <a href="http://natpryce.com">Nat Pryce</a> has pointed out that <a href="http://natpryce.com/articles/000342.html">state-based testing breaks encapsulation</a>. </p> <p> Interestingly, in his article, Nat Pryce concludes: <blockquote> "I have come to think of object oriented programming as an inversion of functional programming. In a lazy functional language data is pulled through functions that transform the data and combine it into a single result. In an object oriented program, data is pushed out in messages to objects that transform the data and push it out to other objects for further processing." </blockquote> That's an impressively perceptive observation to make in 2004. I wish I was that perspicacious, but I only <a href="https://blog.ploeh.dk/functional-architecture-with-fsharp">reached a similar conclusion ten years later</a>. </p> <p> Functional programming is based on the fundamental principle of <a href="https://en.wikipedia.org/wiki/Referential_transparency">referential transparency</a>, which, among other things, means that data must be immutable. Thus, no objects change state. Instead, functions can return data that contains immutable state. In unit tests, you can verify that return values are as expected. <a href="/2015/05/07/functional-design-is-intrinsically-testable">Functional design is intrinsically testable</a>; we can consider it a kind of state-based testing, although the states you'd be verifying are immutable return values. </p> <p> In this article series, you'll see three different styles of testing, from interaction-based testing with Mocks and Stubs in C#, over strictly functional state-based testing in <a href="https://www.haskell.org">Haskell</a>, to pragmatic state-based testing in <a href="https://fsharp.org">F#</a>, finally looping back to C# to apply the lessons from functional programming. <ul> <li><a href="/2019/02/25/an-example-of-interaction-based-testing-in-c">An example of interaction-based testing in C#</a></li> <li><a href="/2019/03/11/an-example-of-state-based-testing-in-haskell">An example of state-based testing in Haskell</a></li> <li><a href="/2019/03/25/an-example-of-state-based-testing-in-f">An example of state based-testing in F#</a></li> <li><a href="/2019/04/01/an-example-of-state-based-testing-in-c">An example of state-based testing in C#</a></li> <li><a href="/2019/04/08/a-pure-test-spy">A pure Test Spy</a></li> </ul> The code for all of these articles is <a href="https://github.com/ploeh/UserManagement">available on GitHub</a>. </p> <h3 id="d370a0ae3bc34440b68f8fddab6c1b25"> Summary <a href="#d370a0ae3bc34440b68f8fddab6c1b25" title="permalink">#</a> </h3> <p> Adopting a more functional design, even in a fundamentally object-oriented language like C# can, in my experience, lead to a more sustainable code base. Various maintenance tasks become easier, including unit tests. Functional programming, however, is no panacea. My intent with this article series is only to inspire; to show alternatives to the ways things are normally done. Adopting one of those alternatives could lead to better code, but you must still exercise context-specific judgement. </p> <p> <strong>Next:</strong> <a href="/2019/02/25/an-example-of-interaction-based-testing-in-c">An example of interaction-based testing in C#</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Asynchronous Injection https://blog.ploeh.dk/2019/02/11/asynchronous-injection 2019-02-11T07:43:00+00:00 Mark Seemann <div id="post"> <p> <em>How to combine asynchronous programming with Dependency Injection without leaky abstractions.</em> </p> <p> C# has decent support for asynchronous programming, but it ultimately leads to leaky abstractions. This is often conspicuous when combined with Dependency Injection (DI). This leads to frequently asked questions around the combination of DI and asynchronous programming. This article outlines the problem and suggests an alternative. </p> <p> The code base supporting this article is <a href="https://github.com/ploeh/asynchronous-injection">available on GitHub</a>. </p> <h3 id="0463aa2fd41b46bbbb837709ed9bc58b"> A synchronous example <a href="#0463aa2fd41b46bbbb837709ed9bc58b" title="permalink">#</a> </h3> <p> In this article, you'll see various stages of a small sample code base that pretends to implement the server-side behaviour of an on-line restaurant reservation system (my favourite example scenario). In the first stage, the code uses DI, but no asynchronous I/O. </p> <p> At the boundary of the application, a <code>Post</code> method receives a <code>Reservation</code> object: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ReservationsController</span>&nbsp;:&nbsp;<span style="color:#2b91af;">ControllerBase</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;ReservationsController(<span style="color:#2b91af;">IMaîtreD</span>&nbsp;maîtreD) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MaîtreD&nbsp;=&nbsp;maîtreD; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IMaîtreD</span>&nbsp;MaîtreD&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IActionResult</span>&nbsp;Post(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>?&nbsp;id&nbsp;=&nbsp;MaîtreD.TryAccept(reservation); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(id&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;InternalServerError(<span style="color:#a31515;">&quot;Table&nbsp;unavailable&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Ok(id.Value); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The <code>Reservation</code> object is just a simple bundle of properties: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Reservation</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;Date&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;Email&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;Name&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Quantity&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;IsAccepted&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} }</pre> </p> <p> In a production code base, I'd favour a separation of <a href="https://en.wikipedia.org/wiki/Data_transfer_object">DTOs</a> and domain objects with proper encapsulation, but in order to keep the code example simple, here the two roles are combined. </p> <p> The <code>Post</code> method simply delegates most work to an injected <code>IMaîtreD</code> object, and translates the return value to an HTTP response. </p> <p> The code example is overly simplistic, to the point where you may wonder what is the point of DI, since it seems that the <code>Post</code> method doesn't perform any work itself. A slightly <a href="/2017/01/27/dependency-injection-is-passing-an-argument">more realistic example includes some input validation and mapping between layers</a>. </p> <p> The <code>IMaîtreD</code> implementation is this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">MaîtreD</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IMaîtreD</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;MaîtreD(<span style="color:blue;">int</span>&nbsp;capacity,&nbsp;<span style="color:#2b91af;">IReservationsRepository</span>&nbsp;repository) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Capacity&nbsp;=&nbsp;capacity; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Repository&nbsp;=&nbsp;repository; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Capacity&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IReservationsRepository</span>&nbsp;Repository&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>?&nbsp;TryAccept(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;reservations&nbsp;=&nbsp;Repository.ReadReservations(reservation.Date); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;reservedSeats&nbsp;=&nbsp;reservations.Sum(r&nbsp;=&gt;&nbsp;r.Quantity); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(Capacity&nbsp;&lt;&nbsp;reservedSeats&nbsp;+&nbsp;reservation.Quantity) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reservation.IsAccepted&nbsp;=&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Repository.Create(reservation); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The protocol for the <code>TryAccept</code> method is that it returns the reservation ID if it accepts the reservation. If the restaurant has too little remaining <code>Capacity</code> for the requested date, it instead returns <code>null</code>. Regular readers of this blog will know that I'm <a href="/2015/11/13/null-has-no-type-but-maybe-has">no fan of null</a>, but this keeps the example realistic. I'm also no fan of state mutation, but the example does that as well, by setting <code>IsAccepted</code> to <code>true</code>. </p> <h3 id="fa919e35f82a4387822c9888dd5d7537"> Introducing asynchrony <a href="#fa919e35f82a4387822c9888dd5d7537" title="permalink">#</a> </h3> <p> The above example is entirely synchronous, but perhaps you wish to introduce some asynchrony. For example, the <code>IReservationsRepository</code> implies synchrony: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IReservationsRepository</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Reservation</span>[]&nbsp;ReadReservations(<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;date); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;Create(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation); }</pre> </p> <p> In reality, though, you know that the implementation of this interface queries and writes to a relational database. Perhaps making this communication asynchronous could improve application performance. It's worth a try, at least. </p> <p> How do you make something asynchronous in C#? You change the return type of the methods in question. Therefore, you have to change the <code>IReservationsRepository</code> interface: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IReservationsRepository</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:#2b91af;">Reservation</span>[]&gt;&nbsp;ReadReservations(<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;date); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;Create(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation); }</pre> </p> <p> The Repository methods now return Tasks. This is the first leaky abstraction. From the <a href="http://en.wikipedia.org/wiki/Dependency_inversion_principle">Dependency Inversion Principle</a> it follows that <blockquote> <p>"clients [...] own the abstract interfaces"</p> <footer><cite>Robert C. Martin, <a href="http://amzn.to/19W4JHk">APPP</a>, chapter 11</cite></footer> </blockquote> The <code>MaîtreD</code> class is the client of the <code>IReservationsRepository</code> interface, which should be designed to support the needs of that class. <code>MaîtreD</code> doesn't need <code>IReservationsRepository</code> to be asynchronous. </p> <p> The change of the interface has nothing to with what <code>MaîtreD</code> needs, but rather with a particular implementation of the <code>IReservationsRepository</code> interface. Because this implementation queries and writes to a relational database, this implementation detail leaks into the interface definition. It is, therefore, a leaky abstraction. </p> <p> On a more practical level, accommodating the change is easily done. Just add <code>async</code> and <code>await</code> keywords in appropriate places: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:blue;">int</span>?&gt;&nbsp;TryAccept(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;reservations&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;Repository.ReadReservations(reservation.Date); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;reservedSeats&nbsp;=&nbsp;reservations.Sum(r&nbsp;=&gt;&nbsp;r.Quantity); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(Capacity&nbsp;&lt;&nbsp;reservedSeats&nbsp;+&nbsp;reservation.Quantity) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;reservation.IsAccepted&nbsp;=&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">await</span>&nbsp;Repository.Create(reservation); }</pre> </p> <p> In order to compile, however, you also have to fix the <code>IMaîtreD</code> interface: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IMaîtreD</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:blue;">int</span>?&gt;&nbsp;TryAccept(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation); }</pre> </p> <p> This is the second leaky abstraction, and it's worse than the first. Perhaps you could successfully argue that it was conceptually acceptable to model <code>IReservationsRepository</code> as asynchronous. After all, a Repository conceptually represents a data store, and these are generally out-of-process resources that require I/O. </p> <p> The <code>IMaîtreD</code> interface, on the other hand, is a domain object. It models how business is done, not how data should be accessed. Why should business logic be asynchronous? </p> <p> It's hardly news that <a href="http://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function/">async and await is infectious</a>. Once you introduce Tasks, it's <em>async all the way!</em> </p> <p> That doesn't mean that asynchrony isn't one big leaky abstraction. It is. </p> <p> You've probably already realised what this means in the context of the little example. You must also patch the <code>Post</code> method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:#2b91af;">IActionResult</span>&gt;&nbsp;Post(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>?&nbsp;id&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;MaîtreD.TryAccept(reservation); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(id&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;InternalServerError(<span style="color:#a31515;">&quot;Table&nbsp;unavailable&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Ok(id.Value); }</pre> </p> <p> Pragmatically, I'd be ready to accept the argument that this isn't a big deal. After all, you just replace all return values with Tasks, and add <code>async</code> and <code>await</code> keywords where they need to go. This hardly impacts the maintainability of a code base. </p> <p> In C#, I'd be inclined to just acknowledge that, <em>hey, there's a leaky abstraction. Moving on...</em> </p> <p> On the other hand, sometimes people imply that it has to be like this. That there is no other way. </p> <p> <a href="https://en.wikipedia.org/wiki/Falsifiability">Falsifiable claims</a> like that often get my attention. <em>Oh, really?!</em> </p> <h3 id="46df3267f63a4c908ed710c2b4f9d3f9"> Move impure interactions to the boundary of the system <a href="#46df3267f63a4c908ed710c2b4f9d3f9" title="permalink">#</a> </h3> <p> We can <a href="/2018/09/24/asynchronous-functors">pretend that <code>Task&lt;T&gt;</code> forms a functor</a>. It's also a monad. Monads are those incredibly useful programming abstractions that have been propagating from their origin in statically typed functional programming languages to more mainstream languages like C#. </p> <p> In functional programming, <a href="/2016/03/18/functional-architecture-is-ports-and-adapters">impure interactions happen at the boundary of the system</a>. Taking inspiration from functional programming, you can <a href="/2017/01/27/from-dependency-injection-to-dependency-rejection">move the impure interactions to the boundary of the system</a>. </p> <p> In the interest of keeping the example simple, I'll only move the impure operations one level out: from <code>MaîtreD</code> to <code>ReservationsController</code>. The approach can be generalised, although you may have to look into how to handle <a href="/2017/07/10/pure-interactions">pure interactions</a>. </p> <p> Where are the impure interactions in <code>MaîtreD</code>? They are in the two interactions with <code>IReservationsRepository</code>. The <code>ReadReservations</code> method is non-deterministic, because the same input value can return different results, depending on the state of the database when you call it. The <code>Create</code> method causes a side effect to happen, because it creates a row in the database. This is one way in which the state of the database could change, which makes <code>ReadReservations</code> non-deterministic. Additionally, <code>Create</code> also violates <a href="https://en.wikipedia.org/wiki/Command%E2%80%93query_separation">Command Query Separation</a> (CQS) by returning the ID of the row it creates. This, again, is non-deterministic, because the same input value will produce a new return value every time the method is called. (Incidentally, you should <a href="/2014/08/11/cqs-versus-server-generated-ids">design <code>Create</code> methods so that they don't violate CQS</a>.) </p> <h3 id="dea3b9778dd54d6c9b18e9b3e9c95153"> Move reservations to a method argument <a href="#dea3b9778dd54d6c9b18e9b3e9c95153" title="permalink">#</a> </h3> <p> The first refactoring is the easiest. Move the <code>ReadReservations</code> method call to the application boundary. In the above state of the code, the <code>TryAccept</code> method unconditionally calls <code>Repository.ReadReservations</code> to populate the <code>reservations</code> variable. Instead of doing this from within <code>TryAccept</code>, just pass <code>reservations</code> as a method argument: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:blue;">int</span>?&gt;&nbsp;TryAccept( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Reservation</span>[]&nbsp;reservations, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;reservedSeats&nbsp;=&nbsp;reservations.Sum(r&nbsp;=&gt;&nbsp;r.Quantity); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(Capacity&nbsp;&lt;&nbsp;reservedSeats&nbsp;+&nbsp;reservation.Quantity) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">null</span>; &nbsp;&nbsp;&nbsp;&nbsp;reservation.IsAccepted&nbsp;=&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">await</span>&nbsp;Repository.Create(reservation); }</pre> </p> <p> This no longer compiles until you also change the <code>IMaîtreD</code> interface: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IMaîtreD</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:blue;">int</span>?&gt;&nbsp;TryAccept(<span style="color:#2b91af;">Reservation</span>[]&nbsp;reservations,&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;reservation); }</pre> </p> <p> You probably think that this is a much worse leaky abstraction than returning a Task. I'd be inclined to agree, but trust me: ultimately, this will matter not at all. </p> <p> When you move an impure operation outwards, it means that when you remove it from one place, you must add it to another. In this case, you'll have to query the Repository from the <code>ReservationsController</code>, which also means that you need to add the Repository as a dependency there: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ReservationsController</span>&nbsp;:&nbsp;<span style="color:#2b91af;">ControllerBase</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;ReservationsController( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IMaîtreD</span>&nbsp;maîtreD, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IReservationsRepository</span>&nbsp;repository) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MaîtreD&nbsp;=&nbsp;maîtreD; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Repository&nbsp;=&nbsp;repository; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IMaîtreD</span>&nbsp;MaîtreD&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IReservationsRepository</span>&nbsp;Repository&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:#2b91af;">IActionResult</span>&gt;&nbsp;Post(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;reservations&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;Repository.ReadReservations(reservation.Date); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>?&nbsp;id&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;MaîtreD.TryAccept(reservations,&nbsp;reservation); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(id&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;InternalServerError(<span style="color:#a31515;">&quot;Table&nbsp;unavailable&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;Ok(id.Value); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This is a refactoring in the true sense of the word. It just reorganises the code without changing the overall behaviour of the system. Now the <code>Post</code> method has to query the Repository before it can delegate the business decision to <code>MaîtreD</code>. </p> <h3 id="6a492b66a0d14edda105eb3fb3b4ebb4"> Separate decision from effect <a href="#6a492b66a0d14edda105eb3fb3b4ebb4" title="permalink">#</a> </h3> <p> As far as I can tell, the main reason to use DI is because some impure interactions are conditional. This is also the case for the <code>TryAccept</code> method. Only if there's sufficient remaining capacity does it call <code>Repository.Create</code>. If it detects that there's too little remaining capacity, it immediately returns <code>null</code> and doesn't call <code>Repository.Create</code>. </p> <p> In object-oriented code, DI is the most common way to decouple decisions from effects. Imperative code reaches a decision and calls a method on an object based on that decision. The effect of calling the method can vary because of polymorphism. </p> <p> In functional programming, you typically use a <a href="/2018/03/22/functors">functor</a> like <a href="/2018/03/26/the-maybe-functor">Maybe</a> or <a href="/2018/06/11/church-encoded-either">Either</a> to <a href="/2016/09/26/decoupling-decisions-from-effects">separate decisions from effects</a>. You can do the same here. </p> <p> The protocol of the <code>TryAccept</code> method already communicates the decision reached by the method. An <code>int</code> value is the reservation ID; this implies that the reservation was accepted. On the other hand, <code>null</code> indicates that the reservation was declined. </p> <p> You can use the same sort of protocol, but instead of returning a <code>Nullable&lt;int&gt;</code>, you can return a <code>Maybe&lt;Reservation&gt;</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&gt;&nbsp;TryAccept( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Reservation</span>[]&nbsp;reservations, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;reservedSeats&nbsp;=&nbsp;reservations.Sum(r&nbsp;=&gt;&nbsp;r.Quantity); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(Capacity&nbsp;&lt;&nbsp;reservedSeats&nbsp;+&nbsp;reservation.Quantity) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">Maybe</span>.Empty&lt;<span style="color:#2b91af;">Reservation</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;reservation.IsAccepted&nbsp;=&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;reservation.ToMaybe(); }</pre> </p> <p> This completely decouples the decision from the effect. By returning <code>Maybe&lt;Reservation&gt;</code>, the <code>TryAccept</code> method communicates the decision it made, while leaving further processing entirely up to the caller. </p> <p> In this case, the caller is the <code>Post</code> method, which can now compose the result of invoking <code>TryAccept</code> with <code>Repository.Create</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:#2b91af;">IActionResult</span>&gt;&nbsp;Post(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;reservations&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;Repository.ReadReservations(reservation.Date); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;m&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;MaîtreD.TryAccept(reservations,&nbsp;reservation); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">await</span>&nbsp;m &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Select(<span style="color:blue;">async</span>&nbsp;r&nbsp;=&gt;&nbsp;<span style="color:blue;">await</span>&nbsp;Repository.Create(r)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nothing:&nbsp;<span style="color:#2b91af;">Task</span>.FromResult(InternalServerError(<span style="color:#a31515;">&quot;Table&nbsp;unavailable&quot;</span>)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;just:&nbsp;<span style="color:blue;">async</span>&nbsp;id&nbsp;=&gt;&nbsp;Ok(<span style="color:blue;">await</span>&nbsp;id)); }</pre> </p> <p> Notice that the <code>Post</code> method never attempts to <a href="/2019/02/04/how-to-get-the-value-out-of-the-monad">extract 'the value' from <code>m</code></a>. Instead, it injects the desired behaviour (<code>Repository.Create</code>) into the monad. The result of calling <code>Select</code> with an asynchronous lambda expression like that is a <code>Maybe&lt;Task&lt;int&gt;&gt;</code>, which is a awkward combination. You can fix that later. </p> <p> The <code>Match</code> method is the <a href="/2019/05/20/maybe-catamorphism">catamorphism for Maybe</a>. It looks exactly like the <code>Match</code> method on the <a href="/2018/06/04/church-encoded-maybe">Church-encoded Maybe</a>. It handles both the case when <code>m</code> is empty, and the case when <code>m</code> is populated. In both cases, it returns a <code>Task&lt;IActionResult&gt;</code>. </p> <h3 id="cfd87db1b9304255bda17d1da93c32ee"> Synchronous domain logic <a href="#cfd87db1b9304255bda17d1da93c32ee" title="permalink">#</a> </h3> <p> At this point, you have a compiler warning in your code: <blockquote> Warning CS1998 This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread. </blockquote> Indeed, the current incarnation of <code>TryAccept</code> is synchronous, so remove the <code>async</code> keyword and change the return type: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;TryAccept( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Reservation</span>[]&nbsp;reservations, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;reservedSeats&nbsp;=&nbsp;reservations.Sum(r&nbsp;=&gt;&nbsp;r.Quantity); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(Capacity&nbsp;&lt;&nbsp;reservedSeats&nbsp;+&nbsp;reservation.Quantity) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">Maybe</span>.Empty&lt;<span style="color:#2b91af;">Reservation</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;reservation.IsAccepted&nbsp;=&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;reservation.ToMaybe(); }</pre> </p> <p> This requires a minimal change to the <code>Post</code> method: it no longer has to <code>await</code> <code>TryAccept</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:#2b91af;">IActionResult</span>&gt;&nbsp;Post(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;reservations&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;Repository.ReadReservations(reservation.Date); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;m&nbsp;=&nbsp;MaîtreD.TryAccept(reservations,&nbsp;reservation); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">await</span>&nbsp;m &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Select(<span style="color:blue;">async</span>&nbsp;r&nbsp;=&gt;&nbsp;<span style="color:blue;">await</span>&nbsp;Repository.Create(r)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nothing:&nbsp;<span style="color:#2b91af;">Task</span>.FromResult(InternalServerError(<span style="color:#a31515;">&quot;Table&nbsp;unavailable&quot;</span>)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;just:&nbsp;<span style="color:blue;">async</span>&nbsp;id&nbsp;=&gt;&nbsp;Ok(<span style="color:blue;">await</span>&nbsp;id)); }</pre> </p> <p> Apart from that, this version of <code>Post</code> is the same as the one above. </p> <p> Notice that at this point, the domain logic (<code>TryAccept</code>) is no longer asynchronous. The leaky abstraction is gone. </p> <h3 id="da9febf381b1483883423b96c5ceb9a3"> Redundant abstraction <a href="#da9febf381b1483883423b96c5ceb9a3" title="permalink">#</a> </h3> <p> The overall work is done, but there's some tidying up remaining. If you review the <code>TryAccept</code> method, you'll notice that it no longer uses the injected <code>Repository</code>. You might as well simplify the class by removing the dependency: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">MaîtreD</span>&nbsp;:&nbsp;<span style="color:#2b91af;">IMaîtreD</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;MaîtreD(<span style="color:blue;">int</span>&nbsp;capacity) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Capacity&nbsp;=&nbsp;capacity; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Capacity&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;TryAccept( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Reservation</span>[]&nbsp;reservations, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;reservedSeats&nbsp;=&nbsp;reservations.Sum(r&nbsp;=&gt;&nbsp;r.Quantity); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(Capacity&nbsp;&lt;&nbsp;reservedSeats&nbsp;+&nbsp;reservation.Quantity) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">Maybe</span>.Empty&lt;<span style="color:#2b91af;">Reservation</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reservation.IsAccepted&nbsp;=&nbsp;<span style="color:blue;">true</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;reservation.ToMaybe(); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> The <code>TryAccept</code> method is now deterministic. The same input will always return the same input. This is not yet a <a href="https://en.wikipedia.org/wiki/Pure_function">pure function</a>, because it still has a single side effect: it mutates the state of <code>reservation</code> by setting <code>IsAccepted</code> to <code>true</code>. You could, however, without too much trouble refactor <code>Reservation</code> to an immutable <a href="https://en.wikipedia.org/wiki/Value_object">Value Object</a>. </p> <p> This would enable you to write the last part of the <code>TryAccept</code> method like this: </p> <p> <pre><span style="color:blue;">return</span>&nbsp;reservation.Accept().ToMaybe();</pre> </p> <p> In any case, the method is close enough to be <a href="/2015/05/07/functional-design-is-intrinsically-testable">pure that it's testable</a>. The interactions of <code>TryAccept</code> and any client code (including unit tests) is completely controllable and observable by the client. </p> <p> This means that there's no reason to <a href="/2013/10/23/mocks-for-commands-stubs-for-queries">Stub it out</a>. You might as well just use the function directly in the <code>Post</code> method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ReservationsController</span>&nbsp;:&nbsp;<span style="color:#2b91af;">ControllerBase</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;ReservationsController( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;capacity, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IReservationsRepository</span>&nbsp;repository) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Capacity&nbsp;=&nbsp;capacity; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Repository&nbsp;=&nbsp;repository; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Capacity&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IReservationsRepository</span>&nbsp;Repository&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:#2b91af;">IActionResult</span>&gt;&nbsp;Post(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;reservations&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">await</span>&nbsp;Repository.ReadReservations(reservation.Date); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;m&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">MaîtreD</span>(Capacity).TryAccept(reservations,&nbsp;reservation); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">await</span>&nbsp;m &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Select(<span style="color:blue;">async</span>&nbsp;r&nbsp;=&gt;&nbsp;<span style="color:blue;">await</span>&nbsp;Repository.Create(r)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Match( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nothing:&nbsp;<span style="color:#2b91af;">Task</span>.FromResult(InternalServerError(<span style="color:#a31515;">&quot;Table&nbsp;unavailable&quot;</span>)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;just:&nbsp;<span style="color:blue;">async</span>&nbsp;id&nbsp;=&gt;&nbsp;Ok(<span style="color:blue;">await</span>&nbsp;id)); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Notice that <code>ReservationsController</code> no longer has an <code>IMaîtreD</code> dependency. </p> <p> All this time, whenever you make a change to the <code>TryAccept</code> method signature, you'd also have to fix the <code>IMaîtreD</code> interface to make the code compile. If you worried that all of these changes were leaky abstractions, you'll be happy to learn that <a href="https://amzn.to/2PzDpJu">in the end, it doesn't even matter</a>. No code uses that interface, so you can delete it. </p> <h3 id="843e56ff0b74406eb236bebd2fad4828"> Grooming <a href="#843e56ff0b74406eb236bebd2fad4828" title="permalink">#</a> </h3> <p> The <code>MaîtreD</code> class looks fine, but the <code>Post</code> method could use some grooming. I'm not going to tire you with all the small refactoring steps. You can follow them in the GitHub repository if you're interested. Eventually, you could arrive at an implementation like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">ReservationsController</span>&nbsp;:&nbsp;<span style="color:#2b91af;">ControllerBase</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;ReservationsController( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;capacity, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IReservationsRepository</span>&nbsp;repository) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Capacity&nbsp;=&nbsp;capacity; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Repository&nbsp;=&nbsp;repository; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;maîtreD&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">MaîtreD</span>(capacity); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Capacity&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">IReservationsRepository</span>&nbsp;Repository&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">MaîtreD</span>&nbsp;maîtreD; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:#2b91af;">IActionResult</span>&gt;&nbsp;Post(<span style="color:#2b91af;">Reservation</span>&nbsp;reservation) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">await</span>&nbsp;Repository.ReadReservations(reservation.Date) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Select(rs&nbsp;=&gt;&nbsp;maîtreD.TryAccept(rs,&nbsp;reservation)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.SelectMany(m&nbsp;=&gt;&nbsp;m.Traverse(Repository.Create)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Match(InternalServerError(<span style="color:#a31515;">&quot;Table&nbsp;unavailable&quot;</span>),&nbsp;Ok); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Now the <code>Post</code> method is just a single, composed asynchronous pipeline. Is it a coincidence that this is possible? </p> <p> This is no coincidence. This top-level method executes in the 'Task monad', and a monad is, by definition, composable. You can chain operations together, and they don't all have to be asynchronous. Specifically, <code>maîtreD.TryAccept</code> is a synchronous piece of business logic. It's unaware that it's being injected into an asynchronous context. This type of design would be completely run of the mill in <a href="https://fsharp.org">F#</a> with its <a href="https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/asynchronous-workflows">asynchronous workflows</a>. </p> <h3 id="ff3eb0a846d24e1b82291004b5acf126"> Summary <a href="#ff3eb0a846d24e1b82291004b5acf126" title="permalink">#</a> </h3> <p> Dependency Injection frequently involves I/O-bound operations. Those typically get hidden behind interfaces so that they can be mocked or stubbed. You may want to access those I/O-bound resources asynchronously, but with C#'s support for asynchronous programming, you'll have to make your abstractions asynchronous. </p> <p> When you make the leaf nodes in your call graph asynchronous, that design change ripples through the entire code base, forcing you to be <em>async all the way</em>. One result of this is that the domain model must also accommodate asynchrony, although this is rarely required by the logic it implements. These concessions to asynchrony are leaky abstractions. </p> <p> Pragmatically, it's hardly a big problem. You can use the <code>async</code> and <code>await</code> keywords to deal with the asynchrony, and it's unlikely to, in itself, cause a problem with maintenance. </p> <p> In functional programming, monads can address asynchrony without introducing sweeping leaky abstractions. Instead of making DI asynchronous, you can inject desired behaviour into an asynchronous context. </p> <p> Behaviour Injection, not Dependency Injection. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="6d45d75e1bfd495a8cd688768f960c06"> <div class="comment-author">Ramon Pfeiffer</div> <div class="comment-content"> <p> Hi Mark, </p> <p> aren't you loading more responsibilities on the <code>ReservationsController</code>? Previously, it only had to delegate all the work to <code>MaîtreD</code> and return an appropriate result, now it additionally fetches reservations from the repository. You are also loading the handling of any errors the reservations repository might throw onto the controller, instead of handling them in the <code>MaîtreD</code> class. </p> <p> You are also hard wiring a dependency on <code>MaîtreD</code> into the <code>ReservationsController</code>; I thought one of the advantages of DI were to avoid newing up dependencies to concrete implementations outside of a centralized "builder class". </p> <p> Could you elaborate on these points? Thanks! </p> </div> <div class="comment-date">2019-02-11 10:39 UTC</div> </div> <div class="comment" id="0961c2d19e0f4990941ba597ffc0514f"> <div class="comment-author"><a href="/">Mark Seemann</a></div> <div class="comment-content"> <p> Ramon, thank you for writing. Am I loading more responsibilities on the Controller? Yes, I am. Too many? I don't think so. </p> <p> To be fair, however, this example is unrealistically simplified (in order to make it easily understandable). There isn't much going on, overall, so one has to imagine that more things are happening than is actually the case. For instance, at the beginning of the example, so little is going on in the Controller that I think it'd be fair to ask why it's even necessary to distinguish between a Controller and a <code>MaîtreD</code> class. </p> <p> Usually, I'd say that the responsibility of a Controller object is to facilitate the translation of what goes on at the boundary of the application and what happens in the domain model. Using the terminology of the <a href="/2016/03/18/functional-architecture-is-ports-and-adapters">ports and adapters</a> architecture, you could say that a Controller's responsibility is to serve as an <a href="https://en.wikipedia.org/wiki/Adapter_pattern">Adapter</a> between the technology-agnostic domain model and the technology-specific SDKs you'll need to bring into play to communicate with the 'real world'. Talking to databases fits that responsibility, I think. </p> <p> The <code>MaîtreD</code> class didn't handle any database errors before, so I don't agree that I've moved that responsibility. </p> <p> When it comes to using a <code>MaîtreD</code> object from inside the Controller, I don't agree that I've 'hard-wired' it. It's not a dependency in the Dependency Injection sense; it's an implementation detail. Notice that it's a <code>private</code> class field. </p> <p> Is it an 'advantage of DI' that you can <em>"avoid newing up dependencies to concrete implementations outside of a centralized "builder class"?"</em> How is that an advantage? Is that a goal? </p> <p> In future articles, I'll discuss this sort of 'dependency elimination' in more details. </p> </div> <div class="comment-date">2019-02-11 15:29 UTC</div> </div> <div class="comment" id="b1876a214ea54221935748c21e148d2a"> <div class="comment-author">Ramon Pfeiffer</div> <div class="comment-content"> <p> Mark, thanks for replying. </p> <p> I assumed that some exception handling would be happening in the <code>MaitreD</code> class that would then migrate to the <code>ReservationsController</code> and you left it out for the sake of simplicity. But granted, that can still happen inside the respository class. </p> <p> Let's imagine that for some reason, you want to write to the filesystem in addition to the database (eg. writing some reservation data like table number that can be printed and given to the customer). Following your reasoning, there would now be a reference to some <code>IReservationPrinter</code> in the Controller. It suddenly has to hold references to all data exchange classes that it was previously unaware of, only caring about the result <code>MaîtreD</code> was returning. </p> <p> Maybe I didn't express myself properly: I thought Dependency Injection is a technique to resolve all implementation types at a single composition root. Of course this only applies to dependencies in the sense of DI, so where do you draw the line between implementation detail and dependency? </p> <p> In any case I'm looking forward to reading more articles on this topic! </p> </div> <div class="comment-date">2019-02-11 18:55 UTC</div> </div> <div class="comment" id="0ca54917c833417c816bf086d6324af5"> <div class="comment-author"><a href="/">Mark Seemann</a></div> <div class="comment-content"> <p> Ramon, in general when it comes to exception handling, you either handle exceptions at the source (i.e. in the Repository) or at the boundary of the application (which is typically done by frameworks already). <a href="/2013/07/08/defensive-coding">I'm no fan of defensive coding</a>. <blockquote> "It suddenly has to hold references to all data exchange classes that it was previously unaware of" </blockquote> Yes, but now <code>MaîtreD</code> doesn't have to do that. Is there anything inherently associated with business logic that stipulates that it handles data access? </p> <p> The following line of argument may be increasingly difficult to relate to as time moves forward, and business becomes increasingly digital, but there once was a time when business logic was paper-based. In paper-based organisations, data would flow through a business in the shape of paper; typically as forms. Data would arrive at the desk of a clerk or domain expert who would add more data or annotations to a form, and put it in his or her out-box for later collection. </p> <p> My point is that I see nothing inherent in business logic to stipulate that business objects should be responsible for data retrieval or persistence. I recommend <a href="https://amzn.to/2OyI51M">Domain Modeling Made Functional</a> if you're interested in a comprehensive treatment of this way of looking at modelling business logic. <blockquote> "I thought Dependency Injection is a technique to resolve all implementation types at a single composition root." </blockquote> It is, and that still happens here. There are, however, fewer dependencies overall. I would argue that with the final design outlined here, the remaining dependency (<code>IReservationsRepository</code>) is also, architecturally, the only real dependency of the application. The initial <code>IMaîtreD</code> dependency is, in my opinion, an implementation detail. Exposing it as a dependency makes the code more brittle, and harder to refactor, but that's what I'm going to cover in future articles. </p> </div> <div class="comment-date">2019-02-12 9:24 UTC</div> </div> <div class="comment" id="2f8b9958df594883994859909d1bd5d3"> <div class="comment-author">Ramon Pfeiffer</div> <div class="comment-content"> <p> Mark, I have to admit that I'm still not convinced (without having read the book you mentioned): </p> <p> Expanding on your analogy, a clerk would maybe make a phone call or walk over to another desk if he needs more information regarding his current form (I know I do at my office). A maître d'hôtel would presumably open his book of reservations to check if he still has a table available and would write a new reservation in his book. </p> <p> The <code>MaîtreD</code> doesn't need to know if the data it needs comes from the file system or a database or a web service (that's the responsibility of the repository class), all it cares about is that it needs some data. Currently, some other part of the system decides what data <code>MaîtreD</code> has to work with. </p> <p> Again, I didn't have a look at the reading recommendation yet. Maybe I should. ;) </p> </div> <div class="comment-date">2019-02-12 10:50 UTC</div> </div> <div class="comment" id="82fd379429464775b97f52b48c0a88c1"> <div class="comment-author">Tyson Williams</div> <div class="comment-content"> <p> I definitely agree with Mark that the business logic (in the final version of <code>MaîtreD.TryAccept</code>) should be in a function that is pure and synchronous. However, I am also sympathetic to Ramon's argument. </p> <p> There are two UIs for the application that I am currently building at work. The primary interface is over HTTP and uses web controllers just like in Mark's example. The second interface is a CLI (that is only accessable to administrators with phsyical access to the server). Suppose my application was also an on-line restaurant reservation system and that a reservation could be made with both UIs. </p> <p> Looking back at the final implementation of <code>ReservationsController.Post</code>, the first three lines are independent of <code>ControllerBase</code> and would also need to be executed when accessing the system though the CLI. My understanding is that Ramon's primary suggestion is to move these three lines into <code>MaîtreD.TryAccept</code>. I am sympathetic to Ramon's argument in that I am in favor of extracting those three lines. However, I don't want them to be colocated with the final implimentatiion of <code>MaîtreD.TryAccept</code>. </p> <p> In my mind, the single responsibility of <code>ReservationsController.Post</code> is to translate the result of the reseravation request into the expected type of response. That would be just the fourth line in the final implementation of this method. In terms of naming, I like Ramon's suggestion that the first three lines of <code>ReservationsController.Post</code> be moved to <code>MaîtreD.TryAccept</code>. But then I also want to move the final implementation of <code>MaîtreD.TryAccept</code> to a method on a different type. As we all know, naming is an impossible problem, so I don't have a good name for this new third type. </p> <p> What do you think Ramon? Have I understood your concerns and suggested something that you could get behind? </p> <p> What about you Mark? You said that there was <blockquote> so little...going on in the Controller that I think it'd be fair to ask why it's even necessary to distinguish between a Controller and a <code>MaîtreD</code> class. </blockquote> Would two UIs be sufficient motivation in your eyes to justify distinguishing between a Controller and a <code>MaîtreD</code> class? </p> </div> <div class="comment-date">2019-02-12 17:00 UTC</div> </div> <div class="comment" id="0ecaa41899db43c6ae6eced05d7cf6e6"> <div class="comment-author"><a href="/">Mark Seemann</a></div> <div class="comment-content"> <p> Tyson, thank you for joining the discussion. By adding a particular problem (more than one user interface) to be addressed, you make the discussion more specific. I think this helps to clarify some issues. </p> <p> Ramon wrote: <blockquote> "I have to admit that I'm still not convinced" </blockquote> That's okay; you don't have to be. I rarely write articles with the explicit intent of telling people that they <em>must</em> do something, or that they should <em>never</em> do something else. While it does happen, this article isn't such an article. If it helps you address a problem, then take what you find useful. If it doesn't, then ignore it. </p> <p> With Tyson's help, though, we can now discuss something more concrete. I think some of those observations identify a tender spot in my line of argument. In the initial version of <code>ReservationsController</code>, the only responsibility of the <code>Post</code> method was to translate from and to HTTP. That's a distinct separation of responsibility, so clearly preferable. </p> <p> When I add the <code>Repository</code> dependency, I widen the scope of the <code>ReservationsController</code>'s responsibility, which now includes 'all IO'. This does blur the demarcation of responsibility, but often still works out well in practice, I find. Still, it depends on how much other stuff is going on related to IO. If you have too much IO going on, another separation of responsibilities is in order. </p> <p> I do find, however, that when implementing the same sort of software capability in different user interfaces, I need to specifically design for each user interface paradigm. A web-based user interface is quite different from a command-line interface, which is again different from a native application, or a voice-based interface, and so on. A web-based interface is, for example, stateless, whereas a native smart phone application would often be stateful. You can rarely reuse the 'user interface controller layer' for one type of application in a different type of application. </p> <p> Even a command-line interface could be stateful by <a href="/2017/07/17/a-pure-command-line-wizard">interactively asking a series of questions</a>. That's such a different user interface paradigm that an object designed for one type of interaction is rarely reusable in another context. </p> <p> What I do find is that fine-grained building blocks still compose. When <code>TryAccept</code> is a pure function, it's <em>always</em> composable. This means that my chance of being able to reuse it becomes much higher than if it's an object injected with various dependencies. <blockquote> "a clerk would maybe make a phone call or walk over to another desk if he needs more information regarding his current form" </blockquote> Indeed, but how do you model this in software? A program doesn't have the degree of ad-hoc flexibility that people have. It can't just arbitrarily decide to make a phone call if it doesn't have a 'phone' dependency. Even when using Dependency Injection, you'll have to add that dependency to a business object. You'll have to explicitly write code to give it that capability, and even so, an injected dependency doesn't magically imbue a business object with the capability to make 'ad-hoc phone calls'. A dependency comes with specific methods you can call in order to answer specific questions. </p> <p> Once you're adding code that enables an object to ask specific questions, you might as well just answer those questions up-front and pass the answer as method arguments. That's what this article's refactoring does. It knows that the <code>MaîtreD</code> object is going to ask about the existing reservations for the requested date, so it just passes that information as part of an 'execution context'. <blockquote> "A maître d'hôtel would presumably open his book of reservations to check if he still has a table available and would write a new reservation in his book" </blockquote> That's a brilliant observation! This just once again demonstrates what Evans wrote in <a href="http://amzn.to/WBCwx7">DDD</a>, that insight about the domain arrive piecemeal. A maître d'hôtel clearly doesn't depend on any <em>repository</em>, but rather on the book of reservations. You can add that as a dependency, or pass it as a method argument. I'd lean toward doing the latter, because I'd tend to view a book as a piece of data. </p> <p> Ultimately, if we are to take the idea of <em>inversion of control</em> seriously, we should, well, invert control. When we inject dependencies, we let the object with those dependencies control its interactions with them. Granted, those interactions are now polymorphic, but control isn't inverted. </p> <p> If you truly want to invert control, then load data, pass it to functions, and persist the return values. In that way, functions have no control of where data comes from, or what happens to it afterwards. This keeps a software design supple. </p> </div> <div class="comment-date">2019-02-13 7:26 UTC</div> </div> <div class="comment" id="c5208d5f4c534984891cdc6d5f5a9ca6"> <div class="comment-author">Marek Calus</div> <div class="comment-content"> <p> Hi Mark, Thanks for your post, I think it's very valuable. </p> <p> In the past, I had a situation when I was a junior software developer and just started working on a small, internal web application (ASP.NET MVC) to support HR processes in our company. At the time, I was discovering blogs like yours, or fsharpforfunandprofit.com and was especially fond of the sandwich architecture. I was preparing to refactor one of the controllers just like your example in this post (Controller retrieving necessary data from the repository, passing it to the pure business logic, then wrapping the results in a request). Unfortunately, My more experienced colleague said that it's a "fat controller antipattern" and that the controller can have only one line of code - redirecting the request to the proper business logic method. I wanted to explain to him that he is wrong, but couldn't find proper arguments, or examples. </p> <p> Now I have them. This post is great for this particular purpose. </p> </div> <div class="comment-date">2019-02-13 11:54 UTC</div> </div> <div class="comment" id="38791df654f84d438db15b3e79112e6d"> <div class="comment-author">Ramon Pfeiffer</div> <div class="comment-content"> <p> I guess it comes down to the amount of responsibilities the controller should have. </p> <p> Marek named the fat controller antipattern. I remember reading about some years ago and it stuck, that's why I usually model my controllers to delegate the request to a worker class, maybe map a return value to a transfer object and wrap it all in some <code>ActionResult</code>. I can relate to the argument that all I/O should happen at the boundaries of the system, though I'm not seeing it on the controller's responsibility list, all the more so when I/O exceeds a simple database call. </p> <blockquote> If you have too much IO going on, another separation of responsibilities is in order. </blockquote> <p> I think that is what I was aiming for. The third type that Tyson is looking a name for could then be some kind of thin Data Access Layer, serving as a façade to encapsulate all calls to I/O, that can be injected into the <code>MaîtreD</code> class. </p> <p> Isn't code flexibility usually modeled using conditionals? Assume we are a very important guest and our maître d'hôtel really wishes to make a reservation for us, but all tables are taken. He could decide to phone all currently known guests to ask for a confirmation, if some guest cannot make it, he could give the table to us. </p> <p> Using the initial version of <code>TryAccept</code>, it would lead to something like this: </p> <pre> public async Task&lt;int?&gt; TryAccept(Reservation reservation) { if(await CheckTableAvailability(reservation)) { reservation.IsAccepted = true; return await Repository.Create(reservation); } else { return null; } } private async Task&lt;bool&gt; CheckTableAvailability(Reservation reservation) { var reservations = await Repository.ReadReservations(reservation.Date); int reservedSeats = reservations.Sum(r => r.Quantity); if(Capacity < reservedSeats + reservation.Quantity) { foreach(var r in reservations) { if(!(await Telephone.AskConfirmation(r.Guest.PhoneNumber))) { //some guest cannot make it for his reservation return true; } } //all guests have confirmed their reservation - no table for us return false; } return true; } </pre> <p> That is assuming that <code>MaîtreD</code> has a dependency on both the Repository and a Telephone. Not the best code I've ever written, but it serves its purpose. If the dependency on <code>Reservation</code> is taken out of the <code>MaîtreD</code>, so could the dependency on <code>Telephone</code>. But then, you are deciding beforehand in the controller that <code>MaîtreD</code> <i>might</i> need to make a telephone call - that's business logic in the controller class and a weaker separation of concerns. </p> <blockquote> A maître d'hôtel clearly doesn't depend on any repository, but rather on the book of reservations. You can add that as a dependency, or pass it as a method argument. I'd lean toward doing the latter, because I'd tend to view a book as a piece of data. </blockquote> <p> And this is where I tend to disagree. The book of reservations in my eyes is owned and preciously guarded by the maître d'hôtel. Imagine some lowly garçon scribbling reservations in it. Unbelievable! Joking aside, the reservations in the book are pieces of data, no doubt about that - but I'd see the whole book as a resource owned by le maître and only him being able to request data from it. Of course, this depends on the model of the restaurant that I have in my mind, it might very well be different from yours - we didn't talk about a common model beforehand. </p> </div> <div class="comment-date">2019-02-13 19:54 UTC</div> </div> <div class="comment" id="451723a601f9487397c9aa7fa09381dc"> <div class="comment-author">Ramon Pfeiffer</div> <div class="comment-content"> <p> Apparently, I answered my own question when I moved the table availability check into its own private method. This way, a new dependency <code>TableAvailabilityChecker</code> can handle the availability check (complete with reservations book and phone calls), acting as a common data access layer. </p> <p> I have created a <a href="https://github.com/Thaoden/RestaurantReservation" target="_blank">repository</a>, where I tried to follow the steps outlined in this blog post with the new dependency. After all refactorings the controller looks like this: </p> <pre> public class ReservationsController : ControllerBase { private readonly MaitreD _maitreD; public ReservationsController(int capacity, IReservationsRepository repository, ITelephone telephone) { _maitreD = new MaitreD(capacity); Repository = repository; Telephone = telephone; } public IReservationsRepository Repository { get; } public ITelephone Telephone { get; } public async Task<IActionResult> Post(Reservation reservation) { Reservation[] currentReservations = await Repository.ReadReservations(reservation.Date); var confirmationCalls = currentReservations.Select(cr => Telephone.AskConfirmation(cr.Guest.PhoneNumber)); return _maitreD.CheckTableAvailability(currentReservations, reservation) .Match( some: r => new Maybe<Reservation>(r), none: _maitreD.AskConfirmation(await Task.WhenAll(confirmationCalls), reservation) ) .Match( some: r => Ok(Repository.Create(_maitreD.Accept(r))), none: new ContentResult { Content = "Table unavailable", StatusCode = StatusCodes.Status500InternalServerError } as ActionResult ); } } </pre> <p> During the refactorings, I was able to remove the <code>TableAvailabilityChecker</code> again; I'm quite happy that the maître d'hôtel is checking the table availability and asking for the confirmations with the resources that are given to him. I'm not so happy with the <code>Task.WhenAll()</code> part, but I don't know how to make this more readable and at the same time make the calls only if we need them. </p> <p> All in all, I now think a bit differently about the controller responsibilities: Being at the boundary of the system, it is arguably the best place to make calls to external systems. If and how the information gathered from the outside <i>is used</i> however is still up to the business objects. Thanks, Mark, for the insight! </p> </div> <div class="comment-date">2019-02-15 11:40 UTC</div> </div> <div class="comment" id="b66a6b327b8949b69db335722c9501e1"> <div class="comment-author">Max</div> <div class="comment-content"> <p> Thanks for writing this article. Doesn't testability suffer from turning the Maître d into an implementation detail of the ReservationsController? Now, we not only have to test for the controller's specific responsibilities but also for the behaviour that is implemented by the Maître d. Previously we could have provided an appropriate test double when instantiating the controller, knowing that the Maître d is tested and working. The resulting test classes would be more specific and focused. Is this a trade-off you made in favour of bringing the article's point across? </p> </div> <div class="comment-date">2019-02-17 14:00 UTC</div> </div> <div class="comment" id="28415cd446b34649a210bc03c4e084e9"> <div class="comment-author"><a href="/">Mark Seemann</a></div> <div class="comment-content"> <p> Max, thank you for writing. I don't think that testability suffers; on the contrary, I think that it improves. Once the <code>MaîtreD</code> class becomes deterministic, you no longer have to hide it behind a Test Double in order to be able to control its behaviour. You can control its behaviour simply by making sure that it receives the appropriate input arguments. </p> <p> The <a href="/2012/06/27/FacadeTest">Facade Tests</a> that cover <code>ReservationsController</code> in <a href="https://github.com/ploeh/asynchronous-injection">the repository</a> are, in my opinion, readable and maintainable. </p> <p> I've started <a href="/2019/02/18/from-interaction-based-to-state-based-testing">a new article series about this topic</a>, since I knew it'd come up. I hope that these articles will help illustrate my position. </p> </div> <div class="comment-date">2019-02-18 8:33 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. How to get the value out of the monad https://blog.ploeh.dk/2019/02/04/how-to-get-the-value-out-of-the-monad 2019-02-04T07:45:00+00:00 Mark Seemann <div id="post"> <p> <em>How do I get the value out of my monad? You don't. You inject the desired behaviour into the monad.</em> </p> <p> A frequently asked question about monads can be paraphrased as: <em>How do I get the value out of my monad?</em> This seems to particularly come up when the monad in question is <a href="https://www.haskell.org">Haskell</a>'s <code>IO</code> monad, from which you <em>can't</em> extract the value. This is by design, but then beginners are often stumped on how to write the code they have in mind. </p> <p> You can encounter variations of the question, or at least the underlying conceptual misunderstanding, with other monads. This seems to be particularly prevalent when object-oriented or procedural programmers start working with <a href="/2018/03/26/the-maybe-functor">Maybe</a> or <a href="/2019/01/14/an-either-functor">Either</a>. People really want to extract 'the value' from those monads as well, despite the lack of guarantee that there will be a value. </p> <p> So how do you extract the value from a monad? </p> <p> The answer isn't <em>use a comonad</em>, although it could be, for a limited set of monads. Rather, the answer is <a href="https://en.wikipedia.org/wiki/Mu_(negative)">mu</a>. </p> <h3 id="8edf2d16396b46ad9b5a7d595c36b4a5"> Unit containers <a href="#8edf2d16396b46ad9b5a7d595c36b4a5" title="permalink">#</a> </h3> <p> Before I attempt to address how to work with monads, I think it's worthwhile to speculate on what misleads people into thinking that it makes sense to even contemplate extracting 'the value' from a monad. After all, you rarely encounter the question: <em>How do I get the value out of my collection?</em> </p> <p> Various collections form monads, but everyone intuitively understand that there isn't a single value in a collection. Collections could be empty, or contain many elements. Collections could easily be the most ordinary monad. Programmers deal with collections all the time. </p> <p> Yet, I think that most programmers don't realise that collections form monads. The reason for this could be that mainstream languages rarely makes this relationship explicit. Even <a href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/query-syntax-and-method-syntax-in-linq">C# query syntax</a>, which is nothing but monads in disguise, hides this fact. </p> <p> What happens, I think, is that when programmers first come across monads, they often encounter one of a few <em>unit containers</em>. </p> <p> What's a <em>unit container?</em> I admit that the word is one I made up, because I couldn't detect existing terminology on this topic. The idea, though, is that it's a <a href="/2018/03/22/functors">functor</a> guaranteed to contain exactly one value. Since <a href="https://bartoszmilewski.com/2014/01/14/functors-are-containers">functors are containers</a>, I call such types <em>unit containers</em>. Examples include <a href="/2018/09/03/the-identity-functor">Identity</a>, <a href="/2018/09/10/the-lazy-functor">Lazy</a>, and <a href="/2018/09/24/asynchronous-functors">asynchronous functors</a>. </p> <p> You can extract 'the value' from most unit containers (with <code>IO</code> being the notable exception from the rule). Trivially, you can get the item contained in an Identity container: </p> <p> <pre>&gt; <span style="color:#2b91af;">Identity</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;x&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Identity</span>&lt;<span style="color:blue;">string</span>&gt;(<span style="color:#a31515;">"bar"</span>); &gt; x.Item "bar"</pre> </p> <p> Likewise, you can extract the value from lazy and asynchronous values: </p> <p> <pre>&gt; <span style="color:#4ec9b0;">Lazy</span>&lt;<span style="color:#569cd6;">int</span>&gt;&nbsp;x&nbsp;<span style="color:#b4b4b4;">=</span>&nbsp;<span style="color:#569cd6;">new</span>&nbsp;<span style="color:#4ec9b0;">Lazy</span>&lt;<span style="color:#569cd6;">int</span>&gt;(()&nbsp;<span style="color:#b4b4b4;">=&gt;</span>&nbsp;<span style="color:#b5cea8;">42</span>); &gt; x.Value 42 &gt; <span style="color:#4ec9b0;">Task</span>&lt;<span style="color:#569cd6;">int</span>&gt;&nbsp;y&nbsp;<span style="color:#b4b4b4;">=</span>&nbsp;<span style="color:#4ec9b0;">Task</span><span style="color:#b4b4b4;">.</span>Run(()&nbsp;<span style="color:#b4b4b4;">=&gt;</span>&nbsp;<span style="color:#b5cea8;">1337</span>); &gt; <span style="color:#569cd6;">await</span>&nbsp;y 1337</pre> </p> <p> My theory, then, is that some programmers are introduced to the concept of monads via lazy or asynchronous computations, and that this could establish incorrect mental models. </p> <h3 id="393e8bcfa18244a988ff8ea56a1209b3"> Semi-containers <a href="#393e8bcfa18244a988ff8ea56a1209b3" title="permalink">#</a> </h3> <p> There's another category of monad that we could call <em>semi-containers</em> (again, I'm open to suggestions for a better name). These are data containers that contain either a single value, or no value. In this set of monads, we find <a href="https://stackoverflow.com/a/48490711/126014">Nullable&lt;T&gt;</a>, Maybe, and Either. </p> <p> Unfortunately, Maybe implementations often come with an API that enables you to ask a Maybe object if it's populated or empty, and a way to extract the value from the Maybe container. This misleads many programmers to write code like this: </p> <p> <pre><span style="color:#2b91af;">Maybe</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;id&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:blue;">if</span>&nbsp;(id.HasItem) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Customer</span>(id.Item); <span style="color:blue;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DontKnowWhatToDoException</span>();</pre> </p> <p> Granted, in many cases, people do something more reasonable than throwing a useless exception. In a specific context, it may be clear what to do with an empty Maybe object, but there are problems with this Tester-Doer approach: <ul> <li>It doesn't compose.</li> <li>There's no systematic technique to apply. You always need to handle empty objects in a context-specific way.</li> </ul> These issues interact in unpleasant ways. </p> <p> If you throw an exception when the object is empty, you'll likely have to deal with that exception further up in the call stack. </p> <p> If you return a magic value (like returning <code>-1</code> when a natural number is expected), you again force all callers to check for that magic number. </p> <p> If you set a flag that indicates that an object was empty, again, you put the burden on callers to check for the flag. </p> <p> This leads to <a href="/2013/07/08/defensive-coding">defensive coding</a>, which, at best, makes the code unreadable. </p> <h3 id="f2fd9c0c72fd45ea86908edb606740b6"> Behaviour Injection <a href="#f2fd9c0c72fd45ea86908edb606740b6" title="permalink">#</a> </h3> <p> Interestingly, programmers rarely take a Tester-Doer approach to working with collections. Instead, they rely on APIs for collections and arrays. </p> <p> In C#, <a href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq">LINQ</a> has been around since 2007, and most programmers love it. It's common knowledge that you can use the <code>Select</code> method to, for example, convert an array of numbers to an array of strings: </p> <p> <pre>&gt; <span style="color:blue;">new</span>[]&nbsp;{&nbsp;42,&nbsp;1337,&nbsp;2112,&nbsp;90125&nbsp;}.Select(i&nbsp;=&gt;&nbsp;i.ToString()) string[4] { "42", "1337", "2112", "90125" }</pre> </p> <p> You can do that with all functors, including Maybe: </p> <p> <pre><span style="color:#2b91af;">Maybe</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;id&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Customer</span>&gt;&nbsp;c&nbsp;=&nbsp;id.Select(x&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Customer</span>(x));</pre> </p> <p> A <a href="/2018/03/26/the-maybe-functor">previous article</a> offers a slightly more compelling example: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;viewModel&nbsp;=&nbsp;repository.Read(id).Select(r&nbsp;=&gt;&nbsp;r.ToViewModel());</pre> </p> <p> Common to all the three above examples is that instead of trying to extract a value from the monad (which makes no sense in the array example), you inject the desired behaviour into the context of the data container. What that eventually brings about depends on the monad in question. </p> <p> In the array example, the behaviour being injected is that of turning a number into a string. Since this behaviour is injected into a collection, it's applied to every element in the source array. </p> <p> In the second example, the behaviour being injected is that of turning an integer into a <code>Customer</code> object. Since this behaviour is injected into a Maybe, it's only applied if the source object is populated. </p> <p> In the third example, the behaviour being injected is that of turning a <code>Reservation</code> domain object into a View Model. Again, this only happens if the original Maybe object is populated. </p> <h3 id="9bfd775fc56740198bec05809c6c1b06"> Composability <a href="#9bfd775fc56740198bec05809c6c1b06" title="permalink">#</a> </h3> <p> The marvellous quality of a monad is that it's composable. You could, for example, start by attempting to parse a string into a number: </p> <p> <pre><span style="color:blue;">string</span>&nbsp;candidate&nbsp;=&nbsp;<span style="color:green;">//&nbsp;Some&nbsp;string&nbsp;from&nbsp;application&nbsp;boundary</span> <span style="color:#2b91af;">Maybe</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;idm&nbsp;=&nbsp;TryParseInt(candidate);</pre> </p> <p> This code could be defined in a part of your code base that deals with user input. Instead of trying to get 'the value' out of <code>idm</code>, you can pass the entire object to other parts of the code. The next step, defined in a different method, in a different class, perhaps even in a different library, then queries a database to read a <code>Reservation</code> object corresponding to that ID - if the ID is there, that is: </p> <p> <pre><span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;rm&nbsp;=&nbsp;idm.SelectMany(repository.Read);</pre> </p> <p> The <code>Read</code> method on the repository has this signature: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Reservation</span>&gt;&nbsp;Read(<span style="color:blue;">int</span>&nbsp;id)</pre> </p> <p> The <code>Read</code> method returns a <code>Maybe&lt;Reservation&gt;</code> object because you could pass any <code>int</code> to the method, but there may not be a row in the database that corresponds to that number. Had you used <code>Select</code> on <code>idm</code>, the return type would have been <code>Maybe&lt;Maybe&lt;Reservation&gt;&gt;</code>. This is a typical example of a nested functor, so instead, you use <code>SelectMany</code>, which flattens the functor. You can do this because Maybe is a monad. </p> <p> The result at this stage is a <code>Maybe&lt;Reservation&gt;</code> object. If all goes according to plan, it's populated with a <code>Reservation</code> object from the database. Two things could go wrong at this stage, though: <ol> <li>The <code>candidate</code> string didn't represent a number.</li> <li>The database didn't contain a row for the parsed ID.</li> </ol> If any of these errors occur, <code>idm</code> is empty. </p> <p> You can now pass <code>rm</code> to another part of the code base, which then performs this step: </p> <p> <pre><span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">ReservationViewModel</span>&gt;&nbsp;vm&nbsp;=&nbsp;rm.Select(r&nbsp;=&gt;&nbsp;r.ToViewModel());</pre> </p> <p> Functors and monads are composable (i.e. 'chainable'). This is a fundamental trait of functors; they're (endo)morphisms, which, by definition, are composable. In order to leverage that composability, though, you must retain the monad. If you extract 'the value' from the monad, composability is lost. </p> <p> For that reason, you're not supposed to 'get the value out of the monad'. Instead, you inject the desired behaviour into the monad in question, so that it stays composable. In the above example, <code>repository.Read</code> and <code>r.ToViewModel()</code> are behaviors injected into the Maybe monad. </p> <h3 id="e3be91ccb5a04a99a67d57c39e49b446"> Summary <a href="#e3be91ccb5a04a99a67d57c39e49b446" title="permalink">#</a> </h3> <p> When we learn something new, there's always a phase where we struggle to understand a new concept. Sometimes, we may, inadvertently, erect a tentative, but misleading mental model of a concept. It seems to me that this happens to many people while they're grappling with the concept of functors and monads. </p> <p> One common mental wrong turn that many people seem to take is to try to 'get the value out of the monad'. This seems to be particularly common with <code>IO</code> in Haskell, where the issue is a <a href="https://stackoverflow.com/q/51614573/126014">frequently</a> <a href="https://stackoverflow.com/q/8567743/126014">asked</a> <a href="https://stackoverflow.com/q/7314789/126014">question</a>. </p> <p> I've also reviewed enough <a href="https://fsharp.org">F#</a> code to have noticed that people often take the imperative, Tester-Doer road to <code>'a option</code>. That's the reason this article uses a majority of its space on various Maybe examples. </p> <p> In a future article, I'll show a more complete and compelling example of behaviour injection. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="3375d8afbf574cefa3bd95072d2a936c"> <div class="comment-author">Sean Donohue</div> <div class="comment-content"> <p> Hi Mark, was very interested in your post as I do try and use Option Monads in my code, and I think I understand the point you are making about not thinking of an optional value as something that is composable. However, I recently had a couple of situations where I reluctantly had to check the value, would really appreciate any thoughts you may have? </p> <p> The first example was where I have a UI and the user may specify a Latitude and a Longitude. The user may not yet have specified both values, so each is held as an Option<double>. We then need to calculate the rhumb bearing to a fixed location, so I wrote: <pre> if(latitude.HasValue && longitude.HasValue) Bearing = CalculateRhumbBearing(latitude.Value, longitude.Value, fixedLatitude, fixedLongitude).ToOptionMonad(); else Bearing = OptionMonad<double>.None; </pre> </p> <p> Having read your article, I realise I could change this to a Select statement on latitude, but that lambda would still need to check longitude.HasValue. Should I combine the two options somehow before doing a single Select? </p> <p> </p> <p> The second example again relates to a UI where the user can enter values in a grid, or leave a row blank. I would like to calculate the mean, standard deviation and root mean square of the values, and normally all these functions would have the signature: double Mean(ICollection<double> values) </p> <p> If I keep this then I need a function like <pre> foreach(var item in values) { if(item.HasValue) { yield return item.Value; } } </pre> </p> <p> Or some equivalent Where/Select combination. Can you advise me please, how you recommend transforming an IEnumerable<OptionMonad<X>> to an enumerable<X>? Or should I write a signature overload double Mean(ICollection<OptionMonad<double>> possibleValues) and ditto for SD and RMS? </p> <p> Thanks, Sean </p> </div> <div class="comment-date">2018-02-05 11:30 UTC</div> </div> <div class="comment" id="f8d3e120bf9a46d09b479106812e7020"> <div class="comment-author"><a href="/">Mark Seemann</a></div> <div class="comment-content"> <p> Sean, thank you for writing. The first example you give is quite common, and is easily addressed with using the <a href="/2018/10/29/the-maybe-applicative-functor">applicative</a> or monadic capabilities of <em>Maybe</em>. Often, in a language like C#, it's easiest to use monadic <em>bind</em> (in C# called <code>SelectMany</code>): </p> <p> <pre>Bearing&nbsp;=&nbsp;latitude &nbsp;&nbsp;&nbsp;&nbsp;.SelectMany(lat&nbsp;=&gt;&nbsp;longitude &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Select(lon&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CalculateRhumbBearing(lat,&nbsp;lon,&nbsp;fixedLatitude,&nbsp;fixedLongitude)));</pre> </p> <p> If you find code like that disagreeable, you can also write it with query syntax: </p> <p> <pre>Bearing&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;lat&nbsp;<span style="color:blue;">in</span>&nbsp;latitude &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;lon&nbsp;<span style="color:blue;">in</span>&nbsp;longitude &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;CalculateRhumbBearing(lat,&nbsp;lon,&nbsp;fixedLatitude,&nbsp;fixedLongitude);</pre> </p> <p> Here, <code>Bearing</code> is a <em>Maybe</em> value. As you can see, in neither of the above alternatives is it necessary to check and extract the values. <code>Bearing</code> will be populated when both <code>latitude</code> and <code>longitude</code> are populated, and empty otherwise. </p> <p> Regarding the other question, being able to filter out empty values from a collection is a standard operation in both F# and Haskell. In C#, you can write it like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;Choose&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">IMaybe</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;&nbsp;source) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;source.SelectMany(m&nbsp;=&gt;&nbsp;m.Match(<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">T</span>[0],&nbsp;x&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;x&nbsp;})); }</pre> </p> <p> This example is based on the <a href="/2018/06/04/church-encoded-maybe">Church-encoded Maybe</a>, which is currently my favourite implementation. I decided to call the method <code>Choose</code>, as this is also the name it has in F#. In Haskell, this function is called <code>catMaybes</code>. </p> </div> <div class="comment-date">2019-02-05 16:25 UTC</div> </div> <div class="comment" id="cd846c7abe1040edaeaa8ab97f7190ff"> <div class="comment-author">Achim Stuy</div> <div class="comment-content"> <p> Hi Mark, did you ever think about publishing a Library containing all these types missing in .net Framework like <code>Either</code>? Or can you recommend an existing library? </p> </div> <div class="comment-date">2019-02-07 07:59 UTC</div> </div> <div class="comment" id="885dd7685e8142d7b5af72796ea8c770"> <div class="comment-author"><a href="/">Mark Seemann</a></div> <div class="comment-content"> <p> Achim, thank you for writing. The thought has crossed my mind, but my position on this question seems to be changing. </p> <p> Had you asked me one or two years ago, I'd have answered that I hadn't seriously considered doing that, and that I saw little reason to do so. There is, as far as I can tell, plenty of such libraries out there, although I can't recommend any in particular. This seems to be something that many people create as part of a learning exercise. It seems to be a rite of passage for many people, similarly to developing a Dependency Injection container, or an ORM. </p> <p> Besides, a reusable library would mean another dependency that your code would have to take on. </p> <p> These days, however, I'm beginning to reconsider my position. It seems that no such library is emerging as dominant, and some of the types involved (particularly Maybe) would really be useful. </p> <p> Ideally, these types ought be in the .NET Base Class Library, but perhaps a second-best alternative would be to put them in a commonly-used shared library. </p> </div> <div class="comment-date">2019-02-07 11:15 UTC</div> </div> <div class="comment" id="88e67e633795438f9b1e927a6ca1410c"> <div class="comment-author">Ralph Hendriks</div> <div class="comment-content"> <p> Hi Mark, thank you for the interesting article series. </p> <p> Can you maybe provide guidance of how asynchronous operations can become part of a chain of operations? How would the 'functor flattening' be combined with the built Task/Task<T> types? Extending your example, how would you go about if we would like to enrich the reservation retrieved from repository with that day's special, which happens to be async: <pre> Task<ReservationWithMenuSuggestion> EnrichWithSpecialOfTheDayAsync(Reservation reservation) </pre> </p> <p> I tried with your Church encoded Maybe implementation, but I got stuck with the Task<T> wrapping/unwrapping/awaiting. </div> <div class="comment-date">2019-02-07 15:06 UTC</div> </div> <div class="comment" id="3588d6472d4f4e76832925221aae374a"> <div class="comment-author"><a href="/">Mark Seemann</a></div> <div class="comment-content"> <p> Ralph, thank you for writing. Please see if my new article <a href="/2019/02/11/asynchronous-injection">Asynchronous Injection</a> answers your question. </p> </div> <div class="comment-date">2019-02-11 7:56 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Better abstractions revisited https://blog.ploeh.dk/2019/01/28/better-abstractions-revisited 2019-01-28T07:45:00+00:00 Mark Seemann <div id="post"> <p> <em>How do you design better abstractions? A retrospective look on an old article for object-oriented programmers.</em> </p> <p> About a decade ago, I had already been doing test-driven development (TDD) and used Dependency Injection for many years, but I'd started to notice some patterns about software design. I'd noticed that <a href="/2010/12/02/Interfacesarenotabstractions">interfaces aren't abstractions</a> and that <a href="/2010/12/22/TheTDDApostate">TDD isn't a design methodology</a>. Sometimes, I'd arrive at interfaces that turned out to be good abstractions, but at other times, the interfaces I created seemed to serve no other purpose than enabling unit testing. </p> <p> In 2010 I thought that I'd noticed some patterns for good abstractions, so I wrote an article called <a href="/2010/12/03/Towardsbetterabstractions">Towards better abstractions</a>. I still consider it a decent attempt at communicating my findings, but I don't think that I succeeded. My thinking on the subject was still too immature, and I lacked a proper vocabulary. </p> <p> While I had hoped that I would be able to elaborate on such observations, and perhaps turn them into heuristics, my efforts soon after petered out. I moved on to other things, and essentially gave up on this particular research programme. Years later, while trying to learn <a href="https://en.wikipedia.org/wiki/Category_theory">category theory</a>, I suddenly realised that mathematical disciplines like category theory and abstract algebra could supply the vocabulary. After some further work, I started publishing a substantial and long-running article series called <a href="/2017/10/04/from-design-patterns-to-category-theory">From design patterns to category theory</a>. It goes beyond my initial attempt, but it finally enabled me to crystallise those older observations. </p> <p> In this article, I'll revisit that old article, <em>Towards better abstractions</em>, and translate the vague terminology I used then, to the terminology presented in <em>From design patterns to category theory</em>. </p> <p> The thrust of the old article is that if you can create a <a href="http://en.wikipedia.org/wiki/Composite_pattern">Composite</a> or a <a href="http://en.wikipedia.org/wiki/Null_Object_pattern">Null Object</a> from an interface, then it's likely to be a good abstraction. I still consider that a useful rule of thumb. </p> <p> When can you create a Composite? <a href="/2018/03/12/composite-as-a-monoid">When the abstraction gives rise to a monoid</a>. When can you create a Null Object? <a href="/2018/04/23/null-object-as-identity">When the abstraction gives rise to a monoid</a>. </p> <p> <img src="/content/binary/better-abstractions-as-monoids.png" alt="The terms from the better abstractions article embedded in the set of monoids."> </p> <p> All the 'API shapes' I'd identified in <em>Towards better abstractions</em> form <a href="/2017/10/06/monoids">monoids</a>. </p> <h3 id="8ef17cb1e20547af9515c874d23a574d"> Commands <a href="#8ef17cb1e20547af9515c874d23a574d" title="permalink">#</a> </h3> <p> A <a href="https://en.wikipedia.org/wiki/Command_pattern">Command</a> seems to be universally identified by a method typically called <code>Execute</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;Execute()</pre> </p> <p> From <a href="/2018/01/15/unit-isomorphisms">unit isomorphisms</a> we know that methods with the <code>void</code> return type are <a href="/2018/01/08/software-design-isomorphisms">isomorphic</a> to (impure) functions that return <em>unit</em>, and that <em>unit</em> forms a monoid. </p> <p> Furthermore, we know from <a href="/2017/11/06/function-monoids">function monoids</a> that methods that return a monoid themselves form monoids. Therefore, Commands form monoids. </p> <p> In early 2011 I'd already explicitly noticed that <a href="/2011/03/22/CommandsareComposable">Commands are composable</a>. Now I know the deeper reason for this: they're monoids. </p> <h3 id="42ac895ddb2a4fa8be678f4c74493cfc"> Closure of operations <a href="#42ac895ddb2a4fa8be678f4c74493cfc" title="permalink">#</a> </h3> <p> In <a href="http://amzn.to/WBCwx7">Domain-Driven Design</a>, Eric Evans discusses the benefits of designing APIs that exhibit <em>closure of operations</em>. This means that a method returns the same type as all its input arguments. The simplest example is the one that I show in the old article: </p> <p> <pre><span style="color:blue;">public&nbsp;static</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;DoIt(<span style="color:#2b91af;">T</span>&nbsp;x)</pre> </p> <p> That's just an <a href="/2017/11/13/endomorphism-monoid">endomorphism, which forms a monoid</a>. </p> <p> Another variation is a method that takes two arguments: </p> <p> <pre><span style="color:blue;">public&nbsp;static</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;DoIt(<span style="color:#2b91af;">T</span>&nbsp;x,&nbsp;<span style="color:#2b91af;">T</span>&nbsp;y)</pre> </p> <p> This is a binary operation. While it's certainly a <a href="/2017/12/27/magmas">magma</a>, in itself it's not guaranteed to be a monoid. In fact, Evans' <a href="/2018/01/02/colour-mixing-magma">colour-mixing example is only a magma</a>, but not a monoid. You can, however, also view this as a special case of the <em>reduction of input</em> shape, below, where the 'extra' arguments just happen to have the same type as the return type. In that interpretation, such a method still forms a monoid, but it's not guaranteed to be meaningful. (Just like modulo 31 addition forms a monoid; it's hardly useful.) </p> <p> The same sort of argument goes for methods with closure of operations, but more input arguments, like: </p> <p> <pre><span style="color:blue;">public&nbsp;static</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;DoIt(<span style="color:#2b91af;">T</span>&nbsp;x,&nbsp;<span style="color:#2b91af;">T</span>&nbsp;y,&nbsp;<span style="color:#2b91af;">T</span>&nbsp;z)</pre> </p> <p> This sort of method is, however, rare, unless you're working in a <a href="http://wiki.c2.com/?StringlyTyped">stringly typed</a> code base where methods look like this: </p> <p> <pre><span style="color:blue;">public&nbsp;static</span>&nbsp;<span style="color:blue">string</span>&nbsp;DoIt(<span style="color:blue">string</span>&nbsp;x,&nbsp;<span style="color:blue">string</span>&nbsp;y,&nbsp;<span style="color:blue">string</span>&nbsp;z)</pre> </p> <p> That's a different situation, though, because those strings should probably be <a href="/2015/01/19/from-primitive-obsession-to-domain-modelling">turned into domain types</a> that properly communicate their roles. Once you do that, you'll probably find that the method arguments have different types. </p> <p> In any case, regardless of cardinality, you can view all methods with closure of operations as special cases of the <em>reduction of input</em> shape below. </p> <h3 id="047886dcfa5a4a1398965138669e0ddc"> Reduction of input <a href="#047886dcfa5a4a1398965138669e0ddc" title="permalink">#</a> </h3> <p> This is the part of the original article where my struggles with vocabulary began in earnest. The situation is when you have a method that looks like this, perhaps as an interface method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IInputReducer</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">T1</span>&nbsp;DoIt(<span style="color:#2b91af;">T1</span>&nbsp;x,&nbsp;<span style="color:#2b91af;">T2</span>&nbsp;y,&nbsp;<span style="color:#2b91af;">T3</span>&nbsp;z); }</pre> </p> <p> In order to stay true to the terminology of my original article, I've named this <em>reduction of input</em> generic example <code>IInputReducer</code>. The reason I originally called it <em>reduction of input</em> is that such a method takes a set of input types as arguments, but only returns a value of a type that's a subset of the set of input types. Thus, the method looks like it's reducing the range of input types to a single one of those types. </p> <p> <img src="/content/binary/reduction-of-input.png" alt="Diagram showing three generic types T1, T2, and T3 entering a funnel that only lets T1 pass through."> </p> <p> A realistic example could be a piece of HTTP middleware that defines an <em>action filter</em> as an interface that you can implement to intercept each HTTP request: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">interface</span>&nbsp;<span style="color:#2b91af;">IActionFilter</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:#2b91af;">HttpResponseMessage</span>&gt;&nbsp;ExecuteActionFilterAsync( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">HttpActionContext</span>&nbsp;actionContext, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">CancellationToken</span>&nbsp;cancellationToken, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:#2b91af;">HttpResponseMessage</span>&gt;&nbsp;continuation); }</pre> </p> <p> This is a slightly modified version of <a href="https://docs.microsoft.com/en-us/previous-versions/aspnet/mt175073(v%3dvs.118)">an earlier version of the ASP.NET Web API</a>. Notice that in this example, it's not the first argument's type that doubles as the return type, but rather the third and last argument. The <em>reduction of input</em> 'shape' can take an arbitrary number of arguments, and any of the argument types can double as a return type, regardless of position. </p> <p> Returning to the generic <code>IInputReducer</code> example, you can easily make a Composite of it: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">CompositeInputReducer</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt;&nbsp;:&nbsp;<span style="color:#2b91af;">IInputReducer</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IInputReducer</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt;[]&nbsp;reducers; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;CompositeInputReducer(<span style="color:blue;">params</span>&nbsp;<span style="color:#2b91af;">IInputReducer</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt;[]&nbsp;reducers) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.reducers&nbsp;=&nbsp;reducers; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T1</span>&nbsp;DoIt(<span style="color:#2b91af;">T1</span>&nbsp;x,&nbsp;<span style="color:#2b91af;">T2</span>&nbsp;y,&nbsp;<span style="color:#2b91af;">T3</span>&nbsp;z) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;acc&nbsp;=&nbsp;x; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">foreach</span>&nbsp;(<span style="color:blue;">var</span>&nbsp;reducer&nbsp;<span style="color:blue;">in</span>&nbsp;reducers) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acc&nbsp;=&nbsp;reducer.DoIt(acc,&nbsp;y,&nbsp;z); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;acc; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> Notice that you call <code>DoIt</code> on all the composed <code>reducers</code>. The arguments that aren't part of the return type, <code>y</code> and <code>z</code>, are passed to each call to <code>DoIt</code> unmodified, whereas the <code>T1</code> value <code>x</code> is only used to initialise the accumulator <code>acc</code>. Each call to <code>DoIt</code> also returns a <code>T1</code> object, so the <code>acc</code> value is updated to that object, so that you can use it as an input for the next iteration. </p> <p> This is an imperative implementation, but as you'll see below, you can also implement the same behaviour in a functional manner. </p> <p> For the sake of argument, pretend that you reorder the method arguments so that the method looks like this: </p> <p> <pre><span style="color:#2b91af;">T1</span>&nbsp;DoIt(<span style="color:#2b91af;">T3</span>&nbsp;z,&nbsp;<span style="color:#2b91af;">T2</span>&nbsp;y,&nbsp;<span style="color:#2b91af;">T1</span>&nbsp;x);</pre> </p> <p> From <a href="/2018/02/05/uncurry-isomorphisms">Uncurry isomorphisms</a> you know that a method like that is isomorphic to a function with the type <code>'T3 -&gt; 'T2 -&gt; 'T1 -&gt; 'T1</code> (<a href="https://fsharp.org">F#</a> syntax). You can think of such a curried function as a function that returns a function that returns a function: <code>'T3 -&gt; ('T2 -&gt; ('T1 -&gt; 'T1))</code>. The rightmost function <code>'T1 -&gt; 'T1</code> is clearly an endomorphism, and you already know that an endomorphism gives rise to a monoid. Finally, <a href="/2017/11/06/function-monoids">Function monoids</a> informs us that a function that returns a monoid itself forms a monoid, so <code>'T2 -&gt; ('T1 -&gt; 'T1)</code> forms a monoid. This argument applies recursively, because if that's a monoid, then <code>'T3 -&gt; ('T2 -&gt; ('T1 -&gt; 'T1))</code> is also a monoid. </p> <p> What does that look like in C#? </p> <p> In the rest of this article, I'll revert the <code>DoIt</code> method signature to <code>T1 DoIt(T1 x, T2 y, T3 z);</code>. The monoid implementation looks much like the endomorphism code. Start with a binary operation: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IInputReducer</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt;&nbsp;Append&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IInputReducer</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt;&nbsp;r1, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IInputReducer</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt;&nbsp;r2) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">AppendedReducer</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt;(r1,&nbsp;r2); } <span style="color:blue;">private</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">AppendedReducer</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt;&nbsp;:&nbsp;<span style="color:#2b91af;">IInputReducer</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IInputReducer</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt;&nbsp;r1; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">IInputReducer</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt;&nbsp;r2; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;AppendedReducer( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IInputReducer</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt;&nbsp;r1, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IInputReducer</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt;&nbsp;r2) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.r1&nbsp;=&nbsp;r1; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.r2&nbsp;=&nbsp;r2; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T1</span>&nbsp;DoIt(<span style="color:#2b91af;">T1</span>&nbsp;x,&nbsp;<span style="color:#2b91af;">T2</span>&nbsp;y,&nbsp;<span style="color:#2b91af;">T3</span>&nbsp;z) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;r2.DoIt(r1.DoIt(x,&nbsp;y,&nbsp;z),&nbsp;y,&nbsp;z); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This is similar to the endomorphism <code>Append</code> implementation. When you combine two <code>IInputReducer</code> objects, you receive an <code>AppendedReducer</code> that implements <code>DoIt</code> by first calling <code>DoIt</code> on the first object, and then using the return value from that method call as the input for the second <code>DoIt</code> method call. Notice that <code>y</code> and <code>z</code> are just 'context' variables used for both reducers. </p> <p> Just like the endomorphism, you can also implement the <em>identity</em> input reducer: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">IdentityInputReducer</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt;&nbsp;:&nbsp;<span style="color:#2b91af;">IInputReducer</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T1</span>&nbsp;DoIt(<span style="color:#2b91af;">T1</span>&nbsp;x,&nbsp;<span style="color:#2b91af;">T2</span>&nbsp;y,&nbsp;<span style="color:#2b91af;">T3</span>&nbsp;z) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;x; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This simply returns <code>x</code> while ignoring <code>y</code> and <code>z</code>. The <code>Append</code> method is associative, and the <code>IdentityInputReducer</code> is both <em>left</em> and <em>right identity</em> for the operation, so this is a monoid. Since <a href="/2017/11/20/monoids-accumulate">monoids accumulate</a>, you can also implement an <code>Accumulate</code> extension method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IInputReducer</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt;&nbsp;Accumulate&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">IInputReducer</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt;&gt;&nbsp;reducers) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IInputReducer</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt;&nbsp;identity&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">IdentityInputReducer</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;reducers.Aggregate(identity,&nbsp;(acc,&nbsp;reducer)&nbsp;=&gt;&nbsp;acc.Append(reducer)); }</pre> </p> <p> This implementation follows the overall implementation pattern for accumulating monoidal values: start with the identity and combine pairwise. While I usually show this in a more imperative form, I've here used a proper functional implementation for the method. </p> <p> The <code>IInputReducer</code> object returned from that <code>Accumulate</code> function has exactly the same behaviour as the <code>CompositeInputReducer</code>. </p> <p> The <em>reduction of input</em> shape forms another monoid, and is therefore composable. The Null Object is the <code>IdentityInputReducer&lt;T1, T2, T3&gt;</code> class. If you set <code>T1 = T2 = T3</code>, you have the <em>closure of operations</em> 'shapes' discussed above; they're just special cases, so form at least this type of monoid. </p> <h3 id="53d27a7c707943b5b2d502e6f7977526"> Composable return types <a href="#53d27a7c707943b5b2d502e6f7977526" title="permalink">#</a> </h3> <p> The original article finally discusses methods that in themselves don't look composable, but turn out to be so anyway, because their return types are composable. Without knowing it, I'd figured out that <a href="/2017/11/06/function-monoids">methods that return monoids are themselves monoids</a>. </p> <p> In 2010 I didn't have the vocabulary to put this into specific language, but that's all it says. </p> <h3 id="8b801b9446f34a2587d844b9eb67a6bd"> Summary <a href="#8b801b9446f34a2587d844b9eb67a6bd" title="permalink">#</a> </h3> <p> In 2010 I apparently discovered an ad-hoc, informally specified, vaguely glimpsed, half-understood description of half of abstract algebra. </p> <p> Riffs on <a href="https://en.wikipedia.org/wiki/Greenspun%27s_tenth_rule">Greenspun's tenth rule</a> aside, things clicked for me once I started to investigate what category theory was about, and why it seemed so closely linked to <a href="https://www.haskell.org">Haskell</a>. That's one of the reasons I started writing the <a href="/2017/10/04/from-design-patterns-to-category-theory">From design patterns to category theory</a> article series. </p> <p> The patterns I thought that I could see in 2010 all form monoids, but there are many other universal abstractions from mathematics that apply to programming as well. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Some thoughts on anti-patterns https://blog.ploeh.dk/2019/01/21/some-thoughts-on-anti-patterns 2019-01-21T07:30:00+00:00 Mark Seemann <div id="post"> <p> <em>What's an anti-pattern? Are there rules to identify them, or is it just name-calling? Before I use the term, I try to apply some rules of thumb.</em> </p> <p> It takes time to write a book. Months, even years. It took me two years to write the first edition of <a href="http://amzn.to/12p90MG">Dependency Injection in .NET</a>. The <a href="https://amzn.to/2TE8tJx">second edition of Dependency Injection in .NET</a> is also the result of much work; not so much by me, but by my co-author <a href="http://www.cuttingedge.it/blogs/steven">Steven van Deursen</a>. </p> <p> When you write a book single-handedly, you can be as opinionated as you'd like. When you have a co-author, regardless of how much you think alike, there's bound to be some disagreements. Steven and I agreed about most of the changes we'd like to make to the second edition, but each of us had to yield or compromise a few times. </p> <p> An interesting experience has been that on more than one occasion where I've reluctantly had to yield to Steven, over the time, I've come to appreciate his position. Two minds think better than one. </p> <h3 id="6497bdfd47db433c92a6ec4fabfd4e92"> Ambient Context <a href="#6497bdfd47db433c92a6ec4fabfd4e92" title="permalink">#</a> </h3> <p> One of the changes that Steven wanted to make was that he wanted to change the status of the <em>Ambient Context</em> pattern to an anti-pattern. While I never use that pattern myself, I included it in the first edition in the spirit of the original <a href="http://amzn.to/XBYukB">Design Patterns</a> book. The <em>Gang of Four</em> made it clear that the patterns they'd described weren't invented, but rather discovered: <blockquote> <p> "We have included only designs that have been applied more than once in different systems." </p> <footer><cite>Gamma et al, <em>Design Patterns</em>, 1994, p. 2</cite></footer> </blockquote> The spirit, as I understand it, is to identify solutions that already exist, and catalogue them. When I wrote the first edition of my book, I tried to do that as well. </p> <p> I'd noticed what I eventually named the <em>Ambient Context</em> pattern several places in the .NET Base Class Library. Some of those APIs are still around today. <a href="https://docs.microsoft.com/dotnet/api/system.threading.thread.currentprincipal">Thread.CurrentPrincipal</a>, <a href="https://docs.microsoft.com/dotnet/api/system.globalization.cultureinfo.currentculture">CultureInfo.CurrentCulture</a>, <a href="https://en.wikipedia.org/wiki/Thread-local_storage">thread-local storage</a>, <a href="https://docs.microsoft.com/dotnet/api/system.web.httpcontext.current">HttpContext.Current</a>, and so on. </p> <p> None of these really have anything to do with Dependency Injection (DI), but people sometimes attempt to use them to solve problems similar to the problems that DI addresses. For that reason, and because the pattern was so prevalent, I included it in the book - as a pattern, not an anti-pattern. </p> <p> Steven wanted to make it an anti-pattern, and I conceded. I wasn't sure I was ready to explicitly call it out as an anti-pattern, but I agreed to the change. I'm becoming increasingly happy that Steven talked me into it. </p> <h3 id="08fbf0fd59154a57bbba936128f6c60e"> Pareto efficiency <a href="#08fbf0fd59154a57bbba936128f6c60e" title="permalink">#</a> </h3> <p> I've heard said of me that I'm one of those people who call everything I don't like an anti-pattern. I don't think that's true. </p> <p> I think people's perception of me is skewed because even today, the most visited page (my greatest hit, if you will) is an article called <a href="/2010/02/03/ServiceLocatorisanAnti-Pattern">Service Locator is an Anti-Pattern</a>. (It concerns me a bit that an article from 2010 seems to be my crowning achievement. I hope I haven't peaked yet, but the numbers tell a different tale.) </p> <p> While I've used the term <em>anti-pattern</em> in other connections, I prefer to be conservative with my use of the word. I tend to use it only when I feel confident that something is, indeed, an anti-pattern. </p> <p> What's an anti-pattern? <a href="https://amzn.to/2VHDX3m">AntiPatterns</a> defines it like this: <blockquote> <p> "An AntiPattern is a literary form that describes a commonly occurring solution to a problem that generates decidedly negative consequences." </p> <footer><cite>Brown et al, <em>AntiPatterns</em>, 1998, p. 7</cite></footer> </blockquote> As definitions go, it's quite amphibolous. Is it the problem that generates negative consequences? Hardly. In the context, it's clear that it's the solution that causes problems. In any case, just because it's in a book doesn't necessarily make it right, but I find it a good start. </p> <p> I think that the phrase <em>decidedly negative consequences</em> is key. Most solutions come with <em>some</em> disadvantages, but in order for a 'solution' to be an anti-pattern, the disadvantages must clearly outweigh any advantages produced. </p> <p> I usually look at it another way. If I can solve the problem in a different way that generates at least as many advantages, but fewer disadvantages, then the first 'solution' might be an anti-pattern. This way of viewing the problem may stem from my background in economics. In that perspective, an anti-pattern simply isn't <a href="https://en.wikipedia.org/wiki/Pareto_efficiency">Pareto optimal</a>. </p> <h3 id="eb04af30ae5b40529fda1588525aee6d"> Falsifiability <a href="#eb04af30ae5b40529fda1588525aee6d" title="permalink">#</a> </h3> <p> Another rule of thumb I employ to determine whether a solution could be an anti-pattern is <a href="https://en.wikipedia.org/wiki/Karl_Popper">Popper</a>'s concept of <a href="https://en.wikipedia.org/wiki/Falsifiability">falsifiability</a>. As a continuation of the Pareto efficiency perspective, an anti-pattern is a 'solution' that you can improve without any (significant) trade-offs. </p> <p> That turns claims about anti-patterns into falsifiable statements, which I consider is the most intellectually honest way to go about claiming that things are bad. </p> <p> Take, for example, the claim that <em>Service Locator is an anti-pattern</em>. In light of Pareto efficiency, that's a falsifiable claim. All you have to do to prove me wrong is to present a situation where Service Locator solves a problem, and I can't come up with a better solution. </p> <p> I made the claim about Service Locator in 2010, and so far, no one has been able to present such a situation, even though several have tried. I'm fairly confident making that claim. </p> <p> This way of looking at the term anti-pattern, however, makes me wary of declaiming solutions anti-patterns just because I don't like them. Could there be a counter-argument, some niche scenario, where the pattern actually couldn't be improved without trade-offs? </p> <p> I didn't take it lightly when Steven suggested making Ambient Context an anti-pattern. </p> <h3 id="e027fe12038c4160a3be19bce065cd74"> Preliminary status <a href="#e027fe12038c4160a3be19bce065cd74" title="permalink">#</a> </h3> <p> I've had some time to think about Ambient Context since I had the (civil) discussion with Steven. The more I think about it, the more I think that he's right; that Ambient Context really <em>is</em> an anti-pattern. </p> <p> I never use that pattern myself, so it's clear to me that for all the situations that I typically encounter, there's always better solutions, with no significant trade-offs. </p> <p> The question is: could there be some niche scenario that I'm not aware of, where Ambient Context is a bona fide good solution? </p> <p> The more I think about this, the more I'm beginning to believe that there isn't. It remains to be seen, though. It remains to be falsified. </p> <h3 id="ed7478fa76324922a5543964e9c8c48e"> Summary <a href="#ed7478fa76324922a5543964e9c8c48e" title="permalink">#</a> </h3> <p> I'm so happy that Steven van Deursen agreed to co-author the second edition of <em>Dependency Injection in .NET</em> with me. The few areas where we've disagreed, I've ultimately come around to agree with him. He's truly taken a good book and made it better. </p> <p> One of the changes is that Ambient Context is now classified as an anti-pattern. Originally, I wasn't sure that this was the correct thing to do, but I've since changed my mind. I do think that Ambient Context belongs in the anti-patterns chapter. </p> <p> I could be wrong, though. I was before. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="4493b077ecd84d6d8217d4944e11471a"> <div class="comment-author"><a href="https://rebus.fm">Mogens Heller Grabe</a></div> <div class="comment-content"> <p> Thanks for great input for discussion :P </p> <p> Like with all other patterns and anti-patterns, I think there's a time and a place. </p> <p> Simply looking at it in a one-dimensional manner, i.e. asking "does there exist a solution to this problem with the same advantages but less downsides?" must be qualified with "IN THIS TIME AND PLACE", in my opinion. </p> <p> This way, the patterns/anti-patterns distinction does not make that much sense in a global perspective, because all patterns can be an anti-patterns in some situations, and vice versa. </p> <p> For example, I like what <em>Ambient Context</em> does in <a href="https://github.com/rebus-org/Rebus">Rebus</a>: It provides a mechanism that enables user code to transparently enlist its bus operations in a unit of work, without requiring user code to pass that unit of work to each operation. </p> <p> This is very handy, e.g. in OWIN-based applications, where the unit of work can be managed by an OWIN middleware that uses a <a href="https://github.com/rebus-org/Rebus/wiki/Transactions#i-am-not-in-a-transaction---i-want-to-either-publish-a-message-or-send-several-messages"><code>RebusTransactionScope</code></a>, this way enlisting all send/publish operations on the bus in that unit of work. </p> <p> Had it not been possible to automatically pick up an ongoing ambient Rebus transaction context, one would probably need to pollute the interfaces of one's application with an <code>ITransactionContext</code> argument, thus not handling the cross-cutting concern of managing the unit of work in a cross-cutting manner. </p> </div> <div class="comment-date">2019-01-21 12:37 UTC</div> </div> <div class="comment" id="6d2c0b22097341acadc41e37ce4f1dc8"> <div class="comment-author"><a href="/">Mark Seemann</a></div> <div class="comment-content"> <p> Mogens, thank you for writing. The reason I explicitly framed my treatment in a discourse related to <a href="https://en.wikipedia.org/wiki/Pareto_efficiency">Pareto efficiency</a> is exactly because this view on optima is multi-dimensional. When considering whether a 'solution coordinate' is Pareto-optimal or not, the question is exactly whether or not it's possible to improve at least one dimension without exacerbating any other dimension. If you can make one dimension better <em>without trade-offs</em>, then you can make a Pareto improvement. If you can only make one dimension better at the cost of one or more other dimensions, then you already have a Pareto-optimal solution. </p> <p> The theory of Pareto efficiency doesn't say anything about the number of dimensions. Usually, as in the linked Wikipedia article, the concept is illustrated in the plane, but conceptually, it applies to an arbitrary number of dimensions. </p> <p> In the context of anti-patterns, those dimensions include time and place, as you say. </p> <p> I consider something to be an anti-pattern if I can make a change that constitutes an improvement in at least one dimension, without trading off of any other dimensions. In other words, in this article, I'm very deliberately not looking at it in a one-dimensional manner. </p> <p> As I wrote, I'm still not sure that Ambient Context is an anti-pattern (although I increasingly believe it to be). How can we even test that hypothesis when we can't really quantify software design? </p> <p> On the other hand, if we leave the question about Ambient Context for a moment, I feel confident that Service Locator is an anti-pattern, even in what you call a global perspective. The reason I believe that is that I made that falsifiable claim in 2010, and here, almost nine years later, no-one has successfully produced a valid counter-example. </p> <p> I don't have the same long history with the claim about Ambient Context, so I could be wrong. Perhaps you are, right now, proving me wrong. I can't tell, though, because I don't (yet) know enough about Rebus to be able to tell whether what you describe is Pareto-optimal. </p> <p> The question isn't whether the current design is 'handy'. The question is whether it's possible to come up with a design that's 'globally' better; i.e. either has all the advantages of the current design, but fewer disadvantages; or has more advantages, and only the same disadvantages. </p> <p> I may be able to suggest such an improvement if provided with some code examples, but in the end we may never agree whether one design is better than another. After all, since we can't quantify software design, a subjective judgement will always remain. </p> </div> <div class="comment-date">2019-01-24 8:00 UTC</div> </div> <div class="comment" id="2f2b71feaf034750bdddffdd66af1ab9"> <div class="comment-author"><a href="https://drew.from-wi.com/">Drew Douglas</a></div> <div class="comment-content"> <p> Mark, <br/>Thanks for this thoughtful meta-analysis of what it means to be an anti-pattern and how we think about them. What are some considerations when designing a replacement for ambient context objects? What patterns have you found succesful in object-oriented code? I'm sure you'd mention <a href="https://blog.ploeh.dk/2011/03/03/InjectionConstructorsshouldbesimple/">Constructor Injection</a> and <a href="https://msdn.microsoft.com/en-us/magazine/gg983487.aspx">CQRS</a> with <a href="https://blog.ploeh.dk/2011/03/22/CommandsareComposable/">decorators</a>. </p> <p>How might one refactor ambient context out of a solution? Some say <a href="https://stackoverflow.com/questions/21050247/refactoring-out-singletons-globals-to-use-dependency-injection-for-unit-testing">passing around an object</a>, maybe some sort of <a href="https://en.wikipedia.org/wiki/Continuation-passing_style">continuation</a>?</p> </div> <div class="comment-date">2019-04-12 6:09 UTC</div> </div> <div class="comment" id="86b35a424def4985a24f4ff2c5e06ef1"> <div class="comment-author"><a href="/">Mark Seemann</a></div> <div class="comment-content"> <p> Drew, thank you for writing. Indeed, in object-oriented code, I'd typically replace Ambient Contexts with either injected dependencies or Decorators. I'm not sure I see how CQRS fits into this picture. </p> <p> When refactoring away an Ambient Context, it's typically, before the refactoring, used like <code>Context.DoSomething()</code>. The first step is often to inject a dependency and make it available to the class as a property called <code>Context</code>. IIRC, the C# overload resolution system should then pick the instance property over the static class, so that <code>Context.DoSomething()</code> calls <code>DoSomething</code> on the injected <code>Context</code> property. </p> <p> There may be some static member that also use the Ambient Context. If that's the case, you'll first have to make those static members instance members. </p> <p> Once you've made those replacements everywhere, you should be able to delete the Ambient Context. </p> </div> <div class="comment-date">2019-04-13 16:43 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. An Either functor https://blog.ploeh.dk/2019/01/14/an-either-functor 2019-01-14T07:27:00+00:00 Mark Seemann <div id="post"> <p> <em>Either forms a normal functor. A placeholder article for object-oriented programmers.</em> </p> <p> This article is an instalment in <a href="/2018/03/22/functors">an article series about functors</a>. As another article explains, <a href="/2019/01/07/either-bifunctor">Either is a bifunctor</a>. This makes it trivially a functor. As such, this article is mostly a place-holder to fit the spot in the <em>functor table of contents</em>, thereby indicating that Either is a functor. </p> <p> Since Either is a bifunctor, it's actually not one, but two, functors. Many languages, C# included, are best equipped to deal with unambiguous functors. This is also true in <a href="https://haskell.org">Haskell</a>, where <code>Either l r</code> is only a <code>Functor</code> over the right side. Likewise, in C#, you can make <code>IEither&lt;L, R&gt;</code> a functor by implementing <code>Select</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IEither</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">R1</span>&gt;&nbsp;Select&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">R</span>,&nbsp;<span style="color:#2b91af;">R1</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IEither</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">R</span>&gt;&nbsp;source, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">R</span>,&nbsp;<span style="color:#2b91af;">R1</span>&gt;&nbsp;selector) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;source.SelectRight(selector); }</pre> </p> <p> This method simply delegates all implementation to the <code>SelectRight</code> method; it's just <code>SelectRight</code> by another name. It obeys the functor laws, since these are just specializations of the bifunctor laws, and we know that Either is a proper bifunctor. </p> <p> It would have been technically possible to instead implement a <code>Select</code> method by calling <code>SelectLeft</code>, but it seems generally more useful to enable syntactic sugar for mapping over 'happy path' scenarios. This enables you to write projections over operations that can fail. </p> <p> Here's some <em>C# Interactive</em> examples that use the <code>FindWinner</code> helper method from <a href="/2018/06/11/church-encoded-either">the Church-encoded Either article</a>. Imagine that you're collecting votes; you're trying to pick the highest-voted integer, but in reality, you're only interested in seeing if the number is positive or not. Since <code>FindWinner</code> returns <code>IEither&lt;VoteError, T&gt;</code>, and this type is a functor, you can project the right result, while any left result short-circuits the query. First, here's a successful query: </p> <p> <pre>&gt; <span style="color:blue;">from</span>&nbsp;i&nbsp;<span style="color:blue;">in</span>&nbsp;FindWinner(1,&nbsp;2,&nbsp;-3,&nbsp;-1,&nbsp;2,&nbsp;-1,&nbsp;-1)&nbsp;<span style="color:blue;">select</span>&nbsp;i&nbsp;&gt;&nbsp;0 Right&lt;VoteError, bool&gt;(false)</pre> </p> <p> This query succeeds, resulting in a <code>Right</code> object. The contained value is <code>false</code> because the winner of the vote is <code>-1</code>, which isn't a positive number. </p> <p> On the other hand, the following query fails because of a tie. </p> <p> <pre>&gt; <span style="color:blue;">from</span>&nbsp;i&nbsp;<span style="color:blue;">in</span>&nbsp;FindWinner(1,&nbsp;2,&nbsp;-3,&nbsp;-1,&nbsp;2,&nbsp;-1)&nbsp;<span style="color:blue;">select</span>&nbsp;i&nbsp;&gt;&nbsp;0 Left&lt;VoteError, bool&gt;(Tie)</pre> </p> <p> Because the result is tied on <code>-1</code>, the return value is a <code>Left</code> object containing the <code>VoteError</code> value <code>Tie</code>. </p> <p> Another source of error is an empty input collection: </p> <p> <pre>&gt; <span style="color:blue;">from</span>&nbsp;i&nbsp;<span style="color:blue;">in</span>&nbsp;FindWinner&lt;<span style="color:blue;">int</span>&gt;()&nbsp;<span style="color:blue;">select</span>&nbsp;i&nbsp;&gt;&nbsp;0 Left&lt;VoteError, bool&gt;(Empty)</pre> </p> <p> This time, the <code>Left</code> object contains the <code>Empty</code> error value, since no winner can be found from an empty collection. </p> <p> While the <code>Select</code> method doesn't implement any behaviour that <code>SelectRight</code> doesn't already afford, it enables you to use C# query syntax, as demonstrated by the above examples. </p> <p> <strong>Next:</strong> <a href="/2018/08/06/a-tree-functor">A Tree functor</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Either bifunctor https://blog.ploeh.dk/2019/01/07/either-bifunctor 2019-01-07T09:13:00+00:00 Mark Seemann <div id="post"> <p> <em>Either forms a bifunctor. An article for object-oriented programmers.</em> </p> <p> This article is an instalment in <a href="/2018/12/24/bifunctors">an article series about bifunctors</a>. As the overview article explains, essentially there's two practically useful bifunctors: pairs and <a href="/2018/06/11/church-encoded-either">Either</a>. In <a href="/2018/12/31/tuple-bifunctor">the previous article</a>, you saw how a pair (a two-tuple) forms a bifunctor. In this article, you'll see how Either also forms a bifunctor. </p> <h3 id="b30721a6a66a427eb1a7159d66290337"> Mapping both dimensions <a href="#b30721a6a66a427eb1a7159d66290337" title="permalink">#</a> </h3> <p> In the previous article, you saw how, if you have maps over both dimensions, you can trivially implement <code>SelectBoth</code> (what <a href="https://www.haskell.org">Haskell</a> calls <code>bimap</code>): </p> <p> <pre><span style="color:blue;">return</span>&nbsp;source.SelectFirst(selector1).SelectSecond(selector2);</pre> </p> <p> The relationship can, however, go both ways. If you implement <code>SelectBoth</code>, you can derive <code>SelectFirst</code> and <code>SelectSecond</code> from it. In this article, you'll see how to do that for Either. </p> <p> Given the <a href="/2018/06/11/church-encoded-either">Church-encoded Either</a>, the implementation of <code>SelectBoth</code> can be achieved in a single expression: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IEither</span>&lt;<span style="color:#2b91af;">L1</span>,&nbsp;<span style="color:#2b91af;">R1</span>&gt;&nbsp;SelectBoth&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">L1</span>,&nbsp;<span style="color:#2b91af;">R</span>,&nbsp;<span style="color:#2b91af;">R1</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IEither</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">R</span>&gt;&nbsp;source, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">L1</span>&gt;&nbsp;selectLeft, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">R</span>,&nbsp;<span style="color:#2b91af;">R1</span>&gt;&nbsp;selectRight) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;source.Match&lt;<span style="color:#2b91af;">IEither</span>&lt;<span style="color:#2b91af;">L1</span>,&nbsp;<span style="color:#2b91af;">R1</span>&gt;&gt;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onLeft:&nbsp;&nbsp;l&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;&nbsp;<span style="color:#2b91af;">Left</span>&lt;<span style="color:#2b91af;">L1</span>,&nbsp;<span style="color:#2b91af;">R1</span>&gt;(&nbsp;selectLeft(l)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onRight:&nbsp;r&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Right</span>&lt;<span style="color:#2b91af;">L1</span>,&nbsp;<span style="color:#2b91af;">R1</span>&gt;(selectRight(r))); }</pre> </p> <p> Given that the input <code>source</code> is an <code>IEither&lt;L, R&gt;</code> object, there's isn't much you can do. That interface only defines a single member, <code>Match</code>, so that's the only method you can call. When you do that, you have to supply the two arguments <code>onLeft</code> and <code>onRight</code>. </p> <p> The <code>Match</code> method is defined like this: </p> <p> <pre><span style="color:#2b91af;">T</span>&nbsp;Match&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;onLeft,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">R</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;onRight)</pre> </p> <p> Given the desired return type of <code>SelectBoth</code>, you know that <code>T</code> should be <code>IEither&lt;L1, R1&gt;</code>. This means, then, that for <code>onLeft</code>, you must supply a function of the type <code>Func&lt;L, IEither&lt;L1, R1&gt;&gt;</code>. Since a functor is a structure-preserving map, you should translate a <em>left</em> case to a <em>left</em> case, and a <em>right</em> case to a <em>right</em> case. This implies that the concrete return type that matches <code>IEither&lt;L1, R1&gt;</code> for the <code>onLeft</code> argument is <code>Left&lt;L1, R1&gt;</code>. </p> <p> When you write the function with the type <code>Func&lt;L, IEither&lt;L1, R1&gt;&gt;</code> as a lambda expression, the input argument <code>l</code> has the type <code>L</code>. In order to create a <code>new Left&lt;L1, R1&gt;</code>, however, you need an <code>L1</code> object. How do you produce an <code>L1</code> object from an <code>L</code> object? You call <code>selectLeft</code> with <code>l</code>, because <code>selectLeft</code> is a function of the type <code>Func&lt;L, L1&gt;</code>. </p> <p> You can apply the same line of reasoning to the <code>onRight</code> argument. Write a lambda expression that takes an <code>R</code> object <code>r</code> as input, call <code>selectRight</code> to turn that into an <code>R1</code> object, and return it wrapped in a <code>new Right&lt;L1, R1&gt;</code> object. </p> <p> This works as expected: </p> <p> <pre>&gt; <span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Left</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;(<span style="color:#a31515;">&quot;foo&quot;</span>).SelectBoth(<span style="color:blue;">string</span>.IsNullOrWhiteSpace,&nbsp;i&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(i)) Left&lt;bool, DateTime&gt;(false) &gt; <span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Right</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;(1337).SelectBoth(<span style="color:blue;">string</span>.IsNullOrWhiteSpace,&nbsp;i&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(i)) Right&lt;bool, DateTime&gt;([01.01.0001 00:00:00])</pre> </p> <p> Notice that both of the above statements evaluated in <em>C# Interactive</em> use the same projections as input to <code>SelectBoth</code>. Clearly, though, because the inputs are first a <code>Left</code> value, and secondly a <code>Right</code> value, the outputs differ. </p> <h3 id="10a641d7ad0b44158f74665fd87feb5e"> Mapping the left side <a href="#10a641d7ad0b44158f74665fd87feb5e" title="permalink">#</a> </h3> <p> When you have <code>SelectBoth</code>, you can trivially implement the translations for each dimension in isolation. In the previous article, I called these methods <code>SelectFirst</code> and <code>SelectSecond</code>. In this article, I've chosen to instead name them <code>SelectLeft</code> and <code>SelectRight</code>, but they still corresponds to Haskell's <code>first</code> and <code>second</code> <code>Bifunctor</code> functions. </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IEither</span>&lt;<span style="color:#2b91af;">L1</span>,&nbsp;<span style="color:#2b91af;">R</span>&gt;&nbsp;SelectLeft&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">L1</span>,&nbsp;<span style="color:#2b91af;">R</span>&gt;(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IEither</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">R</span>&gt;&nbsp;source,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">L1</span>&gt;&nbsp;selector) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;source.SelectBoth(selector,&nbsp;r&nbsp;=&gt;&nbsp;r); }</pre> </p> <p> The method body is literally a one-liner. Just call <code>SelectBoth</code> with <code>selector</code> as the projection for the left side, and the identity function as the projection for the right side. This ensures that if the actual value is a <code>Right&lt;L, R&gt;</code> object, nothing's going to happen. Only if the input is a <code>Left&lt;L, R&gt;</code> object will the projection run: </p> <p> <pre>&gt; <span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Left</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;(<span style="color:#a31515;">&quot;&quot;</span>).SelectLeft(<span style="color:blue;">string</span>.IsNullOrWhiteSpace) Left&lt;bool, int&gt;(true) &gt; <span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Left</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;(<span style="color:#a31515;">&quot;bar&quot;</span>).SelectLeft(<span style="color:blue;">string</span>.IsNullOrWhiteSpace) Left&lt;bool, int&gt;(false) &gt; <span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Right</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;(42).SelectLeft(<span style="color:blue;">string</span>.IsNullOrWhiteSpace) Right&lt;bool, int&gt;(42)</pre> </p> <p> In the above <em>C# Interactive</em> session, you can see how projecting three different objects using <code>string.IsNullOrWhiteSpace</code> works. When the <code>Left</code> object indeed does contain an empty string, the result is a <code>Left</code> value containing <code>true</code>. When the object contains <code>"bar"</code>, however, it contains <code>false</code>. Furthermore, when the object is a <code>Right</code> value, the mapping has no effect. </p> <h3 id="b9027a241d6444d5b869a8a1b92659c5"> Mapping the right side <a href="#b9027a241d6444d5b869a8a1b92659c5" title="permalink">#</a> </h3> <p> Similar to <code>SelectLeft</code>, you can also trivially implement <code>SelectRight</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IEither</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">R1</span>&gt;&nbsp;SelectRight&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">R</span>,&nbsp;<span style="color:#2b91af;">R1</span>&gt;(<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IEither</span>&lt;<span style="color:#2b91af;">L</span>,&nbsp;<span style="color:#2b91af;">R</span>&gt;&nbsp;source,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">R</span>,&nbsp;<span style="color:#2b91af;">R1</span>&gt;&nbsp;selector) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;source.SelectBoth(l&nbsp;=&gt;&nbsp;l,&nbsp;selector); }</pre> </p> <p> This is another one-liner calling <code>SelectBoth</code>, with the difference that the identity function <code>l =&gt; l</code> is passed as the first argument, instead of as the last. This ensures that only <code>Right</code> values are mapped: </p> <p> <pre>&gt; <span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Left</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;(<span style="color:#a31515;">&quot;baz&quot;</span>).SelectRight(i&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(i)) Left&lt;string, DateTime&gt;("baz") &gt; <span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Right</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;(1_234_567_890).SelectRight(i&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(i)) Right&lt;string, DateTime&gt;([01.01.0001 00:02:03])</pre> </p> <p> In the above examples, <code>Right</code> integers are projected into <code>DateTime</code> values, whereas <code>Left</code> strings stay strings. </p> <h3 id="a80a8d7ebbce46b4914749f46cd99e89"> Identity laws <a href="#a80a8d7ebbce46b4914749f46cd99e89" title="permalink">#</a> </h3> <p> Either obeys all the bifunctor laws. While it's formal work to prove that this is the case, you can get an intuition for it via examples. Often, I use a property-based testing library like <a href="https://fscheck.github.io/FsCheck">FsCheck</a> or <a href="https://github.com/hedgehogqa/fsharp-hedgehog">Hedgehog</a> to demonstrate (not prove) that laws hold, but in this article, I'll keep it simple and only cover each law with a parametrised test. </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Id&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">T</span>&nbsp;x)&nbsp;=&gt;&nbsp;x; <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:blue;">object</span>[]&gt;&nbsp;BifunctorLawsData { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">get</span> &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">yield</span>&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:blue;">new</span>&nbsp;&nbsp;<span style="color:#2b91af;">Left</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;(<span style="color:#a31515;">&quot;foo&quot;</span>)&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">yield</span>&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:blue;">new</span>&nbsp;&nbsp;<span style="color:#2b91af;">Left</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;(<span style="color:#a31515;">&quot;bar&quot;</span>)&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">yield</span>&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:blue;">new</span>&nbsp;&nbsp;<span style="color:#2b91af;">Left</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;(<span style="color:#a31515;">&quot;baz&quot;</span>)&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">yield</span>&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Right</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;(&nbsp;&nbsp;&nbsp;42)&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">yield</span>&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Right</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;(&nbsp;1337)&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">yield</span>&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Right</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;(&nbsp;&nbsp;&nbsp;&nbsp;0)&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;} } [<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">MemberData</span>(<span style="color:blue;">nameof</span>(BifunctorLawsData))] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;SelectLeftObeysFirstFunctorLaw(<span style="color:#2b91af;">IEither</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;e) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(e,&nbsp;e.SelectLeft(Id)); }</pre> </p> <p> This test uses <a href="https://xunit.github.io">xUnit.net</a>'s <code>[Theory]</code> feature to supply a small set of example input values. The input values are defined by the <code>BifunctorLawsData</code> property, since I'll reuse the same values for all the bifunctor law demonstration tests. </p> <p> The tests also use the identity function implemented as a <code>private</code> function called <code>Id</code>, since C# doesn't come equipped with such a function in the Base Class Library. </p> <p> For all the <code>IEither&lt;string, int&gt;</code> objects <code>e</code>, the test simply verifies that the original Either <code>e</code> is equal to the Either projected over the first axis with the <code>Id</code> function. </p> <p> Likewise, the first functor law applies when translating over the second dimension: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">MemberData</span>(<span style="color:blue;">nameof</span>(BifunctorLawsData))] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;SelectRightObeysFirstFunctorLaw(<span style="color:#2b91af;">IEither</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;e) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(e,&nbsp;e.SelectRight(Id)); }</pre> </p> <p> This is the same test as the previous test, with the only exception that it calls <code>SelectRight</code> instead of <code>SelectLeft</code>. </p> <p> Both <code>SelectLeft</code> and <code>SelectRight</code> are implemented by <code>SelectBoth</code>, so the real test is whether this method obeys the identity law: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">MemberData</span>(<span style="color:blue;">nameof</span>(BifunctorLawsData))] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;SelectBothObeysIdentityLaw(<span style="color:#2b91af;">IEither</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;e) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(e,&nbsp;e.SelectBoth(Id,&nbsp;Id)); }</pre> </p> <p> Projecting over both dimensions with the identity function does, indeed, return an object equal to the input object. </p> <h3 id="5ee147f408d64bc78171ade1f968b9aa"> Consistency law <a href="#5ee147f408d64bc78171ade1f968b9aa" title="permalink">#</a> </h3> <p> In general, it shouldn't matter whether you map with <code>SelectBoth</code> or a combination of <code>SelectLeft</code> and <code>SelectRight</code>: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">MemberData</span>(<span style="color:blue;">nameof</span>(BifunctorLawsData))] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;ConsistencyLawHolds(<span style="color:#2b91af;">IEither</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;e) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">bool</span>&nbsp;f(<span style="color:blue;">string</span>&nbsp;s)&nbsp;=&gt;&nbsp;<span style="color:blue;">string</span>.IsNullOrWhiteSpace(s); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTime</span>&nbsp;g(<span style="color:blue;">int</span>&nbsp;i)&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(i); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(e.SelectBoth(f,&nbsp;g),&nbsp;e.SelectRight(g).SelectLeft(f)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.SelectLeft(f).SelectRight(g), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.SelectRight(g).SelectLeft(f)); }</pre> </p> <p> This example creates two local functions <code>f</code> and <code>g</code>. The first function, <code>f</code>, just delegates to <code>string.IsNullOrWhiteSpace</code>, although I want to stress that this is just an example. The law should hold for any two (<a href="https://en.wikipedia.org/wiki/Pure_function">pure</a>) functions. The second function, <code>g</code>, creates a new <code>DateTime</code> object from an integer, using one of the <code>DateTime</code> constructor overloads. </p> <p> The test then verifies that you get the same result from calling <code>SelectBoth</code> as when you call <code>SelectLeft</code> followed by <code>SelectRight</code>, or the other way around. </p> <h3 id="509bf515dac14e0e95346ee604ba26af"> Composition laws <a href="#509bf515dac14e0e95346ee604ba26af" title="permalink">#</a> </h3> <p> The composition laws insist that you can compose functions, or translations, and that again, the choice to do one or the other doesn't matter. Along each of the axes, it's just the second functor law applied. This parametrised test demonstrates that the law holds for <code>SelectLeft</code>: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">MemberData</span>(<span style="color:blue;">nameof</span>(BifunctorLawsData))] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;SecondFunctorLawHoldsForSelectLeft(<span style="color:#2b91af;">IEither</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;e) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">bool</span>&nbsp;f(<span style="color:blue;">int</span>&nbsp;x)&nbsp;=&gt;&nbsp;x&nbsp;%&nbsp;2&nbsp;==&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;g(<span style="color:blue;">string</span>&nbsp;s)&nbsp;=&gt;&nbsp;s.Length; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(e.SelectLeft(x&nbsp;=&gt;&nbsp;f(g(x))),&nbsp;e.SelectLeft(g).SelectLeft(f)); }</pre> </p> <p> Here, <code>f</code> is the <em>even</em> function, whereas <code>g</code> is a local function that returns the length of a string. The second functor law states that mapping <code>f(g(x))</code> in a single step is equivalent to first mapping over <code>g</code> and then map the result of that using <code>f</code>. </p> <p> The same law applies if you fix the first dimension and translate over the second: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">MemberData</span>(<span style="color:blue;">nameof</span>(BifunctorLawsData))] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;SecondFunctorLawHoldsForSelectRight(<span style="color:#2b91af;">IEither</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;e) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">char</span>&nbsp;f(<span style="color:blue;">bool</span>&nbsp;b)&nbsp;=&gt;&nbsp;b&nbsp;?&nbsp;<span style="color:#a31515;">&#39;T&#39;</span>&nbsp;:&nbsp;<span style="color:#a31515;">&#39;F&#39;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">bool</span>&nbsp;g(<span style="color:blue;">int</span>&nbsp;i)&nbsp;=&gt;&nbsp;i&nbsp;%&nbsp;2&nbsp;==&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(e.SelectRight(x&nbsp;=&gt;&nbsp;f(g(x))),&nbsp;e.SelectRight(g).SelectRight(f)); }</pre> </p> <p> Here, <code>f</code> is a local function that returns <code>'T'</code> for <code>true</code> and <code>'F'</code> for <code>false</code>, and <code>g</code> is a local function that, as you've seen before, determines whether a number is even. Again, the test demonstrates that the output is the same whether you map over an intermediary step, or whether you map using only a single step. </p> <p> This generalises to the composition law for <code>SelectBoth</code>: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>,&nbsp;<span style="color:#2b91af;">MemberData</span>(<span style="color:blue;">nameof</span>(BifunctorLawsData))] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;SelectBothCompositionLawHolds(<span style="color:#2b91af;">IEither</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;e) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">bool</span>&nbsp;f(<span style="color:blue;">int</span>&nbsp;x)&nbsp;=&gt;&nbsp;x&nbsp;%&nbsp;2&nbsp;==&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;g(<span style="color:blue;">string</span>&nbsp;s)&nbsp;=&gt;&nbsp;s.Length; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">char</span>&nbsp;h(<span style="color:blue;">bool</span>&nbsp;b)&nbsp;=&gt;&nbsp;b&nbsp;?&nbsp;<span style="color:#a31515;">&#39;T&#39;</span>&nbsp;:&nbsp;<span style="color:#a31515;">&#39;F&#39;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">bool</span>&nbsp;i(<span style="color:blue;">int</span>&nbsp;x)&nbsp;=&gt;&nbsp;x&nbsp;%&nbsp;2&nbsp;==&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.SelectBoth(x&nbsp;=&gt;&nbsp;f(g(x)),&nbsp;y&nbsp;=&gt;&nbsp;h(i(y))), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.SelectBoth(g,&nbsp;i).SelectBoth(f,&nbsp;h)); }</pre> </p> <p> Again, whether you translate in one or two steps shouldn't affect the outcome. </p> <p> As all of these tests demonstrate, the bifunctor laws hold for Either. The tests only showcase six examples for either a string or an integer, but I hope it gives you an intuition how any Either object is a bifunctor. After all, the <code>SelectLeft</code>, <code>SelectRight</code>, and <code>SelectBoth</code> methods are all generic, and they behave the same for all generic type arguments. </p> <h3 id="7b8babee4af64eeaa93fe4562ed721bd"> Summary <a href="#7b8babee4af64eeaa93fe4562ed721bd" title="permalink">#</a> </h3> <p> Either objects are bifunctors. You can translate the first and second dimension of an Either object independently of each other, and the bifunctor laws hold for any pure translation, no matter how you compose the projections. </p> <p> As always, there can be performance differences between the various compositions, but the outputs will be the same regardless of composition. </p> <p> A functor, and by extension, a bifunctor, is a structure-preserving map. This means that any projection preserves the structure of the underlying container. For Either objects, it means that <em>left</em> objects remain <em>left</em> objects, and <em>right</em> objects remain <em>right</em> objects, even if the contained values change. Either is characterised by containing exactly one value, but it can be either a <em>left</em> value or a <em>right</em> value. No matter how you translate it, it still contains only a single value - <em>left</em> or <em>right</em>. </p> <p> The other common bifunctor, <em>pair</em>, is complementary. Not only does it also have two dimensions, but a pair always contains both values at once. </p> <p> <strong>Next:</strong> <a href="/2018/01/08/software-design-isomorphisms">Software design isomorphisms</a> </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="79f5d74763e34cb0997a7a79df1e05f0"> <div class="comment-author">Tyson Williams</div> <div class="comment-content"> <p> I feel like the concepts of functor and bifunctor were used somewhat interchangeably in this post. Can we clarify this relationship? </p> <p> To help us with this, consider <a href="https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)">type variance</a>. The generic type <span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">A</span>&gt; is covariant, but more specifically, it is covariant on <span style="color:#2b91af;">A</span>. That additional prepositional phrase is often omitted because it can be inferred. In contrast, the generic type <span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">A</span>, <span style="color:#2b91af;">B</span>&gt; is both covariant and contravariant but (of course) not on the same type parameter. It is covariant in <span style="color:#2b91af;">B</span> and contravariant in <span style="color:#2b91af;">A</span>. </p> <p> I feel like saying that a generic type with two type parameters is a (bi)functor also needs an additional prepositional phrase. Like, <span style="color:#2b91af;">Either</span>&lt;<span style="color:#2b91af;">L</span>, <span style="color:#2b91af;">R</span>&gt; is a bifunctor in <span style="color:#2b91af;">L</span> and <span style="color:#2b91af;">R</span>, so it is also a functor in <span style="color:#2b91af;">L</span> and a functor in <span style="color:#2b91af;">R</span>. </p> <p> Does this seem like a clearer way to talk about a specific type being both a bifunctor and a fuctor? </p> </div> <div class="comment-date">2019-05-15 11:51 UTC</div> </div> <div class="comment" id="19253916a1bd49a08b19b15898816657"> <div class="comment-author"><a href="/">Mark Seemann</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. I find no fault with what you wrote. Is it clearer? I don't know. </p> <p> One thing that's surprised me throughout <a href="/2017/10/04/from-design-patterns-to-category-theory">this endeavour</a> is exactly what does or doesn't confuse readers. This, I can't predict. </p> <p> A functor is, by its definition, assumed to be covariant. Contravariant functors also exist, but they're explicitly named <em>contravariant functors</em> to distinguish them from standard functors. </p> <p> Ultimately, co- or contravariance of generic type arguments is (I think) insufficient to identify a type as a functor. Whether or not something is a functor is determined by whether or not it obeys the functor laws. Can we guarantee that all types with a covariant type argument will obey the functor laws? </p> </div> <div class="comment-date">2019-05-15 12:31 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Tuple bifunctor https://blog.ploeh.dk/2018/12/31/tuple-bifunctor 2018-12-31T12:13:00+00:00 Mark Seemann <div id="post"> <p> <em>A Pair (a two-tuple) forms a bifunctor. An article for object-oriented programmers.</em> </p> <p> This article is an instalment in <a href="/2018/12/24/bifunctors">an article series about bifunctors</a>. In the previous overview, you learned about the general concept of a bifunctor. In practice, there's two useful bifunctor instances: pairs (two-tuples) and <a href="/2018/06/11/church-encoded-either">Either</a>. In this article, you'll see how a pair is a bifunctor, and in the next article, you'll see how Either fits the same abstraction. </p> <h3 id="d918d0271c33406ba3047ef162212100"> Tuple as a functor <a href="#d918d0271c33406ba3047ef162212100" title="permalink">#</a> </h3> <p> You can treat a normal pair (two-tuple) as a <a href="/2018/03/22/functors">functor</a> by mapping one of the elements, while keeping the other generic type fixed. In <a href="https://www.haskell.org">Haskell</a>, when you have types with multiple type arguments, you often 'fix' the types from the left, leaving the right-most type free to vary. Doing this for a pair, which in C# has the type <code>Tuple&lt;T, U&gt;</code>, this means that tuples are functors if we keep <code>T</code> fixed and enable translation of the second element from <code>U1</code> to <code>U2</code>. </p> <p> This is easy to implement with a standard <code>Select</code> extension method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">U2</span>&gt;&nbsp;Select&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">U1</span>,&nbsp;<span style="color:#2b91af;">U2</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">U1</span>&gt;&nbsp;source, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">U1</span>,&nbsp;<span style="color:#2b91af;">U2</span>&gt;&nbsp;selector) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">Tuple</span>.Create(source.Item1,&nbsp;selector(source.Item2)); }</pre> </p> <p> You simply return a new tuple by carrying <code>source.Item1</code> over without modification, while on the other hand calling <code>selector</code> with <code>source.Item2</code>. Here's a simple example, which also highlights that C# understands functors: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;t&nbsp;=&nbsp;<span style="color:#2b91af;">Tuple</span>.Create(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;42); <span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:blue;">from</span>&nbsp;i&nbsp;<span style="color:blue;">in</span>&nbsp;t &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;i&nbsp;%&nbsp;2&nbsp;==&nbsp;0;</pre> </p> <p> Here, <code>actual</code> is a <code>Tuple&lt;string, bool&gt;</code> with the values <code>"foo"</code> and <code>true</code>. Inside the query expression, <code>i</code> is an <code>int</code>, and the <code>select</code> expression returns a <code>bool</code> value indicating whether the number is even or odd. Notice that the <code>string</code> in the first element disappears inside the query expression. It's still there, but the code inside the query expression can't see <code>"foo"</code>. </p> <h3 id="589f054d8dab47a4bb308951ee76f96b"> Mapping the first element <a href="#589f054d8dab47a4bb308951ee76f96b" title="permalink">#</a> </h3> <p> There's no technical reason why the mapping has to be over the second element; it's just how Haskell does it by convention. There are other, more philosophical reasons for that convention, but in the end, they boil down to the ultimately arbitrary cultural choice of reading from left to right (in Western scripts). </p> <p> You can translate the first element of a tuple as easily: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">U</span>&gt;&nbsp;SelectFirst&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">U</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">U</span>&gt;&nbsp;source, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>&gt;&nbsp;selector) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">Tuple</span>.Create(selector(source.Item1),&nbsp;source.Item2); }</pre> </p> <p> While, technically, you <em>can</em> call this method <code>Select</code>, this can confuse the C# compiler's overload resolution system - at least if you have a tuple of two identical types (e.g. <code>Tuple&lt;int, int&gt;</code> or <code>Tuple&lt;string, string&gt;</code>). In order to avoid that sort of confusion, I decided to give the method another name, and in keeping with how C# LINQ methods tend to get names, I thought <code>SelectFirst</code> sounded reasonable. </p> <p> In Haskell, this function is called <code>first</code>, and is part of the <code>Bifunctor</code> type class: </p> <p> <pre>Prelude Data.Bifunctor&gt; first (even . length) ("foo", 42) (False,42)</pre> </p> <p> In C#, you can perform the same translation using the above <code>SelectFirst</code> extension method: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;t&nbsp;=&nbsp;<span style="color:#2b91af;">Tuple</span>.Create(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;42); <span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;t.SelectFirst(s&nbsp;=&gt;&nbsp;s.Length&nbsp;%&nbsp;2&nbsp;==&nbsp;0);</pre> </p> <p> This also returns a <code>Tuple&lt;bool, int&gt;</code> containing the values <code>false</code> and <code>42</code>. Notice that in this case, the first element <code>"foo"</code> is translated into <code>false</code> (because its length is odd), while the second element <code>42</code> carries over unchanged. </p> <h3 id="7da2d817bebb40cbb2221212158324f8"> Mapping the second element <a href="#7da2d817bebb40cbb2221212158324f8" title="permalink">#</a> </h3> <p> You've already seen how the above <code>Select</code> method maps over the second element of a pair. This means that you can already map over both dimensions of the bifunctor, but perhaps, for consistency's sake, you'd also like to add an explicit <code>SelectSecond</code> method. This is now trivial to implement, since it can delegate its work to <code>Select</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">U2</span>&gt;&nbsp;SelectSecond&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">U1</span>,&nbsp;<span style="color:#2b91af;">U2</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">U1</span>&gt;&nbsp;source, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">U1</span>,&nbsp;<span style="color:#2b91af;">U2</span>&gt;&nbsp;selector) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;source.Select(selector); }</pre> </p> <p> There's no separate implementation; the only thing this method does is to delegate work to the <code>Select</code> method. It's literally the <code>Select</code> method, just with another name. </p> <p> Clearly, you could also have done it the other way around: implement <code>SelectSecond</code> and then call it from <code>Select</code>. </p> <p> The <code>SelectSecond</code> method works as you'd expect: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;t&nbsp;=&nbsp;<span style="color:#2b91af;">Tuple</span>.Create(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;1337); <span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;t.SelectSecond(i&nbsp;=&gt;&nbsp;i&nbsp;%&nbsp;2&nbsp;==&nbsp;0);</pre> </p> <p> Again, <code>actual</code> is a tuple containing the values <code>"foo"</code> and <code>false</code>, because <code>1337</code> isn't even. </p> <p> This fits with the Haskell implementation, where <code>SelectSecond</code> is called <code>second</code>: </p> <p> <pre>Prelude Data.Bifunctor&gt; second even ("foo", 1337) ("foo",False)</pre> </p> <p> The result is still a pair where the first element is <code>"foo"</code> and the second element <code>False</code>, exactly like in the C# example. </p> <h3 id="17ce6b48946b4877924ddc5e3f841283"> Mapping both elements <a href="#17ce6b48946b4877924ddc5e3f841283" title="permalink">#</a> </h3> <p> With <code>SelectFirst</code> and <code>SelectSecond</code>, you can trivially implement <code>SelectBoth</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">U2</span>&gt;&nbsp;SelectBoth&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">U1</span>,&nbsp;<span style="color:#2b91af;">U2</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">U1</span>&gt;&nbsp;source, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>&gt;&nbsp;selector1, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">U1</span>,&nbsp;<span style="color:#2b91af;">U2</span>&gt;&nbsp;selector2) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;source.SelectFirst(selector1).SelectSecond(selector2); }</pre> </p> <p> This method takes two translations, <code>selector1</code> and <code>selector2</code>, and first uses <code>SelectFirst</code> to project along the first axis, and then <code>SelectSecond</code> to map the second dimension. </p> <p> This implementation creates an intermediary pair that callers never see, so this could theoretically be inefficient. In this article, however, I want to show you that it's possible to implement <code>SelectBoth</code> based on <code>SelectFirst</code> and <code>SelectSecond</code>. In the next article, you'll see how to do it the other way around. </p> <p> Using <code>SelectBoth</code> is easy: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;t&nbsp;=&nbsp;<span style="color:#2b91af;">Tuple</span>.Create(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;42); <span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;t.SelectBoth(s&nbsp;=&gt;&nbsp;s.First(),&nbsp;i&nbsp;=&gt;&nbsp;i&nbsp;%&nbsp;2&nbsp;==&nbsp;0);</pre> </p> <p> This translation returns a pair where the first element is <code>'f'</code> and the second element is <code>true</code>. This is because the first lambda expression <code>s =&gt; s.First()</code> returns the first element of the input string <code>"foo"</code>, whereas the second lambda expression <code>i =&gt; i % 2 == 0</code> determines that <code>42</code> is even. </p> <p> In Haskell, <code>SelectBoth</code> is called <code>bimap</code>: </p> <p> <pre>Prelude Data.Bifunctor&gt; bimap head even ("foo", 42) ('f',True)</pre> </p> <p> The return value is consistent with the C# example, since the input is also equivalent. </p> <h3 id="93cde34382844098885a14a06aa03948"> Identity laws <a href="#93cde34382844098885a14a06aa03948" title="permalink">#</a> </h3> <p> Pairs obey all the bifunctor laws. While it's formal work to prove that this is the case, you can get an intuition for it via examples. Often, I use a property-based testing library like <a href="https://fscheck.github.io/FsCheck">FsCheck</a> or <a href="https://github.com/hedgehogqa/fsharp-hedgehog">Hedgehog</a> to demonstrate (not prove) that laws hold, but in this article, I'll keep it simple and only cover each law with a parametrised test. </p> <p> <pre><span style="color:blue;">private</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Id&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:#2b91af;">T</span>&nbsp;x)&nbsp;=&gt;&nbsp;x; [<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;42)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;bar&quot;</span>,&nbsp;1337)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;foobar&quot;</span>,&nbsp;0)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;ploeh&quot;</span>,&nbsp;7)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;fnaah&quot;</span>,&nbsp;-6)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;SelectFirstObeysFirstFunctorLaw(<span style="color:blue;">string</span>&nbsp;first,&nbsp;<span style="color:blue;">int</span>&nbsp;second) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;t&nbsp;=&nbsp;<span style="color:#2b91af;">Tuple</span>.Create(first,&nbsp;second); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(t,&nbsp;t.SelectFirst(Id)); }</pre> </p> <p> This test uses <a href="https://xunit.github.io">xUnit.net</a>'s <code>[Theory]</code> feature to supply a small set of example input values. It defines the identity function as a <code>private</code> function called <code>Id</code>, since C# doesn't come equipped with such a function in the Base Class Library. </p> <p> The test simply creates a tuple with the input values and verifies that the original tuple <code>t</code> is equal to the tuple projected over the first axis with the <code>Id</code> function. </p> <p> Likewise, the first functor law applies when translating over the second dimension: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;42)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;bar&quot;</span>,&nbsp;1337)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;foobar&quot;</span>,&nbsp;0)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;ploeh&quot;</span>,&nbsp;7)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;fnaah&quot;</span>,&nbsp;-6)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;SelectSecondObeysFirstFunctorLaw(<span style="color:blue;">string</span>&nbsp;first,&nbsp;<span style="color:blue;">int</span>&nbsp;second) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;t&nbsp;=&nbsp;<span style="color:#2b91af;">Tuple</span>.Create(first,&nbsp;second); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(t,&nbsp;t.SelectSecond(Id)); }</pre> </p> <p> This is the same test as the previous test, with the only exception that it calls <code>SelectSecond</code> instead of <code>SelectFirst</code>. </p> <p> Since <code>SelectBoth</code> in this example is implemented by composing <code>SelectFirst</code> and <code>SelectSecond</code>, you should expect it to obey the general identity law for bifunctors. It does, but it's always nice to see it with your own eyes: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;42)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;bar&quot;</span>,&nbsp;1337)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;foobar&quot;</span>,&nbsp;0)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;ploeh&quot;</span>,&nbsp;7)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;fnaah&quot;</span>,&nbsp;-6)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;SelectBothObeysIdentityLaw(<span style="color:blue;">string</span>&nbsp;first,&nbsp;<span style="color:blue;">int</span>&nbsp;second) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;t&nbsp;=&nbsp;<span style="color:#2b91af;">Tuple</span>.Create(first,&nbsp;second); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(t,&nbsp;t.SelectBoth(Id,&nbsp;Id)); }</pre> </p> <p> Here you can see that projecting over both dimensions with the identity function returns the original tuple. </p> <h3 id="6bd839540b9646db9acfe25b0227b241"> Consistency law <a href="#6bd839540b9646db9acfe25b0227b241" title="permalink">#</a> </h3> <p> In general, it shouldn't matter whether you map with <code>SelectBoth</code> or a combination of <code>SelectFirst</code> and <code>SelectSecond</code>: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;42)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;bar&quot;</span>,&nbsp;1337)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;foobar&quot;</span>,&nbsp;0)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;ploeh&quot;</span>,&nbsp;7)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;ConsistencyLawHolds(<span style="color:blue;">string</span>&nbsp;first,&nbsp;<span style="color:blue;">int</span>&nbsp;second) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">bool</span>&gt;&nbsp;f&nbsp;=&nbsp;<span style="color:blue;">string</span>.IsNullOrWhiteSpace; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:#2b91af;">DateTime</span>&gt;&nbsp;g&nbsp;=&nbsp;i&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTime</span>(i); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;t&nbsp;=&nbsp;<span style="color:#2b91af;">Tuple</span>.Create(first,&nbsp;second); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.SelectBoth(f,&nbsp;g),&nbsp;t.SelectSecond(g).SelectFirst(f)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.SelectFirst(f).SelectSecond(g), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.SelectSecond(g).SelectFirst(f)); }</pre> </p> <p> This example creates two functions <code>f</code> and <code>g</code>. The first function, <code>f</code>, is just an alias for <code>string.IsNullOrWhiteSpace</code>, although I want to stress that it's just an example. The law should hold for any two (<a href="https://en.wikipedia.org/wiki/Pure_function">pure</a>) functions. The second function, <code>g</code>, creates a new <code>DateTime</code> object from an integer, using one of the <code>DateTime</code> constructor overloads. </p> <p> The test then verifies that you get the same result from calling <code>SelectBoth</code> as when you call <code>SelectFirst</code> followed by <code>SelectSecond</code>, or the other way around. </p> <h3 id="866a68ce5f3e4513882409498e743bb3"> Composition laws <a href="#866a68ce5f3e4513882409498e743bb3" title="permalink">#</a> </h3> <p> The composition laws insist that you can compose functions, or translations, and that again, the choice to do one or the other doesn't matter. Along each of the axes, it's just the second functor law applied. You've already seen that <code>SelectSecond</code> is nothing but an alias for <code>Select</code>, so surely, the second functor law must hold for <code>SelectSecond</code> as well. This parametrised test demonstrates that it does: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;42)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;bar&quot;</span>,&nbsp;1337)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;foobar&quot;</span>,&nbsp;0)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;ploeh&quot;</span>,&nbsp;7)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;fnaah&quot;</span>,&nbsp;-6)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;SecondFunctorLawHoldsForSelectSecond(<span style="color:blue;">string</span>&nbsp;first,&nbsp;<span style="color:blue;">int</span>&nbsp;second) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">bool</span>,&nbsp;<span style="color:blue;">char</span>&gt;&nbsp;f&nbsp;=&nbsp;b&nbsp;=&gt;&nbsp;b&nbsp;?&nbsp;<span style="color:#a31515;">&#39;T&#39;</span>&nbsp;:&nbsp;<span style="color:#a31515;">&#39;F&#39;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">bool</span>&gt;&nbsp;g&nbsp;=&nbsp;i&nbsp;=&gt;&nbsp;i&nbsp;%&nbsp;2&nbsp;==&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;t&nbsp;=&nbsp;<span style="color:#2b91af;">Tuple</span>.Create(first,&nbsp;second); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.SelectSecond(x&nbsp;=&gt;&nbsp;f(g(x))), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.SelectSecond(g).SelectSecond(f)); }</pre> </p> <p> Here, <code>f</code> is a function that returns <code>'T'</code> for <code>true</code> and <code>'F'</code> for <code>false</code>, and <code>g</code> is a function that, as you've seen before, determines whether a number is even. The second functor law states that mapping <code>f(g(x))</code> in a single step is equivalent to first mapping over <code>g</code> and then map the result of that using <code>f</code>. </p> <p> The same law applies if you fix the second dimension and translate over the first: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;42)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;bar&quot;</span>,&nbsp;1337)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;foobar&quot;</span>,&nbsp;0)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;ploeh&quot;</span>,&nbsp;7)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;fnaah&quot;</span>,&nbsp;-6)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;SecondFunctorLawHoldsForSelectFirst(<span style="color:blue;">string</span>&nbsp;first,&nbsp;<span style="color:blue;">int</span>&nbsp;second) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">bool</span>&gt;&nbsp;f&nbsp;=&nbsp;x&nbsp;=&gt;&nbsp;x&nbsp;%&nbsp;2&nbsp;==&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;g&nbsp;=&nbsp;s&nbsp;=&gt;&nbsp;s.Length; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;t&nbsp;=&nbsp;<span style="color:#2b91af;">Tuple</span>.Create(first,&nbsp;second); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.SelectFirst(x&nbsp;=&gt;&nbsp;f(g(x))), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.SelectFirst(g).SelectFirst(f)); }</pre> </p> <p> Here, <code>f</code> is the <em>even</em> function, whereas <code>g</code> is a function that returns the length of a string. Again, the test demonstrates that the output is the same whether you map over an intermediary step, or whether you map using only a single step. </p> <p> This generalises to the composition law for <code>SelectBoth</code>: </p> <p> <pre>[<span style="color:#2b91af;">Theory</span>] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;42)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;bar&quot;</span>,&nbsp;1337)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;foobar&quot;</span>,&nbsp;0)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;ploeh&quot;</span>,&nbsp;7)] [<span style="color:#2b91af;">InlineData</span>(<span style="color:#a31515;">&quot;fnaah&quot;</span>,&nbsp;-6)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;SelectBothCompositionLawHolds(<span style="color:blue;">string</span>&nbsp;first,&nbsp;<span style="color:blue;">int</span>&nbsp;second) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">bool</span>&gt;&nbsp;f&nbsp;=&nbsp;x&nbsp;=&gt;&nbsp;x&nbsp;%&nbsp;2&nbsp;==&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;g&nbsp;=&nbsp;s&nbsp;=&gt;&nbsp;s.Length; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">bool</span>,&nbsp;<span style="color:blue;">char</span>&gt;&nbsp;h&nbsp;=&nbsp;b&nbsp;=&gt;&nbsp;b&nbsp;?&nbsp;<span style="color:#a31515;">&#39;T&#39;</span>&nbsp;:&nbsp;<span style="color:#a31515;">&#39;F&#39;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">bool</span>&gt;&nbsp;i&nbsp;=&nbsp;x&nbsp;=&gt;&nbsp;x&nbsp;%&nbsp;2&nbsp;==&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;t&nbsp;=&nbsp;<span style="color:#2b91af;">Tuple</span>.Create(first,&nbsp;second); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.SelectBoth(x&nbsp;=&gt;&nbsp;f(g(x)),&nbsp;y&nbsp;=&gt;&nbsp;h(i(y))), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.SelectBoth(g,&nbsp;i).SelectBoth(f,&nbsp;h)); }</pre> </p> <p> Again, whether you translate in one or two steps shouldn't affect the outcome. </p> <p> As all of these tests demonstrate, the bifunctor laws hold for pairs. The tests only showcase 4-5 examples for a pair of string and integer, but I hope it gives you an intuition how any pair is a bifunctor. After all, the <code>SelectFirst</code>, <code>SelectSecond</code>, and <code>SelectBoth</code> methods are all generic, and they behave the same for all generic type arguments. </p> <h3 id="dfd4aa6edc66462c9d02d46ce44b1bda"> Summary <a href="#dfd4aa6edc66462c9d02d46ce44b1bda" title="permalink">#</a> </h3> <p> Pairs (two-tuples) are bifunctors. You can translate the first and second element of a pair independently of each other, and the bifunctor laws hold for any pure translation, no matter how you compose the projections. </p> <p> As always, there can be performance differences between the various compositions, but the outputs will be the same regardless of composition. </p> <p> A functor, and by extension, a bifunctor, is a structure-preserving map. This means that any projection preserves the structure of the underlying container. In practice that means that for pairs, no matter how you translate a pair, it remains a pair. A pair is characterised by containing two values at once, and no matter how you translate it, it'll still contain two values. </p> <p> The other common bifunctor, Either, is complementary. While it has two dimensions, it only contains one value, which is of either the one type or the other. It's still a bifunctor, though, because mappings preserve the structure of Either, too. </p> <p> <strong>Next:</strong> <a href="/2019/01/07/either-bifunctor">Either bifunctor</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Bifunctors https://blog.ploeh.dk/2018/12/24/bifunctors 2018-12-24T14:40:00+00:00 Mark Seemann <div id="post"> <p> <em>Bifunctors are like functors, only they vary in two dimensions instead of one. An article for object-oriented programmers.</em> </p> <p> This article is a continuation of <a href="/2018/03/22/functors">the article series about functors</a> and about <a href="/2018/10/01/applicative-functors">applicative functors</a>. In this article, you'll learn about a generalisation called a <em>bifunctor</em>. The prefix <em>bi</em> typically indicates that there's <em>two</em> of something, and that's also the case here. </p> <p> As you've already seen in the functor articles, a functor is a mappable <a href="https://bartoszmilewski.com/2014/01/14/functors-are-containers">container</a> of generic values, like <code>Foo&lt;T&gt;</code>, where the type of the contained value(s) can be any generic type <code>T</code>. A bifunctor is just a container with two independent generic types, like <code>Bar&lt;T, U&gt;</code>. If you can map each of the types independently of the other, you may have a bifunctor. </p> <p> The two most common bifunctors are tuples and Either. </p> <h3 id="c8062c28a0ed471a8995bee180d14aa6"> Maps <a href="#c8062c28a0ed471a8995bee180d14aa6" title="permalink">#</a> </h3> <p> A normal functor is based on a structure-preserving map of the contents within a container. You can, for example, translate an <code>IEnumerable&lt;int&gt;</code> to an <code>IEnumerable&lt;string&gt;</code>, or a <code>Maybe&lt;DateTime&gt;</code> to a <code>Maybe&lt;bool&gt;</code>. The axis of variability is the generic type argument <code>T</code>. You can translate <code>T1</code> to <code>T2</code> inside a container, but the type of the container remains the same: you can translate <code>Tree&lt;T1&gt;</code> to <code>Tree&lt;T2&gt;</code>, but it remains a <code>Tree&lt;&gt;</code>. </p> <p> <img src="/content/binary/monofunctors.png" alt="Three translations: IEnumerable, Maybe, and Tree."> </p> <p> A bifunctor involves a pair of maps, one for each generic type. You can map a <code>Bar&lt;string, int&gt;</code> to a <code>Bar&lt;bool, int&gt;</code>, or to a <code>Bar&lt;string, DateTime&gt;</code>, or even to a <code>Bar&lt;bool, DateTime&gt;</code>. Notice that the last example, mapping from <code>Bar&lt;string, int&gt;</code> to <code>Bar&lt;bool, DateTime&gt;</code> could be viewed as translating both axes simultaneously. </p> <p> <img src="/content/binary/bifunctor.png" alt="Bifunctor diagram."> </p> <p> In <a href="https://www.haskell.org">Haskell</a>, the two maps are called <code>first</code> and <code>second</code>, while the 'simultaneous' map is called <code>bimap</code>. </p> <p> The <code>first</code> translation translates the first, or left-most, value in the container. You can use it to map <code>Bar&lt;string, int&gt;</code> to a <code>Bar&lt;bool, int&gt;</code>. In C#, we could decide to call the method <code>SelectFirst</code>, or <code>SelectLeft</code>, in order to align with the C# naming convention of calling the functor morphism <code>Select</code>. </p> <p> Likewise, the <code>second</code> map translates the second, or right-most, value in the container. This is where you map <code>Bar&lt;string, int&gt;</code> to <code>Bar&lt;string, DateTime&gt;</code>. In C#, we could call the method <code>SelectSecond</code>, or <code>SelectRight</code>. </p> <p> The <code>bimap</code> function maps both values in the container in one go. This corresponds to a translation from <code>Bar&lt;string, int&gt;</code> to <code>Bar&lt;bool, DateTime&gt;</code>. In C#, we could call the method <code>SelectBoth</code>. There's no established naming conventions for bifunctors in C# that I know of, so these names are just some that I made up. </p> <p> You'll see examples of how to implement and use such functions in the next articles: <ul> <li><a href="/2018/12/31/tuple-bifunctor">Tuple bifunctor</a></li> <li><a href="/2019/01/07/either-bifunctor">Either bifunctor</a></li> </ul> Other bifunctors exist, but those two are the most common. </p> <h3 id="838097e59afd4555b989594b62082f39"> Identity laws <a href="#838097e59afd4555b989594b62082f39" title="permalink">#</a> </h3> <p> As is the case with functors, laws govern bifunctors. Some of the functor laws carry over, but are simply repeated over both axes, while other laws are generalisations of the functor laws. For example, the first functor law states that if you translate a container with the identity function, the result is the original input. This generalises to bifunctors as well: </p> <p> <pre>bimap id id ≡ id</pre> </p> <p> This just states that if you translate both axes using the <a href="/2017/11/13/endomorphism-monoid">endomorphic</a> Identity, it's equivalent to applying the Identity. </p> <p> <img src="/content/binary/bimap-id-id-bifunctor-law.png" alt="The bifunctor law for applying id to both axes simultaneously."> </p> <p> Using C# syntax, you could express the law like this: </p> <p> <pre>bf.SelectBoth(id,&nbsp;id)&nbsp;==&nbsp;bf;</pre> </p> <p> Here, <code>bf</code> is some bifunctor, and <code>id</code> is the identity function. The point is that if you translate over both axes, but actually don't perform a real translation, nothing happens. </p> <p> Likewise, if you consider a bifunctor a functor over two dimensions, the first functor law should hold for both: </p> <p> <pre>first id ≡ id second id ≡ id</pre> </p> <p> Both of those equalities only restate the first functor law for each dimension. If you map an axis with the identity function, nothing happens: </p> <p> <img src="/content/binary/first-id-second-id-bifunctor-laws.png" alt="The first functor law applied to both dimensions of a bifunctor."> </p> <p> In C#, you can express both laws like this: </p> <p> <pre>bf.SelectFirst(id)&nbsp;==&nbsp;bf; bf.SelectSecond(id)&nbsp;==&nbsp;bf;</pre> </p> <p> When calling <code>SelectFirst</code>, you translate only the first axis while you keep the second axis constant. When calling <code>SelectSecond</code> it's the other way around: you translate only the second axis while keeping the first axis constant. In both cases, though, if you use the identity function for the translation, you effectively keep the mapped dimension constant as well. Therefore, one would expect the result to be the same as the input. </p> <h3 id="57cfa8fd78084e8ca19d073d856a8c8a"> Consistency law <a href="#57cfa8fd78084e8ca19d073d856a8c8a" title="permalink">#</a> </h3> <p> As you'll see in the articles on tuple and Either bifunctors, you can derive <code>bimap</code> or <code>SelectBoth</code> from <code>first</code>/<code>SelectFirst</code> and <code>second</code>/<code>SelectSecond</code>, or the other way around. If, however, you decide to implement all three functions, they must act in a consistent manner. The name <em>Consistency law</em>, however, is entirely my own invention. If it has a more well-known name, I'm not aware of it. </p> <p> In pseudo-Haskell syntax, you can express the law like this: </p> <p> <pre>bimap f g ≡ first f . second g</pre> </p> <p> This states that mapping (using the functions <code>f</code> and <code>g</code>) simultaneously should produce the same result as mapping using an intermediary step: </p> <p> <img src="/content/binary/bifunctor-consistency-law.png" alt="The bifunctor Consistency law."> </p> <p> In C#, you could express it like this: </p> <p> <pre>bf.SelectBoth(f,&nbsp;g)&nbsp;==&nbsp;bf.SelectSecond(g).SelectFirst(f);</pre> </p> <p> You can project the input bifunctor <code>bf</code> using both <code>f</code> and <code>g</code> in a single step, or you can first translate the second dimension with <code>g</code> and then subsequently map that intermediary result along the first axis with <code>f</code>. </p> <p> The above diagram ought to commute: </p> <p> <img src="/content/binary/bifunctor-consistency-commutativity-law.png" alt="The bifunctor Consistency law."> </p> <p> It shouldn't matter whether the intermediary step is applying <code>f</code> along the first axis or <code>g</code> along the second axis. In C#, we can write it like this: </p> <p> <pre>bf.SelectFirst(f).SelectSecond(g)&nbsp;==&nbsp;bf.SelectSecond(g).SelectFirst(f);</pre> </p> <p> On the left-hand side, you first translate the bifunctor <code>bf</code> along the first axis, using <code>f</code>, and then translate that intermediary result along the second axis, using <code>g</code>. On the right-hand side, you first project <code>bf</code> along the second axis, using <code>g</code>, and then map that intermediary result over the first dimension, using <code>f</code>. </p> <p> Regardless of order of translation, the result should be the same. </p> <h3 id="a1d12a9d1afe428684b3d693a39cb5d1"> Composition laws <a href="#a1d12a9d1afe428684b3d693a39cb5d1" title="permalink">#</a> </h3> <p> Similar to how the first functor law generalises to bifunctors, the second functor law generalises as well. For (mono)functors, the second functor law states that if you have two functions over the same dimension, it shouldn't matter whether you perform a projection in one, composed step, or in two steps with an intermediary result. </p> <p> For bifunctors, you can generalise that law and state that you can project over both dimensions in one or two steps: </p> <p> <pre>bimap (f . g) (h . i) ≡ bimap f h . bimap g i</pre> </p> <p> If you have two functions, <code>f</code> and <code>g</code>, that compose, and two other functions, <code>h</code> and <code>i</code>, that also compose, you can translate in either one or two steps; the result should be the same. </p> <p> <img src="/content/binary/bimap-composition-law.png" alt="The bifunctor composition law."> </p> <p> In C#, you can express the law like this: </p> <p> <pre>bf.SelectBoth(x&nbsp;=&gt;&nbsp;f(g(x)),&nbsp;y&nbsp;=&gt;&nbsp;h(i(y)))&nbsp;==&nbsp;bf.SelectBoth(g,&nbsp;i).SelectBoth(f,&nbsp;h);</pre> </p> <p> On the left-hand side, the first dimension is translated in one step. For each <code>x</code> contained in <code>bf</code>, the translation first invokes <code>g(x)</code>, and then immediately calls <code>f</code> with the output of <code>g(x)</code>. The second dimension also gets translated in one step. For each <code>y</code> contained in <code>bf</code>, the translation first invokes <code>i(y)</code>, and then immediately calls <code>h</code> with the output of <code>i(y)</code>. </p> <p> On the right-hand side, you first translate <code>bf</code> along both axes using <code>g</code> and <code>i</code>. This produces an intermediary result that you can use as input for a second translation with <code>f</code> and <code>h</code>. </p> <p> The translation on the left-hand side should produce the same output as the right-hand side. </p> <p> Finally, if you keep one of the dimensions fixed, you essentially have a normal functor, and the second functor law should still hold. For example, if you hold the second dimension fixed, translating over the first dimension is equivalent to a normal functor projection, so the second functor law should hold: </p> <p> <pre>first (f . g) ≡ first f . first g</pre> </p> <p> If you replace <code>first</code> with <code>fmap</code>, you have the second functor law. </p> <p> <img src="/content/binary/second-functor-law-over-first-bifunctor-dimension.png" alt="The second functor law applied to the first dimension of a bifunctor."> </p> <p> In C#, you can write it like this: </p> <p> <pre>bf.SelectFirst(x&nbsp;=&gt;&nbsp;f(g(x)))&nbsp;==&nbsp;bf.SelectFirst(g).SelectFirst(f);</pre> </p> <p> Likewise, you can keep the first dimension constant and apply the second functor law to projections along the second axis: </p> <p> <pre>second (f . g) ≡ second f . second g</pre> </p> <p> Again, if you replace <code>second</code> with <code>fmap</code>, you have the second functor law. </p> <p> <img src="/content/binary/second-functor-law-over-second-bifunctor-dimension.png" alt="The second functor law applied to the second dimension of a bifunctor."> </p> <p> In C#, you express it like this: </p> <p> <pre>bf.SelectSecond(x&nbsp;=&gt;&nbsp;f(g(x)))&nbsp;==&nbsp;bf.SelectSecond(g).SelectSecond(f);</pre> </p> <p> The last two of these composition laws are specialisations of the general composition law, but where you fix either one or the other dimension. </p> <h3 id="41120f4d6ead482f9254a62853d9e1c4"> Summary <a href="#41120f4d6ead482f9254a62853d9e1c4" title="permalink">#</a> </h3> <p> A bifunctor is a container that can be translated over two dimensions, instead of a (mono)functor, which is a container that can be translated over a single dimension. In reality, there isn't a multitude of different bifunctors. While others exist, tuples and Either are the two most common bifunctors. They share an abstraction, but are still fundamentally different. A tuple always contains values of both dimensions at the same time, whereas Either only contains one of the values. </p> <p> Do trifunctors, quadfunctors, and so on, exist? Nothing prevents that, but they aren't particularly useful; in practice, you never run into them. </p> <p> <strong>Next:</strong> <a href="/2018/12/31/tuple-bifunctor">Tuple bifunctor</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The Lazy applicative functor https://blog.ploeh.dk/2018/12/17/the-lazy-applicative-functor 2018-12-17T07:52:00+00:00 Mark Seemann <div id="post"> <p> <em>Lazy computations form an applicative functor.</em> </p> <p> This article is an instalment in <a href="/2018/10/01/applicative-functors">an article series about applicative functors</a>. A previous article has described how <a href="/2018/09/10/the-lazy-functor">lazy computations form a functor</a>. In this article, you'll see that lazy computations also form an applicative functor. </p> <h3 id="c04ca371d39847cc86a90240a7f53745"> Apply <a href="#c04ca371d39847cc86a90240a7f53745" title="permalink">#</a> </h3> <p> As you have <a href="/2018/10/15/an-applicative-password-list">previously seen</a>, C# isn't the best fit for the concept of applicative functors. Nevertheless, you can write an <code>Apply</code> extension method following the applicative 'code template': </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;Apply&lt;<span style="color:#2b91af;">TResult</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;&nbsp;selector, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;source) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;(()&nbsp;=&gt;&nbsp;selector.Value(source.Value)); }</pre> </p> <p> The <code>Apply</code> method takes both a lazy <code>selector</code> and a lazy value called <code>source</code>. It applies the function to the value and returns the result, still as a lazy value. If you have a lazy function <code>f</code> and a lazy value <code>x</code>, you can use the method like this: </p> <p> <pre><span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&gt;&nbsp;f&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;x&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;y&nbsp;=&nbsp;f.Apply(x);</pre> </p> <p> The utility of <code>Apply</code>, however, mostly tends to emerge when you need to chain multiple <a href="https://bartoszmilewski.com/2014/01/14/functors-are-containers">containers</a> together; in this case, multiple lazy values. You can do that by adding as many overloads to <code>Apply</code> as you need: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;&nbsp;Apply&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;&nbsp;selector, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">T1</span>&gt;&nbsp;source) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;(()&nbsp;=&gt;&nbsp;y&nbsp;=&gt;&nbsp;selector.Value(source.Value,&nbsp;y)); }</pre> </p> <p> This overload partially applies the input function. When <code>selector</code> is a function that takes two arguments, you can apply a single of those two arguments, and the result is a new function that closes over the value, but still waits for its second input argument. You can use it like this: </p> <p> <pre><span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">char</span>,&nbsp;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&gt;&nbsp;f&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">char</span>&gt;&nbsp;c&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;i&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;s&nbsp;=&nbsp;f.Apply(c).Apply(i);</pre> </p> <p> Notice that you can chain the various overloads of <code>Apply</code>. In the above example, you have a lazy function that takes a <code>char</code> and an <code>int</code> as input, and returns a <code>string</code>. It could, for instance, be a function that invokes <a href="https://docs.microsoft.com/en-us/dotnet/api/system.string.-ctor?view=netframework-4.7.2#System_String__ctor_System_Char_System_Int32_">the equivalent <code>string</code> constructor overload</a>. </p> <p> Calling <code>f.Apply(c)</code> uses the overload that takes a <code>Lazy&lt;Func&lt;T1, T2, TResult&gt;&gt;</code> as input. The return value is a <code>Lazy&lt;Func&lt;int, string&gt;&gt;</code>, which the first <code>Apply</code> then picks up, to return a <code>Lazy&lt;string&gt;</code>. </p> <p> Usually, you may have one, two, or several lazy values, whereas your function itself isn't contained in a <code>Lazy</code> container. While you can use a helper method such as <code>Lazy.FromValue</code> to 'elevate' a 'normal' function to a lazy function value, it's often more convenient if you have another <code>Apply</code> overload like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;&nbsp;Apply&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;selector, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">T1</span>&gt;&nbsp;source) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;(()&nbsp;=&gt;&nbsp;y&nbsp;=&gt;&nbsp;selector(source.Value,&nbsp;y)); }</pre> </p> <p> The only difference to the equivalent overload is that in this overload, <code>selector</code> isn't a <code>Lazy</code> value, while <code>source</code> still is. This simplifies usage: </p> <p> <pre><span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">char</span>,&nbsp;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;f&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">char</span>&gt;&nbsp;c&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;i&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:#2b91af;">Lazy</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;s&nbsp;=&nbsp;f.Apply(c).Apply(i);</pre> </p> <p> Notice that in this variation of the example, <code>f</code> is no longer a <code>Lazy&lt;Func&lt;...&gt;&gt;</code>, but just a normal <code>Func</code>. </p> <h3 id="966d763816c84c839b7704cdebfd7bac"> F# <a href="#966d763816c84c839b7704cdebfd7bac" title="permalink">#</a> </h3> <p> <a href="https://fsharp.org">F#</a>'s type inference is more powerful than C#'s, so you don't have to resort to various overloads to make things work. You could, for example, create a minimal <code>Lazy</code> module: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;Lazy&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b)&nbsp;-&gt;&nbsp;Lazy&lt;&#39;a&gt;&nbsp;-&gt;&nbsp;Lazy&lt;&#39;b&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;map&nbsp;f&nbsp;(x&nbsp;:&nbsp;Lazy&lt;&#39;a&gt;)&nbsp;=&nbsp;<span style="color:blue;">lazy</span>&nbsp;f&nbsp;x.Value &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Lazy&lt;(&#39;a&nbsp;-&gt;&nbsp;&#39;b)&gt;&nbsp;-&gt;&nbsp;Lazy&lt;&#39;a&gt;&nbsp;-&gt;&nbsp;Lazy&lt;&#39;b&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;apply&nbsp;(x&nbsp;:&nbsp;Lazy&lt;_&gt;)&nbsp;(f&nbsp;:&nbsp;Lazy&lt;_&gt;)&nbsp;=&nbsp;<span style="color:blue;">lazy</span>&nbsp;f.Value&nbsp;x.Value</pre> </p> <p> In this code listing, I've repeated the <code>map</code> function shown in a <a href="/2018/09/10/the-lazy-functor">previous article</a>. It's not required for the implementation of <code>apply</code>, but you'll see it in use shortly, so I thought it was convenient to include it in the listing. </p> <p> If you belong to the camp of F# programmers who think that F# should emulate <a href="https://www.haskell.org">Haskell</a>, you can also introduce an operator: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;(&lt;*&gt;)&nbsp;f&nbsp;x&nbsp;=&nbsp;Lazy.apply&nbsp;x&nbsp;f</pre> </p> <p> Notice that this <code>&lt;*&gt;</code> operator simply flips the arguments of <code>Lazy.apply</code>. If you introduce such an operator, be aware that the admonition from <a href="/2018/10/01/applicative-functors">the overview article</a> still applies. In Haskell, the <code>&lt;*&gt;</code> operator applies to any <code>Applicative</code>, which makes it truly general. In F#, once you define an operator like this, it applies specifically to a particular container type, which, in this case, is <code>Lazy&lt;'a&gt;</code>. </p> <p> You can replicate the first of the above C# examples like this: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;f&nbsp;:&nbsp;Lazy&lt;int&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;string&gt;&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:blue;">let</span>&nbsp;x&nbsp;:&nbsp;Lazy&lt;int&gt;&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:blue;">let</span>&nbsp;y&nbsp;:&nbsp;Lazy&lt;string&gt;&nbsp;=&nbsp;Lazy.apply&nbsp;x&nbsp;f</pre> </p> <p> Alternatively, if you want to use the <code>&lt;*&gt;</code> operator, you can compute <code>y</code> like this: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;y&nbsp;:&nbsp;Lazy&lt;string&gt;&nbsp;=&nbsp;f&nbsp;&lt;*&gt;&nbsp;x</pre> </p> <p> Chaining multiple lazy computations together also works: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;f&nbsp;:&nbsp;Lazy&lt;char&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;int&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;string&gt;&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:blue;">let</span>&nbsp;c&nbsp;:&nbsp;Lazy&lt;char&gt;&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:blue;">let</span>&nbsp;i&nbsp;:&nbsp;Lazy&lt;int&gt;&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:blue;">let</span>&nbsp;s&nbsp;=&nbsp;Lazy.apply&nbsp;c&nbsp;f&nbsp;|&gt;&nbsp;Lazy.apply&nbsp;i</pre> </p> <p> Again, you can compute <code>s</code> with the operator, if that's more to your liking: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;s&nbsp;:&nbsp;Lazy&lt;string&gt;&nbsp;=&nbsp;f&nbsp;&lt;*&gt;&nbsp;c&nbsp;&lt;*&gt;&nbsp;i</pre> </p> <p> Finally, if your function isn't contained in a <code>Lazy</code> value, you can start out with <code>Lazy.map</code>: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;f&nbsp;:&nbsp;char&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;int&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;string&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:blue;">let</span>&nbsp;c&nbsp;:&nbsp;Lazy&lt;char&gt;&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:blue;">let</span>&nbsp;i&nbsp;:&nbsp;Lazy&lt;int&gt;&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:blue;">let</span>&nbsp;s&nbsp;:&nbsp;Lazy&lt;string&gt;&nbsp;=&nbsp;Lazy.map&nbsp;f&nbsp;c&nbsp;|&gt;&nbsp;Lazy.apply&nbsp;i</pre> </p> <p> This works without requiring additional overloads. Since F# natively supports partial function application, the first step in the pipeline, <code>Lazy.map f c</code> has the type <code>Lazy&lt;int -&gt; string&gt;</code> because <code>f</code> is a function of the type <code>char -&gt; int -&gt; string</code>, but in the first step, <code>Lazy.map f c</code> only supplies <code>c</code>, which contains a <code>char</code> value. </p> <p> Once more, if you prefer the infix operator, you can also compute <code>s</code> as: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;s&nbsp;:&nbsp;Lazy&lt;string&gt;&nbsp;=&nbsp;<span style="color:blue;">lazy</span>&nbsp;f&nbsp;&lt;*&gt;&nbsp;c&nbsp;&lt;*&gt;&nbsp;i</pre> </p> <p> While I find <a href="/2018/07/02/terse-operators-make-business-code-more-readable">operator-based syntax attractive in Haskell code</a>, I'm more hesitant about such syntax in F#. </p> <h3 id="63a6579eeb644627b02efc3f9aabd202"> Haskell <a href="#63a6579eeb644627b02efc3f9aabd202" title="permalink">#</a> </h3> <p> As outlined in the <a href="/2018/09/10/the-lazy-functor">previous article</a>, Haskell is already lazily evaluated, so it makes little sense to introduce an explicit <code>Lazy</code> data container. While Haskell's built-in <code>Identity</code> isn't quite equivalent to .NET's <code>Lazy&lt;T&gt;</code> object, some similarities remain; most notably, the <a href="/2018/09/03/the-identity-functor">Identity functor</a> is also applicative: </p> <p> <pre>Prelude Data.Functor.Identity&gt; :t f f :: a -&gt; Int -&gt; [a] Prelude Data.Functor.Identity&gt; :t c c :: Identity Char Prelude Data.Functor.Identity&gt; :t i i :: Num a =&gt; Identity a Prelude Data.Functor.Identity&gt; :t f &lt;$&gt; c &lt;*&gt; i f &lt;$&gt; c &lt;*&gt; i :: Identity String</pre> </p> <p> This little GHCi session simply illustrates that if you have a 'normal' function <code>f</code> and two <code>Identity</code> values <code>c</code> and <code>i</code>, you can compose them using the infix <em>map</em> operator <code>&lt;$&gt;</code>, followed by the infix <em>apply</em> operator <code>&lt;*&gt;</code>. This is equivalent to the F# expression <code>Lazy.map f c |&gt; Lazy.apply i</code>. </p> <p> Still, this makes little sense, since all Haskell expressions are already lazily evaluated. </p> <h3 id="d3ddcafbb8e14bfea2c18828cf44f244"> Summary <a href="#d3ddcafbb8e14bfea2c18828cf44f244" title="permalink">#</a> </h3> <p> The Lazy functor is also an <em>applicative functor</em>. This can be used to combine multiple lazily computed values into a single lazily computed value. </p> <p> <strong>Next:</strong> <a href="/2019/04/22/applicative-monoids">Applicative monoids</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Danish CPR numbers in F# https://blog.ploeh.dk/2018/12/10/danish-cpr-numbers-in-f 2018-12-10T08:14:00+00:00 Mark Seemann <div id="post"> <p> <em>An example of domain-modelling in F#, including a fine example of using the option type as an applicative functor.</em> </p> <p> This article is an instalment in <a href="/2018/10/01/applicative-functors">an article series about applicative functors</a>, although the applicative functor example doesn't appear until towards the end. This article also serves the purpose of showing an example of <a href="http://amzn.to/WBCwx7">Domain-Driven Design</a> in F#. </p> <h3 id="163a2e2ecb144172a974d1eb23ff6703"> Danish personal identification numbers <a href="#163a2e2ecb144172a974d1eb23ff6703" title="permalink">#</a> </h3> <p> As outlined in the <a href="/2018/11/26/the-test-data-generator-applicative-functor">previous article</a>, in Denmark, everyone has a <a href="https://en.wikipedia.org/wiki/Personal_identification_number_(Denmark)">personal identification number</a>, in Danish called <em>CPR-nummer</em> (<em>CPR number</em>). </p> <p> CPR numbers have a simple format: <code>DDMMYY-SSSS</code>, where the first six digits indicate a person's birth date, and the last four digits are a sequence number. Some information, however, is also embedded in the sequence number. An example could be <code>010203-1234</code>, which indicates a woman born February 1, 1903. </p> <p> One way to model this in <a href="https://fsharp.org">F#</a> is with a single-case discriminated union: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:teal;">CprNumber</span>&nbsp;=&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:navy;">CprNumber</span>&nbsp;<span style="color:blue;">of</span>&nbsp;(<span style="color:teal;">int</span>&nbsp;*&nbsp;<span style="color:teal;">int</span>&nbsp;*&nbsp;<span style="color:teal;">int</span>&nbsp;*&nbsp;<span style="color:teal;">int</span>)&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">override</span>&nbsp;x.<span style="color:navy;">ToString</span>&nbsp;()&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;(<span style="color:navy;">CprNumber</span>&nbsp;(day,&nbsp;month,&nbsp;year,&nbsp;sequenceNumber))&nbsp;=&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">sprintf</span>&nbsp;<span style="color:#a31515;">&quot;</span><span style="color:teal;">%02d%02d%02d</span><span style="color:#a31515;">-</span><span style="color:teal;">%04d</span><span style="color:#a31515;">&quot;</span>&nbsp;day&nbsp;month&nbsp;year&nbsp;sequenceNumber</pre> </p> <p> This is a common idiom in F#. In object-oriented design with e.g. C# or Java, you'd typically create a class and put guard clauses in its constructor. This would prevent a user from initialising an object with invalid data (such as <code>401500-1234</code>). While you <em>can</em> create classes in F# as well, a single-case union with a private case constructor can achieve the same degree of encapsulation. </p> <p> In this case, I decided to use a quadruple (4-tuple) as the internal representation, but this isn't visible to users. This gives me the option to refactor the internal representation, if I need to, without breaking existing clients. </p> <h3 id="83eaa41329eb4d8b894e0640f600f110"> Creating CPR number values <a href="#83eaa41329eb4d8b894e0640f600f110" title="permalink">#</a> </h3> <p> Since the <code>CprNumber</code> case constructor is private, you can't just create new values like this: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:#9b9b9b;">cpr</span>&nbsp;=&nbsp;CprNumber&nbsp;(1,&nbsp;1,&nbsp;1,&nbsp;1118) </pre> </p> <p> If you're outside the <code>Cpr</code> module that defines the type, this <strong>doesn't compile</strong>. This is by design, but obviously you need a way to create values. For convenience, input values for day, month, and so on, are represented as <code>int</code>s, which can be zero, negative, or way too large for CPR numbers. There's no way to statically guarantee that you can create a value, so you'll have to settle for a <code>tryCreate</code> function; i.e. a function that returns <code>Some CprNumber</code> if the input is valid, or <code>None</code> if it isn't. In <a href="https://www.haskell.org">Haskell</a>, this pattern is called a <em>smart constructor</em>. </p> <p> There's a couple of rules to check. All integer values must fall in certain ranges. Days must be between 1 and 31, months must be between 1 and 12, and so on. One way to enable succinct checks like that is with an <a href="https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/active-patterns">active pattern</a>: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;<span style="color:blue;">private</span>&nbsp;(|<span style="color:navy;">Between</span>|_|)&nbsp;min&nbsp;max&nbsp;candidate&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;min&nbsp;&lt;=&nbsp;candidate&nbsp;&amp;&amp;&nbsp;candidate&nbsp;&lt;=&nbsp;max &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;<span style="color:navy;">Some</span>&nbsp;candidate &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;<span style="color:navy;">None</span></pre> </p> <p> Straightforward: return <code>Some candidate</code> if <code>candidate</code> is between <code>min</code> and <code>max</code>; otherwise, <code>None</code>. This enables you to pattern-match input integer values to particular ranges. </p> <p> Perhaps you've already noticed that years are represented with only two digits. CPR is an old system (from 1968), and back then, bits were expensive. No reason to waste bits on recording the millennium or century in which people were born. It turns out, after all, that there's a way to at least partially figure out the century in which people were born. The first digit of the sequence number contains that information: </p> <p> <pre><span style="color:green;">//&nbsp;int&nbsp;-&gt;&nbsp;int&nbsp;-&gt;&nbsp;int</span> <span style="color:blue;">let</span>&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:navy;">calculateFourDigitYear</span>&nbsp;year&nbsp;sequenceNumber&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;centuryDigit&nbsp;=&nbsp;sequenceNumber&nbsp;/&nbsp;1000&nbsp;<span style="color:green;">//&nbsp;Integer&nbsp;division</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Table&nbsp;from&nbsp;https://da.wikipedia.org/wiki/CPR-nummer</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;centuryDigit,&nbsp;year&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Between</span>&nbsp;0&nbsp;3&nbsp;_,&nbsp;_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;1900 &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;,&nbsp;<span style="color:navy;">Between</span>&nbsp;0&nbsp;36&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;2000 &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;1900 &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Between</span>&nbsp;5&nbsp;8&nbsp;_,&nbsp;<span style="color:navy;">Between</span>&nbsp;0&nbsp;57&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;2000 &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Between</span>&nbsp;5&nbsp;8&nbsp;_,&nbsp;_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;1800 &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;,&nbsp;<span style="color:navy;">Between</span>&nbsp;0&nbsp;36&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;2000 &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;1900 &nbsp;&nbsp;&nbsp;&nbsp;+&nbsp;year</pre> </p> <p> As the code comment informs the reader, there's a table that defines the century, based on the two-digit year and the first digit of the sequence number. Note that birth dates in the nineteenth century are possible. No Danes born before 1900 are alive any longer, but at the time the CPR system was introduced, one person in the system was born in 1863! </p> <p> The <code>calculateFourDigitYear</code> function starts by pulling the first digit out of the sequence number. This is a four-digit number, so dividing by 1,000 produces the digit. I left a comment about integer division, because I often miss that detail when I read code. </p> <p> The big pattern-match expression uses the <code>Between</code> active pattern, but it ignores the return value from the pattern. This explains the wild cards (<code>_</code>), I hope. </p> <p> Although a pattern-match expression is often formatted over several lines of code, it's a single expression that produces a single value. Often, you see code where a <code>let</code>-binding binds a named value to a pattern-match expression. Another occasional idiom is to pipe a pattern-match expression to a function. In the <code>calculateFourDigitYear</code> function I use a language construct I've never seen anywhere else: the eight-lines pattern-match expression returns an <code>int</code>, which I simply add to <code>year</code> using the <code>+</code> operator. </p> <p> Both <code>calculateFourDigitYear</code> and the <code>Between</code> active pattern are private functions. They're only there as support functions for the public API. You can now implement a <code>tryCreate</code> function: </p> <p> <pre><span style="color:green;">//&nbsp;int&nbsp;-&gt;&nbsp;int&nbsp;-&gt;&nbsp;int&nbsp;-&gt;&nbsp;int&nbsp;-&gt;&nbsp;CprNumber&nbsp;option</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">tryCreate</span>&nbsp;day&nbsp;month&nbsp;year&nbsp;sequenceNumber&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;month,&nbsp;year,&nbsp;sequenceNumber&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Between</span>&nbsp;1&nbsp;12&nbsp;m,&nbsp;<span style="color:navy;">Between</span>&nbsp;0&nbsp;99&nbsp;y,&nbsp;<span style="color:navy;">Between</span>&nbsp;0&nbsp;9999&nbsp;s&nbsp;<span style="color:blue;">-&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;fourDigitYear&nbsp;=&nbsp;<span style="color:navy;">calculateFourDigitYear</span>&nbsp;y&nbsp;s &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;1&nbsp;&lt;=&nbsp;day&nbsp;&amp;&amp;&nbsp;day&nbsp;&lt;=&nbsp;<span style="color:teal;">DateTime</span>.<span style="color:navy;">DaysInMonth</span>&nbsp;(fourDigitYear,&nbsp;m) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;<span style="color:navy;">Some</span>&nbsp;(<span style="color:navy;">CprNumber</span>&nbsp;(day,&nbsp;m,&nbsp;y,&nbsp;s)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;<span style="color:navy;">None</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">None</span></pre> </p> <p> The <code>tryCreate</code> function begins by pattern-matching a triple (3-tuple) using the <code>Between</code> active pattern. The <code>month</code> must always be between <code>1</code> and <code>12</code> (both included), the <code>year</code> must be between <code>0</code> and <code>99</code>, and the <code>sequenceNumber</code> must always be between <code>0</code> and <code>9999</code> (in fact, I'm not completely sure if <code>0000</code> is valid). </p> <p> Finding the appropriate range for the <code>day</code> is more intricate. Is <code>31</code> always valid? Clearly not, because there's no November 31, for example. Is <code>30</code> always valid? No, because there's never a February 30. Is <code>29</code> valid? This depends on whether or not the year is a leap year. </p> <p> This reveals why you need <code>calculateFourDigitYear</code>. While you can use <code>DateTime.DaysInMonth</code> to figure out how many days a given month has, you need the year. Specifically, February 19<strong>00</strong> had 28 days, while February 20<strong>00</strong> had 29. </p> <p> Ergo, if <code>day</code>, <code>month</code>, <code>year</code>, and <code>sequenceNumber</code> all fall within their appropriate ranges, <code>tryCreate</code> returns a <code>Some CprNumber</code> value; otherwise, it returns <code>None</code>. </p> <p> Notice how this is different from an object-oriented constructor with guard clauses. If you try to create an object with invalid input, it'll throw an exception. If you try to create a <code>CprNumber</code> value, you'll receive a <code>CprNumber option</code>, and you, as the client developer, <em>must</em> handle both the <code>Some</code> and the <code>None</code> case. The compiler will enforce this. </p> <p> <pre>&gt; let gjern = Cpr.tryCreate 11 11 11 1118;; val gjern : Cpr.CprNumber option = Some 111111-1118 &gt; gjern |&gt; Option.map Cpr.born;; val it : DateTime option = Some 11.11.1911 00:00:00</pre> </p> <p> As most F# developers know, F# gives you enough syntactic sugar to make this a joy rather than a chore... and the warm and fuzzy feeling of safety is priceless. </p> <h3 id="78793bd7fceb4dc99bab81ccb7abb305"> CPR data <a href="#78793bd7fceb4dc99bab81ccb7abb305" title="permalink">#</a> </h3> <p> The above FSI session uses <code>Cpr.born</code>, which you haven't seen yet. With the tools available so far, it's trivial to implement; all the work is already done: </p> <p> <pre><span style="color:green;">//&nbsp;CprNumber&nbsp;-&gt;&nbsp;DateTime</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">born</span>&nbsp;(<span style="color:navy;">CprNumber</span>&nbsp;(day,&nbsp;month,&nbsp;year,&nbsp;sequenceNumber))&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">DateTime</span>&nbsp;(<span style="color:navy;">calculateFourDigitYear</span>&nbsp;year&nbsp;sequenceNumber,&nbsp;month,&nbsp;day)</pre> </p> <p> While the <code>CprNumber</code> case constructor is <code>private</code>, it's still available from inside of the module. The <code>born</code> function pattern-matches <code>day</code>, <code>month</code>, <code>year</code>, and <code>sequenceNumber</code> out of its input argument, and trivially delegates the hard work to <code>calculateFourDigitYear</code>. </p> <p> Another piece of data you can deduce from a CPR number is the gender of the person: </p> <p> <pre><span style="color:green;">//&nbsp;CprNumber&nbsp;-&gt;&nbsp;bool</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">isFemale</span>&nbsp;(<span style="color:navy;">CprNumber</span>&nbsp;(_,&nbsp;_,&nbsp;_,&nbsp;sequenceNumber))&nbsp;=&nbsp;sequenceNumber&nbsp;%&nbsp;2&nbsp;&nbsp;=&nbsp;0 <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">isMale</span>&nbsp;&nbsp;&nbsp;(<span style="color:navy;">CprNumber</span>&nbsp;(_,&nbsp;_,&nbsp;_,&nbsp;sequenceNumber))&nbsp;=&nbsp;sequenceNumber&nbsp;%&nbsp;2&nbsp;&lt;&gt;&nbsp;0</pre> </p> <p> The rule is that if the sequence number is even, then the person is female; otherwise, the person is male (and if you change sex, you get a new CPR number). </p> <p> <pre>&gt; gjern |&gt; Option.map Cpr.isFemale;; val it : bool option = Some true</pre> </p> <p> Since <code>1118</code> is even, this is a woman. </p> <h3 id="235df75fd03a41e49b5a1fc3767e6ce8"> Parsing CPR strings <a href="#235df75fd03a41e49b5a1fc3767e6ce8" title="permalink">#</a> </h3> <p> CPR numbers are often passed around as text, so you'll need to be able to parse a <code>string</code> representation. As described in the <a href="/2018/11/26/the-test-data-generator-applicative-functor">previous article</a>, you should follow <a href="https://en.wikipedia.org/wiki/Robustness_principle">Postel's law</a>. Input could include extra white space, and the middle dash could be missing. </p> <p> The .NET Base Class Library contains enough utility methods working on <code>string</code> values that this isn't going to be particularly difficult. It can, however, be awkward to interoperate with object-oriented APIs, so you'll often benefit from adding a few utility functions that give you curried functions instead of objects with methods. Here's one that adapts <code>Int32.TryParse</code>: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:teal;">Int</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;string&nbsp;-&gt;&nbsp;int&nbsp;option</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">tryParse</span>&nbsp;candidate&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;candidate&nbsp;|&gt;&nbsp;<span style="color:teal;">Int32</span>.<span style="color:navy;">TryParse</span>&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:blue;">true</span>,&nbsp;i&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">Some</span>&nbsp;i &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">None</span></pre> </p> <p> Nothing much goes on here. While F# has pleasant syntax for handling <code>out</code> parameters, it can be inconvenient to have to pattern-match every time you'd like to try to parse an integer. </p> <p> Here's another helper function: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:teal;">String</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;int&nbsp;-&gt;&nbsp;int&nbsp;-&gt;&nbsp;string&nbsp;-&gt;&nbsp;string&nbsp;option</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">trySubstring</span>&nbsp;startIndex&nbsp;length&nbsp;(s&nbsp;:&nbsp;<span style="color:teal;">string</span>)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;s.Length&nbsp;&lt;&nbsp;startIndex&nbsp;+&nbsp;length &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">then</span>&nbsp;<span style="color:navy;">None</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span>&nbsp;<span style="color:navy;">Some</span>&nbsp;(s.<span style="color:navy;">Substring</span>&nbsp;(startIndex,&nbsp;length))</pre> </p> <p> This one comes with two benefits: The first benefit is that it's curried, which enables partial application and piping. You'll see an example of this further down. The second benefit is that it handles at least one error condition in a type-safe manner. When trying to extract a sub-string from a string, the <code>Substring</code> <em>method</em> can throw an exception if the index or length arguments are out of range. This function checks whether it can extract the requested sub-string, and returns <code>None</code> if it can't. </p> <p> I wouldn't be surprised if there are edge cases (for example involving negative integers) that <code>trySubstring</code> doesn't handle gracefully, but as you may have noticed, this is a function in a <code>private</code> module. I only need it to handle a particular use case, and it does that. </p> <p> You can now add the <code>tryParse</code> function: </p> <p> <pre><span style="color:green;">//&nbsp;string&nbsp;-&gt;&nbsp;CprNumber&nbsp;option</span> <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">tryParse</span>&nbsp;(candidate&nbsp;:&nbsp;<span style="color:teal;">string</span>&nbsp;)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;(&lt;*&gt;)&nbsp;fo&nbsp;xo&nbsp;=&nbsp;fo&nbsp;|&gt;&nbsp;<span style="color:teal;">Option</span>.<span style="color:navy;">bind</span>&nbsp;(<span style="color:blue;">fun</span>&nbsp;<span style="color:navy;">f</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;xo&nbsp;|&gt;&nbsp;<span style="color:teal;">Option</span>.<span style="color:navy;">map</span>&nbsp;<span style="color:navy;">f</span>) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;canonicalized&nbsp;=&nbsp;candidate.<span style="color:navy;">Trim</span>().<span style="color:navy;">Replace</span>(<span style="color:#a31515;">&quot;-&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;&quot;</span>) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;dayCandidate&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;canonicalized&nbsp;|&gt;&nbsp;<span style="color:teal;">String</span>.<span style="color:navy;">trySubstring</span>&nbsp;0&nbsp;2 &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;monthCandidate&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;canonicalized&nbsp;|&gt;&nbsp;<span style="color:teal;">String</span>.<span style="color:navy;">trySubstring</span>&nbsp;2&nbsp;2 &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;yearCandidate&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;canonicalized&nbsp;|&gt;&nbsp;<span style="color:teal;">String</span>.<span style="color:navy;">trySubstring</span>&nbsp;4&nbsp;2 &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;sequenceNumberCandidate&nbsp;=&nbsp;canonicalized&nbsp;|&gt;&nbsp;<span style="color:teal;">String</span>.<span style="color:navy;">trySubstring</span>&nbsp;6&nbsp;4 &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">Some</span>&nbsp;<span style="color:navy;">tryCreate</span> &nbsp;&nbsp;&nbsp;&nbsp;&lt;*&gt;&nbsp;<span style="color:teal;">Option</span>.<span style="color:navy;">bind</span>&nbsp;<span style="color:teal;">Int</span>.<span style="color:navy;">tryParse</span>&nbsp;dayCandidate &nbsp;&nbsp;&nbsp;&nbsp;&lt;*&gt;&nbsp;<span style="color:teal;">Option</span>.<span style="color:navy;">bind</span>&nbsp;<span style="color:teal;">Int</span>.<span style="color:navy;">tryParse</span>&nbsp;monthCandidate &nbsp;&nbsp;&nbsp;&nbsp;&lt;*&gt;&nbsp;<span style="color:teal;">Option</span>.<span style="color:navy;">bind</span>&nbsp;<span style="color:teal;">Int</span>.<span style="color:navy;">tryParse</span>&nbsp;yearCandidate &nbsp;&nbsp;&nbsp;&nbsp;&lt;*&gt;&nbsp;<span style="color:teal;">Option</span>.<span style="color:navy;">bind</span>&nbsp;<span style="color:teal;">Int</span>.<span style="color:navy;">tryParse</span>&nbsp;sequenceNumberCandidate &nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;&nbsp;<span style="color:teal;">Option</span>.<span style="color:navy;">bind</span>&nbsp;<span style="color:navy;">id</span></pre> </p> <p> The function starts by defining a private <code>&lt;*&gt;</code> operator. Readers of the <a href="/2018/10/01/applicative-functors">applicative functor article series</a> will recognise this as the 'apply' operator. The reason I added it as a private operator is that I don't need it anywhere else in the code base, and in F#, I'm always worried that if I add <code>&lt;*&gt;</code> at a more visible level, it could collide with other definitions of <code>&lt;*&gt;</code> - for example <a href="/2018/10/08/full-deck">one for lists</a>. This one particularly makes <code>option</code> an applicative functor. </p> <p> The first step in parsing <code>candidate</code> is to remove surrounding white space and the interior dash. </p> <p> The next step is to use <code>String.trySubstring</code> to pull out candidates for <em>day</em>, <em>month</em>, and so on. Each of these four are <code>string option</code> values. </p> <p> All four of these must be <code>Some</code> values before we can even start to attempt to turn them into a <code>CprNumber</code> value. If only a single value is <code>None</code>, <code>tryParse</code> should return <code>None</code> as well. </p> <p> You may want to re-read the <a href="/2018/10/15/an-applicative-password-list">article on the List applicative functor</a> for a detailed explanation of how the <code>&lt;*&gt;</code> operator works. In <code>tryParse</code>, you have four <code>option</code> values, so you apply them all using four <code>&lt;*&gt;</code> operators. Since four values are being applied, you'll need a function that takes four curried input arguments of the appropriate types. In this case, all four are <code>int option</code> values, so for the first expression in the <code>&lt;*&gt;</code> chain, you'll need an option of a function that takes four <code>int</code> arguments. </p> <p> Lo and behold! <code>tryCreate</code> takes four <code>int</code> arguments, so the only action you need to take is to make it an <code>option</code> by putting it in a <code>Some</code> case. </p> <p> The only remaining hurdle is that <code>tryCreate</code> returns <code>CprNumber option</code>, and since you're already 'in' the <code>option</code> applicative functor, you now have a <code>CprNumber option option</code>. Fortunately, <code>bind id</code> is always the 'flattening' combo, so that's easily dealt with. </p> <p> <pre>&gt; let andreas = Cpr.tryParse " 0109636221";; val andreas : Cpr.CprNumber option = Some 010963-6221</pre> </p> <p> Since you now have both a way to parse a string, and turn a <code>CprNumber</code> into a string, you can write the usual round-trip property: </p> <p> <pre>[&lt;<span style="color:teal;">Fact</span>&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``CprNumber&nbsp;correctly&nbsp;round-trips``</span>&nbsp;()&nbsp;=&nbsp;<span style="color:teal;">Property</span>.<span style="color:navy;">check</span>&nbsp;&lt;|&nbsp;<span style="color:blue;">property</span>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;expected&nbsp;=&nbsp;<span style="color:teal;">Gen</span>.cprNumber &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;&nbsp;actual&nbsp;&nbsp;&nbsp;=&nbsp;expected&nbsp;|&gt;&nbsp;<span style="color:navy;">string</span>&nbsp;|&gt;&nbsp;<span style="color:teal;">Cpr</span>.<span style="color:navy;">tryParse</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">Some</span>&nbsp;expected&nbsp;=!&nbsp;actual&nbsp;}</pre> </p> <p> This test uses <a href="https://github.com/hedgehogqa/fsharp-hedgehog">Hedgehog</a>, <a href="https://github.com/SwensenSoftware/unquote">Unquote</a>, and <a href="https://xunit.github.io">xUnit.net</a>. The previous article demonstrates a way to test that <code>Cpr.tryParse</code> can handle mangled input. </p> <h3 id="503bb1791d344b1f917734cffe2320f6"> Summary <a href="#503bb1791d344b1f917734cffe2320f6" title="permalink">#</a> </h3> <p> This article mostly exhibited various F# design techniques you can use to achieve an even better degree of encapsulation than you can easily get with object-oriented languages. Towards the end, you saw how using <code>option</code> as an applicative functor enables you to compose more complex optional values from smaller values. If just a single value is <code>None</code>, the entire expression becomes <code>None</code>, but if all values are <code>Some</code> values, the computation succeeds. </p> <p> This article is an entry in the <a href="https://sergeytihon.com/2018/10/22/f-advent-calendar-in-english-2018">F# Advent Calendar in English 2018</a>. </p> <p> <strong>Next: </strong> <a href="/2018/12/17/the-lazy-applicative-functor">The Lazy applicative functor</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="559c8543d61042a0bdcbe938ac97de12"> <div class="comment-author"><a href="https://activesolution.se">Viktor Andersson</a></div> <div class="comment-content"> <p> Great post, a very good read! Interestingly enough we recently made an F# implementation for the swedish personal identification number. In fact <a href="https://github.com/ActiveLogin/ActiveLogin.Identity">v1.0.0</a> will be published any day now. Interesting to see how the problem with four-digit years are handled differently in Denmark and Sweden. </p> <p> I really like the Between Active pattern of your solution, we did not really take as a generic approach, instead we modeled with types for Year, Month, Day, etc. But I find your solution to be very concise and clear. Also we worked with the Result type instead of Option to be able to provide the client with helpful error messages. For our Object Oriented friends we are also exposing a C#-friendly facade which adds a bit of boiler plate code. </p> </div> <div class="comment-date">2018-12-18 06:26 UTC</div> </div> <div class="comment" id="26683afcb79c444f9887f112323b935a"> <div class="comment-author"><a href="/">Mark Seemann</a></div> <div class="comment-content"> <p> Viktor, thank you for your kind words. The <code>Result</code> (or <a href="/2018/06/11/church-encoded-either">Either</a>) type does, indeed, provide more information when things go wrong. This can be useful when client code needs to handle different error cases in different ways. Sometimes, it may also be useful, as you write, when you want to provide more <a href="/2014/12/23/exception-messages-are-for-programmers">helpful error messages</a>. </p> <p> Particularly when it comes to parsing or input validation, <a href="/2018/11/05/applicative-validation">Either can be useful</a>. </p> <p> The main reason I chose to model with <code>option</code> in this article was that I wanted to demonstrate how to use the <a href="/2018/10/01/applicative-functors">applicative</a> nature of <code>option</code>, but I suppose I could have equally done so with <code>Result</code>. </p> </div> <div class="comment-date">2018-12-18 9:09 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Set is not a functor https://blog.ploeh.dk/2018/12/03/set-is-not-a-functor 2018-12-03T07:58:00+00:00 Mark Seemann <div id="post"> <p> <em>Sets aren't functors. An example for object-oriented programmers.</em> </p> <p> This article is an instalment in <a href="/2018/03/22/functors">an article series about functors</a>. The way functors are frequently presented to programmes is as a generic <a href="https://bartoszmilewski.com/2014/01/14/functors-are-containers">container</a> (<code>Foo&lt;T&gt;</code>) equipped with a translation method, normally called <em>map</em>, but in C# <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatically</a> called <code>Select</code>. </p> <p> It'd be tempting to believe that any generic type with a <code>Select</code> method is a functor, but it takes more than that. The <code>Select</code> method must also obey the <a href="/2018/03/22/functors">functor laws</a>. This article shows an example of a translation that violates the second functor law. </p> <h3 id="5a13402d14f54ce689a7ae4e62f54233"> Mapping sets <a href="#5a13402d14f54ce689a7ae4e62f54233" title="permalink">#</a> </h3> <p> The .NET Base Class Library comes with a class called <a href="https://docs.microsoft.com/en-gb/dotnet/api/system.collections.generic.hashset-1">HashSet&lt;T&gt;</a>. This generic class implements <code>IEnumerable&lt;T&gt;</code>, so, via extension methods, it has a <code>Select</code> method. </p> <p> Unfortunately, that <code>Select</code> method isn't a structure-preserving translation of sets. The problem is that it treats sets as enumerable sequences, which implies an order to elements that isn't part of a set's structure. </p> <p> In order to understand what the problem is, consider this <a href="https://xunit.github.io">xUnit.net</a> test: </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;SetAsEnumerableSelectLeadsToWrongConclusions() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;set1&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HashSet</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;{&nbsp;1,&nbsp;2,&nbsp;3&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;set2&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HashSet</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;{&nbsp;3,&nbsp;2,&nbsp;1&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.True(set1.SetEquals(set2)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;id&nbsp;=&nbsp;x&nbsp;=&gt;&nbsp;x; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;seq1&nbsp;=&nbsp;set1.AsEnumerable().Select(id); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;seq2&nbsp;=&nbsp;set2.AsEnumerable().Select(id); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.False(seq1.SequenceEqual(seq2)); }</pre> </p> <p> This test creates two sets, and by using a <a href="http://xunitpatterns.com/Guard%20Assertion.html">Guard Assertion</a> demonstrates that they're equal to each other. It then proceeds to <code>Select</code> over both sets, using the identity function <code>id</code>. The return values aren't <code>HashSet&lt;T&gt;</code> objects, but rather <code>IEnumerable&lt;T&gt;</code> sequences. Due to an implementation detail in <code>HashSet&lt;T&gt;</code>, these two sequences are different, because they were populated in reverse order of each other. </p> <p> The problem is that the <code>Select</code> extension method has the signature <code>IEnumerable&lt;TResult&gt; Select&lt;T, TResult&gt;(IEnumerable&lt;T&gt;, Func&lt;T, TResult&gt;)</code>. It doesn't operate on <code>HashSet&lt;T&gt;</code>, but instead treats it as an ordered sequence of elements. A set isn't intrinsically ordered, so that's not the translation you need. </p> <p> To be clear, <code>IEnumerable&lt;T&gt;</code> <em>is</em> a functor (as long as the sequence is <a href="https://en.wikipedia.org/wiki/Referential_transparency">referentially transparent</a>). It's just not the functor you're looking for. What you need is a method with the signature <code>HashSet&lt;TResult&gt; Select&lt;T, TResult&gt;(HashSet&lt;T&gt;, Func&lt;T, TResult&gt;)</code>. Fortunately, such a method is trivial to implement: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">HashSet</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;Select&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">HashSet</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;source, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;selector) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HashSet</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;(source.AsEnumerable().Select(selector)); }</pre> </p> <p> This extension method offers a proper <code>Select</code> method that translates one <code>HashSet&lt;T&gt;</code> into another. It does so by enumerating the elements of the set, then using the <code>Select</code> method for <code>IEnumerable&lt;T&gt;</code>, and finally wrapping the resulting sequence in a new <code>HashSet&lt;TResult&gt;</code> object. </p> <p> This is a better candidate for a translation of sets. Is it a functor, then? </p> <h3 id="44c78cf51897490a8d844a89eaa8ef73"> Second functor law <a href="#44c78cf51897490a8d844a89eaa8ef73" title="permalink">#</a> </h3> <p> Even though <code>HashSet&lt;T&gt;</code> and the new <code>Select</code> method have the correct types, it's still only a functor if it obeys the functor laws. It's easy, however, to come up with a counter-example that demonstrates that <code>Select</code> violates the second functor law. </p> <p> Consider a pair of conversion methods between <code>string</code> and <code>DateTimeOffset</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;ParseDateTime(<span style="color:blue;">string</span>&nbsp;s) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;dt; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(<span style="color:#2b91af;">DateTimeOffset</span>.TryParse(s,&nbsp;<span style="color:blue;">out</span>&nbsp;dt)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;dt; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>.MinValue; } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:blue;">string</span>&nbsp;FormatDateTime(<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;dt) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;dt.ToString(<span style="color:#a31515;">&quot;yyyy&#39;-&#39;MM&#39;-&#39;dd&#39;T&#39;HH&#39;:&#39;mm&#39;:&#39;sszzz&quot;</span>); }</pre> </p> <p> The first method, <code>ParseDateTime</code>, converts a <code>string</code> into a <code>DateTimeOffset</code> value. It tries to parse the input, and returns the corresponding <code>DateTimeOffset</code> value if this is possible; for all other input values, it simply returns <code>DateTimeOffset.MinValue</code>. You may dislike how the method deals with input values it can't parse, but that's not important. In order to prove that sets aren't functors, I just need <em>one</em> counter-example, and the one I'll pick will not involve unparseable strings. </p> <p> The <code>FormatDateTime</code> method converts any <code>DateTimeOffset</code> value to an <a href="https://en.wikipedia.org/wiki/ISO_8601">ISO 8601 string</a>. </p> <p> The <code>DateTimeOffset</code> type is the interesting piece of the puzzle. In case you're not intimately familiar with it, a <code>DateTimeOffset</code> value contains a date, a time, and a time-zone offset. You can, for example create one like this: </p> <p> <pre>&gt; <span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>(2018,&nbsp;4,&nbsp;17,&nbsp;15,&nbsp;9,&nbsp;55,&nbsp;<span style="color:#2b91af;">TimeSpan</span>.FromHours(2)) [17.04.2018 15:09:55 +02:00]</pre> </p> <p> This represents April 17, 2018, at 15:09:55 at UTC+2. You can convert that value to UTC: </p> <p> <pre>&gt; <span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>(2018,&nbsp;4,&nbsp;17,&nbsp;15,&nbsp;9,&nbsp;55,&nbsp;<span style="color:#2b91af;">TimeSpan</span>.FromHours(2)).ToUniversalTime() [17.04.2018 13:09:55 +00:00]</pre> </p> <p> This value clearly contains different constituent elements, but it does represent the same instant in time. This is how <code>DateTimeOffset</code> implements <code>Equals</code>. These two object are considered equal, as the following test will demonstrate. </p> <p> In a sense, you could argue that there's some sense in implementing equality for <code>DateTimeOffset</code> in that way, but this unusual behaviour provides just the counter-example that demonstrates that sets aren't functors: </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;SetViolatesSecondFunctorLaw() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;x&nbsp;=&nbsp;<span style="color:#a31515;">&quot;2018-04-17T13:05:28+02:00&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;y&nbsp;=&nbsp;<span style="color:#a31515;">&quot;2018-04-17T11:05:28+00:00&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(ParseDateTime(x),&nbsp;ParseDateTime(y)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;set&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">HashSet</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;{&nbsp;x,&nbsp;y&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;l&nbsp;=&nbsp;set.Select(ParseDateTime).Select(FormatDateTime); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;r&nbsp;=&nbsp;set.Select(s&nbsp;=&gt;&nbsp;FormatDateTime(ParseDateTime(s))); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.False(l.SetEquals(r)); }</pre> </p> <p> This passing test provides the required counter-example. It first creates two ISO 8601 representations of the same instant. As the Guard Assertion demonstrates, the corresponding <code>DateTimeOffset</code> values are considered equal. </p> <p> In <a href="/2013/06/24/a-heuristic-for-formatting-code-according-to-the-aaa-pattern">the <em>act</em> phase</a>, the test creates a <code>set</code> of these two strings. It then performs two round-trips over <code>DateTimeOffset</code> and back to <code>string</code>. The second functor law states that it shouldn't matter whether you do it in one or two steps, but it does. <code>Assert.False</code> passes because <code>l</code> is <em>not</em> equal to <code>r</code>. Q.E.D. </p> <h3 id="f392310c11984d08a31bf6456849dd01"> An illustration <a href="#f392310c11984d08a31bf6456849dd01" title="permalink">#</a> </h3> <p> The problem is that sets only contain one element of each value, and due to the way that <code>DateTimeOffset</code> interprets equality, two values that both represent the same instant are collapsed into a single element when taking an intermediary step. You can illustrate it like this: </p> <p> <img src="/content/binary/set-violating-second-functor-law.png" alt="Diagram illustrating the above counter-example."> </p> <p> In this illustration, I've hidden the date behind an ellipsis in order to improve clarity. The date segments are irrelevant in this example, since they're all identical. </p> <p> You start with a set of two <code>string</code> values. These are obviously different, so the set contains two elements. When you map the set using the <code>ParseDateTime</code> function, you get two <code>DateTimeOffset</code> values that .NET considers equal. For that reason, <code>HashSet&lt;DateTimeOffset&gt;</code> considers the second value redundant, and ignores it. Therefore, the intermediary set contains only a single element. When you map that set with <code>FormatDateTime</code>, there's only a single element to translate, so the final set contains only a single <code>string</code> value. </p> <p> On the other hand, when you map the input set without an intermediary set, each <code>string</code> value is first converted into a <code>DateTimeOffset</code> value, and then immediately thereafter converted back to a <code>string</code> value. Since the two resulting strings are different, the resulting set contains two values. </p> <p> Which of these paths is correct, then? Both of them. That's the problem. When considering the semantics of sets, both translations produce correct results. Since the results diverge, however, the translation isn't a functor. </p> <h3 id="ee52a36b611b40c5a7d015bea3cbf1dc"> Summary <a href="#ee52a36b611b40c5a7d015bea3cbf1dc" title="permalink">#</a> </h3> <p> A functor must not only preserve the structure of the data container, but also of functions. The functor laws express that a translation of functions preserves the structure of how the functions compose, in addition to preserving the structure of the containers. Mapping a set doesn't preserve those structures, and that's the reason that sets aren't functors. </p> <h3 id="649aceb6a241450abc861ec63f34ff0a"> P.S. <a href="#649aceb6a241450abc861ec63f34ff0a" title="permalink">#</a> </h3> <p> <strong>2019-01-09.</strong> There's been some controversy around this post, and more than one person has pointed out to me that the underlying reason that the functor laws are violated in the above example is because <code>DateTimeOffset</code> behaves oddly. Specifically, its <code>Equals</code> implementation breaks the <em>substitution property</em> of equality. See e.g. <a href="https://giacomociti.github.io">Giacomo Citi</a>'s <a href="https://giacomociti.github.io/2019/01/02/Abstract-types-are-more-equal.html">response</a> for details. </p> <p> The term <em>functor</em> originates from <a href="https://en.wikipedia.org/wiki/Category_theory">category theory</a>, and I don't claim to be an expert on that topic. It's unclear to me if the underlying concept of <em>equality</em> is explicitly defined in category theory, but it wouldn't surprise me if it is. If that definition involves the substitution property, then the counter-example presented here says nothing about Set as a functor, but rather that <code>DateTimeOffset</code> has unlawful equality behaviour. </p> <p> <strong>Next:</strong> <a href="/2018/10/01/applicative-functors">Applicative functors</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The Test Data Generator applicative functor https://blog.ploeh.dk/2018/11/26/the-test-data-generator-applicative-functor 2018-11-26T07:24:00+00:00 Mark Seemann <div id="post"> <p> <em>Test Data Generator modelled as an applicative functor, with examples in C# and F#.</em> </p> <p> This article is an instalment in <a href="/2018/10/01/applicative-functors">an article series about applicative functors</a>. It shows yet another example of an applicative functor. I tend to think of applicative functors as an abstraction of 'combinations' of values and functions, but this is most evident for lists and other collections. Even so, I think that the intuition also holds for <a href="/2018/10/29/the-maybe-applicative-functor">Maybe as an applicative functor</a> as well as <a href="/2018/11/05/applicative-validation">validation as an applicative functor</a>. This is also the case for the <a href="/2017/09/18/the-test-data-generator-functor">Test Data Generator functor</a>. </p> <h3 id="bc5212f7353f44f793ed20399d359bfe"> Applicative generator in C# <a href="#bc5212f7353f44f793ed20399d359bfe" title="permalink">#</a> </h3> <p> In a <a href="/2017/09/18/the-test-data-generator-functor">previous article</a>, you saw how to implement a Test Data Generator as a functor in C#. The core class is this <code>Generator&lt;T&gt;</code> class: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Generator</span>&lt;<span style="color:#2b91af;">T</span>&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;<span style="color:blue;">readonly</span>&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Random</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;generate; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Generator(<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Random</span>,&nbsp;<span style="color:#2b91af;">T</span>&gt;&nbsp;generate) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(generate&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(generate)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>.generate&nbsp;=&nbsp;generate; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">Generator</span>&lt;<span style="color:#2b91af;">T1</span>&gt;&nbsp;Select&lt;<span style="color:#2b91af;">T1</span>&gt;(<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">T1</span>&gt;&nbsp;f) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(f&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(f)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Random</span>,&nbsp;<span style="color:#2b91af;">T1</span>&gt;&nbsp;newGenerator&nbsp;=&nbsp;r&nbsp;=&gt;&nbsp;f(<span style="color:blue;">this</span>.generate(r)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Generator</span>&lt;<span style="color:#2b91af;">T1</span>&gt;(newGenerator); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Generate(<span style="color:#2b91af;">Random</span>&nbsp;random) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(random&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(random)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">this</span>.generate(random); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre> </p> <p> This is merely repetition from the earlier article. </p> <p> <code>Generator&lt;T&gt;</code> is already a functor, but you can make it an <em>applicative</em> functor by adding an extension method like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Generator</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;Apply&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Generator</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;&nbsp;selectors, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Generator</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;generator) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(selectors&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(selectors)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(generator&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(generator)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Random</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;newGenerator&nbsp;=&nbsp;r&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;f&nbsp;=&nbsp;selectors.Generate(r); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;x&nbsp;=&nbsp;generator.Generate(r); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;f(x); &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Generator</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;(newGenerator); }</pre> </p> <p> The <code>Apply</code> function combines a generator of functions with a generator of values. Given these two generators, it defines a closure over both, and packages that function in a new generator object. </p> <h3 id="58282543d8c4409cb596547e131bf249"> CPR example <a href="#58282543d8c4409cb596547e131bf249" title="permalink">#</a> </h3> <p> When is it interesting to combine a randomly selected function with a randomly generated value? Here's an example. </p> <p> In Denmark, everyone has a <a href="https://en.wikipedia.org/wiki/Personal_identification_number_(Denmark)">personal identification number</a>, in Danish called <em>CPR-nummer</em> (<em>CPR number</em>). It's somewhat comparable to the <a href="https://en.wikipedia.org/wiki/Social_Security_number">U.S. Social Security number</a>, but surely more Orwellian. </p> <p> CPR numbers have a simple format: <code>DDMMYY-SSSS</code>, where the first six digits indicate a person's birth date, and the last four digits are a sequence number. An example could be <code>010203-1234</code>, which indicates a woman born February 1, 1903. Assume that you're writing a system that has to accept CPR numbers as input. You've represented CPR number as a class called <code>CprNumber</code>, and you've already written a parser, but now it turns out that sometimes users enter slightly malformed data. </p> <p> Sometimes users copy and paste CPR numbers from other sources, and occasionally, they inadvertently include a leading or trailing space. Other users forget the dash between the birth date and the sequence number. In other words, sometimes you receive input like <code>"050301-4231 "</code> or <code>"1211993321"</code>. </p> <p> Using your fancy Test Data Generator, you'd like to write a test that verifies that your parser follows <a href="https://en.wikipedia.org/wiki/Robustness_principle">Postel's law</a>: <em>Be conservative in what you send, be liberal in what you accept</em>. </p> <p> Assume that you already have a generator that can produce valid <code>CprNumber</code> objects. One test strategy, then, could be to generate a valid <code>CprNumber</code> object, convert it to a string, slightly taint or mangle that string, and then attempt to parse the tainted string. </p> <p> There's more than one way to taint a valid CPR number string. You could, for example, define three functions like these: </p> <p> <pre><span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;addLeadingSpace&nbsp;=&nbsp;s&nbsp;=&gt;&nbsp;<span style="color:#a31515;">&quot;&nbsp;&quot;</span>&nbsp;+&nbsp;s; <span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;addTrailingSpace&nbsp;=&nbsp;s&nbsp;=&gt;&nbsp;s&nbsp;+&nbsp;<span style="color:#a31515;">&quot;&nbsp;&quot;</span>; <span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;removeDash&nbsp;=&nbsp;s&nbsp;=&gt;&nbsp;s.Replace(<span style="color:#a31515;">&quot;-&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;&quot;</span>);</pre> </p> <p> The first function adds a leading space to a string, the second adds a trailing space, and the third removes all dashes it finds. Can you make a generator out of those functions? </p> <p> Yes, you can, with this common general-purpose method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Generator</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;Elements&lt;<span style="color:#2b91af;">T</span>&gt;(<span style="color:blue;">params</span>&nbsp;<span style="color:#2b91af;">T</span>[]&nbsp;alternatives) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(alternatives&nbsp;==&nbsp;<span style="color:blue;">null</span>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">throw</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:blue;">nameof</span>(alternatives)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Generator</span>&lt;<span style="color:#2b91af;">T</span>&gt;(r&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;index&nbsp;=&nbsp;r.Next(alternatives.Length); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;alternatives[index]; &nbsp;&nbsp;&nbsp;&nbsp;}); }</pre> </p> <p> This generator randomly selects a value from an array of values, with equal probability. It's a common method, because you'll find equivalent functions in <a href="https://en.wikipedia.org/wiki/QuickCheck">QuickCheck</a>, <a href="https://fscheck.github.io/FsCheck">FsCheck</a>, and <a href="https://github.com/hedgehogqa/fsharp-hedgehog">Hedgehog</a> (where it's called <code>item</code>). </p> <p> You can write the entire test like this: </p> <p> <pre>[<span style="color:#2b91af;">Fact</span>] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;ParseCorrectlyHandlesTaintedInput() { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;addLeadingSpace&nbsp;=&nbsp;s&nbsp;=&gt;&nbsp;<span style="color:#a31515;">&quot;&nbsp;&quot;</span>&nbsp;+&nbsp;s; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;addTrailingSpace&nbsp;=&nbsp;s&nbsp;=&gt;&nbsp;s&nbsp;+&nbsp;<span style="color:#a31515;">&quot;&nbsp;&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;removeDash&nbsp;=&nbsp;s&nbsp;=&gt;&nbsp;s.Replace(<span style="color:#a31515;">&quot;-&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Generator</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;&gt;&nbsp;tainters&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Gen</span>.Elements( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;addLeadingSpace, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;addTrailingSpace, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;removeDash, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s&nbsp;=&gt;&nbsp;addLeadingSpace(removeDash(s)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s&nbsp;=&gt;&nbsp;addTrailingSpace(addLeadingSpace(s))); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;g&nbsp;=&nbsp;tainters.Apply(<span style="color:#2b91af;">Gen</span>.CprNumber.Select(n&nbsp;=&gt;&nbsp;n.ToString())); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;rnd&nbsp;=&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Random</span>(); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;candidate&nbsp;=&nbsp;g.Generate(rnd); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">CprNumber</span>&nbsp;dummy; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:#2b91af;">CprNumber</span>.TryParse(candidate,&nbsp;<span style="color:blue;">out</span>&nbsp;dummy); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.True(actual); }</pre> </p> <p> You first define the three 'tainting' functions, and then create the <code>tainters</code> generator. Notice that this generator not only contains the three functions, but also combinations of these functions. I didn't include all possible combinations of functions, but in <a href="/2018/10/22/applicative-combinations-of-functions">an earlier article</a>, you saw how to do just that. </p> <p> <code>Gen.CprNumber</code> is a <code>Generator&lt;CprNumber&gt;</code>, while you actually need a <code>Generator&lt;string&gt;</code>. Since <code>Generator&lt;T&gt;</code> is a functor, you can easily map <code>Gen.CprNumber</code> with its <code>Select</code> method. </p> <p> You now have a <code>Generator&lt;Func&lt;string, string&gt;&gt;</code> (<code>tainters</code>) and a <code>Generator&lt;string&gt;</code>, so you can combine them using <code>Apply</code>. The result <code>g</code> is another <code>Generator&lt;string&gt;</code> that generates 'tainted', but still valid, CPR string representations. </p> <p> In order to keep the example as simple as possible, the <em>Arrange</em> and <em>Assert</em> phases of the test only checks if <code>CprNumber.TryParse</code> returns <code>true</code>. A better test would also verify that the resulting <code>CprNumber</code> value was correct, but in order to do that, you need to keep the originally generated <code>CprNumber</code> object around so that you can compare this expected value to the actual value. This is possible, but complicates the code, so I left it out of the example. </p> <h3 id="f3d0db787eaf4a018a593daaa78f9e17"> F# Hedgehog <a href="#f3d0db787eaf4a018a593daaa78f9e17" title="permalink">#</a> </h3> <p> You can translate the above unit test to F#, using <a href="https://github.com/hedgehogqa/fsharp-hedgehog">Hedgehog</a> as a property-based testing library. Another option would be <a href="https://fscheck.github.io/FsCheck">FsCheck</a>. Without further ado, I present to you the test: </p> <p> <pre>[&lt;<span style="color:teal;">Fact</span>&gt;] <span style="color:blue;">let</span>&nbsp;<span style="color:navy;">``Correctly&nbsp;parses&nbsp;tainted&nbsp;text``</span>&nbsp;()&nbsp;=&nbsp;<span style="color:teal;">Property</span>.<span style="color:navy;">check</span>&nbsp;&lt;|&nbsp;<span style="color:blue;">property</span>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">removeDash</span>&nbsp;(s&nbsp;:&nbsp;<span style="color:teal;">string</span>)&nbsp;=&nbsp;s.<span style="color:navy;">Replace</span>&nbsp;(<span style="color:#a31515;">&quot;-&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;&quot;</span>) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;candidate&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:teal;">Gen</span>.<span style="color:navy;">item</span>&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">sprintf</span>&nbsp;<span style="color:#a31515;">&quot;&nbsp;</span><span style="color:teal;">%s</span><span style="color:#a31515;">&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">sprintf</span>&nbsp;<span style="color:#a31515;">&quot;</span><span style="color:teal;">%s</span><span style="color:#a31515;">&nbsp;&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">removeDash</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">removeDash</span>&nbsp;&gt;&gt;&nbsp;<span style="color:navy;">sprintf</span>&nbsp;<span style="color:#a31515;">&quot;&nbsp;</span><span style="color:teal;">%s</span><span style="color:#a31515;">&quot;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">sprintf</span>&nbsp;<span style="color:#a31515;">&quot;&nbsp;</span><span style="color:teal;">%s</span><span style="color:#a31515;">&nbsp;&quot;</span>] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;*&gt;&nbsp;<span style="color:teal;">Gen</span>.<span style="color:navy;">map</span>&nbsp;<span style="color:navy;">string</span>&nbsp;<span style="color:teal;">Gen</span>.cprNumber &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;actual&nbsp;=&nbsp;<span style="color:teal;">Cpr</span>.<span style="color:navy;">tryParse</span>&nbsp;candidate &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">test</span>&nbsp;<span style="background:yellow;">&lt;@&nbsp;</span><span style="background:yellow;">actual</span><span style="background:yellow;">&nbsp;</span><span style="background:yellow;">|&gt;</span><span style="background:yellow;">&nbsp;</span><span style="color:teal;background:yellow;">Option</span><span style="background:yellow;">.</span><span style="color:navy;background:yellow;">isSome</span><span style="background:yellow;">&nbsp;@&gt;</span>&nbsp;}</pre> </p> <p> As I already mentioned in passing, Hedgehog's equivalent to the above <code>Elements</code> method is called <code>Gen.item</code>. Here, you see the same five functions as above passed in a list. Thanks to partial application and type inference, an expression like <code>sprintf " %s"</code> is already a function of the type <code>string -&gt; string</code>, as is <code>removeDash</code>. For the last of the five functions, I found it easier to write (and read) <code>sprintf " %s "</code> instead of <code>sprintf " %s" &gt;&gt; sprintf "%s "</code>. </p> <p> Equivalent to the C# example, <code>Gen.cprNumber</code> is a <code>Gen&lt;CprNumber&gt;</code>, so mapping it with the built-in <code>string</code> function translates it to a <code>Gen&lt;string&gt;</code>. </p> <p> Hedgehog already includes an <code>&lt;*&gt;</code> operator; <code>Gen</code> is an applicative functor. </p> <h3 id="f7c420f0d4e641a384749187d7bbf217"> Summary <a href="#f7c420f0d4e641a384749187d7bbf217" title="permalink">#</a> </h3> <p> Applicative functors are fairly common. I find it intuitive to think of them as an abstraction of how to make combinations of things. Modelling a Test Data Generator as an applicative functor enables you to create random combinations of functions and values. </p> <p> While working on the Hedgehog example, I discovered another great use of <code>option</code> as an applicative functor. You can see this in the next article. </p> <p> <strong>Next: </strong> <a href="/2018/12/10/danish-cpr-numbers-in-f">Danish CPR numbers in F#</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Functional architecture: a definition https://blog.ploeh.dk/2018/11/19/functional-architecture-a-definition 2018-11-19T09:44:00+00:00 Mark Seemann <div id="post"> <p> <em>How do you know whether your software architecture follows good functional programming practices? Here's a way to tell.</em> </p> <p> Over the years, I've written articles on functional architecture, including <a href="/2016/03/18/functional-architecture-is-ports-and-adapters">Functional architecture is Ports and Adapters</a>, given <a href="https://vimeo.com/180287057">conference talks</a>, and even produced <a href="https://blog.ploeh.dk/functional-architecture-with-fsharp">a Pluralsight course</a> on the topic. How should we define <em>functional architecture</em>, though? </p> <p> People sometimes ask me about their <a href="https://fsharp.org">F#</a> code: <em>How do I know that my F# code is functional?</em> </p> <p> Please permit me a little detour before I answer that question. </p> <h3 id="f752f048f6ef445d8625f1710317620b"> What's the definition of object-oriented design? <a href="#f752f048f6ef445d8625f1710317620b" title="permalink">#</a> </h3> <p> Object-oriented design (OOD) has been around for decades; at least since the nineteen-sixties. Sometimes people get into discussions about whether or not a particular design is good object-oriented design. I know, since I've found myself in such discussions more than once. </p> <p> These discussions usually die out without resolution, because it seems that no-one can provide a sufficiently rigorous definition of OOD that enables people to determine an outcome. One thing's certain, though, so I'd like to posit this corollary to <a href="https://en.wikipedia.org/wiki/Godwin%27s_law">Godwin's law</a>: <blockquote> As a discussion about OOD grows longer, the probability of a comparison involving Alan Kay approaches 1. </blockquote> Not that I, in any way, wish to suggest any logical relationship between <a href="https://en.wikipedia.org/wiki/Alan_Kay">Alan Kay</a> and Hitler, but in a discussion about OOD, sooner or later someone states: <blockquote> "That's not what Alan Kay had in mind!" </blockquote> That may be true, even. </p> <p> My problem with that assertion is that I've never been able to figure out exactly what Alan Kay had in mind. It's something that involves message-passing and <a href="https://en.wikipedia.org/wiki/Smalltalk">Smalltalk</a>, and conceivably, the best modern example of this style of programming might be <a href="https://www.erlang.org">Erlang</a> (often, ironically, touted as a functional programming language). </p> <p> This doesn't seem to be a good basis for determining whether or not something is object-oriented. </p> <p> In any case, despite what Alan Kay had in mind, that wasn't the object-oriented programming we got. While <a href="https://en.wikipedia.org/wiki/Eiffel_(programming_language)">Eiffel</a> is in many ways a strange programming language, the philosophy of OOD presented in <a href="http://amzn.to/1claOin">Object-Oriented Software Construction</a> feels, to me, like something from which <a href="https://www.java.com">Java</a> could develop. </p> <p> I'm not aware of the detailed history of Java, but the spirit of the language seems more compatible with Bertrand Meyer's vision than with Alan Kay's. </p> <p> Subsequently, C# would hardly look the way it does had it not been for Java. </p> <p> The OOD we got wasn't the OOD originally envisioned. To make matters worse, the OOD we did get seems to be driven by unclear principles. Yes, there's the idea about encapsulation, but while Meyer had some very specific ideas about design-by-contract, that was the distinguishing trait of his vision that <em>didn't</em> make the transition to Java or C#. </p> <p> It's not clear what OOD is, but I think we can do better when it comes to functional programming (FP). </p> <h3 id="305ccccf2de84354bdb68b5b80d34fc9"> Referential transparency <a href="#305ccccf2de84354bdb68b5b80d34fc9" title="permalink">#</a> </h3> <p> It's possible to pinpoint what FP is to a degree not possible with OOD. Some people may be uncomfortable with the following definition; I don't claim that this is a generally accepted definition. It does have, however, the advantage that it's precise and supports <a href="https://en.wikipedia.org/wiki/Falsifiability">falsification</a>. </p> <p> The foundation of FP is <a href="https://en.wikipedia.org/wiki/Referential_transparency">referential transparency</a>. It's the idea that, for an expression, the left- and right-hand sides of the equal sign are truly equal: </p> <p> <pre>two = 1 + 1</pre> </p> <p> In <a href="https://www.haskell.org">Haskell</a>, this is enforced by the compiler. The <code>=</code> operator truly implies equality. To be clear, this isn't the case in C#: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;two&nbsp;=&nbsp;1&nbsp;+&nbsp;1;</pre> </p> <p> In C#, Java, and other imperative languages, the <code>=</code> implies <em>assignment</em>, not equality. Here, <code>two</code> can change, despite the absurdity of the claim. </p> <p> When code is referentially transparent, then you can substitute the expression on the right-hand side with the symbol on the left-hand side. This seems obvious when we consider addition of two numbers, but becomes less clear when we consider function invocation: </p> <p> <pre>i = findBestNumber [42, 1337, 2112, 90125]</pre> </p> <p> In Haskell, functions are referentially transparent. You don't know exactly what <code>findBestNumber</code> does, but you do know that you can substitute <code>i</code> with <code>findBestNumber [42, 1337, 2112, 90125]</code>, or vice versa. </p> <p> In order for a function to be referentially transparent (also known as a <a href="https://en.wikipedia.org/wiki/Pure_function">pure function</a>), it must have two properties: <ul> <li>It must always return the same output for the same input. We call this quality <em>determinism</em>.</li> <li>It must have no side effects.</li> </ul> As far as I can tell, all else in FP follows from this definition. For example, values must be immutable, because if they aren't, you could mutate them, and that would count as a side effect. </p> <p> The reason I prefer this definition is that it supports falsification. You can assert that a function or value is pure; all it takes is a single counter-example to prove that it's not. A counter-example can be either an input value that doesn't always produce the same return value, or a function call that produces a side effect. </p> <p> I'm not aware of any other definition that offers similar decision power. </p> <h3 id="43505c00cc25416db2b47a89912fd731"> IO <a href="#43505c00cc25416db2b47a89912fd731" title="permalink">#</a> </h3> <p> All software produces side effects: Changing a pixel on a monitor is a side effect. Writing a byte to disk is a side effect. Transmitting a bit over a network is a side effect. It seems that it'd be impossible to interact with pure functions, and indeed, it is, without some sort of affordance for impurity. </p> <p> Haskell resolves this problem with the <code>IO</code> monad, but the purpose of this article isn't to serve as an introduction to Haskell, monads, or <code>IO</code>. The point is only that in FP, you need some sort of 'wormhole' that will enable you to interact with the real world. There's no way around that, but logically, the rules still apply. Pure functions must stay deterministic and free of side effects. </p> <p> It follows that you have two groups of operations: impure activities and pure functions. </p> <p> <img src="/content/binary/impure-actions-pure-functions-no-arrows.png" alt="Two sets: the set of impure activities and the set of pure functions."> </p> <p> While there are rules for pure functions, those rules still allow for interaction. One pure function can call another pure function. Such an interaction doesn't change the properties of any of those functions. Both caller and callee remain side-effect-free and deterministic. </p> <p> <img src="/content/binary/impure-actions-pure-functions-pure-arrows.png" alt="Set of impure activities and set of pure functions. The pure functions now have arrows between them."> </p> <p> The impure activities can also interact. No rules apply to them: </p> <p> <img src="/content/binary/impure-actions-pure-functions-impure-and-pure-arrows.png" alt="Sets of impure activities and pure functions. Both sets now have internal arrows."> </p> <p> Finally, since no rules apply to impure activities, they can invoke pure functions: </p> <p> <img src="/content/binary/impure-actions-pure-functions-all-valid-arrows.png" alt="Sets of impure activities and pure functions, now also with arrows going from impure to pure."> </p> <p> Impure activities are unbound by rules, so they can do anything they need to do, including painting pixels, writing to files, or calling pure functions. A pure function is deterministic and has no side effects. Those properties don't change just because the result is subsequently displayed on a screen. </p> <p> The fourth combination of arrows is, however, illegal. <blockquote> A pure function can't invoke an impure activity. </blockquote> If it did, it would either transitively produce a side effect or non-deterministic behaviour. </p> <p> This is the rule of functional architecture. You can also explain it with a table: <table> <col> <col> <colgroup span="2"></colgroup> <thead> <tr> <td colspan="2" rowspan="2"></td> <th colspan="2" scope="colgroup">Callee</th> </tr> <tr> <th scope="col">Impure</th> <th scope="col">Pure</th> </tr> </thead> <tbody> <tr> <th rowspan="2" scope="rowgroup">Caller</th> <th scope="row">Impure</th> <td><span style="color:green">Valid</span></td> <td><span style="color:green">Valid</span></td> </tr> <tr> <th scope="row">Pure</th> <td><span style="color:red">Invalid</span></td> <td><span style="color:green">Valid</span></td> </tr> </tbody> </table> Let's call the above rule the <em>functional interaction law</em>: a pure function can't invoke an impure activity. A functional architecture, then, is a code base that obeys that law, and has a significant portion of pure code. </p> <p> Clearly, you can trivially obey the functional interaction law by writing exclusively impure code. In a sense, this is what you do by default in imperative programming languages. If you're familiar with Haskell, imagine writing an entire program in <code>IO</code>. That would be possible, but pointless. </p> <p> Thus, we need to add the qualifier that a significant part of the code base should consist of pure code. How much? The more, the better. Subjectively, I'd say significantly more than half the code base should be pure. I'm concerned, though, that stating a hard limit is as useful here <a href="/2015/11/16/code-coverage-is-a-useless-target-measure">as it is for code coverage</a>. </p> <h3 id="1009a90ea922424285e5f9a4bb30524e"> Tooling <a href="#1009a90ea922424285e5f9a4bb30524e" title="permalink">#</a> </h3> <p> How do you verify that you obey the functional interaction law? Unfortunately, in most languages the answer is that this requires painstaking analysis. This can be surprisingly tricky to get right. Consider this realistic F# example: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;createEmailNotification&nbsp;templates&nbsp;msg&nbsp;(user&nbsp;:&nbsp;UserEmailData)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;{&nbsp;SubjectLine&nbsp;=&nbsp;subjectTemplate;&nbsp;Content&nbsp;=&nbsp;contentTemplate&nbsp;}&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;templates &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Map.tryFind&nbsp;user.Localization &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&gt;&nbsp;Option.defaultValue&nbsp;(Map.find&nbsp;Localizations.english&nbsp;templates) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;r&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Templating.append &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(Templating.replacementOfEnvelope&nbsp;msg) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(Templating.replacementOfFlatRecord&nbsp;user) &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;subject&nbsp;=&nbsp;Templating.run&nbsp;subjectTemplate&nbsp;r &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;content&nbsp;=&nbsp;Templating.run&nbsp;contentTemplate&nbsp;r &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RecipientUserId&nbsp;=&nbsp;user.UserId &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EmailAddress&nbsp;=&nbsp;user.EmailAddress &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NotificationSubjectLine&nbsp;=&nbsp;subject &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NotificationText&nbsp;=&nbsp;content &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CreatedDate&nbsp;=&nbsp;DateTime.UtcNow &nbsp;&nbsp;&nbsp;&nbsp;}</pre> </p> <p> Is this a pure function? </p> <p> You may protest that this isn't a fair question, because you don't know what, say, <code>Templating.replacementOfFlatRecord</code> does, but that turns out to be irrelevant. The presence of <code>DateTime.UtcNow</code> makes the entire function impure, because getting the current date and time is non-deterministic. This trait is transitive, which means that any code that calls <code>createEmailNotification</code> is also going to be impure. </p> <p> That means that the purity of an expression like the following easily becomes obscure. </p> <p> <pre><span style="color:blue;">let</span>&nbsp;emailMessages&nbsp;=&nbsp;specificUsers&nbsp;|&gt;&nbsp;Seq.map&nbsp;(createEmailNotification&nbsp;templates&nbsp;msg)</pre> </p> <p> Is this a pure expression? In this case, we've just established that <code>createEmailNotification</code> is impure, so that wasn't hard to answer. The problem, however, is that the burden is on you, the code reader, to remember which functions are pure, and which ones aren't. In a large code base, this soon becomes a formidable endeavour. </p> <p> It'd be nice if there was a tool that could automatically check the functional interaction law. </p> <p> This is where many people in the functional programming community become uncomfortable about this definition of functional architecture. The only tools that I'm aware of that enforce the functional interaction law are a few programming languages, most notably Haskell (others exist, too). </p> <p> Haskell enforces the functional interaction law via its <code>IO</code> type. You can't use an <code>IO</code> value from within a pure function (a function that doesn't return <code>IO</code>). If you try, your code doesn't compile. </p> <p> I've personally used Haskell repeatedly to understand the limits of functional architecture, for example to establish that <a href="/2017/01/30/partial-application-is-dependency-injection">Dependency Injection isn't functional</a> because it makes everything impure. </p> <p> The overall lack of tooling, however, may make people uncomfortable, because it means that most so-called functional languages (e.g. F#, Erlang, <a href="https://elixir-lang.org/">Elixir</a>, and <a href="https://clojure.org">Clojure</a>) offer no support for validating or enforcing functional architecture. </p> <p> My own experience with writing entire applications in F# is that I frequently, inadvertently violate the functional interaction law somewhere deep in the bowels of my code. </p> <h3 id="8e33b2ffd51f46f18e38c5add37f1728"> Conclusion <a href="#8e33b2ffd51f46f18e38c5add37f1728" title="permalink">#</a> </h3> <p> What's functional architecture? I propose that it's code that obeys the functional architecture law, and that is made up of a significant portion of pure functions. </p> <p> This is a narrow definition. It excludes a lot of code bases that could easily be considered 'functional enough'. By the definition, I don't intend to denigrate fine programming languages like F#, Clojure, Erlang, etcetera. I personally find it a joy to write in F#, which is my default language choice for .NET programming. </p> <p> My motivation for offering this definition, albeit restrictive, is to avoid the OOD situation where it seems entirely subjective whether or not something is object-oriented. With the functional interaction law, we may conclude that most (non-Haskell) programs are probably not 'really' functional, but at least we establish an falsifiable ideal to strive for. </p> <p> This would enable us to look at, say, an F# code base and start discussing <em>how close to the ideal is it?</em> </p> <p> Ultimately, functional architecture isn't a goal in itself. It's a means to achieve an objective, such as a sustainable code base. I find that FP helps me keep a code base sustainable, but often, 'functional enough' is sufficient to accomplish that. </p> </div> <div id="comments"> <hr /> <h2 id="comments-header">Comments</h2> <div class="comment" id="ccf908a42b644b8b9bb2bf6bcbc25052"> <div class="comment-author"> <a href="https://github.com/MaxKot">Max Kiselev</a> </div> <div class="comment-content"> <p> Good idea of basing the definition on falsifiability! </p> <p> The createEmailNotification example makes me wonder though. If it is not a functional design, then what design it actually is? I mean it has to be some design and it does not looks like object-oriented or procedural one. </p> </div> <div class="comment-date">2018-11-20 21:36 UTC</div> </div> <div class="comment" id="6a4ca5d3da3e4a63a413982f589c384c"> <div class="comment-author"><a href="/">Mark Seemann</a></div> <div class="comment-content"> <p> Max, thank you for writing. I'm not sure whether the assertion that it <em>has</em> to be some design or other is axiomatically obvious, but I suppose that we humans have an innate need to categorise things. </p> <p> Don Syme calls F# a <em>functional-first</em> language, and that epithet could easily apply to that style of programming as well. Other options could be <em>near-functional</em>, or perhaps, if we're in a slightly more academic mood, <em>quasi-functional</em>. </p> <p> In daily use, we'd probably still call code like that <em>functional</em>, and I don't think it'll cause much confusion. </p> <p> If I remember the history of programming correctly, the first attempts at functional programming didn't focus on referential transparency, but simply on the concept of functions as first-class language features, including lambda expressions and higher-order functions. The little I know of <a href="https://en.wikipedia.org/wiki/Lisp_(programming_language)">Lisp</a> corroborates that view on the history of functional programming. </p> <p> Only later (I think) did languages appear that make compile-time distinction between pure and impure code. </p> <p> My purpose with this article wasn't to exclude a large number of languages or code bases from being functional, but just to offer a falsifiable test that we can use for evaluation purposes, if we're ever in doubt. This is something that I feel is sorely missing from the object-oriented debate, and while I can't think of a way to remedy the OOD situation, I hope that the present definition of functional architecture presents firmer ground upon which to have a debate. </p> </div> <div class="comment-date">2018-11-21 6:42 UTC</div> </div> <div class="comment" id="902887b4a1954daf8be09e35ce685fbb"> <div class="comment-author"><a href="https://github.com/wachulski">Marcin Wachulski</a></div> <div class="comment-content"> <p> Thank you for your definition that forms good grounds for reasoning about functional property of code. I remember some people say that even C# is functional as it allows for <em>delegates</em>. Several thoughts: </p> <p> 1. Remember <a href="https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.contracts.pureattribute?view=netcore-2.1">[Pure]</a> attribute and CodeContracts? It aided the purpose of defensive programming to hold pre/postconditions and invariants. I have this impression that both functional purity and defensive programming help us find ad-hoc quasi-mathematical proofs of code correctness after we load a codebase to our heads. It's way easier then to presume how it works and make changes. Of course it's not mathematical in the strict sense, but still - we tend to trust the code more (e.g. threading). I'm pretty sure unit tests belong to this family too. </p> <p> 2. Isn't the purity concept anywhere close to gateways (code that crosses the boundaries of program determinism control zone, e.g. IO, time, volatile memory)? It's especially evident while refactoring legacy code to make it unit testable. We often extract out infrastructure dependent parts (impure activities, e.g. DateTime.Now) for the other be deterministic (pure) and hence - testable. </p> <p> 3. Can the whole program/system be as pure as this: <pre>INPUT -> PURE_CODE -> OUTPUT</pre> I'm afraid not as it'd mean the pure code needed to know all the required input data upfront. That's impossible most of times. So, when it comes to measures, I'd argue that the number of pure code lines is enough to tell how pure the codebase is. I'd accompany this with e.g. percentile distribution of pure chunk length (functions/blocks of contingent im/purity etc.). E.g. I'd personally favour 1) over 2) in the following: <pre> 1. INPUT -> 3 x LONG_PURE_CHUNK + 2 x LONG_IMPURE_CHUNK -> OUTPUT 2. INPUT -> 10 x SHORT_PURE_CHUNK + 7 x SHORT_IMPURE_CHUNK -> OUTPUT </pre> </p> <p> 4. I'd love to see such tools too :) I believe the purity concept does not pertain only to FP nor OO and is related to the early foundational days of computer programming with mathematical proofs they had. </p> </div> <div class="comment-date">2018-11-23 10:51 UTC</div> </div> <div class="comment" id="82b14ead068b42698186710ba7bcf145"> <div class="comment-author"><a href="/">Mark Seemann</a></div> <div class="comment-content"> <p> Marcin, thank you for writing. To be clear, you can perform side effects or non-deterministic behaviour in C# delegates, so delegates aren't guaranteed to be referentially transparent. In fact, <a href="/2018/01/22/function-isomorphisms">delegates, and particularly closures, are isomorphic to objects</a>. In C#, <a href="/2014/03/10/solid-the-next-step-is-functional">they even compile to classes</a>. </p> <p> I'm aware that some people think that they're doing functional programming when they use lambda expressions in C#, but I disagree. </p> <p> I'll see if I can address your other comments below. </p> <h3 id="c085eef3102d437ba9c6cbe3363b8976"> Re: 1. <a href="#c085eef3102d437ba9c6cbe3363b8976" title="permalink">#</a> </h3> <p> Some functions are certainly ad-hoc, while <a href="/2017/10/04/from-design-patterns-to-category-theory">others are grounded in mathematics</a>. I agree that pure functions 'fit better in your brain', mostly because it's clear that the only stimuli that can affect the outcome of the function is the input. Granted, if we imagine a concrete, ad-hoc function that takes 23 function arguments, we might still consider it bad code. Small functions, though, tend to be easy to reason about. </p> <p> I've previously written about the <a href="/2016/02/10/types-properties-software">relationship between types and tests</a>, and I believe that there's some sort of linearity in that relationship. The better the type system, the fewer tests you need. The weaker the type system, the more tests you need. It's no wonder that the unit testing culture related to languages like Ruby seems stronger than in the Haskell community. </p> <h3 id="405184fce774458ba8d3484c2580f322"> Re: 2. <a href="#405184fce774458ba8d3484c2580f322" title="permalink">#</a> </h3> <p> In my experience, most people make legacy code more testable by introducing some variation of Dependency Injection. This may enable you to control some otherwise non-deterministic behaviour from tests, but <a href="/2017/01/27/from-dependency-injection-to-dependency-rejection">it doesn't make the design more functional</a>. Those types of dependencies (e.g. on <code>DateTime.Now</code>) are inherently impure, and therefore they make everything that invokes them impure as well. The above <em>functional interaction law</em> excludes such a design from being considered functional. </p> <p> <a href="/2015/05/07/functional-design-is-intrinsically-testable">Functional code, on the other hand, is intrinsically testable</a>. Watch out for a future series of articles that show how to move an object-oriented architecture towards something both more functional, more sustainable, and more testable. </p> <h3 id="5201a19bec7b413ebb6e7ca3e2dfbc81"> Re: 3. <a href="#5201a19bec7b413ebb6e7ca3e2dfbc81" title="permalink">#</a> </h3> <p> A command-line utility <em>could</em> be as pure as you suggest, but most other programs will need to at least <ol> <li>load some more data from impure sources, such as files, databases, or the current time,</li> <li>run some pure functions,</li> <li>output the results to some impure destinations, such as files, databases, UI, email, and so on.</li> </ol> I call this type of architecture a <a href="/2017/02/02/dependency-rejection">pure-impure-pure sandwich</a>, and you can <a href="/2017/07/10/pure-interactions">often, but not always, structure your application code in that way</a>. </p> <h3 id="e8befe231edf474899477eb39074d8d0"> Re: 4. <a href="#e8befe231edf474899477eb39074d8d0" title="permalink">#</a> </h3> <p> I'm not sure I understand what you mean by that comment, but the mathematical proofs about computability pre-date computer programming. Gödel's work on recursive functions is from 1933, Church's work on lambda calculus is from 1936, and Turing's paper <em>On Computable Numbers, with an Application to the Entscheidungsproblem</em> is from later in 1936. Turing and Church later showed that <a href="https://en.wikipedia.org/wiki/Church%E2%80%93Turing_thesis">all three definitions of computability are equivalent</a>. </p> <p> I don't know much about Gödel's work, but lambda calculus is defined entirely on the foundation of <em>functions</em>, while the Turing machines described in Turing's paper have nothing to do with functions in the mathematical sense. </p> <p> Mathematical functions are, however, referentially transparent. They're also <em>total</em>, meaning that they always return a result for any input in the function's domain. Due to the halting problem, a Turing-complete language can't guarantee that all functions are total. </p> </div> <div class="comment-date">2018-11-24 9:04 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. What to test and not to test https://blog.ploeh.dk/2018/11/12/what-to-test-and-not-to-test 2018-11-12T07:45:00+00:00 Mark Seemann <div id="post"> <p> <em>Should you unit test everything? Hardly surprising, the answer is that It Depends™. This article outlines some of the circumstances you might consider.</em> </p> <p> Some years ago, I, somewhat to my own surprise, found myself on the wrong side of a controversy about whether one should <a href="/2013/03/08/test-trivial-code">test trivial code</a>. The context was a discussion about Test-Driven Development (TDD), and for reasons that I still stand behind today, I argued that you should test all code, including trivial code, such as property getters. </p> <p> Most of the 'TDD community' reacted quite negatively to that article, some in not-so-nice ways. Some people reacted, I believe, based on their dislike of the conclusion, without responding to my arguments. Others, however, gave reasoned rebuttals. When people like <a href="https://lostechies.com/derickbailey/2013/03/11/on-testing-trivia-code/">Derick Bailey</a> and <a href="https://rendlelabs.com/blog/dont-unit-test-trivial-code">Mark Rendle disagree with me</a>, in a reasoned matter, even, I consider that a good reason to revisit my thinking. </p> <p> Could I have been wrong? That certainly wouldn't be the first time, but even re-reading the article today, I find my logic sound. Yet, I've substantially nuanced my position since then. </p> <p> It took me some time to understand how I could disagree so radically with people I respect. It didn't take me five years, though, but while I've been at peace with the apparent conflict for years, I've never written a coherent description of my current understanding of this problem space. This article is my attempt to remedy that omission. </p> <h3 id="eb43b60a35394df6b812514ac794cefe"> Context matters <a href="#eb43b60a35394df6b812514ac794cefe" title="permalink">#</a> </h3> <p> Whenever you consult an expert about how to address a problem, you'll invariably get the answer that <em>it depends</em>. I'd suggest that if you don't get that answer, the person is probably not an expert, after all. A useful expert will also be able to tell you on <em>what</em> 'it' depends. </p> <p> In an abstract sense, what 'it' depends on is <em>the context</em>. </p> <p> I wrote my original piece from a particular context. Part of that context is explicitly present in the article, but another part is entirely implicit. People read the article from within their own contexts, which in many cases turned out to be incongruent with mine. No wonder people disagreed. </p> <h3 id="9181a2688ce042d882cfe73a3bfed57e"> Watching the wildlife <a href="#9181a2688ce042d882cfe73a3bfed57e" title="permalink">#</a> </h3> <p> My context at that time was that I had some success with <a href="https://github.com/AutoFixture/AutoFixture">AutoFixture</a>, an open source library, which is what I consider <a href="/2012/12/18/RangersandZookeepers">wildlife</a> software. Once you've released a version of the software, you have no control of where it's installed, or how it's used. </p> <p> This means that backwards compatibility becomes important. If I broke something, I would inconvenience the users of my software. Making sure that compatibility didn't break became one of my highest priorities. I used unit tests for regression tests, and I did, indeed, test the entire public API of AutoFixture, to make sure that no breaking changes were introduced. </p> <p> That was my implicit context. Read in that light, my dogmatic insistence on testing everything hopefully makes a little more sense. </p> <p> Does that mean that my conclusion transfers to other circumstances? No, of course it doesn't. If you're developing and maintaining <a href="/2012/12/18/RangersandZookeepers">zoo software</a>, breaking changes are of less concern. From that perspective, my article could easily look like the creation of an unhinged mind. </p> <h3 id="a15643275c1a4d7ab484cacafa53b7da"> The purpose of tests <a href="#a15643275c1a4d7ab484cacafa53b7da" title="permalink">#</a> </h3> <p> In order to figure out what to test, and what not to test, you should ask yourself the question: <em>what's the purpose of testing?</em> </p> <p> At first glance, that may seem like an inane question, but there's actually more than one purpose of a unit test. When doing TDD, the purpose of a test is to provide feedback about the API you're developing. <a href="/2011/11/10/TDDimprovesreusability">A unit test is the first client of the production API</a>. If a test is difficult to write, the production API is difficult to use. More on TDD later, though. </p> <p> You may say that another purpose of automated tests is that they prevent errors. That's not the case, though. Automated tests prevent <em>regressions</em>. </p> <p> If you wrote the correct test, your test suite may also help to prevent errors, but a unit test is only as good as the programmer who wrote it. You could have made a mistake when you wrote the test. Or perhaps you misunderstood the specification of what you were trying to implement. <a href="/2013/04/02/why-trust-tests">Why do you even trust tests?</a> </p> <h3 id="d5b17ea45ed741899002d50fbb3e98c9"> The cost of regressions <a href="#d5b17ea45ed741899002d50fbb3e98c9" title="permalink">#</a> </h3> <p> Why do you want to prevent regressions? Because they're costly? </p> <p> Based on the little risk management I know about, you operate with two dimensions of risk: the impact of an event, should it occur, and the probability that the event occurs. </p> <p> Should we all be worried that an asteroid will hit the Earth and wipe out most life? The impact of such an event is truly catastrophic, yet the probability is diminishingly small, so the <em>risk</em> is insignificant. The risk of going for a drive in a car is much higher. </p> <p> How do you reduce risk? You either decrease the probability that the adverse event will happen, or you reduce the impact of it, should it happen. </p> <p> <a href="http://www.higherorderlogic.com/">Steve Freeman</a> once wrote a nice article about the distinction between fail-safe software, and software that could safely fail. Unfortunately, that article seems to have disappeared from the internet. The point, however, was that with unit tests, we attempt to make our software fail-safe. The unit tests act as a gate that prevents bad versions of the software from being released. That's not a bad strategy for managing risk, but only half of the strategies available. </p> <p> For example, <a href="http://amzn.to/1axt5YA">Continuous Delivery</a> describes how you can use <a href="https://martinfowler.com/bliki/CanaryRelease.html">Canary Releases</a> and automated rollbacks to reduce the impact of errors. That's what Steve Freeman called <em>safe fail</em>. </p> <p> I apologise for this detour around risk management, but I think that it's important that you make an explicit decision about automated testing. You can use unit tests to prevent regressions. What's the impact of an error in production, though? </p> <p> This depends on the type of software you're developing. When considering alternatives, I often envision the various options as inhabiting a continuum: </p> <p> <img src="/content/binary/test-coverage-continuum.png" alt="Test coverage continuum; no coverage to the left, maximum coverage to the right."> </p> <p> For some types of software, an error 'in production' could be fatal. This would be the case for guidance software for <a href="https://en.wikipedia.org/wiki/Voyager_1">Voyager 1</a>, <a href="https://en.wikipedia.org/wiki/Voyager_2">2</a>, other guidance software, software for medical devices, and so on. If you deploy a defect to Voyager 2, you've probably lost the craft for ever. </p> <p> (I'd be surprised if the Voyager guidance software is actually covered by unit tests, but I'd expect that other quality assurance checks are in place. For comparison, the space shuttle software development process has been appraised at CMMI level 5.) </p> <p> On the other side of the continuum, as a software developer, you probably write small ad-hoc development tools for your own use. For example, a few years ago I did a lot of <a href="https://en.wikipedia.org/wiki/Representational_state_transfer">REST</a> API development, and many of the APIs I worked with required <a href="https://en.wikipedia.org/wiki/OAuth">OAuth</a> authentication. I wrote a little command-line program that I could use to log on to an internal system and exchange that to a token. I don't think that I wrote any tests for that program. If there were problems with it, I'd just open the source code and fix the problem. Errors were cheap in that situation. </p> <p> Most software probably falls somewhere in the middle of those extremes. The cost of errors in wildlife software is probably higher than it is for zoo software, but most software can get by with less coverage than <em>everything</em>. </p> <h3 id="7db262526ca2459b813c0fda4f1e6999"> Cyclomatic complexity <a href="#7db262526ca2459b813c0fda4f1e6999" title="permalink">#</a> </h3> <p> How do you know that your software works? You test it. If you want to automate your testing efforts, you write unit tests... but a unit test suite is software. How do you know that your tests work? Is it going to be <a href="https://en.wikipedia.org/wiki/Turtles_all_the_way_down">turtles all the way down</a>? </p> <p> I think that we can <a href="/2013/04/02/why-trust-tests">trust tests for other reasons</a>, but one of them is that each test case exercises a deterministic path through a unit that supports many paths of execution. </p> <p> <img src="/content/binary/one-test-path-through-complex-unit.png" alt="Diagram that shows a unit test exercising one path through a unit."> </p> <p> In other words, each unit test is an example of a singular execution path. Tests, then, should have a <a href="http://en.wikipedia.org/wiki/Cyclomatic_complexity">cyclomatic complexity</a> of 1. In other words, you write (test) code with a cyclomatic complexity of 1 in order to test code with a higher cyclomatic complexity. </p> <p> Should you test code that has a cyclomatic complexity of 1? </p> <p> What would be the point of that? Why would you write a unit test with a cyclomatic complexity of 1 to test a piece of code with a cyclomatic complexity of 1? Wouldn't you just be adding more code? </p> <p> From the perspective of <em>trusting</em> the code, there's no reason to trust such a test more than the code that it exercises. In that light, I think it makes sense to <em>not</em> write that test. </p> <p> To be clear, there could be other reasons to test code with a cyclomatic complexity of 1. One reason, that I pointed out in my original article, is that you don't know if the simple piece of code will <em>stay</em> simple. Another reason is to prevent regressions. A common metaphor for unit testing is <a href="http://en.wikipedia.org/wiki/Double-entry_bookkeeping_system">double-entry bookkeeping</a>. If you write the unit test in a different way than the implementation, the two views on that behaviour may keep each other in check. You could do that with triangulation using <a href="http://xunitpatterns.com/Parameterized%20Test.html">parametrised tests</a>, or perhaps with property-based testing. </p> <p> I tend to use a heuristic where the farther to the left I am on the above continuum, the more I'm inclined to skip testing of simple functionality. Code with a cyclomatic complexity of 1 falls into that bucket. </p> <h3 id="5899a274dd0f4697b86772d3d0a5d6ce"> TDD <a href="#5899a274dd0f4697b86772d3d0a5d6ce" title="permalink">#</a> </h3> <p> Let's return our attention to TDD. The previous paragraphs have mostly discussed automated tests as a way to prevent regressions. TDD gives us an entirely different motivation for writing tests: the tests provide feedback on the design of our production code. </p> <p> Viewed like this, the tests themselves are only artefacts of the TDD process. It's usually a good idea to keep them around after the standard red-green-refactor cycle, because they serve double-duty as regression tests. </p> <p> Should you test-drive everything? If you're inexperienced with TDD, you get the best exercise by test-driving as much as possible. This still doesn't have to mean that you must write a an explicit test case for each class member. That's what both Mark Rendle and Derick Bailey pointed out. It's often enough if the tests somehow exercise those members. </p> <p> Revisiting my old article, my mistake was that I conflated TDD with regression testing. My motivation for writing an explicit test case for each member, no matter how trivial, was to preserve backwards compatibility. It really had nothing to do with TDD. </p> <h3 id="f0834719ee414d7e81a32c7cb32e8256"> When in doubt <a href="#f0834719ee414d7e81a32c7cb32e8256" title="permalink">#</a> </h3> <p> Despite all other rules of thumb I've so far listed, I'll suggest a few exceptions. </p> <p> Even if a piece of code theoretically has a cyclomatic complexity of 1, if you're in doubt of how it works, then write a test. </p> <p> If you have a defect in production, then reproduce that defect with one or more tests, even if the code in question is 'trivial'. Obviously, it wasn't trivial after all, if it caused a bug in production. </p> <h3 id="51cfec77312e45f7a2dd60b55096ba17"> Pragmatism <a href="#51cfec77312e45f7a2dd60b55096ba17" title="permalink">#</a> </h3> <p> When you're leaning something new, you're typically struggling with even the most basic concepts. That's just how learning works. In that phase of learning, it often pays to follow explicit rules. A way to think about this is the <a href="http://en.wikipedia.org/wiki/Dreyfus_model_of_skill_acquisition">Dreyfus model of skill acquisition</a>. Once you gain some experience, you can start deviating from the rules. We could call this <em>pragmatism</em>. </p> <p> I often discuss TDD with people who plead for pragmatism. Those people have typically practised TDD for years, if not decades. So have I, and, believe it or not, I'm often quite pragmatic when I practice TDD 'for real'. This is, however, a prerogative of experience. <blockquote> You can only be pragmatic if you know how to be dogmatic. </blockquote> I use the concept of <em>dogmatism</em> as an antonym to <em>pragmatism</em>. I view pragmatism in programming as the choice of practical solutions over theoretical principles. It's a <em>choice</em>, which means that you must be aware of alternatives. </p> <p> If you don't know the (principled) alternative, there's no choice. </p> <p> When you're learning something new, you're still not aware of how to do things according to principle. That's natural. I find myself in that situation all the time. If you keep at it, though, eventually you'll have gained enough experience that you can make actual choices. </p> <p> This applies to TDD as well. When you're still learning TDD, stick to the principles, particularly when it's inconvenient. Once you've done TDD for a few years, you've earned the right to be pragmatic. </p> <h3 id="11ce36adfdeb4b8e8c6b640e28691aa0"> Conclusion <a href="#11ce36adfdeb4b8e8c6b640e28691aa0" title="permalink">#</a> </h3> <p> Which parts of your code base should you (unit) test? It Depends™. </p> <p> It depends on why you are unit testing, and on the cost of defects in production, and probably many other things I didn't think of. </p> <p> What's the purpose of tests? Are you using TDD to get feedback on your API design ideas? Or is the main purpose of tests to prevent regressions? Your answers to such questions should guide your decisions on how much to test. </p> <p> Recently, I've mostly been writing about topics related to computer science, such as the <a href="/2017/10/04/from-design-patterns-to-category-theory">relationships between various branches of mathematics to computation</a>. In such realms, laws apply, and answers tend to be either right or wrong. A piece like this article is different. </p> <p> This is fundamentally a deeply subjective essay. It's based on my experience with writing automated tests in various circumstances since 2003. I've tried to be as explicit about my context as possible, but I've most likely failed to identify one or more implicit assumptions or biases. I do, therefore, encourage comments. </p> <p> I wrote this commentary because people keep asking me about how much to test, and when. I suppose it's because they wish to learn from me, and I'm happy to share what I know, to the best of my ability. I have much to learn myself, though, so read this only as the partial, flawed, personal answer that it is. </p> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Applicative validation https://blog.ploeh.dk/2018/11/05/applicative-validation 2018-11-05T07:05:00+00:00 Mark Seemann <div id="post"> <p> <em>Validate input in applicative style for superior readability and composability.</em> </p> <p> This article is an instalment in <a href="/2018/10/01/applicative-functors">an article series about applicative functors</a>. It demonstrates how applicative style can be used to compose small validation functions to a larger validation function in such a way that no validation messages are lost, and the composition remains readable. </p> <p> All example code in this article is given in <a href="https://www.haskell.org">Haskell</a>. No <a href="https://fsharp.org">F#</a> translation is offered, because <a href="http://fsharpforfunandprofit.com">Scott Wlaschin</a> has an <a href="https://fsharpforfunandprofit.com/posts/elevated-world-3/#validation">equivalent example covering input validation in F#</a>. </p> <h3 id="789d9704d9a2434f8631d718e8be3f17"> JSON validation <a href="#789d9704d9a2434f8631d718e8be3f17" title="permalink">#</a> </h3> <p> In my <a href="https://blog.ploeh.dk/functional-architecture-with-fsharp">Pluralsight course about a functional architecture in F#</a>, you can see an example of an on-line restaurant reservation system. I often return to that example scenario, so for regular readers of this blog, it should be known territory. For newcomers, imagine that you've been asked to develop an HTTP-based API that accepts JSON documents containing restaurant reservations. Such a JSON document could look like this: </p> <p> <pre>{ &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;date&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;2017-06-27&nbsp;18:30:00+02:00&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;name&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;Mark&nbsp;Seemann&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;email&quot;</span>:&nbsp;<span style="color:#a31515;">&quot;mark@example.com&quot;</span>, &nbsp;&nbsp;<span style="color:#2e75b6;">&quot;quantity&quot;</span>:&nbsp;4 }</pre> </p> <p> It contains the date and time of the (requested) reservation, the email address and name of the person making the reservation, as well as the number of people who will be dining. Particularly, notice that the date and time is represented as a string value (specifically, in <a href="https://en.wikipedia.org/wiki/ISO_8601">ISO 8601</a> format), since JSON has no built-in date and time data type. </p> <p> In Haskell, you can represent such a JSON document using a type like this: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;<span style="color:#dd0000;">ReservationJson</span>&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">ReservationJson</span>&nbsp;{ &nbsp;&nbsp;<span style="color:#600277;">jsonDate</span>&nbsp;::&nbsp;String, &nbsp;&nbsp;<span style="color:#600277;">jsonQuantity</span>&nbsp;::&nbsp;Double, &nbsp;&nbsp;<span style="color:#600277;">jsonName</span>&nbsp;::&nbsp;String, &nbsp;&nbsp;<span style="color:#600277;">jsonEmail</span>&nbsp;::&nbsp;String&nbsp;} &nbsp;&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#a31515;">Eq</span>,&nbsp;<span style="color:#a31515;">Show</span>,&nbsp;<span style="color:#a31515;">Read</span>,&nbsp;<span style="color:#a31515;">Generic</span>)</pre> </p> <p> Haskell's strength is in its type system, so you should prefer to model a reservation using a strong type: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;<span style="color:#dd0000;">Reservation</span>&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">Reservation</span>&nbsp;{ &nbsp;&nbsp;<span style="color:#600277;">reservationDate</span>&nbsp;::&nbsp;<span style="color:blue;">ZonedTime</span>, &nbsp;&nbsp;<span style="color:#600277;">reservationQuantity</span>&nbsp;::&nbsp;Int, &nbsp;&nbsp;<span style="color:#600277;">reservationName</span>&nbsp;::&nbsp;String, &nbsp;&nbsp;<span style="color:#600277;">reservationEmail</span>&nbsp;::&nbsp;String&nbsp;} &nbsp;&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#a31515;">Show</span>,&nbsp;<span style="color:#a31515;">Read</span>)</pre> </p> <p> Instead of modelling the date and time as a string, you model it as a <code>ZonedTime</code> value. Additionally, you should model quantity as an integer, since a floating point value doesn't make much sense. </p> <p> While you can always translate a <code>Reservation</code> value to a <code>ReservationJson</code> value, the converse doesn't hold. There are <code>ReservationJson</code> values that you can't translate to <code>Reservation</code>. Such <code>ReservationJson</code> values are invalid. </p> <p> You should write code to validate and translate <code>ReservationJson</code> values to <code>Reservation</code> values, if possible. </p> <h3 id="e71bff7b635a449eb05a503c75c4f887"> Specialised validations <a href="#e71bff7b635a449eb05a503c75c4f887" title="permalink">#</a> </h3> <p> The <code>ReservationJson</code> type is a complex type, because it's composed of multiple (four) elements of different types. You can easily define at least three validation rules that ought to hold: <ol> <li>You should be able to convert the <code>jsonDate</code> value to a <code>ZonedTime</code> value.</li> <li><code>jsonQuantity</code> must be a positive integer.</li> <li><code>jsonEmail</code> should look believably like an email address.</li> </ol> When you have a complex type where more than one validation rule applies, your code will be most readable and maintainable if you can write each rule as an independent function. </p> <p> In Haskell, people often use <code>Either</code> for validation, but instead of using <code>Either</code> directly, I'll introduce a specialised <code>Validation</code> type: </p> <p> <pre><span style="color:blue;">newtype</span>&nbsp;<span style="color:#dd0000;">Validation</span>&nbsp;e&nbsp;r&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">Validation</span>&nbsp;(<span style="color:#dd0000;">Either</span>&nbsp;e&nbsp;r)&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#a31515;">Eq</span>,&nbsp;<span style="color:#a31515;">Show</span>,&nbsp;<span style="color:#a31515;">Functor</span>) </pre> </p> <p> You'll notice that this is simply a redefinition of <code>Either</code>. Haskell can automatically derive its <code>Functor</code> instance with the <code>DeriveFunctor</code> language extension. </p> <p> My motivation for introducing a new type is that the way that <code>Either</code> is <code>Applicative</code> is not quite how I'd like it to be. Introducing a <code>newtype</code> enables you to change how a type behaves. More on that later. First, you can implement the three individual validation functions. </p> <h3 id="89e88d794a0449189eff92795a2bca04"> Date validation <a href="#89e88d794a0449189eff92795a2bca04" title="permalink">#</a> </h3> <p> If the JSON date value is an ISO 8601-formatted string, then you can parse it as a <code>ZonedTime</code>. In that case, you should return the <code>Right</code> case of <code>Validation</code>. If you can't parse the string into a <code>ZonedTime</code> value, you should return a <code>Left</code> value containing a helpful error message. </p> <p> <pre><span style="color:#600277;">validateDate</span>&nbsp;::&nbsp;String&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Validation</span>&nbsp;[String]&nbsp;<span style="color:blue;">ZonedTime</span> validateDate&nbsp;candidate&nbsp;<span style="color:#666666;">=</span> &nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;readMaybe&nbsp;candidate&nbsp;<span style="color:blue;">of</span> &nbsp;&nbsp;&nbsp;&nbsp;Just&nbsp;d&nbsp;&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;<span style="color:#dd0000;">Validation</span>&nbsp;<span style="color:#666666;">$</span>&nbsp;Right&nbsp;d &nbsp;&nbsp;&nbsp;&nbsp;Nothing&nbsp;<span style="color:#666666;">-&gt;</span>&nbsp;<span style="color:#dd0000;">Validation</span>&nbsp;<span style="color:#666666;">$</span>&nbsp;Left&nbsp;[<span style="color:#a31515;">&quot;Not&nbsp;a&nbsp;date.&quot;</span>]</pre> </p> <p> This function uses <code>readMaybe</code> from <code>Text.Read</code> to attempt to parse the <code>candidate</code> <code>String</code>. When <code>readMaybe</code> can read the <code>String</code> value, it returns a <code>Just</code> value with the parsed value inside; otherwise, it returns <code>Nothing</code>. The function pattern-matches on those two cases and returns the appropriate value in each case. </p> <p> Notice that errors are represented as a list of <code>String</code> values, although this particular function only returns a single message in its list of error messages. The reason for that is that you should be able to collect multiple validation issues for a complex value such as <code>ReservationJson</code>, and keeping track of errors in a list makes that possible. </p> <p> Haskell <a href="https://en.wikipedia.org/wiki/Code_golf">golfers</a> may argue that this implementation is overly verbose, and it could, for instance, instead be written as: </p> <p> <pre>validateDate&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">Validation</span>&nbsp;<span style="color:#666666;">.</span>&nbsp;maybe&nbsp;(Left&nbsp;[<span style="color:#a31515;">&quot;Not&nbsp;a&nbsp;date.&quot;</span>])&nbsp;Right&nbsp;<span style="color:#666666;">.</span>&nbsp;readMaybe </pre> </p> <p> which is true, but not as readable. Both versions get the job done, though, as these GCHi-based ad-hoc tests demonstrate: </p> <p> <pre>λ&gt; validateDate "2017/27/06 18:30:00 UTC+2" Validation (Left ["Not a date."]) λ&gt; validateDate "2017-06-27 18:30:00+02:00" Validation (Right 2017-06-27 18:30:00 +0200)</pre> </p> <p> That takes care of parsing dates. On to the next validation function. </p> <h3 id="df33c4c13805494b844793bac0577e5b"> Quantity validation <a href="#df33c4c13805494b844793bac0577e5b" title="permalink">#</a> </h3> <p> JSON numbers aren't guaranteed to be integers, so it's possible that even a well-formed Reservation JSON document could contain a <code>quantity</code> property of <code>9.7</code>, <code>-11.9463</code>, or similar. When handling restaurant reservations, however, it only makes sense to handle positive integers. Even <code>0</code> is useless in this context. Thus, validation must check for two conditions, so in principle, you could write two separate functions for that. In order to keep the example simple, though, I've included both tests in the same function: </p> <p> <pre><span style="color:#600277;">validateQuantity</span>&nbsp;::&nbsp;Double&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Validation</span>&nbsp;[String]&nbsp;Int validateQuantity&nbsp;candidate&nbsp;<span style="color:#666666;">=</span> &nbsp;&nbsp;<span style="color:#af00db;">if</span>&nbsp;isInt&nbsp;candidate&nbsp;<span style="color:#666666;">&amp;&amp;</span>&nbsp;candidate&nbsp;<span style="color:#666666;">&gt;</span>&nbsp;<span style="color:#09885a;">0</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#af00db;">then</span>&nbsp;<span style="color:#dd0000;">Validation</span>&nbsp;<span style="color:#666666;">$</span>&nbsp;Right&nbsp;<span style="color:#666666;">$</span>&nbsp;round&nbsp;candidate &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#af00db;">else</span>&nbsp;<span style="color:#dd0000;">Validation</span>&nbsp;<span style="color:#666666;">$</span>&nbsp;Left&nbsp;[<span style="color:#a31515;">&quot;Not&nbsp;a&nbsp;positive&nbsp;integer.&quot;</span>] &nbsp;&nbsp;<span style="color:blue;">where</span>&nbsp;isInt&nbsp;x&nbsp;<span style="color:#666666;">=</span>&nbsp;x&nbsp;<span style="color:#666666;">==</span>&nbsp;fromInteger&nbsp;(round&nbsp;x)</pre> </p> <p> If <code>candidate</code> is both an integer, and greater than zero, then <code>validateQuantity</code> returns <code>Right</code>; otherwise, it returns a <code>Left</code> value containing an error message. Like <code>validateDate</code>, you can easily test <code>validateQuantity</code> in GHCi: </p> <p> <pre>λ&gt; validateQuantity 4 Validation (Right 4) λ&gt; validateQuantity (-1) Validation (Left ["Not a positive integer."]) λ&gt; validateQuantity 2.32 Validation (Left ["Not a positive integer."])</pre> </p> <p> Perhaps you can think of rules for names, but I can't, so we'll leave the name be and move on to validating email addresses. </p> <h3 id="7eb2c9f79bfa4f7fa866094d094c5e2c"> Email validation <a href="#7eb2c9f79bfa4f7fa866094d094c5e2c" title="permalink">#</a> </h3> <p> It's <a href="http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx">notoriously difficult to validate SMTP addresses</a>, so you shouldn't even try. It seems fairly safe to assume, however, that an email address must contain at least one <code>@</code> character, so that's going to be all the validation you have to implement: </p> <p> <pre><span style="color:#600277;">validateEmail</span>&nbsp;::&nbsp;String&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Validation</span>&nbsp;[String]&nbsp;String validateEmail&nbsp;candidate&nbsp;<span style="color:#666666;">=</span> &nbsp;&nbsp;<span style="color:#af00db;">if</span>&nbsp;<span style="color:#a31515;">&#39;@&#39;</span>&nbsp;<span style="color:#666666;">`elem`</span>&nbsp;candidate &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#af00db;">then</span>&nbsp;<span style="color:#dd0000;">Validation</span>&nbsp;<span style="color:#666666;">$</span>&nbsp;Right&nbsp;candidate &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#af00db;">else</span>&nbsp;<span style="color:#dd0000;">Validation</span>&nbsp;<span style="color:#666666;">$</span>&nbsp;Left&nbsp;[<span style="color:#a31515;">&quot;Not&nbsp;an&nbsp;email&nbsp;address.&quot;</span>]</pre> </p> <p> Straightforward. Try it out in GHCI: </p> <p> <pre>λ&gt; validateEmail "foo" Validation (Left ["Not an email address."]) λ&gt; validateEmail "foo@example.org" Validation (Right "foo@example.org")</pre> </p> <p> Indeed, that works. </p> <h3 id="bcfce51ff4e94f79a6648a7e373ebc82"> Applicative composition <a href="#bcfce51ff4e94f79a6648a7e373ebc82" title="permalink">#</a> </h3> <p> What you really should be doing is to validate a <code>ReservationJson</code> value. You have the three validation rules implemented, so now you have to compose them. There is, however, a catch: you must evaluate all rules, and return a list of <em>all</em> the errors you encountered. That's probably going to be a better user experience for a user. </p> <p> That's the reason you can't use <code>Either</code>. While it's <code>Applicative</code>, it doesn't behave like you'd like it to behave in this scenario. Particularly, the problem is that it throws away all but the first <code>Left</code> value it finds: </p> <p> <pre>λ&gt; Right (,,) &lt;*&gt; Right 42 &lt;*&gt; Left "foo" &lt;*&gt; Left "bar" Left "foo"</pre> </p> <p> Notice how <code>Left "bar"</code> is ignored. </p> <p> With the new type <code>Validation</code> based on <code>Either</code>, you can now define how it behaves as an applicative functor: </p> <p> <pre><span style="color:blue;">instance</span>&nbsp;<span style="color:blue;">Monoid</span>&nbsp;m&nbsp;<span style="color:blue;">=&gt;</span>&nbsp;<span style="color:blue;">Applicative</span>&nbsp;(<span style="color:blue;">Validation</span>&nbsp;m)&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;pure&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">Validation</span>&nbsp;<span style="color:#666666;">.</span>&nbsp;pure &nbsp;&nbsp;<span style="color:#dd0000;">Validation</span>&nbsp;(Left&nbsp;x)&nbsp;<span style="color:#666666;">&lt;*&gt;</span>&nbsp;<span style="color:#dd0000;">Validation</span>&nbsp;(Left&nbsp;y)&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">Validation</span>&nbsp;(Left&nbsp;(mappend&nbsp;x&nbsp;y)) &nbsp;&nbsp;<span style="color:#dd0000;">Validation</span>&nbsp;f&nbsp;<span style="color:#666666;">&lt;*&gt;</span>&nbsp;<span style="color:#dd0000;">Validation</span>&nbsp;r&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">Validation</span>&nbsp;(f&nbsp;<span style="color:#666666;">&lt;*&gt;</span>&nbsp;r)</pre> </p> <p> This instance is restricted to <code>Monoid</code> <code>Left</code> types. It has special behaviour for the case where both expressions passed to <code>&lt;*&gt;</code> are <code>Left</code> values. In that case, it uses <code>mappend</code> (from <code>Monoid</code>) to 'add' the two <code>Left</code> values together in a new <code>Left</code> value. </p> <p> For all other cases, this instance of <code>Applicative</code> delegates to the behaviour defined for <code>Either</code>. It also uses <code>pure</code> from <code>Either</code> to implement its own <code>pure</code> function. </p> <p> Lists (<code>[]</code>) <a href="/2017/10/10/strings-lists-and-sequences-as-a-monoid">form a monoid</a>, and since all the above validation functions return lists of errors, it means that you can compose them using this definition of <code>Applicative</code>: </p> <p> <pre><span style="color:#600277;">validateReservation</span>&nbsp;::&nbsp;<span style="color:blue;">ReservationJson</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:blue;">Validation</span>&nbsp;[String]&nbsp;<span style="color:blue;">Reservation</span> validateReservation&nbsp;candidate&nbsp;<span style="color:#666666;">=</span> &nbsp;&nbsp;pure&nbsp;<span style="color:#dd0000;">Reservation</span>&nbsp;<span style="color:#666666;">&lt;*&gt;</span>&nbsp;vDate&nbsp;<span style="color:#666666;">&lt;*&gt;</span>&nbsp;vQuantity&nbsp;<span style="color:#666666;">&lt;*&gt;</span>&nbsp;vName&nbsp;<span style="color:#666666;">&lt;*&gt;</span>&nbsp;vEmail &nbsp;&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;&nbsp;&nbsp;vDate&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#666666;">=</span>&nbsp;validateDate&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#666666;">$</span>&nbsp;jsonDate&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;candidate &nbsp;&nbsp;&nbsp;&nbsp;vQuantity&nbsp;<span style="color:#666666;">=</span>&nbsp;validateQuantity&nbsp;<span style="color:#666666;">$</span>&nbsp;jsonQuantity&nbsp;candidate &nbsp;&nbsp;&nbsp;&nbsp;vName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#666666;">=</span>&nbsp;pure&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#666666;">$</span>&nbsp;jsonName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;candidate &nbsp;&nbsp;&nbsp;&nbsp;vEmail&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#666666;">=</span>&nbsp;validateEmail&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#666666;">$</span>&nbsp;jsonEmail&nbsp;&nbsp;&nbsp;&nbsp;candidate</pre> </p> <p> The <code>candidate</code> is a <code>ReservationJson</code> value, but each of the validation functions work on either <code>String</code> or <code>Double</code>, so you'll have to use the <code>ReservationJson</code> type's access functions (<code>jsonDate</code>, <code>jsonQuantity</code>, and so on) to pull the relevant values out of it. Once you have those, you can pass them as arguments to the appropriate validation function. </p> <p> Since there's no rule for <code>jsonName</code>, you can use <code>pure</code> to create a <code>Validation</code> value. All four resulting values (<code>vDate</code>, <code>vQuantity</code>, <code>vName</code>, and <code>vEmail</code>) are <code>Validation [String]</code> values; only their <code>Right</code> types differ. </p> <p> The <code>Reservation</code> record constructor is a function of the type <code>ZonedTime -&gt; Int -&gt; String -&gt; String -&gt; Reservation</code>, so when you arrange the four <code>v*</code> values correctly between the <code>&lt;*&gt;</code> operator, you have the desired composition. </p> <p> Try it in GHCi: </p> <p> <pre>λ&gt; validateReservation $ ReservationJson "2017-06-30 19:00:00+02:00" 4 "Jane Doe" "j@example.com" Validation (Right (Reservation { &nbsp;&nbsp;&nbsp;&nbsp;reservationDate = 2017-06-30 19:00:00 +0200, &nbsp;&nbsp;&nbsp;&nbsp;reservationQuantity = 4, &nbsp;&nbsp;&nbsp;&nbsp;reservationName = "Jane Doe", &nbsp;&nbsp;&nbsp;&nbsp;reservationEmail = "j@example.com"})) λ&gt; validateReservation $ ReservationJson "2017/14/12 6pm" 4.1 "Jane Doe" "jane.example.com" Validation (Left ["Not a date.","Not a positive integer.","Not an email address."]) λ&gt; validateReservation $ ReservationJson "2017-06-30 19:00:00+02:00" (-3) "Jane Doe" "j@example.com" Validation (Left ["Not a positive integer."])</pre> </p> <p> The first <code>ReservationJson</code> value passed to <code>validateReservation</code> is valid, so the return value is a <code>Right</code> value. </p> <p> The next <code>ReservationJson</code> value is about as wrong as it can be, so three different error messages are returned in a <code>Left</code> value. This demonstrates that <code>Validation</code> doesn't give up the first time it encounters a <code>Left</code> value, but rather collects them all. </p> <p> The third example demonstrates that even a single invalid value (in this case a negative quantity) is enough to make the entire input invalid, but as expected, there's only a single error message. </p> <h3 id="b9d3fd6c208647dc988ef0ffc64cf061"> Summary <a href="#b9d3fd6c208647dc988ef0ffc64cf061" title="permalink">#</a> </h3> <p> Validation may be the poster child of applicative functors, but it <em>is</em> a convenient way to solve the problem. In this article you saw how to validate a complex data type, collecting and reporting on all problems, if any. </p> <p> In order to collect all errors, instead of immediately short-circuiting on the first error, you have to deviate from the standard <code>Either</code> implementation of <code>&lt;*&gt;</code>. If you go back to read Scott Wlaschin's article, you should be aware that it specifically implements its applicative functor in that way, instead of the normal behaviour of <code>Either</code>. </p> <p> More applicative functors exist. This article series has, I think, room for more examples. </p> <p> <strong>Next:</strong> <a href="/2018/11/26/the-test-data-generator-applicative-functor">The Test Data Generator applicative functor</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. The Maybe applicative functor https://blog.ploeh.dk/2018/10/29/the-maybe-applicative-functor 2018-10-29T06:17:00+00:00 Mark Seemann <div id="post"> <p> <em>An introduction to the Maybe applicative functor for object-oriented programmers.</em> </p> <p> This article is an instalment in <a href="/2018/10/01/applicative-functors">an article series about applicative functors</a>. Previously, in a related series, you got an introduction to <a href="/2018/03/26/the-maybe-functor">Maybe as a functor</a>. Not all functors are applicative, but some are, and Maybe is one of them (like list). </p> <p> In this article, you'll see how to make a C# Maybe class applicative. While I'm going to start with <a href="https://fsharp.org">F#</a> and <a href="https://www.haskell.org">Haskell</a>, you can skip to the C# section if you'd like. </p> <h3 id="f88a2d0afbe84b7695fe4247e3cfe941"> F# <a href="#f88a2d0afbe84b7695fe4247e3cfe941" title="permalink">#</a> </h3> <p> A few years ago, <a href="/2016/06/28/roman-numerals-via-property-based-tdd">I did the <em>Roman numerals</em> kata</a> in F#. This is an exercise where you have to convert between normal base 10 integers and <a href="https://en.wikipedia.org/wiki/Roman_numerals">Roman numerals</a>. Conversions can fail in both directions, because Roman numerals don't support negative numbers, zero, or numbers greater than 3,999, and Roman numerals represented as strings could be malformed. </p> <p> Some Roman numbers are written in a subtractive style, e.g. "IV" means <em>subtract 1 (I) from 5 (V)</em>. It's easy enough to subtract two numbers, but because parsing isn't guaranteed to succeed, I didn't have two numbers; I had two number <em>options</em> (recall that in F#, Maybe is called <code>option</code>). </p> <p> How do you subtract one <code>int option</code> from another <code>int option</code>? </p> <p> Both of these values could be <code>Some</code>, or they could be <code>None</code>. What should happen in each case? With Maybe, only four combinations are possible, so you can put them in a table: <table> <thead> <tr> <th></th> <th><code>Some x</code></th> <th><code>None</code></th> </tr> </thead> <tbody> <tr> <td><strong><code>Some y</code></strong></td> <td><code>Some (x - y)</code></td> <td><code>None</code></td> </tr> <tr> <td><strong><code>None</code></strong></td> <td><code>None</code></td> <td><code>None</code></td> </tr> </tbody> </table> Only if both values are <code>Some</code> cases should you return a <code>Some</code> case with the result of the subtraction; in all other cases, you should return <code>None</code>. </p> <p> You can do this with regular pattern matching, but it's hardly the most elegant solution: </p> <p> <pre><span style="color:green;">//&nbsp;int&nbsp;option</span> <span style="color:blue;">let</span>&nbsp;difference&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;minuend,&nbsp;subtrahend&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Some</span>&nbsp;m,&nbsp;<span style="color:navy;">Some</span>&nbsp;s&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">Some</span>&nbsp;(m&nbsp;-&nbsp;s) &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">None</span></pre> </p> <p> You <em>could</em> attempt to solve this with a specialised helper function like this: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;<span style="color:teal;">Option</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b&nbsp;-&gt;&nbsp;&#39;c)&nbsp;-&gt;&nbsp;&#39;a&nbsp;option&nbsp;-&gt;&nbsp;&#39;b&nbsp;option&nbsp;-&gt;&nbsp;&#39;c&nbsp;option</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">map2</span>&nbsp;<span style="color:navy;">f</span>&nbsp;xo&nbsp;yo&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;xo,&nbsp;yo&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Some</span>&nbsp;x,&nbsp;<span style="color:navy;">Some</span>&nbsp;y&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">Some</span>&nbsp;(<span style="color:navy;">f</span>&nbsp;x&nbsp;y) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">None</span></pre> </p> <p> which you could use like this: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;difference&nbsp;=&nbsp;<span style="color:teal;">Option</span>.<span style="color:navy;">map2</span>&nbsp;(-)&nbsp;minuend&nbsp;subtrahend </pre> </p> <p> It doesn't, however, generalise well... What if you need to operate on three option values, instead of two? Or four? Should you add <code>map3</code> and <code>map4</code> functions as well? </p> <p> Making <code>option</code> an applicative functor addresses that problem. Here's one possible implementation of <code>&lt;*&gt;</code>: </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b)&nbsp;option&nbsp;-&gt;&nbsp;&#39;a&nbsp;option&nbsp;-&gt;&nbsp;&#39;b&nbsp;option</span> <span style="color:blue;">let</span>&nbsp;(&lt;*&gt;)&nbsp;fo&nbsp;xo&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">match</span>&nbsp;fo,&nbsp;xo&nbsp;<span style="color:blue;">with</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Some</span>&nbsp;<span style="color:navy;">f</span>,&nbsp;<span style="color:navy;">Some</span>&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">Some</span>&nbsp;(<span style="color:navy;">f</span>&nbsp;x) &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">None</span></pre> </p> <p> This enables you two write the subtraction like this: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;difference&nbsp;=&nbsp;<span style="color:navy;">Some</span>&nbsp;(-)&nbsp;&lt;*&gt;&nbsp;minuend&nbsp;&lt;*&gt;&nbsp;subtrahend </pre> </p> <p> For a detailed explanation on how that works, see the <a href="/2018/10/08/full-deck">previous explanation for lists</a>; it works the same way for Maybe as it does for List. </p> <p> In the end, however, I didn't think that this was the most readable code, so in the Roman numeral exercise, I chose to use a <a href="https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/computation-expressions">computation expression</a> instead. </p> <h3 id="264aaa993b7643b692716bed1b3e2b92"> Haskell <a href="#264aaa993b7643b692716bed1b3e2b92" title="permalink">#</a> </h3> <p> In Haskell, <code>Maybe</code> is already <code>Applicative</code> as part of the language. Without further ado, you can simply write: </p> <p> <pre>difference&nbsp;<span style="color:#666666;">=</span>&nbsp;pure&nbsp;<span style="color:#600277;">(-)</span>&nbsp;<span style="color:#666666;">&lt;*&gt;</span>&nbsp;minuend&nbsp;<span style="color:#666666;">&lt;*&gt;</span>&nbsp;subtrahend </pre> </p> <p> As is the case with the F# code, I don't consider this the most <em>readable</em> way to express the subtraction of two integers. In F#, I ultimately decided to use a computation expression. In Haskell, that's equivalent to using <code>do</code> notation: </p> <p> <pre><span style="color:#600277;">difference</span>&nbsp;::&nbsp;Maybe&nbsp;Integer difference&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#af00db;">do</span> &nbsp;&nbsp;m&nbsp;<span style="color:#666666;">&lt;-</span>&nbsp;minuend &nbsp;&nbsp;s&nbsp;<span style="color:#666666;">&lt;-</span>&nbsp;subtrahend &nbsp;&nbsp;return&nbsp;<span style="color:#666666;">$</span>&nbsp;m&nbsp;<span style="color:#666666;">-</span>&nbsp;s</pre> </p> <p> While more verbose, I think it's clearer that one number is being subtracted from another number. </p> <p> This works for <code>Maybe</code> because not only is <code>Maybe</code> <code>Applicative</code>, it's also a <code>Monad</code>. It's its monadness that enables the <code>do</code> notation. Not all applicative functors are monads, but Maybe is. </p> <h3 id="67a752fec39f4944984f505c125632f1"> C# <a href="#67a752fec39f4944984f505c125632f1" title="permalink">#</a> </h3> <p> In a <a href="/2018/03/26/the-maybe-functor">previous article</a> you saw how to implement the Maybe functor in C#. You can extend it so that it also becomes an applicative functor: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;Apply&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;&nbsp;selector, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;source) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(selector.HasItem&nbsp;&amp;&amp;&nbsp;source.HasItem) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;(selector.Item(source.Item)); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;(); } <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;&nbsp;Apply&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;&nbsp;selector, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">T1</span>&gt;&nbsp;source) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">if</span>&nbsp;(selector.HasItem&nbsp;&amp;&amp;&nbsp;source.HasItem) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;g&nbsp;=&nbsp;x&nbsp;=&gt;&nbsp;selector.Item(source.Item,&nbsp;x); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;(g); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">else</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:#2b91af;">Maybe</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;(); }</pre> </p> <p> As was the case for making sequences applicative in C#, you <a href="/2018/10/15/an-applicative-password-list">need overloads of the <code>Apply</code> method</a>, because C#'s type inference is inadequate for this task. </p> <p> If you have two <code>Maybe&lt;int&gt;</code> values, <code>minuend</code> and <code>subtrahend</code>, you can now perform the subtraction: </p> <p> <pre><span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;subtract&nbsp;=&nbsp;(x,&nbsp;y)&nbsp;=&gt;&nbsp;x&nbsp;-&nbsp;y; <span style="color:#2b91af;">Maybe</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;difference&nbsp;=&nbsp;subtract.ToMaybe().Apply(minuend).Apply(subtrahend);</pre> </p> <p> Like in F# and Haskell, applicative style is hardly the most readable way to express subtraction. It'd be nice if you could write it like Haskell's <code>do</code> notation. You can, but to do that, you must make Maybe a monad, and this isn't a monad tutorial. <a href="http://mikehadlow.com">Mike Hadlow</a> has a good <a href="http://mikehadlow.blogspot.dk/2011/01/monads-in-c1-introduction.html">monad tutorial for C# developers</a>, the gist of which is that you must implement <code>SelectMany</code> in order to turn your generic type into a monad. For now, I'll leave this as an exercise for you, but if you add an appropriate <code>SelectMany</code> method, you'd be able to write the subtraction like this: </p> <p> <pre><span style="color:#2b91af;">Maybe</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;difference&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;m&nbsp;<span style="color:blue;">in</span>&nbsp;minuend &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">from</span>&nbsp;s&nbsp;<span style="color:blue;">in</span>&nbsp;subtrahend &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">select</span>&nbsp;m&nbsp;-&nbsp;s;</pre> </p> <p> Again, I think this is more readable, but it does require that the type in question is a monad, and not all applicative functors are (but Maybe is). </p> <h3 id="b03175fcd3a74155a3ceac26fb02aed0"> Summary <a href="#b03175fcd3a74155a3ceac26fb02aed0" title="permalink">#</a> </h3> <p> This article demonstrates that lists or sequences aren't the only applicative functors. Maybe is also an applicative functor, but more exist. The next article will give you another example. </p> <p> <strong>Next:</strong> <a href="/2018/11/05/applicative-validation">Applicative validation</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="d42afea512c243019f500a345638cecb"> <div class="comment-author">Tyson Williams</div> <div class="comment-content"> <blockquote> As was the case for making sequences applicative in C#, you <a href="/2018/10/15/an-applicative-password-list">need overloads of the <code>Apply</code> method</a>, because C#'s type inference is inadequate for this task. </blockquote> <p> I think <a href=/2018/10/15/an-applicative-password-list/#b4e76681ea894aa3be1e6b836343c148">we agreed</a> that the issue is not C#'s weak type inference but its lack of default function currying? My guess is that you wrote this quoted part of this article before my comment on your previous article. </p> </div> <div class="comment-date">2018-11-06 02:44 UTC</div> </div> <div class="comment" id="0bb16509bbea463bab48c9e97acea2c5"> <div class="comment-author"><a href="/">Mark Seemann</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. <blockquote> "My guess is that you wrote this quoted part of this article before my comment on your previous article." </blockquote> Yes, <a href="https://github.com/ploeh/ploeh.github.com/commit/078cb3e938bfb911363cc8ab1139dfc5ad435349">June 27, 2017, in fact</a>... </p> <p> You're correct that this particular issue is related to the uncurried nature of C# methods. </p> <p> I do, however, maintain that C#'s type inference capabilities are weaker than F#'s or Haskell's. To be clear, I view this as the result of priorities. I don't think that the people who designed and wrote the C# compiler are less skilled than the designers of F# or Haskell. The C# compiler has to solve many other problems, such as for example overload resolution, which is a language feature in direct opposition to currying. The C# compiler is excellent at overload resolution, a task with which the F# compiler sometimes struggle (and is not even a language feature in Haskell). </p> <p> Your comment is, however, a reminder that I should consider how I phrase such notions in the future. Thank you for pointing that out. As I'm publishing and get feedback, I constantly learn new things. I'm always grateful when someone like you take the time to educate me. </p> <p> I'll see if I can improve in the future. I do, however, still have a backlog of articles I wrote months, or even more than a year, ago, so it's possible that more errors escape my attention when I proof read them before publication. If that happens, I'll appreciate more corrections. </p> </div> <div class="comment-date">2018-11-06 7:30 UTC</div> </div> <div class="comment" id="01d5086d3f9a4d93ad4bea131521bafa"> <div class="comment-author">Tyson Williams</div> <div class="comment-content"> <p> Thank you very much for your kind reply. I agree with everything you said. </p> <p> I will expand my comment a bit to give a clearer picture of my understanding. </p> <p> First, very little is "needed"; most things are merely sufficient. In particular, we don't <i>need</i> to overload your <code>Apply</code> method to achieve your goal. As <a href="/2018/10/15/an-applicative-password-list/#0ded7ac93aad8ba7b1063dd49c2051f1">I mentioned before</a>, it sufficies to have a single <code>Apply</code> method and instead create overloads of a function called <code>curry</code> that explicitly curries a given function. Furthermore, I think there is a sense in which this latter approach to overcome the lack of default currying is somehow minimal or most abstract or most general. </p> <p> Second, compared to languages like F# or Haskell, type inference is definitely weaker in C#. This issue was also present (in a subtle way) in your previous article, but I decided to largely ignore it in order to keep my comment more focused. In your <a href="/2018/10/15/an-applicative-password-list/">previous article</a>, you expliciltly defined the local variable <code>concat</code> like this <blockquote> <pre><span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;concat&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;(x,&nbsp;y,&nbsp;z,&nbsp;æ,&nbsp;ø,&nbsp;å)&nbsp;=&gt;&nbsp;x&nbsp;+&nbsp;y&nbsp;+&nbsp;z&nbsp;+&nbsp;æ&nbsp;+&nbsp;ø&nbsp;+&nbsp;å;</pre> </blockquote> In particular, you explicitly told the C# compiler that the type of all of these six variable is <code><span style="color:blue;">string</span></code>. That part was necessary; the type inference in C# is not strong enough to innfer (possibily in some use of <code>concat</code>) that the types could be <code><span style="color:blue;">string</span></code>. </p> <p> Suppose instead of defining <code>concat</code> as a local variable (with <code><span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;</code> as its type) you had defined it as a member method on a class. Then its type in C# is some kind "method group". The method group of a method essentially corresponds to the set of methods containing itself and its overloads. Then in order to pass <code>concat</code> into <code>curry</code>, there needs to be a type conversion (or cast) from its method group to <code><span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;</code>. This is also something that the C# system cannot do, and so Language Ext has overloads of a <a href="https://github.com/louthy/language-ext/blob/master/LanguageExt.Core/Prelude/Prelude_Func.cs#L24">function called <code>fun</code></a> to do this explicitly. Using it on our hypothetical member function <code>concat</code> would look like <pre>fun&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;(concat)</pre> Again, I think there is a sense in which this explicit way to specify non-inferable types is somehow minimal or most abstract or most general. </p> <p> My impression is that there is some low hanging fruit here for strengthing the type inference of the C# compiler. If a method group correpsonds to a singleton set (and that method has no <code>ref</code> or <code>out</code> arguments), then I would think it would be straight forward to consider an implicit cast from the method group to the corresponding <code>Func</code> or <code>Action</code> delegate. </p> </div> <div class="comment-date">2018-11-06 15:31 UTC</div> </div> </div> <hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Applicative combinations of functions https://blog.ploeh.dk/2018/10/22/applicative-combinations-of-functions 2018-10-22T10:21:00+00:00 Mark Seemann <div id="post"> <p> <em>Applicative lists and sequences enable you to create combinations of functions as well as values.</em> </p> <p> This article is an instalment in <a href="/2018/10/01/applicative-functors">an article series about applicative functors</a>. In the <a href="/2018/10/15/an-applicative-password-list">previous article</a>, you saw how you can use applicative lists and sequences to generate combinations of values; specifically, the example demonstrated how to generate various password character combinations. </p> <p> People often create passwords by using a common word as basis, and then turn characters into upper- or lower case. Someone feeling particularly tech-savvy may replace certain characters with digits, in an imitation of <a href="https://en.wikipedia.org/wiki/Leet">1337</a>. While this isn't secure, let's look at how to create various combinations of transformations using applicative lists and sequences. </p> <h3 id="302fedff3eed4c09b277bd4ee3523ff3"> List of functions <a href="#302fedff3eed4c09b277bd4ee3523ff3" title="permalink">#</a> </h3> <p> In the previous article, I mentioned that there was a feature of applicative lists that I had, so far, deliberately ignored. </p> <p> If you consider an example like this: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;passwordCombinations&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:navy;">sprintf</span>&nbsp;<span style="color:#a31515;">&quot;</span><span style="color:teal;">%s%s%s%s%s%s</span><span style="color:#a31515;">&quot;</span>] &nbsp;&nbsp;&nbsp;&nbsp;&lt;*&gt;&nbsp;[<span style="color:#a31515;">&quot;P&quot;</span>;&nbsp;<span style="color:#a31515;">&quot;p&quot;</span>]&nbsp;&lt;*&gt;&nbsp;[<span style="color:#a31515;">&quot;a&quot;</span>;&nbsp;<span style="color:#a31515;">&quot;4&quot;</span>]&nbsp;&lt;*&gt;&nbsp;[<span style="color:#a31515;">&quot;ssw&quot;</span>]&nbsp;&lt;*&gt;&nbsp;[<span style="color:#a31515;">&quot;o&quot;</span>;&nbsp;<span style="color:#a31515;">&quot;0&quot;</span>]&nbsp;&lt;*&gt;&nbsp;[<span style="color:#a31515;">&quot;rd&quot;</span>]&nbsp;&lt;*&gt;&nbsp;[<span style="color:#a31515;">&quot;&quot;</span>;&nbsp;<span style="color:#a31515;">&quot;!&quot;</span>]</pre> </p> <p> you may have already noticed that while the left side of the <code>&lt;*&gt;</code> operator is a list of functions, it contains only a single function. What happens if you supply more than a single function? </p> <p> You get a combination of each function and each list element. </p> <p> Assume that you have three functions to convert characters: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;<span style="color:teal;">Char</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;char&nbsp;-&gt;&nbsp;char</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">toUpper</span>&nbsp;c&nbsp;=&nbsp;System.<span style="color:teal;">Char</span>.<span style="color:navy;">ToUpperInvariant</span>&nbsp;c &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;char&nbsp;-&gt;&nbsp;char</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">toLower</span>&nbsp;c&nbsp;=&nbsp;System.<span style="color:teal;">Char</span>.<span style="color:navy;">ToLowerInvariant</span>&nbsp;c &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;Not&nbsp;even&nbsp;trying&nbsp;to&nbsp;be&nbsp;complete:</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;char&nbsp;-&gt;&nbsp;char</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;<span style="color:navy;">to1337</span>&nbsp;=&nbsp;<span style="color:blue;">function</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:#a31515;">&#39;a&#39;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:#a31515;">&#39;A&#39;</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#a31515;">&#39;4&#39;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:#a31515;">&#39;b&#39;</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#a31515;">&#39;6&#39;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:#a31515;">&#39;E&#39;</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#a31515;">&#39;3&#39;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:#a31515;">&#39;H&#39;</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#a31515;">&#39;#&#39;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:#a31515;">&#39;i&#39;</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#a31515;">&#39;!&#39;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:#a31515;">&#39;l&#39;</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#a31515;">&#39;1&#39;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:#a31515;">&#39;o&#39;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:#a31515;">&#39;O&#39;</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#a31515;">&#39;0&#39;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:#a31515;">&#39;t&#39;</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:#a31515;">&#39;+&#39;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;c&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;c</pre> </p> <p> All three are functions that convert one <code>char</code> value to another, although many values could pass through without being modified. Since they all have the same type, you can create a list of them: </p> <p> <pre>&gt; List.map String.map [Char.toUpper; Char.toLower; Char.to1337] &lt;*&gt; ["Hello"; "World"];; val it : string list = ["HELLO"; "WORLD"; "hello"; "world"; "#e110"; "W0r1d"]</pre> </p> <p> There's a bit to unpack there. Recall that all three functions in the <code>Char</code> module have the same type: <code>char -&gt; char</code>. Making a list of them gives you a <code>(char -&gt; char) list</code>, but you really need a <code>(string -&gt; string) list</code>. Fortunately, the built-in <code>String.map</code> function takes a <code>char -&gt; char</code> function and uses it to map each <code>char</code> values in a <code>string</code>. Thus, <code>List.map String.map [Char.toUpper; Char.toLower; Char.to1337]</code> gives you a <code>(string -&gt; string) list</code>. </p> <p> When you apply (<code>&lt;*&gt;</code>) that list of functions with a list of <code>string</code> values, you get all possible combinations of each function used with each string. Both <code>"Hello"</code> and <code>"World"</code> are converted to upper case, lower case, and 1337. </p> <h3 id="fde27240cfaf4479bc31ff50d3f619d0"> Combinations of functions <a href="#fde27240cfaf4479bc31ff50d3f619d0" title="permalink">#</a> </h3> <p> Perhaps you're happy with the above combinations, but can we do better? As an example, you'll notice that <code>to1337</code> only converts an upper-case <code>'E'</code> to <code>'3'</code>, but ignores a lower-case <code>'e'</code>. What if you also want the combination where <code>'e'</code> is first converted to upper case, and then to 1337? You'd like that, but you still want to retain the combinations where each of these transformations are applied without the other. </p> <p> Fear not; functions are values, so you can combine them as well! </p> <p> In the previous article, did you notice how you could model the presence or absence of a particular value? Specifically, the last character in the potential password could be <code>'!'</code>, but <code>'!'</code> could also be omitted. </p> <p> Consider, again, the expression for all password combinations: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;passwordCombinations&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:navy;">sprintf</span>&nbsp;<span style="color:#a31515;">&quot;</span><span style="color:teal;">%s%s%s%s%s%s</span><span style="color:#a31515;">&quot;</span>] &nbsp;&nbsp;&nbsp;&nbsp;&lt;*&gt;&nbsp;[<span style="color:#a31515;">&quot;P&quot;</span>;&nbsp;<span style="color:#a31515;">&quot;p&quot;</span>]&nbsp;&lt;*&gt;&nbsp;[<span style="color:#a31515;">&quot;a&quot;</span>;&nbsp;<span style="color:#a31515;">&quot;4&quot;</span>]&nbsp;&lt;*&gt;&nbsp;[<span style="color:#a31515;">&quot;ssw&quot;</span>]&nbsp;&lt;*&gt;&nbsp;[<span style="color:#a31515;">&quot;o&quot;</span>;&nbsp;<span style="color:#a31515;">&quot;0&quot;</span>]&nbsp;&lt;*&gt;&nbsp;[<span style="color:#a31515;">&quot;rd&quot;</span>]&nbsp;&lt;*&gt;&nbsp;[<span style="color:#a31515;">&quot;&quot;</span>;&nbsp;<span style="color:#a31515;">&quot;!&quot;</span>]</pre> </p> <p> Notice that the last list contains two options: <code>"!"</code> and the empty string (<code>""</code>). You can read about this in <a href="/2017/10/06/monoids">another article series</a>, but <a href="/2017/10/10/strings-lists-and-sequences-as-a-monoid">character strings are monoids</a>, and one of the characteristics of monoids is that they have an <em>identity</em> element - a 'neutral' element, if you will. For strings, it's <code>""</code>; you can append or prepend the empty string as much as you'd like, but it's not going to change the other string. </p> <p> If you have a set of <a href="/2017/11/13/endomorphism-monoid">functions of the type <code>'a -&gt; 'a</code>, then the built-in function <code>id</code> is the identity element</a>. You can compose any <code>'a -&gt; 'a</code> function with <code>id</code>, and it's not going to change the other function. </p> <p> Since functions are values, then, you can create <em>combinations of functions:</em> </p> <p> <pre><span style="color:green;">//&nbsp;(char&nbsp;-&gt;&nbsp;char)&nbsp;list</span> <span style="color:blue;">let</span>&nbsp;maps&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:blue;">fun</span>&nbsp;<span style="color:navy;">f</span>&nbsp;<span style="color:navy;">g</span>&nbsp;<span style="color:navy;">h</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">f</span>&nbsp;&gt;&gt;&nbsp;<span style="color:navy;">g</span>&nbsp;&gt;&gt;&nbsp;<span style="color:navy;">h</span>] &nbsp;&nbsp;&nbsp;&nbsp;&lt;*&gt;&nbsp;[<span style="color:teal;">Char</span>.<span style="color:navy;">toUpper</span>;&nbsp;<span style="color:navy;">id</span>] &nbsp;&nbsp;&nbsp;&nbsp;&lt;*&gt;&nbsp;[<span style="color:teal;">Char</span>.<span style="color:navy;">toLower</span>;&nbsp;<span style="color:navy;">id</span>] &nbsp;&nbsp;&nbsp;&nbsp;&lt;*&gt;&nbsp;[<span style="color:teal;">Char</span>.<span style="color:navy;">to1337</span>;&nbsp;<span style="color:navy;">id</span>]</pre> </p> <p> Here, <code>maps</code> is a list of functions, but it's not only three functions as in the above example. It's <em>eight</em> functions: </p> <p> <pre>&gt; List.length maps;; val it : int = 8</pre> </p> <p> The above applicative composition of <code>maps</code> combines three lists of functions. Each list presents two alternatives: a function (e.g. <code>Char.toUpper</code>), and <code>id</code>. In other words, a choice between doing something, and doing nothing. The lambda expression <code>fun f g h -&gt; f &gt;&gt; g &gt;&gt; h</code> takes three (curried) arguments, and returns the composition of calling <code>f</code>, then passing the result of that to <code>g</code>, and again passing the result of that to <code>h</code>. <code>f</code> is either <code>Char.toUpper</code> or <code>id</code>, <code>g</code> is either <code>Char.toLower</code> or <code>id</code>, and <code>h</code> is either <code>Char.to1337</code> or <code>id</code>. That's eight possible combinations. </p> <p> Combine eight functions with two <code>string</code> values, and you get sixteen alternatives back: </p> <p> <pre>&gt; List.map String.map maps &lt;*&gt; ["Hello"; "World"];; val it : string list = ["he110"; "w0r1d"; "hello"; "world"; "#3LL0"; "W0RLD"; "HELLO"; "WORLD"; "he110"; "w0r1d"; "hello"; "world"; "#e110"; "W0r1d"; "Hello"; "World"]</pre> </p> <p> Notice, for example, how one of the suggested alternatives is <code>"#3LL0"</code>. Previously, there was no translation from <code>'e'</code> to <code>'3'</code>, but now there is, via <code>Char.toUpper &gt;&gt; id &gt;&gt; Char.to1337</code>. </p> <p> Some of the combinations are redundant. For example, <code>"hello"</code> is generated twice, by <code>Char.toUpper &gt;&gt; Char.toLower &gt;&gt; id</code> and <code>id &gt;&gt; Char.toLower &gt;&gt; id</code>, respectively. You can reduce the output with <code>List.distinct</code>: </p> <p> <pre>&gt; List.map String.map maps &lt;*&gt; ["Hello"; "World"] |&gt; List.distinct;; val it : string list = ["he110"; "w0r1d"; "hello"; "world"; "#3LL0"; "W0RLD"; "HELLO"; "WORLD"; "#e110"; "W0r1d"; "Hello"; "World"]</pre> </p> <p> You can write equivalent code in Haskell, but it's so similar to the F# code that there's no reason to show it. </p> <h3 id="2000ef3aa60148e597de68d16048745f"> Translation to C# <a href="#2000ef3aa60148e597de68d16048745f" title="permalink">#</a> </h3> <p> Using the <code>Apply</code> extension methods from the previous article, you can translate the above code to C#. </p> <p> While you can use the .NET Base Class Library's <code>Char.ToUpperInvariant</code> and <code>Char.ToLowerInvariant</code> methods as is, you'll need to supply a <code>to1337</code> function. You can write it as a named static method, but you can also write it as a delegate: </p> <p> <pre><span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">char</span>,&nbsp;<span style="color:blue;">char</span>&gt;&nbsp;to1337&nbsp;=&nbsp;c&nbsp;=&gt; { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">switch</span>&nbsp;(c) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;<span style="color:#a31515;">&#39;A&#39;</span>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;<span style="color:#a31515;">&#39;a&#39;</span>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#a31515;">&#39;4&#39;</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;<span style="color:#a31515;">&#39;b&#39;</span>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#a31515;">&#39;6&#39;</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;<span style="color:#a31515;">&#39;E&#39;</span>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#a31515;">&#39;3&#39;</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;<span style="color:#a31515;">&#39;H&#39;</span>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#a31515;">&#39;#&#39;</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;<span style="color:#a31515;">&#39;i&#39;</span>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#a31515;">&#39;!&#39;</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;<span style="color:#a31515;">&#39;l&#39;</span>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#a31515;">&#39;1&#39;</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;<span style="color:#a31515;">&#39;o&#39;</span>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;<span style="color:#a31515;">&#39;O&#39;</span>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#a31515;">&#39;0&#39;</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">case</span>&nbsp;<span style="color:#a31515;">&#39;t&#39;</span>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;<span style="color:#a31515;">&#39;+&#39;</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">default</span>: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;c; &nbsp;&nbsp;&nbsp;&nbsp;} };</pre> </p> <p> You're also going to need an <code>id</code> function: </p> <p> <pre><span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">char</span>,&nbsp;<span style="color:blue;">char</span>&gt;&nbsp;id&nbsp;=&nbsp;c&nbsp;=&gt;&nbsp;c; </pre> </p> <p> In order to compose three functions to one, you can write something like this: </p> <p> <pre><span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">char</span>,&nbsp;<span style="color:blue;">char</span>&gt;,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">char</span>,&nbsp;<span style="color:blue;">char</span>&gt;,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">char</span>,&nbsp;<span style="color:blue;">char</span>&gt;,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">char</span>,&nbsp;<span style="color:blue;">char</span>&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;compose3&nbsp;=&nbsp;(f,&nbsp;g,&nbsp;h)&nbsp;=&gt;&nbsp;x&nbsp;=&gt;&nbsp;h(g(f(x)));</pre> </p> <p> That's going to be a contender for some of the most obscure C# code I've written in a while. By the double use of <code>=&gt;</code>, you can tell that it's a delegate that returns a delegate. That's not even the worst part: check out the type of the thing! In reality, nothing happens here that doesn't also happen in the above F# code, but it's an example of the superiority of <a href="https://en.wikipedia.org/wiki/Hindley%E2%80%93Milner_type_system">Hindley–Milner type inference</a>: in F#, you don't have to explicitly type out the type. </p> <p> With a function to compose three other functions, you can now apply the three alternative functions: </p> <p> <pre><span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">char</span>,&nbsp;<span style="color:blue;">char</span>&gt;&gt;&nbsp;maps&nbsp;=&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;compose3&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;.Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#2b91af;">Char</span>.ToUpperInvariant,&nbsp;id&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;.Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#2b91af;">Char</span>.ToLowerInvariant,&nbsp;id&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;.Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;to1337,&nbsp;id&nbsp;});</pre> </p> <p> Now you have a sequence of functions that translate <code>char</code> values to <code>char</code> values. What you really need, though, is a sequence of functions that translate <code>string</code> values to <code>string</code> values. </p> <p> The F# core library defines the built-in <code>String.map</code> function, but as far as I can tell, there's no equivalent method in the .NET Base Class Library. Therefore, you must implement it yourself: </p> <p> <pre><span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">char</span>,&nbsp;<span style="color:blue;">char</span>&gt;,&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;&gt;&nbsp;stringMap&nbsp;=&nbsp;f&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;(<span style="color:blue;">string</span>&nbsp;s)&nbsp;=&gt;&nbsp;<span style="color:blue;">new</span>&nbsp;<span style="color:blue;">string</span>(s.Select(f).ToArray());</pre> </p> <p> This is a function that takes a <code>Func&lt;char, char&gt;</code> as input and returns a <code>Func&lt;string, string&gt;</code>. Again, the type declaration isn't the prettiest. </p> <p> You can now apply <code>maps</code> to some <code>string</code> values, using the <code>Apply</code> extension method: </p> <p> <pre><span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;hellos&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;maps.Select(stringMap).Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;Hello&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;World&quot;</span>&nbsp;});</pre> </p> <p> This produces exactly the same output as the above F# example, even in the same order. </p> <p> Applicative functors are elegant in F# and Haskell, but awkward in a language like C# - mostly because of its inferior type inference engine. </p> <h3 id="001d02c7aa40400391b899f723ef9baa"> Summary <a href="#001d02c7aa40400391b899f723ef9baa" title="permalink">#</a> </h3> <p> Previous articles demonstrated how applicative lists can be used to compose several lists into a list that contains all possible combinations. In this article you saw how this also extends to combinations of functions. </p> <p> The last three articles (including the present) focus on lists as applicative functors, but lists aren't the only type of applicative functor. In the next articles, you'll encounter some other applicative functors. </p> <p> <strong>Next:</strong> <a href="/2018/10/29/the-maybe-applicative-functor">The Maybe applicative functor</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. An applicative password list https://blog.ploeh.dk/2018/10/15/an-applicative-password-list 2018-10-15T05:54:00+00:00 Mark Seemann <div id="post"> <p> <em>How to use the applicative functor capabilities of lists to create a password list, with examples that object-oriented programmers can understand.</em> </p> <p> This article is an instalment in <a href="/2018/10/01/applicative-functors">an article series about applicative functors</a>. In the <a href="/2018/10/08/full-deck">previous article</a>, you saw how to use <a href="https://www.haskell.org">Haskell</a> and <a href="https://fsharp.org">F#</a> lists as applicative functors to generate combinations of values. In this article, you'll see a similar example, but this time, there will also be a C# example. </p> <h3 id="ab0acd0982a64569a68ba92b041739a9"> Guess the password variation <a href="#ab0acd0982a64569a68ba92b041739a9" title="permalink">#</a> </h3> <p> Years ago, I worked in an organisation that (among other things) produced much demo software. Often, the demo software would include a demonstration of the security features of a product, which meant that, as a user evaluating the software, you had to log in with a user name and password. In order to keep things simple, the password was usually <em>Passw0rd!</em>, or some variation thereof. </p> <p> (Keep in mind that this was demoware. Password strength wasn't a concern. We explicitly wanted the password to be easy to guess, so that users evaluating the software had a chance to test how log in worked. This was long before social login and the like.) </p> <p> We had more than one package of demoware, and over the years, variations of the standard password had snuck in. Sometimes it'd be all lower-case; sometimes it'd use <em>4</em> instead of <em>a</em>, and so on. As the years went on, the number of possible permutations grew. </p> <p> Recently, I had a similar problem, but for security reasons, I don't want to divulge what it was. Let's just pretend that I had to guess one of those old demo passwords. </p> <p> There weren't <em>that</em> many possible variations, but just enough that I couldn't keep them systematically in my head. <ul> <li>The first letter could be upper or lower case.</li> <li>The second letter could be <em>a</em> or <em>4</em>.</li> <li>The <em>o</em> could be replaced with a zero (<em>0</em>).</li> <li>The password could end with an exclamation mark (<em>!</em>), but it might also be omitted.</li> </ul> Having recently discovered the power of lists as applicative functors, I started F# Interactive (FSI), and wrote the following: </p> <p> <pre>&gt; let (&lt;*&gt;) fs l = fs |&gt; List.collect (fun f -&gt; l |&gt; List.map f);; val ( &lt;*&gt; ) : fs:('a -&gt; 'b) list -&gt; l:'a list -&gt; 'b list &gt; [sprintf "%s%s%s%s%s%s"] &lt;*&gt; ["P"; "p"] &lt;*&gt; ["a"; "4"] &lt;*&gt; ["ssw"] &lt;*&gt; ["o"; "0"] &lt;*&gt; ["rd"] &lt;*&gt; [""; "!"];; val it : string list = ["Password"; "Password!"; "Passw0rd"; "Passw0rd!"; "P4ssword"; "P4ssword!"; "P4ssw0rd"; "P4ssw0rd!"; "password"; "password!"; "passw0rd"; "passw0rd!"; "p4ssword"; "p4ssword!"; "p4ssw0rd"; "p4ssw0rd!"]</pre> </p> <p> This produces a list of all the possible password combinations according to the above rules. Since there weren't that many, I could start trying each from the start, until I found the correct variation. </p> <p> The first list contains a single function. Due to the way <code>sprintf</code> works, <code>sprintf "%s%s%s%s%s%s"</code> is a function that takes six (curried) <code>string</code> arguments, and returns a <code>string</code>. The number 6 is no coincidence, because you'll notice that the <code>&lt;*&gt;</code> operator is used six times. </p> <p> There's no reason to repeat the exegesis from the previous article, but briefly: <ol> <li><code>sprintf "%s%s%s%s%s%s"</code> has the type <code>string -&gt; string -&gt; string -&gt; string -&gt; string -&gt; string -&gt; string</code>.</li> <li><code>[sprintf "%s%s%s%s%s%s"]</code> has the type <code>(string -&gt; string -&gt; string -&gt; string -&gt; string -&gt; string -&gt; string) list</code>.</li> <li><code>[sprintf "%s%s%s%s%s%s"] &lt;*&gt; ["P"; "p"]</code> has the type <code>(string -&gt; string -&gt; string -&gt; string -&gt; string -&gt; string) list</code>.</li> <li><code>[sprintf "%s%s%s%s%s%s"] &lt;*&gt; ["P"; "p"] &lt;*&gt; ["a"; "4"]</code> has the type <code>(string -&gt; string -&gt; string -&gt; string -&gt; string) list</code>.</li> <li>...and so on.</li> </ol> Notice that every time you add another list with <code>&lt;*&gt;</code>, an argument is removed from the resulting function contained in the returned list. When you've applied six lists with the <code>&lt;*&gt;</code> operator, the return value is no longer a list of functions, but a list of values. </p> <p> Clearly, then, that's no coincidence. I deliberately shaped the initial function to take six arguments, so that it would match the six segments I wanted to model. </p> <p> Perhaps the most interesting quality of applicative functors is that you can compose an arbitrary number of objects, as long as you have a function to match the number of arguments. </p> <h3 id="25e1617d768a4705a9756f230ddd9b39"> Haskell <a href="#25e1617d768a4705a9756f230ddd9b39" title="permalink">#</a> </h3> <p> This time I started with F#, but in Haskell, <code>&lt;*&gt;</code> is a built-in operator, so obviously this also works there: </p> <p> <pre><span style="color:#600277;">passwordCombinations</span>&nbsp;::&nbsp;[String] passwordCombinations&nbsp;<span style="color:#666666;">=</span> &nbsp;&nbsp;[printf&nbsp;<span style="color:#a31515;">&quot;%s%s%s%s%s%s&quot;</span>] &nbsp;&nbsp;<span style="color:#666666;">&lt;*&gt;</span>&nbsp;[<span style="color:#a31515;">&quot;P&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;p&quot;</span>]&nbsp;<span style="color:#666666;">&lt;*&gt;</span>&nbsp;[<span style="color:#a31515;">&quot;a&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;4&quot;</span>]&nbsp;<span style="color:#666666;">&lt;*&gt;</span>&nbsp;[<span style="color:#a31515;">&quot;ssw&quot;</span>]&nbsp;<span style="color:#666666;">&lt;*&gt;</span>&nbsp;[<span style="color:#a31515;">&quot;o&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;0&quot;</span>]&nbsp;<span style="color:#666666;">&lt;*&gt;</span>&nbsp;[<span style="color:#a31515;">&quot;rd&quot;</span>]&nbsp;<span style="color:#666666;">&lt;*&gt;</span>&nbsp;[<span style="color:#a31515;">&quot;&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;!&quot;</span>]</pre> </p> <p> The output is the same as the above F# code. </p> <h3 id="9c3412586a58460c8280212a13351331"> C# <a href="#9c3412586a58460c8280212a13351331" title="permalink">#</a> </h3> <p> While you can translate the concept of lists as an applicative functor to C#, this is where you start testing the limits of the language; or perhaps I've simply forgotten too much C# to do it full justice. </p> <p> Instead of making linked lists an applicative functor, let's consider a type closer to the spirit of the C# language: <code>IEnumerable&lt;T&gt;</code>. The following code attempts to turn <code>IEnumerable&lt;T&gt;</code> into an applicative functor. </p> <p> Consider the above F# implementation of <code>&lt;*&gt;</code> (explained in the previous article). It uses <code>List.collect</code> to flatten what would otherwise had been a list of lists. <code>List.collect</code> has the type <code>('a -&gt; 'b list) -&gt; 'a list -&gt; 'b list</code>. The corresponding method for <code>IEnumerable&lt;T&gt;</code> already exists in the .NET Base Class Library; it's called <a href="https://msdn.microsoft.com/en-us/library/bb534336">SelectMany</a>. (Incidentally, this is also the monadic <em>bind</em> function, but this is <em>still</em> not a monad tutorial.) </p> <p> For an applicative functor, we need a method that takes a sequence of functions, and a sequence of values, and produces a sequence of return values. You can translate the above F# function to this C# extension method: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;Apply&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;&nbsp;selectors, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;source) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;selectors.SelectMany(source.Select); }</pre> </p> <p> That's a single line of code! That's not so bad. What's the problem? </p> <p> So far there's no problem. You can, for example, write code like this: </p> <p> <pre><span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">int</span>&gt;&nbsp;sl&nbsp;=&nbsp;s&nbsp;=&gt;&nbsp;s.Length; <span style="color:blue;">var</span>&nbsp;lengths&nbsp;=&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;sl&nbsp;}.Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;foo&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;bar&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;baz&quot;</span>&nbsp;});</pre> </p> <p> This will return a sequence of the numbers <code>3, 3, 3</code>. That seems, however, like quite a convoluted way of getting the lengths of some strings. A normal <code>Select</code> method would have sufficed. </p> <p> Is it possible to repeat the above password enumeration in C#? In order to do that, you need a function that takes six <code>string</code> arguments and returns a <code>string</code>: </p> <p> <pre><span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;concat&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;(x,&nbsp;y,&nbsp;z,&nbsp;æ,&nbsp;ø,&nbsp;å)&nbsp;=&gt;&nbsp;x&nbsp;+&nbsp;y&nbsp;+&nbsp;z&nbsp;+&nbsp;æ&nbsp;+&nbsp;ø&nbsp;+&nbsp;å;</pre> </p> <p> With programmers' penchant to start with the variable name <code>x</code>, and continue with <code>y</code> and <code>z</code>, most people will have a problem with six variables - but not us Danes! Fortunately, we've officially added three extra letters to our alphabet for this very purpose! So with that small problem out of the way, you can now attempt to reproduce the above F# code: </p> <p> <pre><span style="color:blue;">var</span>&nbsp;combinations&nbsp;=&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;concat&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;.Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;P&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;p&quot;</span>&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;.Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;a&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;4&quot;</span>&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;.Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;ssw&quot;</span>&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;.Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;o&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;0&quot;</span>&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;.Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;rd&quot;</span>&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;.Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;!&quot;</span>&nbsp;});</pre> </p> <p> That looks promising, but there's one problem: <em>it doesn't compile</em>. </p> <p> The problem is that <code>concat</code> is a function that takes six arguments, and the above <code>Apply</code> method expects <code>selectors</code> to be functions that take exactly one argument. </p> <p> Alas, while it's not pretty, you can attempt to address the problem with an overload: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>,&nbsp;<span style="color:#2b91af;">T4</span>,&nbsp;<span style="color:#2b91af;">T5</span>,&nbsp;<span style="color:#2b91af;">T6</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;&nbsp;Apply&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>,&nbsp;<span style="color:#2b91af;">T4</span>,&nbsp;<span style="color:#2b91af;">T5</span>,&nbsp;<span style="color:#2b91af;">T6</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>,&nbsp;<span style="color:#2b91af;">T4</span>,&nbsp;<span style="color:#2b91af;">T5</span>,&nbsp;<span style="color:#2b91af;">T6</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;&nbsp;selectors, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">T1</span>&gt;&nbsp;source) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;selectors.SelectMany(f&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;source.Select(x&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>,&nbsp;<span style="color:#2b91af;">T4</span>,&nbsp;<span style="color:#2b91af;">T5</span>,&nbsp;<span style="color:#2b91af;">T6</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;g&nbsp;=&nbsp;(y,&nbsp;z,&nbsp;æ,&nbsp;ø,&nbsp;å)&nbsp;=&gt;&nbsp;f(x,&nbsp;y,&nbsp;z,&nbsp;æ,&nbsp;ø,&nbsp;å); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;g; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;})); }</pre> </p> <p> This overload of <code>Apply</code> takes <code>selectors</code> of arity six, and return a sequence of functions with arity five. </p> <p> Does it work now, then? </p> <p> Unfortunately, it still doesn't compile, because <code>new[] { concat }.Apply(new[] { "P", "p" })</code> has the type <code>IEnumerable&lt;Func&lt;string, string, string, string, string, string&gt;&gt;</code>, and no overload of <code>Apply</code> exists that supports <code>selectors</code> with arity five. </p> <p> You'll have to add such an overload as well: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>,&nbsp;<span style="color:#2b91af;">T4</span>,&nbsp;<span style="color:#2b91af;">T5</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;&nbsp;Apply&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>,&nbsp;<span style="color:#2b91af;">T4</span>,&nbsp;<span style="color:#2b91af;">T5</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T1</span>,&nbsp;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>,&nbsp;<span style="color:#2b91af;">T4</span>,&nbsp;<span style="color:#2b91af;">T5</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;&nbsp;selectors, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">T1</span>&gt;&nbsp;source) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;selectors.SelectMany(f&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;source.Select(x&nbsp;=&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T2</span>,&nbsp;<span style="color:#2b91af;">T3</span>,&nbsp;<span style="color:#2b91af;">T4</span>,&nbsp;<span style="color:#2b91af;">T5</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;g&nbsp;=&nbsp;(y,&nbsp;z,&nbsp;æ,&nbsp;ø)&nbsp;=&gt;&nbsp;f(x,&nbsp;y,&nbsp;z,&nbsp;æ,&nbsp;ø); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;g; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;})); }</pre> </p> <p> You can probably see where this is going. This overload returns a sequence of functions with arity four, so you'll have to add an <code>Apply</code> overload for such functions as well, plus for functions with arity three and two. Once you've done that, the above <a href="https://martinfowler.com/bliki/FluentInterface.html">fluent</a> chain of <code>Apply</code> method calls work, and you get a sequence containing all the password variations. </p> <p> <pre>&gt; <span style="color:blue;">new</span>[]&nbsp;{&nbsp;concat&nbsp;} . .Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;P&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;p&quot;</span>&nbsp;}) . .Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;a&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;4&quot;</span>&nbsp;}) . .Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;ssw&quot;</span>&nbsp;}) . .Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;o&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;0&quot;</span>&nbsp;}) . .Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;rd&quot;</span>&nbsp;}) . .Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;!&quot;</span>&nbsp;}) string[16] { "Password", "Password!", "Passw0rd", "Passw0rd!", "P4ssword", "P4ssword!", "P4ssw0rd", "P4ssw0rd!", "password", "password!", "passw0rd", "passw0rd!", "p4ssword", "p4ssword!", "p4ssw0rd", "p4ssw0rd!" }</pre> </p> <p> In F# and Haskell, the compiler automatically figures out the return type of each application, due to a combination of currying and <a href="https://en.wikipedia.org/wiki/Hindley%E2%80%93Milner_type_system">Hindley–Milner type inference</a>. Perhaps I've allowed my C# skills to atrophy, but I don't think there's an easy resolution to this problem in C#. </p> <p> Obviously, you can always write a reusable library with <code>Apply</code> overloads that support up to some absurd arity. Once those methods are written, they're unlikely to change. Still, it seems to me that we're pushing the envelope. </p> <h3 id="5f60025e198f42ecbba731a0869217ed"> Summary <a href="#5f60025e198f42ecbba731a0869217ed" title="permalink">#</a> </h3> <p> In this article, you saw how to turn C# sequences into an applicative functor. While possible, there are some bumps in the road. </p> <p> There's still an aspect of using lists and sequences as applicative functors that I've been deliberately ignoring so far. The next article covers that. After that, we'll take a break from lists and look at some other applicative functors. </p> <p> <strong>Next:</strong> <a href="/2018/10/22/applicative-combinations-of-functions">Applicative combinations of functions</a>. </p> </div> <div id="comments"> <hr> <h2 id="comments-header"> Comments </h2> <div class="comment" id="0ded7ac93aad8ba7b1063dd49c2051f1"> <div class="comment-author">Tyson Williams</div> <div class="comment-content"> <blockquote> In F# and Haskell, the compiler automatically figures out the return type of each application, due to a combination of currying and Hindley–Milner type inference. Perhaps I've allowed my C# skills to atrophy, but I don't think there's an easy resolution to this problem in C#. </blockquote> <p> I think the difference is that all functions in F# and Haskell are automatically curried, but nothing is automatically curreid in C#. If you explicitly curry <code>concat</code>, then the code complies and works as expected. Here is one way to achieve that. </p> <p> <pre><span style="color:blue;">var</span>&nbsp;combinations&nbsp;=&nbsp;<span style="color:blue;">new</span>[]&nbsp;{&nbsp;LanguageExt.<span style="color:#2b91af;">Prelude</span>.curry(concat)&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;.Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;P&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;p&quot;</span>&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;.Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;a&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;4&quot;</span>&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;.Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;ssw&quot;</span>&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;.Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;o&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;0&quot;</span>&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;.Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;rd&quot;</span>&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;.Apply(<span style="color:blue;">new</span>[]&nbsp;{&nbsp;<span style="color:#a31515;">&quot;&quot;</span>,&nbsp;<span style="color:#a31515;">&quot;!&quot;</span>&nbsp;});</pre> </p> <p> In this example, I curried <code>concat</code> using <a href="https://github.com/louthy/language-ext/blob/master/LanguageExt.Core/Prelude/Prelude_Curry.cs#L65"><code>curry</code></a> from the NuGet package <a href="https://github.com/louthy/language-ext">LanguageExt</a>. It is a base class library for functional programming in C#. </p> <p> So you don't need many overloads of your <code>Apply</code> for varrying numbers of type parameters. You just need many overloads of <code>curry</code>. </p> </div> <div class="comment-date">2018-10-18 01:45 UTC</div> </div> <div class="comment" id="b4e76681ea894aa3be1e6b836343c148"> <div class="comment-author"><a href="/">Mark Seemann</a></div> <div class="comment-content"> <p> Tyson, thank you for writing. Yes, you're right that it's the lack of default currying that makes this sort of style less than ideal in C#. This seems to be clearly demonstrated by your example. </p> </div> <div class="comment-date">2018-10-30 7:43 UTC</div> </div> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Full deck https://blog.ploeh.dk/2018/10/08/full-deck 2018-10-08T06:17:00+00:00 Mark Seemann <div id="post"> <p> <em>An introduction to applicative functors in Haskell, with a translation to F#.</em> </p> <p> This article is an instalment in <a href="/2018/10/01/applicative-functors">an article series about applicative functors</a>. While (non-applicative) <a href="/2018/03/22/functors">functors</a> can be translated to an object-oriented language like C# in a straightforward manner, applicative functors are more entrenched in functional languages like <a href="https://www.haskell.org">Haskell</a>. This article introduces the concept with a motivating example in Haskell, and also shows a translation to <a href="https://fsharp.org">F#</a>. In the next article, you'll also see how to implement an applicative functor in C#. </p> <h3 id="bdf22a40f9f04b6b9929f46dbafe7911"> Deck of cards in Haskell <a href="#bdf22a40f9f04b6b9929f46dbafe7911" title="permalink">#</a> </h3> <p> Imagine that you want to model a card game. In order to do so, you start by defining data types for suits, faces, and cards: </p> <p> <pre><span style="color:blue;">data</span>&nbsp;<span style="color:#dd0000;">Suit</span>&nbsp;<span style="color:#666666;">=</span> &nbsp;&nbsp;<span style="color:#dd0000;">Diamonds</span>&nbsp;<span style="color:#666666;">|</span>&nbsp;<span style="color:#dd0000;">Hearts</span>&nbsp;<span style="color:#666666;">|</span>&nbsp;<span style="color:#dd0000;">Clubs</span>&nbsp;<span style="color:#666666;">|</span>&nbsp;<span style="color:#dd0000;">Spades</span>&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#a31515;">Show</span>,&nbsp;<span style="color:#a31515;">Eq</span>,&nbsp;<span style="color:#a31515;">Enum</span>,&nbsp;<span style="color:#a31515;">Bounded</span>) <span style="color:blue;">data</span>&nbsp;<span style="color:#dd0000;">Face</span>&nbsp;<span style="color:#666666;">=</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#dd0000;">Two</span>&nbsp;&nbsp;<span style="color:#666666;">|</span>&nbsp;<span style="color:#dd0000;">Three</span>&nbsp;<span style="color:#666666;">|</span>&nbsp;<span style="color:#dd0000;">Four</span>&nbsp;<span style="color:#666666;">|</span>&nbsp;<span style="color:#dd0000;">Five</span>&nbsp;<span style="color:#666666;">|</span>&nbsp;<span style="color:#dd0000;">Six</span>&nbsp;<span style="color:#666666;">|</span>&nbsp;<span style="color:#dd0000;">Seven</span>&nbsp;<span style="color:#666666;">|</span>&nbsp;<span style="color:#dd0000;">Eight</span>&nbsp;<span style="color:#666666;">|</span>&nbsp;<span style="color:#dd0000;">Nine</span>&nbsp;<span style="color:#666666;">|</span>&nbsp;<span style="color:#dd0000;">Ten</span> &nbsp;&nbsp;<span style="color:#666666;">|</span>&nbsp;<span style="color:#dd0000;">Jack</span>&nbsp;<span style="color:#666666;">|</span>&nbsp;<span style="color:#dd0000;">Queen</span>&nbsp;<span style="color:#666666;">|</span>&nbsp;<span style="color:#dd0000;">King</span>&nbsp;<span style="color:#666666;">|</span>&nbsp;<span style="color:#dd0000;">Ace</span> &nbsp;&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#a31515;">Show</span>,&nbsp;<span style="color:#a31515;">Eq</span>,&nbsp;<span style="color:#a31515;">Enum</span>,&nbsp;<span style="color:#a31515;">Bounded</span>) <span style="color:blue;">data</span>&nbsp;<span style="color:#dd0000;">Card</span>&nbsp;<span style="color:#666666;">=</span>&nbsp;<span style="color:#dd0000;">Card</span>&nbsp;{&nbsp;face&nbsp;<span style="color:#666666;">::</span>&nbsp;<span style="color:#dd0000;">Face</span>,&nbsp;suit&nbsp;<span style="color:#666666;">::</span>&nbsp;<span style="color:#dd0000;">Suit</span>&nbsp;}&nbsp;<span style="color:blue;">deriving</span>&nbsp;(<span style="color:#a31515;">Show</span>,&nbsp;<span style="color:#a31515;">Eq</span>)</pre> </p> <p> Since both <code>Suit</code> and <code>Face</code> are instances of the <code>Enum</code> and <code>Bounded</code> typeclasses, you can easily enumerate them: </p> <p> <pre><span style="color:#600277;">allFaces</span>&nbsp;::&nbsp;[<span style="color:blue;">Face</span>] allFaces&nbsp;<span style="color:#666666;">=</span>&nbsp;[minBound&nbsp;<span style="color:#666666;">..</span>&nbsp;maxBound] <span style="color:#600277;">allSuits</span>&nbsp;::&nbsp;[<span style="color:blue;">Suit</span>] allSuits&nbsp;<span style="color:#666666;">=</span>&nbsp;[minBound&nbsp;<span style="color:#666666;">..</span>&nbsp;maxBound]</pre> </p> <p> For example, <code>allSuits</code> enumerates all four <code>Suit</code> values: </p> <p> <pre>λ&gt; allSuits [Diamonds,Hearts,Clubs,Spades]</pre> </p> <p> Notice, by the way, how the code for <code>allFaces</code> and <code>allSuits</code> is identical. The behaviour, however, is different, because the types are different. </p> <p> While you can enumerate suits and faces, <em>how do you create a full deck of cards?</em> </p> <p> A full deck of cards should contain one card of every possible combination of suit and face. Here's one way to do it, taking advantage of lists being applicative functors: </p> <p> <pre><span style="color:#600277;">fullDeck</span>&nbsp;::&nbsp;[<span style="color:blue;">Card</span>] fullDeck&nbsp;<span style="color:#666666;">=</span>&nbsp;pure&nbsp;<span style="color:#dd0000;">Card</span>&nbsp;<span style="color:#666666;">&lt;*&gt;</span>&nbsp;allFaces&nbsp;<span style="color:#666666;">&lt;*&gt;</span>&nbsp;allSuits</pre> </p> <p> This will give you all the possible cards. Here are the first six: </p> <p> <pre>λ&gt; forM_ (take 6 fullDeck) print Card {face = Two, suit = Diamonds} Card {face = Two, suit = Hearts} Card {face = Two, suit = Clubs} Card {face = Two, suit = Spades} Card {face = Three, suit = Diamonds} Card {face = Three, suit = Hearts}</pre> </p> <p> How does it work? Let's break it down, starting from left: </p> <p> <pre>λ&gt; :type Card Card :: Face -&gt; Suit -&gt; Card λ&gt; :type pure Card pure Card :: Applicative f =&gt; f (Face -&gt; Suit -&gt; Card) λ&gt; :type pure Card &lt;*&gt; allFaces pure Card &lt;*&gt; allFaces :: [Suit -&gt; Card] λ&gt; :type pure Card &lt;*&gt; allFaces &lt;*&gt; allSuits pure Card &lt;*&gt; allFaces &lt;*&gt; allSuits :: [Card]</pre> </p> <p> From the top, <code>Card</code> is a function that takes a <code>Face</code> value and a <code>Suit</code> value and returns a <code>Card</code> value. Object-oriented programmers can think of it as a constructor. </p> <p> Next, <code>pure Card</code> is the <code>Card</code> function, elevated to an applicative functor. At this point, the compiler hasn't decided which particular applicative functor it is; it could be any applicative functor. Specifically, it turns out to be the list type (<code>[]</code>), which means that <code>pure Card</code> has the type <code>[Face -&gt; Suit -&gt; Card]</code>. That is: it's a list of functions, but a list of only a single function. At this point, however, this is still premature. The type doesn't materialise until we apply the second expression. </p> <p> The type of <code>allFaces</code> is clearly <code>[Face]</code>. Since the <code>&lt;*&gt;</code> operator has the type <code>Applicative f =&gt; f (a -&gt; b) -&gt; f a -&gt; f b</code>, the expression on the left must be the same functor as the expression on the right. The list type (<code>[]</code>) is an applicative functor, and because <code>allFaces</code> is a list, then <code>pure Card</code> must also be a list, in the expression <code>pure Card &lt;*&gt; allFaces</code>. In other words, in the definition of <code>&lt;*&gt;</code>, you can substitute <code>f</code> with <code>[]</code>, and <code>a</code> with <code>Face</code>. The interim result is <code>[Face -&gt; b] -&gt; [Face] -&gt; [b]</code>. What is <code>b</code>, then? </p> <p> You already know that <code>pure Card</code> has the type <code>[Face -&gt; Suit -&gt; Card]</code>, so <code>b</code> must be <code>Suit -&gt; Card</code>. That's the reason that <code>pure Card &lt;*&gt; allFaces</code> has the type <code>[Suit -&gt; Card]</code>. It's a list of functions. This means that you can use <code>&lt;*&gt;</code> a second time, this time with <code>allSuits</code>, which has the type <code>[Suit]</code>. </p> <p> Using the same line of reasoning as before, you can substitute <code>Suit</code> for <code>a</code> in the type of <code>&lt;*&gt;</code>, and you get <code>[Suit -&gt; b] -&gt; [Suit] -&gt; [b]</code>. What is <code>b</code> now? From the previous step, you know that the expression on the left has the type <code>[Suit -&gt; Card]</code>, so <code>b</code> must be <code>Card</code>. That's why the entire expression has the type <code>[Card]</code>. </p> <h3 id="570f38cdb46b420e904fb656faa662ff"> Deck of cards in F# <a href="#570f38cdb46b420e904fb656faa662ff" title="permalink">#</a> </h3> <p> You can translate the above Haskell code to F#. The first step is to make F# lists applicative. F# also supports custom operators, so you can add a function called <code>&lt;*&gt;</code>: </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b)&nbsp;list&nbsp;-&gt;&nbsp;&#39;a&nbsp;list&nbsp;-&gt;&nbsp;&#39;b&nbsp;list</span> <span style="color:blue;">let</span>&nbsp;(&lt;*&gt;)&nbsp;fs&nbsp;l&nbsp;=&nbsp;fs&nbsp;|&gt;&nbsp;<span style="color:teal;">List</span>.<span style="color:navy;">collect</span>&nbsp;(<span style="color:blue;">fun</span>&nbsp;<span style="color:navy;">f</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;l&nbsp;|&gt;&nbsp;<span style="color:teal;">List</span>.<span style="color:navy;">map</span>&nbsp;<span style="color:navy;">f</span>)</pre> </p> <p> This implementation iterates over all the functions in <code>fs</code>; for each function, it maps the list <code>l</code> with that function. Had you done that with a normal <code>List.map</code>, this would have returned a list of lists, but by using <code>List.collect</code>, you flatten the list. </p> <p> It's worth noting that this isn't the only way you could have implemented <code>&lt;*&gt;</code>, but this is the implementation that behaves like the Haskell function. An alternative implementation could have been this: </p> <p> <pre><span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b)&nbsp;list&nbsp;-&gt;&nbsp;&#39;a&nbsp;list&nbsp;-&gt;&nbsp;&#39;b&nbsp;list</span> <span style="color:blue;">let</span>&nbsp;(&lt;*&gt;)&nbsp;fs&nbsp;=&nbsp;<span style="color:teal;">List</span>.<span style="color:navy;">collect</span>&nbsp;(<span style="color:blue;">fun</span>&nbsp;x&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;fs&nbsp;|&gt;&nbsp;<span style="color:teal;">List</span>.<span style="color:navy;">map</span>&nbsp;(<span style="color:blue;">fun</span>&nbsp;<span style="color:navy;">f</span>&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;<span style="color:navy;">f</span>&nbsp;x))</pre> </p> <p> This variation has the same type as the first example, and still returns all combinations, but the order is different. In the following of this article, as well as in subsequent articles, I'll use the first version. </p> <p> Here's the playing cards example translated to F#: </p> <p> <pre><span style="color:blue;">type</span>&nbsp;<span style="color:teal;">Suit</span>&nbsp;=&nbsp;<span style="color:navy;">Diamonds</span>&nbsp;|&nbsp;<span style="color:navy;">Hearts</span>&nbsp;|&nbsp;<span style="color:navy;">Clubs</span>&nbsp;|&nbsp;<span style="color:navy;">Spades</span> <span style="color:blue;">type</span>&nbsp;<span style="color:teal;">Face</span>&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;<span style="color:navy;">Two</span>&nbsp;|&nbsp;<span style="color:navy;">Three</span>&nbsp;|&nbsp;<span style="color:navy;">Four</span>&nbsp;|&nbsp;<span style="color:navy;">Five</span>&nbsp;|&nbsp;<span style="color:navy;">Six</span>&nbsp;|&nbsp;<span style="color:navy;">Seven</span>&nbsp;|&nbsp;<span style="color:navy;">Eight</span>&nbsp;|&nbsp;<span style="color:navy;">Nine</span>&nbsp;|&nbsp;<span style="color:navy;">Ten</span> &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;<span style="color:navy;">Jack</span>&nbsp;|&nbsp;<span style="color:navy;">Queen</span>&nbsp;|&nbsp;<span style="color:navy;">King</span>&nbsp;|&nbsp;<span style="color:navy;">Ace</span> <span style="color:blue;">type</span>&nbsp;<span style="color:teal;">Card</span>&nbsp;=&nbsp;{&nbsp;Face:&nbsp;<span style="color:teal;">Face</span>;&nbsp;Suit&nbsp;:&nbsp;<span style="color:teal;">Suit</span>&nbsp;} <span style="color:green;">//&nbsp;Face&nbsp;list</span> <span style="color:blue;">let</span>&nbsp;allFaces&nbsp;=&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">Two</span>;&nbsp;<span style="color:navy;">Three</span>;&nbsp;<span style="color:navy;">Four</span>;&nbsp;<span style="color:navy;">Five</span>;&nbsp;<span style="color:navy;">Six</span>;&nbsp;<span style="color:navy;">Seven</span>;&nbsp;<span style="color:navy;">Eight</span>;&nbsp;<span style="color:navy;">Nine</span>;&nbsp;<span style="color:navy;">Ten</span>; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:navy;">Jack</span>;&nbsp;<span style="color:navy;">Queen</span>;&nbsp;<span style="color:navy;">King</span>;&nbsp;<span style="color:navy;">Ace</span>] <span style="color:green;">//&nbsp;Suit&nbsp;list</span> <span style="color:blue;">let</span>&nbsp;allSuits&nbsp;=&nbsp;[<span style="color:navy;">Diamonds</span>;&nbsp;<span style="color:navy;">Hearts</span>;&nbsp;<span style="color:navy;">Clubs</span>;&nbsp;<span style="color:navy;">Spades</span>] <span style="color:green;">//&nbsp;Card&nbsp;list</span> <span style="color:blue;">let</span>&nbsp;fullDeck&nbsp;=&nbsp;[<span style="color:blue;">fun</span>&nbsp;f&nbsp;s&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;{&nbsp;Face&nbsp;=&nbsp;f;&nbsp;Suit&nbsp;=&nbsp;s&nbsp;}]&nbsp;&lt;*&gt;&nbsp;allFaces&nbsp;&lt;*&gt;&nbsp;allSuits</pre> </p> <p> The F# code is slightly more verbose than the Haskell code, because you have to repeat all the cases for <code>Suit</code> and <code>Face</code>. You can't enumerate them automatically, like you can in Haskell. </p> <p> It didn't make much sense to me to attempt to define a <code>pure</code> function, so instead I simply inserted a single lambda expression in a list, using the normal square-bracket syntax. F# doesn't have constructors for record types, so you have to pass a lambda expression, whereas in Haskell, you could simply use the <code>Card</code> function. </p> <p> The result is the same, though: </p> <p> <pre>&gt; fullDeck |&gt; List.take 6 |&gt; List.iter (printfn "%A");; {Face = Two; Suit = Diamonds;} {Face = Two; Suit = Hearts;} {Face = Two; Suit = Clubs;} {Face = Two; Suit = Spades;} {Face = Three; Suit = Diamonds;} {Face = Three; Suit = Hearts;}</pre> </p> <p> While the mechanics of applicative functors translate well to F#, it leaves you with at least one problem. If you add the above operator <code>&lt;*&gt;</code>, you've now 'used up' that operator for lists. While you <em>can</em> define an operator of the same name for e.g. <code>option</code>, you'd have to put them in separate modules or namespaces in order to prevent them from colliding. This also means that you can't easily use them together. </p> <p> For that reason, I wouldn't consider this the most <a href="/2015/08/03/idiomatic-or-idiosyncratic">idiomatic</a> way to create a full deck of cards in F#. Normally, I'd do this instead: </p> <p> <pre><span style="color:green;">//&nbsp;Card&nbsp;list</span> <span style="color:blue;">let</span>&nbsp;fullDeck&nbsp;=&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">for</span>&nbsp;suit&nbsp;<span style="color:blue;">in</span>&nbsp;allSuits&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">for</span>&nbsp;face&nbsp;<span style="color:blue;">in</span>&nbsp;allFaces&nbsp;<span style="color:blue;">do</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">yield</span>&nbsp;{&nbsp;Face&nbsp;=&nbsp;face;&nbsp;Suit&nbsp;=&nbsp;suit&nbsp;}&nbsp;]</pre> </p> <p> This alternative syntax takes advantage of F#'s 'extended list comprehension' syntax. FWIW, you could have done something similar in Haskell: </p> <p> <pre><span style="color:#600277;">fullDeck</span>&nbsp;::&nbsp;[<span style="color:blue;">Card</span>] fullDeck&nbsp;<span style="color:#666666;">=</span>&nbsp;[<span style="color:#dd0000;">Card</span>&nbsp;f&nbsp;s&nbsp;<span style="color:#666666;">|</span>&nbsp;f&nbsp;<span style="color:#666666;">&lt;-</span>&nbsp;allFaces,&nbsp;s&nbsp;<span style="color:#666666;">&lt;-</span>&nbsp;allSuits]</pre> </p> <p> List comprehension, however, is (as the name implies) specific to lists, whereas an <em>applicative functor</em> is a more general concept. </p> <h3 id="cef0030207354d1fbf0ab2c1a791eb84"> Summary <a href="#cef0030207354d1fbf0ab2c1a791eb84" title="permalink">#</a> </h3> <p> This article introduced you to lists as an applicative functor, using the motivating example of having to populate a full deck of cards with all possible combinations of suits and faces. </p> <p> The next article in the series shows another list example. The F# and Haskell code will be similar to the code in the present article, but the next article will also include a translation to C#. </p> <p> <strong>Next:</strong> <a href="/2018/10/15/an-applicative-password-list">An applicative password list</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Applicative functors https://blog.ploeh.dk/2018/10/01/applicative-functors 2018-10-01T06:34:00+00:00 Mark Seemann <div id="post"> <p> <em>An applicative functor is a useful abstraction. While typically associated with functional programming, applicative functors can be conjured into existence in C# as well.</em> </p> <p> This article series is part of <a href="/2018/03/19/functors-applicatives-and-friends">a larger series of articles about functors, applicatives, and other mappable containers</a>. </p> <p> In a <a href="/2018/03/22/functors">former article series</a>, you learned about functors, and how they also exist in object-oriented design. Perhaps the utility of this still eludes you, although, if you've ever had experience with LINQ in C#, you should realise that the abstraction is invaluable. Functors are abundant; <em>applicative functors</em> not quite so much. On the other hand, applicative functors enable you to <em>do</em> more. </p> <p> I find it helpful to think of applicative functors as an abstraction that enable you to express <em>combinations</em> of things. </p> <p> In the functor article series, I mostly focused on the mechanics of implementation. In this article series, I think that you'll find it helpful to slightly change the perspective. In these articles, I'll show you various motivating examples of how applicative functors are useful. <ul> <li><a href="/2018/10/08/full-deck">Full deck</a></li> <li><a href="/2018/10/15/an-applicative-password-list">An applicative password list</a></li> <li><a href="/2018/10/22/applicative-combinations-of-functions">Applicative combinations of functions</a></li> <li><a href="/2018/10/29/the-maybe-applicative-functor">The Maybe applicative functor</a></li> <li><a href="/2018/11/05/applicative-validation">Applicative validation</a></li> <li><a href="/2018/11/26/the-test-data-generator-applicative-functor">The Test Data Generator applicative functor</a></li> <li><a href="/2018/12/10/danish-cpr-numbers-in-f">Danish CPR numbers in F#</a></li> <li><a href="/2018/12/17/the-lazy-applicative-functor">The Lazy applicative functor</a></li> <li><a href="/2019/04/22/applicative-monoids">Applicative monoids</a></li> </ul> You should consider reading one or more of these articles before continuing the present article. </p> <h3 id="6173a96aedaa4e97ad868c159e6d06fa"> A Haskell perspective <a href="#6173a96aedaa4e97ad868c159e6d06fa" title="permalink">#</a> </h3> <p> A normal functor maps objects in an 'elevated world' (like C#'s <code>IEnumerable&lt;T&gt;</code> or <code>IObservable&lt;T&gt;</code>) using a function in the 'normal world'. As a variation, an applicative functor maps objects in an 'elevated world' using functions from the same 'elevated world'. </p> <p> In <a href="https://haskell.org">Haskell</a>, an applicative functor is defined <em>like</em> this: </p> <p> <pre><span style="color:blue;">class</span>&nbsp;Functor&nbsp;f&nbsp;=&gt;&nbsp;<span style="color:#a31515;">Applicative</span>&nbsp;f&nbsp;<span style="color:blue;">where</span> &nbsp;&nbsp;<span style="color:#600277;">pure</span>&nbsp;&nbsp;::&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;f&nbsp;a &nbsp;&nbsp;(&lt;*<span style="color:blue;">&gt;</span>)&nbsp;::&nbsp;f&nbsp;(a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;b)&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;f&nbsp;a&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;f&nbsp;b </pre> </p> <p> This is a <strong>simplification</strong>; there's more to the <code>Applicative</code> typeclass than this, but this should highlight the essence. What it says is that an applicative functor must already be a <code>Functor</code>. It could be any sort of <code>Functor</code>, like <code>[]</code> (linked list), <code>Maybe</code>, <code>Either</code>, and so on. Since <code>Functor</code> is an abstraction, it's called <code>f</code>. </p> <p> The definition furthermore says that in order for a functor to be applicative, two functions must exist: <code>pure</code> and <code>&lt;*&gt;</code> (<em>'apply'</em>). </p> <p> <code>pure</code> is easy to understand. It simply 'elevates' a 'normal' value to a functor value. For example, you can elevate the number <code>42</code> to a list value by putting it in a list with a single element: <code>[42]</code>. Or you can elevate <code>"foo"</code> to <code>Maybe</code> by containing it in the <code>Just</code> case: <code>Just "foo"</code>. That is, literally, what <code>pure</code> does for <code>[]</code> (list) and <code>Maybe</code>. </p> <p> The <code>&lt;*&gt;</code> operator applies an 'elevated' function to an 'elevated' value. When <code>f</code> is <code>[]</code>, this literally means that you have a list of functions that you have to apply to a list of values. Perhaps you can already see what I meant by <em>combinations</em> of things. </p> <p> This sounds abstract, but follow the above list of links in order to see several examples. </p> <h3 id="6f9b2f13951e475d8c4fc9682ad96a94"> An F# perspective <a href="#6f9b2f13951e475d8c4fc9682ad96a94" title="permalink">#</a> </h3> <p> Applicative functors aren't explicitly modelled in <a href="https://fsharp.org">F#</a>, but they're easy enough to add if you need them. F# doesn't have typeclasses, so implementing applicative functors tend to be more on a case-by-case basis. </p> <p> If you need <code>list</code> to be applicative, <code>pure</code> should have the type <code>'a -&gt; 'a list</code>, and <code>&lt;*&gt;</code> should have the type <code>('a -&gt; 'b) list -&gt; 'a list -&gt; 'b list</code>. At this point, you already run into the problem that <code>pure</code> is a reserved keyword in F#, so you'll have to find another name, or simply ignore that function. </p> <p> If you need <code>option</code> to be applicative, <code>&lt;*&gt;</code> should have the type <code>('a -&gt; 'b) option -&gt; 'a option -&gt; 'b option</code>. Now you run into your second problem, because which function is <code>&lt;*&gt;</code>? The one for <code>list</code>, or the one for <code>option</code>? It can't be both, so you'll have to resort to all sorts of hygiene to prevent these two versions of the same operator from clashing. This somewhat limits its usefulness. </p> <p> Again, refer to the above list of linked articles for concrete examples. </p> <h3 id="cef395ee19644f30bfd1ad7a84b6f912"> A C# perspective <a href="#cef395ee19644f30bfd1ad7a84b6f912" title="permalink">#</a> </h3> <p> Applicative functors push the limits of what you can express in C#, but the equivalent to <code>&lt;*&gt;</code> would be a method with this signature: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Functor</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;Apply&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Functor</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;&nbsp;selector, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Functor</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;source)</pre> </p> <p> Here, the class <code>Functor&lt;T&gt;</code> is a place-holder for a proper functor class. A concrete example could be for <code>IEnumerable&lt;T&gt;</code>: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;Apply&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&gt;&nbsp;selectors, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;source)</pre> </p> <p> As you can see, here you somehow have to figure out how to combine a sequence of functions with a sequence of values. </p> <p> In some of the examples in the above list of linked articles, you'll see how this will stretch the capability of C#. </p> <h3 id="ffa16273a2a0428f90190f8d559cf0a6"> Summary <a href="#ffa16273a2a0428f90190f8d559cf0a6" title="permalink">#</a> </h3> <p> This article only attempts to provide an overview of applicative functors, while examples are given in linked articles. I find it helpful to think of applicative functors as an abstraction that enables you to model arbitrary <em>combinations</em> of objects. This is a core feature in Haskell, occasionally useful in F#, and somewhat alien to C#. </p> <p> <strong>Next:</strong> <a href="/2018/10/08/full-deck">Full deck</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Asynchronous functors https://blog.ploeh.dk/2018/09/24/asynchronous-functors 2018-09-24T03:37:00+00:00 Mark Seemann <div id="post"> <p> <em>Asynchronous computations form functors. An article for object-oriented programmers.</em> </p> <p> This article is an instalment in <a href="/2018/03/22/functors">an article series about functors</a>. The previous article covered the <a href="/2018/09/10/the-lazy-functor">Lazy functor</a>. In this article, you'll learn about closely related functors: .NET Tasks and <a href="https://fsharp.org">F#</a> <a href="https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/asynchronous-workflows">asynchronous workflows</a>. </p> <p> A word of warning is in order. .NET Tasks aren't <a href="https://en.wikipedia.org/wiki/Referential_transparency">referentially transparent</a>, whereas F# asynchronous computations are. You could argue, then, that .NET Tasks aren't proper functors, but you mostly observe the difference when you perform impure operations. As a general observation, when impure operations are allowed, the conclusions of <a href="/2017/10/04/from-design-patterns-to-category-theory">this overall article series</a> are precarious. We can't radically change how the .NET languages work, so we'll have to soldier on, pretending that impure operations are delegated to other parts of our system. Under this undue assumption, we can pretend that <a href="https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task-1">Task&lt;T&gt;</a> forms a functor. </p> <h3 id="f82479d93d5d40ed83749a6d9fc309dc"> Task functor <a href="#f82479d93d5d40ed83749a6d9fc309dc" title="permalink">#</a> </h3> <p> You can write an idiomatic <code>Select</code> extension method for <code>Task&lt;T&gt;</code> like this: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">async</span>&nbsp;<span style="color:blue;">static</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;Select&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>&nbsp;<span style="color:#2b91af;">Task</span>&lt;<span style="color:#2b91af;">T</span>&gt;&nbsp;source, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">T</span>,&nbsp;<span style="color:#2b91af;">TResult</span>&gt;&nbsp;selector) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;x&nbsp;=&nbsp;<span style="color:blue;">await</span>&nbsp;source; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;selector(x); }</pre> </p> <p> With this extension method in scope, you can compose asynchronous computations like this: </p> <p> <pre><span style="color:#2b91af;">Task</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;x&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:#2b91af;">Task</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;y&nbsp;=&nbsp;x.Select(i&nbsp;=&gt;&nbsp;i.ToString());</pre> </p> <p> Or, if you prefer <em>query syntax:</em> </p> <p> <pre><span style="color:#2b91af;">Task</span>&lt;<span style="color:blue;">int</span>&gt;&nbsp;x&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:#2b91af;">Task</span>&lt;<span style="color:blue;">string</span>&gt;&nbsp;y&nbsp;=&nbsp;<span style="color:blue;">from</span>&nbsp;i&nbsp;<span style="color:blue;">in</span>&nbsp;x&nbsp;<span style="color:blue;">select</span>&nbsp;i.ToString();</pre> </p> <p> In both cases, you start with a <code>Task&lt;int&gt;</code> which you map into a <code>Task&lt;string&gt;</code>. Perhaps you've noticed that these two examples closely resemble the equivalent <a href="/2018/09/10/the-lazy-functor">Lazy functor examples</a>. The resemblance isn't coincidental. The same <em>abstraction</em> is in use in both places. This is the <em>functor</em> abstraction. That's what this article series is all about, after all. </p> <p> The difference between the Task functor and the Lazy functor is that lazy computations don't run until forced. Tasks, on the other hand, typically start running in the background when created. Thus, when you finally <code>await</code> the value, you may actually not have to wait for it. This does, however, depend on how you created the initial task. </p> <h3 id="8d66efff8d5a4e73b3007878b3ec7227"> First functor law for Task <a href="#8d66efff8d5a4e73b3007878b3ec7227" title="permalink">#</a> </h3> <p> The <code>Select</code> method obeys the first functor law. As usual in this article series, actually proving that this is the case belongs to the realm of computer science. Instead of proving that the law holds, you can use property-based testing to demonstrate that it does. The following example shows a single property written with <a href="https://fscheck.github.io/FsCheck/">FsCheck</a> 2.11.0 and <a href="https://xunit.github.io/">xUnit.net</a> 2.4.0. </p> <p> <pre>[<span style="color:#2b91af;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;TaskObeysFirstFunctorLaw(<span style="color:blue;">int</span>&nbsp;i) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;&nbsp;left&nbsp;=&nbsp;<span style="color:#2b91af;">Task</span>.FromResult(i); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;right&nbsp;=&nbsp;<span style="color:#2b91af;">Task</span>.FromResult(i).Select(x&nbsp;=&gt;&nbsp;x); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(left.Result,&nbsp;right.Result); }</pre> </p> <p> This property accesses the <a href="https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task-1.result">Result property</a> of the Tasks involved. This is typically not the preferred way to pull the value of Tasks, but I decided to do it like this since <a href="https://github.com/fscheck/FsCheck/issues/167">FsCheck 2.4.0 doesn't support asynchronous properties</a>. </p> <p> Even though you may feel that a property-based test gives you more confidence than a few hard-coded examples, such a test is nothing but a demonstration of the first functor law. It's no proof, and it only demonstrates that the law holds for <code>Task&lt;int&gt;</code>, not that it holds for <code>Task&lt;string&gt;</code>, <code>Task&lt;Product&gt;</code>, etcetera. </p> <h3 id="12b3ea8d8963414496a6c695f130a4b9"> Second functor law for Task <a href="#12b3ea8d8963414496a6c695f130a4b9" title="permalink">#</a> </h3> <p> As is the case with the first functor law, you can also use a property to demonstrate that the second functor law holds: </p> <p> <pre>[<span style="color:#2b91af;">Property</span>(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)] <span style="color:blue;">public</span>&nbsp;<span style="color:blue;">void</span>&nbsp;TaskObeysSecondFunctorLaw( &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">string</span>,&nbsp;<span style="color:blue;">byte</span>&gt;&nbsp;f, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Func</span>&lt;<span style="color:blue;">int</span>,&nbsp;<span style="color:blue;">string</span>&gt;&nbsp;g, &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;i) { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;&nbsp;left&nbsp;=&nbsp;<span style="color:#2b91af;">Task</span>.FromResult(i).Select(g).Select(f); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">var</span>&nbsp;right&nbsp;=&nbsp;<span style="color:#2b91af;">Task</span>.FromResult(i).Select(x&nbsp;=&gt;&nbsp;f(g(x))); &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">Assert</span>.Equal(left.Result,&nbsp;right.Result); }</pre> </p> <p> Again the same admonitions apply: that property is no proof. It does show, however, that given two functions, <code>f</code> and <code>g</code>, it doesn't matter if you map a Task in one or two steps. The output is the same in both cases. </p> <h3 id="d87480f7e0124db18b22aaacbff3672a"> Async functor <a href="#d87480f7e0124db18b22aaacbff3672a" title="permalink">#</a> </h3> <p> F# had asynchronous workflows long before C#, so it uses a slightly different model, supported by separate types. Instead of <code>Task&lt;T&gt;</code>, F# relies on a generic type called <a href="https://msdn.microsoft.com/en-us/visualfsharpdocs/conceptual/control.async%5b%27t%5d-type-%5bfsharp%5d">Async&lt;'T&gt;</a>. It's still a functor, since you can trivially implement a <code>map</code> function for it: </p> <p> <pre><span style="color:blue;">module</span>&nbsp;Async&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;&#39;a&nbsp;-&gt;&nbsp;Async&lt;&#39;a&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;fromValue&nbsp;x&nbsp;=&nbsp;async&nbsp;{&nbsp;<span style="color:blue;">return</span>&nbsp;x&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;">//&nbsp;(&#39;a&nbsp;-&gt;&nbsp;&#39;b)&nbsp;-&gt;&nbsp;Async&lt;&#39;a&gt;&nbsp;-&gt;&nbsp;Async&lt;&#39;b&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;map&nbsp;f&nbsp;x&nbsp;=&nbsp;async&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let!</span>&nbsp;x&#39;&nbsp;=&nbsp;x &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">return</span>&nbsp;f&nbsp;x&#39;&nbsp;}</pre> </p> <p> The <code>map</code> function uses the <code>async</code> <a href="https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/computation-expressions">computation expression</a> to access the value being computed asynchronously. You can use a <code>let!</code> binding to await the value computed in <code>x</code>, and then use the function <code>f</code> to translate that value. The <code>return</code> keyword turns the result into a new <code>Async</code> value. </p> <p> With the <code>map</code> function, you can write code like this: </p> <p> <pre><span style="color:blue;">let</span>&nbsp;(x&nbsp;:&nbsp;Async&lt;int&gt;)&nbsp;=&nbsp;<span style="color:green;">//&nbsp;...</span> <span style="color:blue;">let</span>&nbsp;(y&nbsp;:&nbsp;Async&lt;string&gt;)&nbsp;=&nbsp;x&nbsp;|&gt;&nbsp;Async.map&nbsp;string</pre> </p> <p> Once you've composed an asynchronous workflow to your liking, you can run it to compute the value in which you're interested: </p> <p> <pre>&gt; Async.RunSynchronously y;; val it : string = "42"</pre> </p> <p> This is the main difference between F# asynchronous workflows and .NET Tasks. You have to explicitly run an asynchronous workflows, whereas Tasks are usually, implicitly, already running in the background. </p> <h3 id="f572e55512ab484584450dc0af7fe9b3"> First functor law for Async <a href="#f572e55512ab484584450dc0af7fe9b3" title="permalink">#</a> </h3> <p> The <code>Async.map</code> function obeys the first functor law. Like above, you can use FsCheck to demonstrate how that works: </p> <p> <pre>[&lt;Property(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)&gt;] <span style="color:blue;">let</span>&nbsp;``Async&nbsp;obeys&nbsp;first&nbsp;functor&nbsp;law``&nbsp;(i&nbsp;:&nbsp;int)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;&nbsp;left&nbsp;=&nbsp;Async.fromValue&nbsp;i &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;right&nbsp;=&nbsp;Async.fromValue&nbsp;i&nbsp;|&gt;&nbsp;Async.map&nbsp;id &nbsp;&nbsp;&nbsp;&nbsp;Async.RunSynchronously&nbsp;left&nbsp;=!&nbsp;Async.RunSynchronously&nbsp;right</pre> </p> <p> In addition to FsCheck and xUnit.net, this property also uses <a href="https://github.com/SwensenSoftware/unquote">Unquote</a> 4.0.0 for the assertion. The <code>=!</code> operator is an assertion that the left-hand side <em>must equal</em> the right-hand side. If it doesn't, then the operator throws a descriptive exception. </p> <h3 id="6fd1ba5d9c0647c49ba06d7580b2c552"> Second functor law for Async <a href="#6fd1ba5d9c0647c49ba06d7580b2c552" title="permalink">#</a> </h3> <p> As is the case with the first functor law, you can also use a property to demonstrate that the second functor law holds: </p> <p> <pre>[&lt;Property(QuietOnSuccess&nbsp;=&nbsp;<span style="color:blue;">true</span>)&gt;] <span style="color:blue;">let</span>&nbsp;``Async&nbsp;obeys&nbsp;second&nbsp;functor&nbsp;law``&nbsp;(f&nbsp;:&nbsp;string&nbsp;<span style="color:blue;">-&gt;</span>&nbsp;byte)&nbsp;g&nbsp;(i&nbsp;:&nbsp;int)&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;&nbsp;left&nbsp;=&nbsp;Async.fromValue&nbsp;i&nbsp;|&gt;&nbsp;Async.map&nbsp;g&nbsp;|&gt;&nbsp;Async.map&nbsp;f &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">let</span>&nbsp;right&nbsp;=&nbsp;Async.fromValue&nbsp;i&nbsp;|&gt;&nbsp;Async.map&nbsp;(g&nbsp;&gt;&gt;&nbsp;f) &nbsp;&nbsp;&nbsp;&nbsp;Async.RunSynchronously&nbsp;left&nbsp;=!&nbsp;Async.RunSynchronously&nbsp;right</pre> </p> <p> As usual, the second functor law states that given two arbitrary (but <a href="https://en.wikipedia.org/wiki/Pure_function">pure</a>) functions <code>f</code> and <code>g</code>, it doesn't matter if you map an asynchronous workflow in one or two steps. There could be a difference in performance, but the functor laws don't concern themselves with the time dimension. </p> <p> Both of the above properties use the <code>Async.fromValue</code> helper function to create <code>Async</code> values. Perhaps you consider it cheating that it immediately produces a value, but you can add a delay if you want to pretend that actual asynchronous computation takes place. It'll make the tests run slower, but otherwise will not affect the outcome. </p> <h3 id="9ad596ae1e5f483cb6d02ad963a0a378"> Task and Async isomorphism <a href="#9ad596ae1e5f483cb6d02ad963a0a378" title="permalink">#</a> </h3> <p> The reason I've covered both <code>Task&lt;T&gt;</code> and <code>Async&lt;'a&gt;</code> in the same article is that they're equivalent. You can translate a Task to an asynchronous workflow, or the other way around. </p> <p> <img src="/content/binary/equivalence-of-task-and-async.png" alt="Equivalence of Task and Async."> </p> <p> There's performance implications of going back and forth between <code>Task&lt;T&gt;</code> and <code>Async&lt;'a&gt;</code>, but there's no loss of information. You can, therefore, consider these to representations of asynchronous computations <a href="/2018/01/08/software-design-isomorphisms">isomorphic</a>. </p> <h3 id="4bb86c51ae37462b94e278247d33f8f5"> Summary <a href="#4bb86c51ae37462b94e278247d33f8f5" title="permalink">#</a> </h3> <p> Whether you do asynchronous programming with <code>Task&lt;T&gt;</code> or <code>Async&lt;'a&gt;</code>, asynchronous computations form functors. This enables you to piecemeal compose asynchronous workflows. </p> <p> <strong>Next:</strong> <a href="/2018/12/03/set-is-not-a-functor">Set is not a functor</a>. </p> </div><hr> This blog is totally free, but if you like it, please consider <a href="https://blog.ploeh.dk/support">supporting it</a>. Typing is not a programming bottleneck https://blog.ploeh.dk/2018/09/17/typing-is-not-a-programming-bottleneck 2018-09-17T08:16:00+00:00 Mark Seemann <div id="post"> <p> <em>Some developers seem to think that typing is a major bottleneck while programming. It's not.</em> </p> <p> I sometimes give programming advice to people. They approach me with a software design problem, and, to the best of my ability, I suggest a remedy. Despite my best intentions, my suggestions sometimes meet resistance. One common reaction is that <a href="/2015/08/03/idiomatic-or-idiosyncratic">my suggestion isn't idiomatic</a>, but recently, another type of criticism seems to be on the rise. </p> <p> The code that I suggest is too verbose. It involves too much typing. </p> <p> I'll use this article to reflect on that criticism. </p> <h3 id="d1f8398f96584ed09acce1b481b45cde"> The purpose of code <a href="#d1f8398f96584ed09acce1b481b45cde" title="permalink">#</a> </h3> <p> Before we get into the details of the issue, I'd like to start with the big picture. What's the purpose of code? </p> <p> I've discussed this extensively in my <a href="https://cleancoders.com">Clean Coders</a> video on <a href="https://cleancoders.com/episode/humane-code-real-episode-1/show">Humane Code</a>. In short, the purpose of source code is to <em>communicate</em> the workings of a piece of software to the next programmer who comes along. This could easily include your future self. </p> <p> Perhaps you disagree with that proposition. Perhaps you think that the purpose of source code is to produce working software. It's that, too, but that's not its only purpose. If that was the only purpose, you might as well write the software in machine code. </p> <p> Why do we have high-level programming languages like C#, Java, JavaScript, Ruby, Python, F#, Visual Basic, Haskell, heck - even C++? </p> <p> As far as I can tell, it's because programmers are all human, and humans have limited cognitive capacity. We can't keep track of hundreds, or thousands, of global variables, or the states of dozens of complex resources. We need tools that help us structure and understand complex systems so that they're broken down into (more) manageable chunks. High-level languages help us do that. </p> <p> The purpose of source code is to be understood. You read source code much more that you write. I'm not aware of any scientific studies, but I think most programmers will agree that over the lifetime of a code base, any line of code will be read orders of magnitude more often that it's edited. </p> <h3 id="b82afa298f114b8c89113ea5f40fc278"> Typing speed <a href="#b82afa298f114b8c89113ea5f40fc278" title="permalink">#</a> </h3> <p> How many lines of code do you produce during a productive working day? </p> <p> To be honest, I can't even answer that question myself. I've never measured it, since I consider it to be irrelevant. <a href="http://bit.ly/mythical-man-month">The Mythical Man-Month</a> gives the number as <em>10</em>, but let's be generous and pretend it's ten times that. This clearly depends on lots of factors, such as the language in which you're writing, the state of the code base, and so on. You'd tend to write more lines in a pristine greenfield code base, whereas you'll write fewer lines of code in a complicated legacy code base. </p> <p> How many characters is a line of code? Let's say it's 80 characters, because that's the maximum width code ought to have. I realise that many people write wider lines, but on the other hand, most developers (fortunately) use indentation, so as a counter-weight, code often has some blank space to the left as well. This is all back-of-the-envelope calculations anyway. </p> <p> When I worked in a Microsoft product group, we typically planned that a productive, 'full' day of coding was five hours. Even on productive days, the rest was used in meetings, administration, breaks, and so on. </p> <p> If you write code for five hours, and produce 100 lines of code, at 80 characters per line, that's 8,000 characters. Your IDE is likely to help you with statement completion and such, but for the sake of argument, let's pretend that you have to type it all in. </p> <p> 8,000 characters in five hours is 1,600 characters per hour, or 27 characters per minute. </p> <p> I'm not a particularly fast typist, but I can type ten times faster than that. </p> <p> Typing isn't a bottleneck. </p> <h3 id="cad0abab133848d98b4aada5e31d35d6"> Is more code worse? <a href="#cad0abab133848d98b4aada5e31d35d6" title="permalink">#</a> </h3> <p> I tend to <a href="/2013/02/04/BewareofProductivityTools">get drawn into these discussions from time to time</a>, but good programming has little to do with how fast you can produce lines of code. </p> <p> To be clear, I entirely accept that statement completion, refactoring support, and such are, in general, benign features. While I spend most of my programming time thinking and reading, I also tend to write code in bursts. The average count of lines per hour may not be great, but that's because averages smooth out the hills and the valleys of my activity. </p> <p> Still, increasingly frequent objection to some of my suggestions is that what I suggest implies 'too much' code. Recently, for example, I had to defend the merits of the <a href="https://martinfowler.com/bliki/FluentInterface.html">Fluent</a> Builder pattern that I suggest in my <a href="/2014/05/19/di-friendly-library">DI-Friendly Library</a> article. </p> <p> As another example, consider two alternatives for modelling a restaurant reservation. First, here's a terse option: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Reservation</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;Date&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;Email&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">string</span>&nbsp;Name&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">int</span>&nbsp;Quantity&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;<span style="color:blue;">bool</span>&nbsp;IsAccepted&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;<span style="color:blue;">set</span>;&nbsp;} }</pre> </p> <p> Here's a longer alternative: </p> <p> <pre><span style="color:blue;">public</span>&nbsp;<span style="color:blue;">sealed</span>&nbsp;<span style="color:blue;">class</span>&nbsp;<span style="color:#2b91af;">Reservation</span> { &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">public</span>&nbsp;Reservation( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;date, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;name, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;email, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;quantity)&nbsp;: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">this</span>(date,&nbsp;name,&nbsp;email,&nbsp;quantity,&nbsp;<span style="color:blue;">false</span>) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">private</span>&nbsp;Reservation( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af;">DateTimeOffset</span>&nbsp;date, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;name, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">string</span>&nbsp;email, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">int</span>&nbsp;quantity, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;">bool</span>&nbsp;isAccepted) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date&nbsp;=&nbsp;date; &nbsp;&n