<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Taylor Fausak</title>
    <description>The latest articles on DEV Community by Taylor Fausak (@tfausak).</description>
    <link>https://dev.to/tfausak</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F382875%2F1db402a8-d8a2-49a0-ae32-7e73963300df.png</url>
      <title>DEV Community: Taylor Fausak</title>
      <link>https://dev.to/tfausak</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tfausak"/>
    <language>en</language>
    <item>
      <title>2020 State of Haskell Survey</title>
      <dc:creator>Taylor Fausak</dc:creator>
      <pubDate>Sun, 01 Nov 2020 13:26:03 +0000</pubDate>
      <link>https://dev.to/tfausak/2020-state-of-haskell-survey-507k</link>
      <guid>https://dev.to/tfausak/2020-state-of-haskell-survey-507k</guid>
      <description>&lt;p&gt;Hi friends! I'm very excited to announce the fourth annual state of Haskell survey. Please take a few minutes to fill it out. Thanks! &lt;a href="https://haskellweekly.news/survey/2020.html"&gt;https://haskellweekly.news/survey/2020.html&lt;/a&gt;&lt;/p&gt;

</description>
      <category>haskell</category>
    </item>
    <item>
      <title>Golfing language extensions</title>
      <dc:creator>Taylor Fausak</dc:creator>
      <pubDate>Wed, 29 Jul 2020 11:37:01 +0000</pubDate>
      <link>https://dev.to/tfausak/golfing-language-extensions-2obl</link>
      <guid>https://dev.to/tfausak/golfing-language-extensions-2obl</guid>
      <description>&lt;p&gt;Recently Baldur Blöndal posted an interesting challenge on Twitter:&lt;/p&gt;

&lt;p&gt;&lt;iframe class="tweet-embed" id="tweet-1287682449863041024-476" src="https://platform.twitter.com/embed/Tweet.html?id=1287682449863041024"&gt;
&lt;/iframe&gt;

  // Detect dark theme
  var iframe = document.getElementById('tweet-1287682449863041024-476');
  if (document.body.className.includes('dark-theme')) {
    iframe.src = "https://platform.twitter.com/embed/Tweet.html?id=1287682449863041024&amp;amp;theme=dark"
  }



&lt;/p&gt;

&lt;p&gt;When I first saw this there were already a few responses. I played with them a little to produce this monstrosity:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;#&lt;/span&gt; &lt;span class="err"&gt;∷&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="err"&gt;∷&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;mdo&lt;/span&gt; &lt;span class="kr"&gt;if&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="kr"&gt;let&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mh"&gt;0x0&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;'&lt;/span&gt;&lt;span class="nb"&gt;()&lt;/span&gt;&lt;span class="p"&gt;,)&lt;/span&gt;&lt;span class="err"&gt;→&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="n"&gt;b_0&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="mf"&gt;0e0&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="err"&gt;←&lt;/span&gt;&lt;span class="kt"&gt;[]&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="err"&gt;←&lt;/span&gt;&lt;span class="kt"&gt;[]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="kr"&gt;then&lt;/span&gt;&lt;span class="nf"&gt;\&lt;/span&gt;&lt;span class="kr"&gt;case&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Those 73 characters require 21 language extensions to compile correctly! This blog post aims to quickly explain what that code does by showing how to require various language extensions with the least amount of code.&lt;/p&gt;

&lt;p&gt;Before we get started, a couple notes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The challenge was to require the most language extensions with the fewest characters. I would be happy to be proven wrong, but I think you can't do much better than about 10 extensions with 23 characters. Most things have so much syntactic overhead that it screws up the ratio.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Because of that, I decided to change the challenge into roughly: "How many extensions can you require from a single 80-character line of code?" This allows showing off more extensions even though they require more than about two characters.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;I'm doing all of this with GHC 8.10 on Ubuntu. My goal is to produce a program that compiles. I don't care about warnings, runtime errors, or portability. The code should fail to compile without each extension. Duplicate extensions like &lt;code&gt;RankNTypes&lt;/code&gt; and &lt;code&gt;Rank2Types&lt;/code&gt; don't count.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Alright. Let's get started.&lt;/p&gt;

&lt;h2&gt;
  
  
  Baseline
&lt;/h2&gt;

&lt;p&gt;Since we're playing some variant of code golf here, the idea is to use the fewest number of characters. We should set a baseline for the smallest program that will compile. Since GHC will compile an empty program, that's a total of zero characters. But that's not very interesting.&lt;/p&gt;

&lt;p&gt;The original challenge looked for a high ratio of extensions to characters, so I thought it made sense to use a regular value declaration, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- shortest non-empty program ghc compiles&lt;/span&gt;
&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There's almost no overhead with that. You have to come up with a name and a value, but those can probably be used later. The equals sign is true overhead, but it's less than other approaches like types or classes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- these ones are a little longer&lt;/span&gt;
&lt;span class="kr"&gt;type&lt;/span&gt; &lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;()&lt;/span&gt;
&lt;span class="kr"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;C&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;
&lt;span class="kr"&gt;instance&lt;/span&gt; &lt;span class="kt"&gt;Show&lt;/span&gt;&lt;span class="nb"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;a href="https://downloads.haskell.org/~ghc/8.10.1/docs/html/users_guide/glasgow_exts.html#extension-MagicHash" rel="noopener noreferrer"&gt;MagicHash&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;The first thing we can do is add a hash to the end of our value's name to require the &lt;code&gt;MagicHash&lt;/code&gt; extension. Like many of the extensions we'll encounter, this is purely syntactic. It simply allows you to name things with a hash after them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;#=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;a href="https://downloads.haskell.org/~ghc/8.10.1/docs/html/users_guide/glasgow_exts.html#extension-ScopedTypeVariables" rel="noopener noreferrer"&gt;ScopedTypeVariables&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Typically you would give a top-level declaration a standalone type signature. Or perhaps you would annotate its value. But did you know that the &lt;code&gt;ScopedTypeVariables&lt;/code&gt; extension allows you to provide the signature inline? It's true!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;#::&lt;/span&gt;&lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Normally this extension is used to add type signatures to polymorphic local bindings, but it works just as well for our purposes. Here's an example of its more typical usage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- typical scoped type variable usage&lt;/span&gt;
&lt;span class="n"&gt;main&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kr"&gt;do&lt;/span&gt;
  &lt;span class="kr"&gt;let&lt;/span&gt; &lt;span class="n"&gt;zero&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
  &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="n"&gt;getLine&lt;/span&gt;
  &lt;span class="n"&gt;print&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;zero&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;a href="https://downloads.haskell.org/~ghc/8.10.1/docs/html/users_guide/glasgow_exts.html#extension-UnicodeSyntax" rel="noopener noreferrer"&gt;UnicodeSyntax&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;That pesky type signature is taking up two whole characters! We can do better. The &lt;code&gt;UnicodeSyntax&lt;/code&gt; language extension allows &lt;code&gt;::&lt;/code&gt; to be replaced by &lt;code&gt;∷&lt;/code&gt;, which is only a single character.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="err"&gt;∷&lt;/span&gt;&lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that code golf usually counts the number of bytes to discourage exploiting Unicode like this. &lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://downloads.haskell.org/~ghc/8.10.1/docs/html/users_guide/glasgow_exts.html#extension-PartialTypeSignatures" rel="noopener noreferrer"&gt;PartialTypeSignatures&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;It's kind of annoying that we have &lt;code&gt;Int&lt;/code&gt; in there. It takes up three characters without really requiring any language extensions itself. Fortunately &lt;code&gt;PartialTypeSignatures&lt;/code&gt; lets us save two characters and require another extension.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;#&lt;/span&gt; &lt;span class="err"&gt;∷&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that we have to put a space after the hash otherwise this would compile without the &lt;code&gt;MagicHash&lt;/code&gt; extension because &lt;code&gt;#∷&lt;/code&gt; would be interpreted as an operator.&lt;/p&gt;

&lt;p&gt;This gets GHC to infer the type even though we have an explicit type signature. In normal usage this is used to specify a small part of a larger type.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://downloads.haskell.org/~ghc/8.10.1/docs/html/users_guide/glasgow_exts.html#extension-TypeApplications" rel="noopener noreferrer"&gt;TypeApplications&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;We've been focused on the left side of the equals sign. Let's turn to the right side. First let's introduce an unnecessary call to &lt;code&gt;id&lt;/code&gt;. It doesn't do anything by itself, but we'll expand on it in a second.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;#&lt;/span&gt; &lt;span class="err"&gt;∷&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since &lt;code&gt;id&lt;/code&gt; is polymorphic, we can use &lt;code&gt;TypeApplications&lt;/code&gt; to pick a concrete type for it. But just like before we don't want to actually pick a type because that will take too many characters to write out. Instead we'll let GHC infer it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;#&lt;/span&gt; &lt;span class="err"&gt;∷&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Although the type application looks like it would require &lt;code&gt;PartialTypeSignatures&lt;/code&gt;, it doesn't. Note that there must be a space before the type application otherwise it won't compile.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://downloads.haskell.org/~ghc/8.10.1/docs/html/users_guide/glasgow_exts.html#extension-KindSignatures" rel="noopener noreferrer"&gt;KindSignatures&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;We have one last type-level trick up our sleeve. In addition to giving type signatures to values, we can give kind signatures to types. Since we have two type signatures, we have to pick which one to decorate with a kind signature. Each case would require parentheses, so we'll add it to the type application since it will allow us to remove the space before the &lt;code&gt;0&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;#&lt;/span&gt; &lt;span class="err"&gt;∷&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="err"&gt;∷&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Curiously this kind signature doesn't require &lt;code&gt;PartialTypeSignatures&lt;/code&gt; either. Also note that we're still using the Unicode &lt;code&gt;∷&lt;/code&gt; instead of &lt;code&gt;::&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://downloads.haskell.org/~ghc/8.10.1/docs/html/users_guide/glasgow_exts.html#extension-BlockArguments" rel="noopener noreferrer"&gt;BlockArguments&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Normally if you want to pass a &lt;code&gt;do&lt;/code&gt; block to a function you either have to use parentheses (&lt;code&gt;f (do ...)&lt;/code&gt;) or some operator (&lt;code&gt;f $ do ...&lt;/code&gt;). The recent &lt;code&gt;BlockArguments&lt;/code&gt; extension allows you to avoid any decoration around your argument.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;#&lt;/span&gt; &lt;span class="err"&gt;∷&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="err"&gt;∷&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="kr"&gt;do&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;a href="https://downloads.haskell.org/~ghc/8.10.1/docs/html/users_guide/glasgow_exts.html#extension-RecursiveDo" rel="noopener noreferrer"&gt;RecursiveDo&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;I've never used this extension, so I don't have much to say about it. By changing our &lt;code&gt;do&lt;/code&gt; to an &lt;code&gt;mdo&lt;/code&gt;, we can require another extension. The mechanics of regular versus recursive &lt;code&gt;do&lt;/code&gt; binds don't matter since we're not using them anyway.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;#&lt;/span&gt; &lt;span class="err"&gt;∷&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="err"&gt;∷&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;mdo&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;a href="https://downloads.haskell.org/~ghc/8.10.1/docs/html/users_guide/glasgow_exts.html#extension-BinaryLiterals" rel="noopener noreferrer"&gt;BinaryLiterals&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;At long last we come to that &lt;code&gt;0&lt;/code&gt; at the far right side. It's not requiring any extensions. Let's fix that! &lt;/p&gt;

&lt;p&gt;By default Haskell lets you write octal (&lt;code&gt;0o0&lt;/code&gt;) and hexadecimal (&lt;code&gt;0x0&lt;/code&gt;) literals. With the &lt;code&gt;BinaryLiterals&lt;/code&gt; extension you can also write numbers in binary.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;#&lt;/span&gt; &lt;span class="err"&gt;∷&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="err"&gt;∷&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;mdo&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="n"&gt;b0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;a href="https://downloads.haskell.org/~ghc/8.10.1/docs/html/users_guide/glasgow_exts.html#extension-NumericUnderscores" rel="noopener noreferrer"&gt;NumericUnderscores&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;NumericUnderscores&lt;/code&gt; extension lets you put underscores in numeric literals, as you might have guessed from the name. We can take advantage of this by adding another underscore that will require another extension.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;#&lt;/span&gt; &lt;span class="err"&gt;∷&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="err"&gt;∷&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;mdo&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="n"&gt;b_0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;a href="https://downloads.haskell.org/~ghc/8.10.1/docs/html/users_guide/glasgow_exts.html#extension-NumDecimals" rel="noopener noreferrer"&gt;NumDecimals&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;There's another extension to the numeric literal syntax that allows you to write integral values using scientific notation. This is useful for writing really big numbers without adding a bunch of zeroes.&lt;/p&gt;

&lt;p&gt;Unfortunately this one is a little tricky to actually require. Simply writing a number like &lt;code&gt;1e2&lt;/code&gt; will infer a &lt;code&gt;Fractional&lt;/code&gt; constraint and compile just fine without the extension. We need a way to write a number like &lt;code&gt;1e2&lt;/code&gt; but also forbid it from being &lt;code&gt;Fractional&lt;/code&gt;. And we need to do all that without eating up too many characters.&lt;/p&gt;

&lt;p&gt;Fortunately there's a handy operator in the &lt;code&gt;Prelude&lt;/code&gt; that puts the right constraints on its arguments:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Num&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;Integral&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So we can put a number that requires the &lt;code&gt;NumDecimals&lt;/code&gt; extension as the second argument to &lt;code&gt;(^)&lt;/code&gt; and we should be good.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;#&lt;/span&gt; &lt;span class="err"&gt;∷&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="err"&gt;∷&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;mdo&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="n"&gt;b_0&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="mf"&gt;0e0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;a href="https://downloads.haskell.org/~ghc/8.10.1/docs/html/users_guide/glasgow_exts.html#extension-ImplicitParams" rel="noopener noreferrer"&gt;ImplicitParams&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;At this point it starts to feel like we've run out of easy wins. Time to reach for something that adds a bunch of characters but also adds a bunch of flexibility: &lt;code&gt;ImplicitParams&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is going to add a bunch of characters because we're forced to also add a &lt;code&gt;let ... in ...&lt;/code&gt; expression in order to bind the implicit parameter. That's alright though, we'll make up for it later.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;#&lt;/span&gt; &lt;span class="err"&gt;∷&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="err"&gt;∷&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;mdo&lt;/span&gt; &lt;span class="kr"&gt;let&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;()&lt;/span&gt;&lt;span class="kr"&gt;in&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="n"&gt;b_0&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="mf"&gt;0e0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;a href="https://downloads.haskell.org/~ghc/8.10.1/docs/html/users_guide/glasgow_exts.html#extension-MultiWayIf" rel="noopener noreferrer"&gt;MultiWayIf&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;It would be nice if we could replace that &lt;code&gt;let ... in ...&lt;/code&gt; with something more exotic. Fortunately multi-way &lt;code&gt;if&lt;/code&gt;s don't have a lot of overhead themselves and allow us to exploit a quirk of Haskell's syntax. This will take a little explaining.&lt;/p&gt;

&lt;p&gt;When you use guard syntax, the expression must return a &lt;code&gt;Bool&lt;/code&gt;. You're allowed to put any expression in there, including something with &lt;code&gt;let ... in ...&lt;/code&gt; as part of it. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="n"&gt;unit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kr"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"example"&lt;/span&gt; &lt;span class="kr"&gt;of&lt;/span&gt;
  &lt;span class="n"&gt;string&lt;/span&gt;
    &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kr"&gt;let&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt; &lt;span class="kr"&gt;in&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;
      &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The weird thing about this is that you can have &lt;code&gt;let&lt;/code&gt; without &lt;code&gt;in&lt;/code&gt; when you're in a guard clause. The following example is completely valid and compiles without warnings:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="n"&gt;unit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kr"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"example"&lt;/span&gt; &lt;span class="kr"&gt;of&lt;/span&gt;
  &lt;span class="n"&gt;string&lt;/span&gt;
    &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kr"&gt;let&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;
      &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The guard clause is effectively empty and always evaluates to &lt;code&gt;True&lt;/code&gt; even though we never produced a &lt;code&gt;Bool&lt;/code&gt; from it. We can use this trick to introduce a &lt;code&gt;let&lt;/code&gt; binding without paying for the corresponding &lt;code&gt;in&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;However that would hardly be worth it if we had to use &lt;code&gt;case&lt;/code&gt; because that introduces so many other characters. Thankfully multi-way &lt;code&gt;if&lt;/code&gt;s are more compact. Bringing it all together we can replace our boring &lt;code&gt;let?_=()in&lt;/code&gt; with the novel &lt;code&gt;if|let?_=()→&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;#&lt;/span&gt; &lt;span class="err"&gt;∷&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="err"&gt;∷&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;mdo&lt;/span&gt; &lt;span class="kr"&gt;if&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="kr"&gt;let&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;()&lt;/span&gt;&lt;span class="err"&gt;→&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="n"&gt;b_0&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="mf"&gt;0e0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that we're using the Unicode &lt;code&gt;→&lt;/code&gt; arrow instead of the ASCII &lt;code&gt;-&amp;gt;&lt;/code&gt; to save a character.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://downloads.haskell.org/~ghc/8.10.1/docs/html/users_guide/glasgow_exts.html#extension-HexFloatLiterals" rel="noopener noreferrer"&gt;HexFloatLiterals&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Once again we are going to abuse numeric literals for our game of code golf. This time we'll be writing a floating point literal using hexadecimal syntax, which requires an extension.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;#&lt;/span&gt; &lt;span class="err"&gt;∷&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="err"&gt;∷&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;mdo&lt;/span&gt; &lt;span class="kr"&gt;if&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="kr"&gt;let&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mh"&gt;0x0&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="err"&gt;→&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="n"&gt;b_0&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="mf"&gt;0e0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;a href="https://downloads.haskell.org/~ghc/8.10.1/docs/html/users_guide/glasgow_exts.html#extension-TupleSections" rel="noopener noreferrer"&gt;TupleSections&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Why stop there? Now that we have an unused binding just sitting there, we can cram a bunch of interesting values into it. We'll start by converting it into a tuple section, which is a concise way of writing a lambda that produces a tuple.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;#&lt;/span&gt; &lt;span class="err"&gt;∷&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="err"&gt;∷&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;mdo&lt;/span&gt; &lt;span class="kr"&gt;if&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="kr"&gt;let&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mh"&gt;0x0&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,)&lt;/span&gt;&lt;span class="err"&gt;→&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="n"&gt;b_0&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="mf"&gt;0e0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;a href="https://downloads.haskell.org/~ghc/8.10.1/docs/html/users_guide/glasgow_exts.html#extension-TemplateHaskellQuotes" rel="noopener noreferrer"&gt;TemplateHaskellQuotes&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Somehow we've made it this far without introducing any Template Haskell or quasi-quotes. As far as I can tell the easiest way to require some form of TH is to use the quoted name syntax. We'll be grabbing the name of &lt;code&gt;()&lt;/code&gt; because it's the shortest constructor name available.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;#&lt;/span&gt; &lt;span class="err"&gt;∷&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="err"&gt;∷&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;mdo&lt;/span&gt; &lt;span class="kr"&gt;if&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="kr"&gt;let&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mh"&gt;0x0&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;'&lt;/span&gt;&lt;span class="nb"&gt;()&lt;/span&gt;&lt;span class="p"&gt;,)&lt;/span&gt;&lt;span class="err"&gt;→&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="n"&gt;b_0&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="mf"&gt;0e0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;a href="https://downloads.haskell.org/~ghc/8.10.1/docs/html/users_guide/glasgow_exts.html#extension-BangPatterns" rel="noopener noreferrer"&gt;BangPatterns&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;This one requires a bit of a walk. It feels like we should have been able to require a bang pattern by now, but I don't think they're allowed on any of the binders we have. We need to introduce a new binder that can have a bang pattern on it. We're not going to do this in the most efficient way, but hopefully it'll be worth it later.&lt;/p&gt;

&lt;p&gt;We're going to use a list comprehension to introduce a new binding. As mentioned, this is a bunch extra characters.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;#&lt;/span&gt; &lt;span class="err"&gt;∷&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="err"&gt;∷&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;mdo&lt;/span&gt; &lt;span class="kr"&gt;if&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="kr"&gt;let&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mh"&gt;0x0&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;'&lt;/span&gt;&lt;span class="nb"&gt;()&lt;/span&gt;&lt;span class="p"&gt;,)&lt;/span&gt;&lt;span class="err"&gt;→&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="n"&gt;b_0&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="mf"&gt;0e0&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="kt"&gt;[]&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that we need a space before the exclamation point otherwise &lt;code&gt;|!&lt;/code&gt; would be interpreted as an operator.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://downloads.haskell.org/~ghc/8.10.1/docs/html/users_guide/glasgow_exts.html#extension-ParallelListComp" rel="noopener noreferrer"&gt;ParallelListComp&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Fortunately for us, list comprehensions have a few associated language extensions. By adding five more characters we can introduce a parallel list comprehension.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;#&lt;/span&gt; &lt;span class="err"&gt;∷&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="err"&gt;∷&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;mdo&lt;/span&gt; &lt;span class="kr"&gt;if&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="kr"&gt;let&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mh"&gt;0x0&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;'&lt;/span&gt;&lt;span class="nb"&gt;()&lt;/span&gt;&lt;span class="p"&gt;,)&lt;/span&gt;&lt;span class="err"&gt;→&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="n"&gt;b_0&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="mf"&gt;0e0&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="err"&gt;←&lt;/span&gt;&lt;span class="kt"&gt;[]&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="err"&gt;←&lt;/span&gt;&lt;span class="kt"&gt;[]&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;a href="https://downloads.haskell.org/~ghc/8.10.1/docs/html/users_guide/glasgow_exts.html#extension-TransformListComp" rel="noopener noreferrer"&gt;TransformListComp&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;I've got to say, this is one of the weirder extensions. It augments list comprehensions with a SQL-like syntax for querying data. But that doesn't really matter for us. All we care about is how many characters it takes to require another extension. Answer: eight.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;#&lt;/span&gt; &lt;span class="err"&gt;∷&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="err"&gt;∷&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;mdo&lt;/span&gt; &lt;span class="kr"&gt;if&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="kr"&gt;let&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mh"&gt;0x0&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;'&lt;/span&gt;&lt;span class="nb"&gt;()&lt;/span&gt;&lt;span class="p"&gt;,)&lt;/span&gt;&lt;span class="err"&gt;→&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="n"&gt;b_0&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="mf"&gt;0e0&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="err"&gt;←&lt;/span&gt;&lt;span class="kt"&gt;[]&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="err"&gt;←&lt;/span&gt;&lt;span class="kt"&gt;[]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="kr"&gt;then&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;a href="https://downloads.haskell.org/~ghc/8.10.1/docs/html/users_guide/glasgow_exts.html#extension-LambdaCase" rel="noopener noreferrer"&gt;LambdaCase&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;We're nearly running out of room on our 80-character line. How can we add more language extensions? There's another lightweight syntactic extension to reach for: &lt;code&gt;LambdaCase&lt;/code&gt;. It lets us write a short lambda and &lt;code&gt;case&lt;/code&gt; together without naming the lambda's variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;#&lt;/span&gt; &lt;span class="err"&gt;∷&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="err"&gt;∷&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;mdo&lt;/span&gt; &lt;span class="kr"&gt;if&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="kr"&gt;let&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mh"&gt;0x0&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;'&lt;/span&gt;&lt;span class="nb"&gt;()&lt;/span&gt;&lt;span class="p"&gt;,)&lt;/span&gt;&lt;span class="err"&gt;→&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="n"&gt;b_0&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="mf"&gt;0e0&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="err"&gt;←&lt;/span&gt;&lt;span class="kt"&gt;[]&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="err"&gt;←&lt;/span&gt;&lt;span class="kt"&gt;[]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="kr"&gt;then&lt;/span&gt;&lt;span class="nf"&gt;\&lt;/span&gt;&lt;span class="kr"&gt;case&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="err"&gt;→&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;a href="https://downloads.haskell.org/~ghc/8.10.1/docs/html/users_guide/glasgow_exts.html#extension-EmptyCase" rel="noopener noreferrer"&gt;EmptyCase&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;The actual body of the &lt;code&gt;case&lt;/code&gt; is kind of annoying. It's taking up five characters without contributing anything! Wouldn't you know it, the &lt;code&gt;EmptyCase&lt;/code&gt; extension lets us get rid of that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;#&lt;/span&gt; &lt;span class="err"&gt;∷&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="err"&gt;∷&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;mdo&lt;/span&gt; &lt;span class="kr"&gt;if&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="kr"&gt;let&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mh"&gt;0x0&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;'&lt;/span&gt;&lt;span class="nb"&gt;()&lt;/span&gt;&lt;span class="p"&gt;,)&lt;/span&gt;&lt;span class="err"&gt;→&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="n"&gt;b_0&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="mf"&gt;0e0&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="err"&gt;←&lt;/span&gt;&lt;span class="kt"&gt;[]&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="err"&gt;←&lt;/span&gt;&lt;span class="kt"&gt;[]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="kr"&gt;then&lt;/span&gt;&lt;span class="nf"&gt;\&lt;/span&gt;&lt;span class="kr"&gt;case&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;So there you have it. That's as many language extensions as I can figure out how to require on a single 80-character line of Haskell code. Can you do better? Are there tricks that I'm missing? Let me know! &lt;/p&gt;




&lt;p&gt;Here's the whole thing written with slightly more normal formatting:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="cp"&gt;{-# language BangPatterns #-}&lt;/span&gt;
&lt;span class="cp"&gt;{-# language BinaryLiterals #-}&lt;/span&gt;
&lt;span class="cp"&gt;{-# language BlockArguments #-}&lt;/span&gt;
&lt;span class="cp"&gt;{-# language EmptyCase #-}&lt;/span&gt;
&lt;span class="cp"&gt;{-# language HexFloatLiterals #-}&lt;/span&gt;
&lt;span class="cp"&gt;{-# language ImplicitParams #-}&lt;/span&gt;
&lt;span class="cp"&gt;{-# language KindSignatures #-}&lt;/span&gt;
&lt;span class="cp"&gt;{-# language LambdaCase #-}&lt;/span&gt;
&lt;span class="cp"&gt;{-# language MagicHash #-}&lt;/span&gt;
&lt;span class="cp"&gt;{-# language MultiWayIf #-}&lt;/span&gt;
&lt;span class="cp"&gt;{-# language NumDecimals #-}&lt;/span&gt;
&lt;span class="cp"&gt;{-# language NumericUnderscores #-}&lt;/span&gt;
&lt;span class="cp"&gt;{-# language ParallelListComp #-}&lt;/span&gt;
&lt;span class="cp"&gt;{-# language PartialTypeSignatures #-}&lt;/span&gt;
&lt;span class="cp"&gt;{-# language RecursiveDo #-}&lt;/span&gt;
&lt;span class="cp"&gt;{-# language ScopedTypeVariables #-}&lt;/span&gt;
&lt;span class="cp"&gt;{-# language TemplateHaskellQuotes #-}&lt;/span&gt;
&lt;span class="cp"&gt;{-# language TransformListComp #-}&lt;/span&gt;
&lt;span class="cp"&gt;{-# language TupleSections #-}&lt;/span&gt;
&lt;span class="cp"&gt;{-# language TypeApplications #-}&lt;/span&gt;
&lt;span class="cp"&gt;{-# language UnicodeSyntax #-}&lt;/span&gt;

&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;#&lt;/span&gt; &lt;span class="err"&gt;∷&lt;/span&gt; &lt;span class="kr"&gt;_&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt; &lt;span class="err"&gt;∷&lt;/span&gt; &lt;span class="kr"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;mdo&lt;/span&gt;
  &lt;span class="kr"&gt;if&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kr"&gt;let&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="mh"&gt;0x0&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;'&lt;/span&gt;&lt;span class="nb"&gt;()&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="err"&gt;→&lt;/span&gt;
      &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="n"&gt;b_0&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="mf"&gt;0e0&lt;/span&gt;
      &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="kr"&gt;_&lt;/span&gt; &lt;span class="err"&gt;←&lt;/span&gt; &lt;span class="kt"&gt;[]&lt;/span&gt;
      &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kr"&gt;_&lt;/span&gt; &lt;span class="err"&gt;←&lt;/span&gt; &lt;span class="kt"&gt;[]&lt;/span&gt;
      &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kr"&gt;then&lt;/span&gt;&lt;span class="nf"&gt;\&lt;/span&gt;&lt;span class="kr"&gt;case&lt;/span&gt;
      &lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>haskell</category>
    </item>
    <item>
      <title>Running HLint as a GHC source plugin</title>
      <dc:creator>Taylor Fausak</dc:creator>
      <pubDate>Mon, 25 May 2020 11:51:28 +0000</pubDate>
      <link>https://dev.to/tfausak/running-hlint-as-a-ghc-source-plugin-17dl</link>
      <guid>https://dev.to/tfausak/running-hlint-as-a-ghc-source-plugin-17dl</guid>
      <description>&lt;p&gt;HLint is a static analyzer for Haskell that suggests how to improve your code and make it more idiomatic. Normally it's run as a standalone executable or an extra test suite. Thanks to &lt;a href="https://www.haskell.org/ghc/blog/20180922-ghc-8.6.1-released.html"&gt;GHC 8.6&lt;/a&gt; and &lt;a href="https://neilmitchell.blogspot.com/2020/05/hlint-30.html"&gt;HLint 3&lt;/a&gt; it's now possible to run HLint as part of GHC by using a source plugin. This post introduces &lt;a href="https://github.com/tfausak/splint"&gt;Splint&lt;/a&gt;, which does exactly that.&lt;/p&gt;

&lt;h2&gt;
  
  
  Motivation
&lt;/h2&gt;

&lt;p&gt;Running HLint by itself is a fine workflow, and you may still prefer doing that even if it's available as a source plugin. But why would you want a linter plugin in the first place?&lt;/p&gt;

&lt;p&gt;The main motivation is parsing. It used to be that HLint used its own custom parser called &lt;code&gt;haskell-src-exts&lt;/code&gt; rather than the one GHC uses to actually compile your code. This can lead to annoying bugs where GHC parses your code just fine but HLint has trouble with it. &lt;/p&gt;

&lt;p&gt;However since version 3 HLint uses GHC for parsing instead of &lt;code&gt;haskell-src-exts&lt;/code&gt;. It seems like this should fix the problem, right? Well, yes and no. You might call GHC with some options, like &lt;code&gt;-XTemplateHaskell&lt;/code&gt; to enable the Template Haskell extension. Even though HLint is using GHC's parser, you still have to make sure it sets up GHC in the same way, otherwise your module may fail to parse. This is possible to do, but it can be tedious to keep things in sync.&lt;/p&gt;

&lt;p&gt;This is why source plugins are nice. You can hook into the compilation process right after the module has been parsed. Then you can pass that parsed module directly to HLint, without parsing it again or doing any serialization. This way you can be sure the module parsed correctly, and you can avoid doing any extra work. &lt;/p&gt;

&lt;h2&gt;
  
  
  Inspiration
&lt;/h2&gt;

&lt;p&gt;Hopefully you're convinced that running HLint as a GHC source plugin is at least not a bad idea. In fact it was a good idea even before HLint switched over to using GHC's parser. Two years ago Ollie Charles made &lt;a href="https://github.com/ocharles/hlint-source-plugin"&gt;hlint-source-plugin&lt;/a&gt; as a proof of concept. &lt;/p&gt;

&lt;p&gt;Wait, what? If hlint-source-plugin already exists, why did I make my own project and write this blog post? As mentioned, Ollie created his project back when HLint used its own parser. That means the plugin has to re-parse modules after GHC has parsed them. It could be upgraded to avoid re-parsing, but the plugin itself is so small that it seemed easier to make a new one from scratch.&lt;/p&gt;

&lt;p&gt;At any rate, thanks to Ollie for showing that something like this was possible! &lt;/p&gt;

&lt;h2&gt;
  
  
  Usage
&lt;/h2&gt;

&lt;p&gt;To use Splint, pass &lt;code&gt;-fplugin=Splint&lt;/code&gt; to GHC. Any ideas suggested by HLint will be reported as warnings by GHC. For example, if you define &lt;code&gt;Main.hs&lt;/code&gt; as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="n"&gt;main&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;print&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt; &lt;span class="n"&gt;concat&lt;/span&gt; &lt;span class="o"&gt;$&lt;/span&gt; &lt;span class="n"&gt;map&lt;/span&gt; &lt;span class="n"&gt;pure&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="sc"&gt;'a'&lt;/span&gt; &lt;span class="o"&gt;..&lt;/span&gt; &lt;span class="sc"&gt;'z'&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You would expect HLint to tell you to use &lt;code&gt;concatMap&lt;/code&gt;. Normally you would need to both compile your module with GHC and lint it with HLint. However with Splint you can compile it and get suggestions from HLint all at once by running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;ghc &lt;span class="nt"&gt;-fplugin&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;Splint Main.hs
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Among all the usual output from GHC, you should see this new warning:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Main.hs:1:8: warning:
    Use concatMap
    Perhaps: print (concatMap pure ['a' .. 'z'])
  |
1 | main = print . concat $ map pure [ 'a' .. 'z' ]
  |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And that's all there is to it! HLint suggestions as part of your normal build process. What a time to be alive.&lt;/p&gt;

&lt;p&gt;If you want to pass arguments through to HLint, you can use &lt;code&gt;-fplugin-opt=Splint:arg&lt;/code&gt;. For example you can ignore the warning above with &lt;code&gt;-fplugin-opt=Splint:'--ignore=Use concatMap'&lt;/code&gt;. Usually this won’t be necessary because Splint will use your &lt;code&gt;.hlint.yaml&lt;/code&gt; configuration file.&lt;/p&gt;

&lt;h2&gt;
  
  
  Trade offs
&lt;/h2&gt;

&lt;p&gt;Running HLint as a GHC source plugin has some upsides:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Modules are only linted when they're compiled, so if they haven't changed they won't be linted again.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;HLint's suggestions are reported just like GHC's warnings. They're formatted the same way and they're reported at the same time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Each module is only parsed once.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Parsing is done by GHC instead of something like &lt;code&gt;haskell-src-exts&lt;/code&gt;. HLint already works like this, but by using a plugin you can be sure that all of the versions and options line up correctly.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;However it's also got some downsides:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Using Splint means adding it as a dependency to the targets you want to lint. Normally HLint is either a test dependency or just installed on the system.&lt;/p&gt;

&lt;p&gt;You may be able to lessen the impact of this by providing a flag to control linting. That way you can enable it locally and in CI, but not require everything downstream of you to depend on HLint.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;flag lint
  default: False
  manual: True
library
  if flag(lint)
    build-depends: splint
    ghc-options: -fplugin=Splint
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It's slower. I've found that it adds about a tenth of a second per module.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can't use the automated refactorings that HLint provides.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Using plugins marks every module as unsafe.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Hopefully this post has explained why you might want to integration HLint into your build process as a GHC source plugin. &lt;a href="https://hackage.haskell.org/package/splint-1.0.0.0"&gt;Splint&lt;/a&gt; makes it a cinch, so please check it out! &lt;/p&gt;

</description>
      <category>haskell</category>
    </item>
    <item>
      <title>How to define JSON instances quickly</title>
      <dc:creator>Taylor Fausak</dc:creator>
      <pubDate>Tue, 19 May 2020 11:23:29 +0000</pubDate>
      <link>https://dev.to/tfausak/how-to-define-json-instances-quickly-5ei7</link>
      <guid>https://dev.to/tfausak/how-to-define-json-instances-quickly-5ei7</guid>
      <description>&lt;p&gt;Working on a large Haskell project means grappling with GHC's long compile times. This can be especially frustrating when doing iterative development. To help with that, Matt Parsons recently posted &lt;a href="https://www.parsonsmatt.org/2019/11/27/keeping_compilation_fast.html"&gt;some tips for keeping compilation fast&lt;/a&gt;. He suggests ways to organize types, instances, and modules in order to speed up the build. In a similar vein, a few years ago I posted about how &lt;a href="https://taylor.fausak.me/2017/08/09/deriving-type-classes-in-haskell-is-slow/"&gt;deriving type class instances&lt;/a&gt; takes longer than you might think. &lt;/p&gt;

&lt;p&gt;Today I'm going to explore various methods for defining type class instances and their relative performance. Is it faster to write instances by hand or to derive them? Should you put everything in one module or put each type in its own? Is it always faster to use more threads? How long does optimization take? Let's find out.&lt;/p&gt;

&lt;h2&gt;
  
  
  Data types
&lt;/h2&gt;

&lt;p&gt;If we're going to talk about the performance of deriving type class instances, we'll need some types to derive the instances for. Fortunately I maintain &lt;a href="https://hackage.haskell.org/package/rattletrap-9.1.1"&gt;Rattletrap&lt;/a&gt;, a command-line program that parses Rocket League replays and outputs JSON. As you might expect, it defines a bunch of types. There's nothing too surprising about them. They fall into three main categories: simple type wrappers, records with fields, and enums with constructors. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- simple type wrapper&lt;/span&gt;
&lt;span class="kr"&gt;newtype&lt;/span&gt; &lt;span class="kt"&gt;Word32le&lt;/span&gt;
  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Word32le&lt;/span&gt; &lt;span class="kt"&gt;Word32&lt;/span&gt;

&lt;span class="c1"&gt;-- record with fields&lt;/span&gt;
&lt;span class="kr"&gt;data&lt;/span&gt; &lt;span class="kt"&gt;Version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Version&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;major&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="kt"&gt;Word32le&lt;/span&gt;
  &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;minor&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="kt"&gt;Word32le&lt;/span&gt;
  &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;patch&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="kt"&gt;Maybe&lt;/span&gt; &lt;span class="kt"&gt;Word32le&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;-- enum with constructors&lt;/span&gt;
&lt;span class="kr"&gt;data&lt;/span&gt; &lt;span class="kt"&gt;RemoteId&lt;/span&gt;
  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;PlayStation&lt;/span&gt; &lt;span class="kt"&gt;Text&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Word8&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kt"&gt;Steam&lt;/span&gt; &lt;span class="kt"&gt;Word64le&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kt"&gt;Xbox&lt;/span&gt; &lt;span class="kt"&gt;Word64le&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Type classes
&lt;/h2&gt;

&lt;p&gt;There are many type classes that you can define instances for. We're going to focus on &lt;a href="https://hackage.haskell.org/package/aeson-1.4.7.1"&gt;Aeson&lt;/a&gt;'s &lt;code&gt;ToJSON&lt;/code&gt; and &lt;code&gt;FromJSON&lt;/code&gt; type classes. Since Aeson is the most popular JSON library on Hackage, you're probably going to need these type classes at some point. Plus they're easy to implement but still interesting enough to avoid being trivial. If you're not already familiar with them, here they are:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="kr"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;ToJSON&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="kr"&gt;where&lt;/span&gt;
  &lt;span class="n"&gt;toJSON&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Value&lt;/span&gt;
  &lt;span class="n"&gt;toEncoding&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Encoding&lt;/span&gt;

&lt;span class="kr"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;FromJSON&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="kr"&gt;where&lt;/span&gt;
  &lt;span class="n"&gt;parseJSON&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="kt"&gt;Value&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Parser&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The extra &lt;code&gt;toEncoding&lt;/code&gt; method on the &lt;code&gt;ToJSON&lt;/code&gt; type class is there for performance reasons. It encodes directly without creating an intermediate value. &lt;/p&gt;

&lt;h2&gt;
  
  
  Instances
&lt;/h2&gt;

&lt;p&gt;I won't get into what the instances do too much since that's not really the point of this post. But when it comes to implementing the instances, you have at least three options:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Write the instances by hand. This is tedious and error prone, but also straightforward and easy to understand.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="kr"&gt;instance&lt;/span&gt; &lt;span class="kt"&gt;ToJSON&lt;/span&gt; &lt;span class="kt"&gt;Version&lt;/span&gt; &lt;span class="kr"&gt;where&lt;/span&gt;
  &lt;span class="n"&gt;toJSON&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;object&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="s"&gt;"major"&lt;/span&gt; &lt;span class="o"&gt;.=&lt;/span&gt; &lt;span class="n"&gt;major&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;
    &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"minor"&lt;/span&gt; &lt;span class="o"&gt;.=&lt;/span&gt; &lt;span class="n"&gt;minor&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;
    &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"patch"&lt;/span&gt; &lt;span class="o"&gt;.=&lt;/span&gt; &lt;span class="n"&gt;patch&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="n"&gt;toEncoding&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pairs&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s"&gt;"major"&lt;/span&gt; &lt;span class="o"&gt;.=&lt;/span&gt; &lt;span class="n"&gt;major&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"minor"&lt;/span&gt; &lt;span class="o"&gt;.=&lt;/span&gt; &lt;span class="n"&gt;minor&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"patch"&lt;/span&gt; &lt;span class="o"&gt;.=&lt;/span&gt; &lt;span class="n"&gt;patch&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kr"&gt;instance&lt;/span&gt; &lt;span class="kt"&gt;FromJSON&lt;/span&gt; &lt;span class="kt"&gt;Version&lt;/span&gt; &lt;span class="kr"&gt;where&lt;/span&gt;
  &lt;span class="n"&gt;parseJSON&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;withObject&lt;/span&gt; &lt;span class="s"&gt;"Version"&lt;/span&gt; &lt;span class="o"&gt;$&lt;/span&gt; &lt;span class="nf"&gt;\&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Version&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;$&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;.:&lt;/span&gt; &lt;span class="s"&gt;"major"&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;*&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;.:&lt;/span&gt; &lt;span class="s"&gt;"minor"&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;*&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;.:&lt;/span&gt; &lt;span class="s"&gt;"patch"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Derive the instances using generics. This requires the &lt;code&gt;DeriveGeneric&lt;/code&gt; and &lt;code&gt;DeriveAnyClass&lt;/code&gt; extensions, along with the &lt;code&gt;GHC.Generics&lt;/code&gt; module. It doesn't require any special syntax and behaves the same as deriving more typical type classes like &lt;code&gt;Eq&lt;/code&gt; and &lt;code&gt;Show&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="cp"&gt;{-# LANGUAGE DeriveGeneric, DeriveAnyClass #-}&lt;/span&gt;
&lt;span class="kr"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;GHC.Generics&lt;/span&gt;
&lt;span class="kr"&gt;data&lt;/span&gt; &lt;span class="kt"&gt;Version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Version&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;-- omitted, but same as before&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="kr"&gt;deriving&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Generic&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;ToJSON&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;FromJSON&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Derive the instances using Template Haskell. Unsurprisingly this requires the &lt;code&gt;TemplateHaskell&lt;/code&gt; extension. It uses a different syntax and runs custom code at compile time.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="cp"&gt;{-# LANGUAGE TemplateHaskell #-}&lt;/span&gt;
&lt;span class="o"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;deriveJSON&lt;/span&gt; &lt;span class="n"&gt;defaultOptions&lt;/span&gt; &lt;span class="sc"&gt;''&lt;/span&gt;&lt;span class="err"&gt;V&lt;/span&gt;&lt;span class="n"&gt;ersion&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Details
&lt;/h2&gt;

&lt;p&gt;Since this is a benchmark, I'm going to be pedantic about how I'm running things. Feel free to skip this section if you don't care. &lt;/p&gt;

&lt;p&gt;I extracted 77 data types from the Rattletrap project. I put them into a Haskell script for generating code in various configurations. I uploaded the code to &lt;a href="https://gist.github.com/tfausak/a5cae9e41e5ccd0b0a5b4e49f1e2104d/55e603e732f3fb07b6fd9f164f119177cfd2229e"&gt;this Gist&lt;/a&gt; so you can run it for yourself. &lt;/p&gt;

&lt;p&gt;I ran the code using Stack 2.3.1 with the LTS-15.13 snapshot, which includes Aeson 1.4.7.1. Instead of using the provided version of GHC, I upgraded to 8.10.1. &lt;/p&gt;

&lt;p&gt;I ran the code on two desktops: a Windows machine with an Intel Core i5-8400 (6 cores, 6 threads) and an Ubuntu machine with an AMD Ryzen 5 2400G (4 cores, 8 thread). Both machines use SSD storage.&lt;/p&gt;

&lt;p&gt;All measurements are given in seconds. Times are taken from all the configurations listed in this post. Initially I ran each configuration three times, but the times didn't change much between runs. So in the end I ran each configuration only once.&lt;/p&gt;

&lt;p&gt;All compilation was done from scratch using &lt;code&gt;-fforce-recomp&lt;/code&gt; along with &lt;code&gt;-v0&lt;/code&gt; and &lt;code&gt;-w&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Optimization
&lt;/h2&gt;

&lt;p&gt;GHC offers three optimization levels, from least optimized to most: &lt;code&gt;-O0&lt;/code&gt;, &lt;code&gt;-O1&lt;/code&gt;, and &lt;code&gt;-O2&lt;/code&gt;. Usually development builds are done with &lt;code&gt;-O0&lt;/code&gt; and production builds are either done with &lt;code&gt;-O1&lt;/code&gt; or &lt;code&gt;-O2&lt;/code&gt;. &lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;code&gt;-On&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;Minimum&lt;/th&gt;
&lt;th&gt;Average&lt;/th&gt;
&lt;th&gt;Maximum&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1.75&lt;/td&gt;
&lt;td&gt;4.74&lt;/td&gt;
&lt;td&gt;8.03&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;15.93&lt;/td&gt;
&lt;td&gt;30.09&lt;/td&gt;
&lt;td&gt;48.57&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;19.32&lt;/td&gt;
&lt;td&gt;32.79&lt;/td&gt;
&lt;td&gt;52.25&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Unsurprisingly &lt;code&gt;-O0&lt;/code&gt; is by far the fastest. For quick feedback from the compiler, make sure you're disabling optimizations by setting &lt;code&gt;-O0&lt;/code&gt;. For production builds, this workload didn't show a big difference between &lt;code&gt;-O1&lt;/code&gt; and &lt;code&gt;-O2&lt;/code&gt;. (Note that I only analyzed compile time performance, not run time.) &lt;/p&gt;

&lt;h2&gt;
  
  
  Layout
&lt;/h2&gt;

&lt;p&gt;When writing code, you can choose to put everything in one module or to put each thing in its own module. (You can, and probably do, do something in between, but I didn't benchmark that.) Working in a single module can be easier to reason about, since you don't have to jump around to different files.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Layout&lt;/th&gt;
&lt;th&gt;Minimum&lt;/th&gt;
&lt;th&gt;Average&lt;/th&gt;
&lt;th&gt;Maximum&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Single module&lt;/td&gt;
&lt;td&gt;2.34&lt;/td&gt;
&lt;td&gt;25.28&lt;/td&gt;
&lt;td&gt;52.25&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Multiple modules&lt;/td&gt;
&lt;td&gt;1.75&lt;/td&gt;
&lt;td&gt;19.81&lt;/td&gt;
&lt;td&gt;48.55&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Splitting up your code into multiple modules will almost always make your code faster to compile. Even if it didn't, it would still be a good idea because after the first compile you'd only have to recompile the things that changed. &lt;/p&gt;

&lt;p&gt;The only time that a single module is faster than multiple modules is when you only have one core/thread at your disposal. And even then it's not too much faster.&lt;/p&gt;

&lt;h2&gt;
  
  
  Jobs
&lt;/h2&gt;

&lt;p&gt;GHC can use multiple threads when compiling. This seems like it should help a lot, since compiling each module can be done more or less in isolation. &lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;code&gt;-jn&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;Minimum&lt;/th&gt;
&lt;th&gt;Average&lt;/th&gt;
&lt;th&gt;Maximum&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;2.34&lt;/td&gt;
&lt;td&gt;26.75&lt;/td&gt;
&lt;td&gt;49.56&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;2.33&lt;/td&gt;
&lt;td&gt;22.50&lt;/td&gt;
&lt;td&gt;48.92&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;1.75&lt;/td&gt;
&lt;td&gt;20.17&lt;/td&gt;
&lt;td&gt;50.23&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;1.81&lt;/td&gt;
&lt;td&gt;20.75&lt;/td&gt;
&lt;td&gt;52.25&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;As expected, using more cores is faster, up to a point. On my machine, going from 4 to 8 threads slowed things down rather than speeding them up. &lt;/p&gt;

&lt;p&gt;Averaging these measurements downplay how significant of an impact they can have on your compile times. For example when compiling multiple modules with hand written instances at &lt;code&gt;-O1&lt;/code&gt;, going from &lt;code&gt;-j1&lt;/code&gt; to &lt;code&gt;-j4&lt;/code&gt; makes the build nearly twice as fast (from 31 seconds to 17). &lt;/p&gt;

&lt;h2&gt;
  
  
  Implementation
&lt;/h2&gt;

&lt;p&gt;As explained in the intro, I looked at three different implementation strategies: writing instances by hand, deriving them with Template Haskell, and deriving them with generics. &lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Implementation&lt;/th&gt;
&lt;th&gt;Minimum&lt;/th&gt;
&lt;th&gt;Average&lt;/th&gt;
&lt;th&gt;Maximum&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Manual&lt;/td&gt;
&lt;td&gt;1.75&lt;/td&gt;
&lt;td&gt;17.37&lt;/td&gt;
&lt;td&gt;35.72&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;TH&lt;/td&gt;
&lt;td&gt;3.79&lt;/td&gt;
&lt;td&gt;21.27&lt;/td&gt;
&lt;td&gt;40.79&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Generics&lt;/td&gt;
&lt;td&gt;4.32&lt;/td&gt;
&lt;td&gt;29.00&lt;/td&gt;
&lt;td&gt;52.25&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;In spite of its reputation as being a heavyweight, Template Haskell easily beats out generics when it comes to speed of deriving instances. However both of them are absolutely crushed by hand written instances. If you want compilation to be as fast as possible, you should write your own instances. Otherwise you should probably use TH.&lt;/p&gt;

&lt;p&gt;That being said, Template Haskell does have one major downside when compared to generics: It recompiles more often. Let's say module &lt;code&gt;A&lt;/code&gt; depends on module &lt;code&gt;B&lt;/code&gt;. If you change module &lt;code&gt;B&lt;/code&gt;, module &lt;code&gt;A&lt;/code&gt; will be compiled if it contains TH. If it doesn't contain TH, it may not be recompiled. (It depends on what changes you made to &lt;code&gt;B&lt;/code&gt;.) So if you frequently change modules below ones with TH in the hierarchy, you may be better off using generics.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;When it comes to type class instances in Haskell, if you want compilation to be fast you should:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Compile with &lt;code&gt;-O0&lt;/code&gt; for development.&lt;/li&gt;
&lt;li&gt;Put each type in its own module.&lt;/li&gt;
&lt;li&gt;Use lots of threads, but not too many. (&lt;code&gt;-j4&lt;/code&gt; is probably a good default.)&lt;/li&gt;
&lt;li&gt;Prefer Template Haskell over generics. (Write them by hand if you don't mind the tedium.) &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Future work
&lt;/h2&gt;

&lt;p&gt;Are there other build configurations I should have benchmarked? Let me know and I'll try them out! &lt;/p&gt;

&lt;p&gt;I had a hard time slicing and dicing these numbers. If you want to take a deeper look at the data, I encourage you to open &lt;a href="https://docs.google.com/spreadsheets/d/1jqtvvMjwKXJmLV7Nc5LRaGbwkcGr6Q03Xvo4LtqtxFI/edit?usp=sharing"&gt;the spreadsheet&lt;/a&gt; and poke around. &lt;/p&gt;

&lt;p&gt;I think it would be interesting to explore using &lt;a href="https://downloads.haskell.org/~ghc/8.10.1/docs/html/users_guide/extending_ghc.html#source-plugins"&gt;GHC source plugins&lt;/a&gt; to derive instances quickly without forcing recompilation as often as Template Haskell. &lt;/p&gt;

</description>
      <category>haskell</category>
    </item>
    <item>
      <title>Match URI templates with Burrito</title>
      <dc:creator>Taylor Fausak</dc:creator>
      <pubDate>Sat, 09 May 2020 22:43:16 +0000</pubDate>
      <link>https://dev.to/tfausak/match-uri-templates-with-burrito-4a1p</link>
      <guid>https://dev.to/tfausak/match-uri-templates-with-burrito-4a1p</guid>
      <description>&lt;p&gt;URI templates are a powerful way to succinctly describe URLs. For example the URI template &lt;code&gt;https://hackage.haskell.org/package/{name}&lt;/code&gt; shows you how to find a Haskell package on Hackage. By expanding that template with &lt;code&gt;name := "parsec"&lt;/code&gt; you can get the full URL of a package: &lt;code&gt;https://hackage.haskell.org/package/parsec&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="kr"&gt;let&lt;/span&gt; &lt;span class="kt"&gt;Just&lt;/span&gt; &lt;span class="n"&gt;template&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;parse&lt;/span&gt; &lt;span class="s"&gt;"https://hackage.haskell.org/package/{name}"&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;expand&lt;/span&gt; &lt;span class="p"&gt;[(&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stringValue&lt;/span&gt; &lt;span class="s"&gt;"parsec"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="n"&gt;template&lt;/span&gt;
&lt;span class="s"&gt;"https://hackage.haskell.org/package/parsec"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://tools.ietf.org/html/rfc6570"&gt;RFC 6570&lt;/a&gt; specifies how URI templates work. Unfortunately it doesn't describe how to go in the other direction. That is, how to take a full URL and parse it according to a template. For example you may want to take a package URL and match it against the template to get the package name. This is kind of like doing template expansion in reverse.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;match&lt;/span&gt; &lt;span class="s"&gt;"https://hackage.haskell.org/package/parsec"&lt;/span&gt; &lt;span class="n"&gt;template&lt;/span&gt;
&lt;span class="p"&gt;[[(&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="s"&gt;"parsec"&lt;/span&gt;&lt;span class="p"&gt;)]]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I'm happy to announce that &lt;a href="https://hackage.haskell.org/package/burrito-1.1.0.0"&gt;version 1.1.0.0 of Burrito&lt;/a&gt;, my URI template library for Haskell, can now perform this matching operation! So if you find yourself needing to expand or match URI templates, please check it out.&lt;/p&gt;

</description>
      <category>haskell</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
