<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Read, Review, Refactor</title>
  <link href="https://queertypes.com/atom.xml" rel="self" />
  <link href="https://queertypes.com" />
  <id>https://queertypes.com/atom.xml</id>
  <author>
    <name>Allele Dev</name>
    <email>allele.dev@gmail.com</email>
  </author>
  <updated>2020-04-02T00:00:00Z</updated>
  <entry>
    <title>A Typed Socket API</title>
    <link href="https://queertypes.com/posts/60-a-typed-socket-api.html" />
    <id>https://queertypes.com/posts/60-a-typed-socket-api.html</id>
    <published>2017-12-02T00:00:00Z</published>
    <updated>2017-12-02T00:00:00Z</updated>
    <summary type="html"><![CDATA[<p>After wanting to for some time, I decided to finally get around to putting together a typed layer over the Haskell <a href="http://hackage.haskell.org/package/network">network</a> library. This post goes into the techniques I used to make that happen, how it works and looks, and what the current short-comings are.</p>
    <p>Singletons and phantom types are involved! For the code written in this post, the following language extensions are assumed to be enabled:</p>
    <div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb1-1" title="1"><span class="ot">{-# LANGUAGE LambdaCase #-}</span></a>
    <a class="sourceLine" id="cb1-2" title="2"><span class="ot">{-# LANGUAGE StandaloneDeriving #-}</span></a>
    <a class="sourceLine" id="cb1-3" title="3"><span class="ot">{-# LANGUAGE ConstraintKinds #-}</span></a>
    <a class="sourceLine" id="cb1-4" title="4"><span class="ot">{-# LANGUAGE DataKinds #-}</span></a>
    <a class="sourceLine" id="cb1-5" title="5"><span class="ot">{-# LANGUAGE EmptyDataDecls #-}</span></a>
    <a class="sourceLine" id="cb1-6" title="6"><span class="ot">{-# LANGUAGE GADTs #-}</span></a>
    <a class="sourceLine" id="cb1-7" title="7"><span class="ot">{-# LANGUAGE KindSignatures #-}</span></a>
    <a class="sourceLine" id="cb1-8" title="8"><span class="ot">{-# LANGUAGE PartialTypeSignatures #-}</span></a>
    <a class="sourceLine" id="cb1-9" title="9"><span class="ot">{-# LANGUAGE RankNTypes #-}</span></a>
    <a class="sourceLine" id="cb1-10" title="10"><span class="ot">{-# LANGUAGE ScopedTypeVariables #-}</span></a>
    <a class="sourceLine" id="cb1-11" title="11"><span class="ot">{-# LANGUAGE TypeFamilies #-}</span></a></code></pre></div>
    <h2 id="contents">Contents</h2>
    <ul>
    <li><a href="#network-sockets">Network Sockets</a></li>
    <li><a href="#capturing-state-machine-assumptions-as-types">Capturing State (Machine) Assumptions as Types</a></li>
    <li><a href="#singletons-mean-1-to-1">Singletons Mean 1-to-1</a></li>
    <li><a href="#coloring-in-the-state-machine">Coloring in the State Machine</a></li>
    <li><a href="#sending-and-receiving-but-only-when-it-makes-sense">Sending and Receiving, but Only When it Makes Sense</a></li>
    <li><a href="#safer-socket-address-initialization">Safer Socket Address Initialization</a></li>
    <li><a href="#making-it-nicer-to-use">Making it Nicer to Use</a></li>
    <li><a href="#the-client-meows-and-the-server-echoes">The Client Meows and the Server Echoes</a></li>
    <li><a href="#testing-it">Testing It</a></li>
    <li><a href="#socket-options-and-socket-wishes">Socket Options and Socket Wishes</a></li>
    <li><a href="#lacking-linearity-leaves-limitations">Lacking Linearity Leaves Limitations</a></li>
    <li><a href="#closing">Closing</a></li>
    <li><a href="https://gitlab.com/queertypes/linear-socket">Library Source Code (→)</a></li>
    </ul>
    <h2 id="network-sockets">Network Sockets</h2>
    <p>Working with sockets was often a bit of an adventure. There’s a particular order to things, which, while well-documented, wasn’t always clear. For example, to create a client socket that communicate using TCP, you’d have to:</p>
    <ul>
    <li>initialize a socket of family Inet4 with Stream packet type</li>
    <li>allocate a network address structure</li>
    <li>connect the socket</li>
    <li>send/receive as needed</li>
    </ul>
    <p>Haskell code to achieve this looks like this:</p>
    <div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb2-1" title="1"><span class="op">&gt;</span> <span class="kw">import</span> <span class="dt">Network.Socket</span></a>
    <a class="sourceLine" id="cb2-2" title="2"><span class="op">&gt;</span> <span class="kw">import</span> <span class="kw">qualified</span> <span class="dt">Network.Socket.ByteString</span> <span class="kw">as</span> <span class="dt">NSB</span></a>
    <a class="sourceLine" id="cb2-3" title="3"></a>
    <a class="sourceLine" id="cb2-4" title="4"><span class="op">&gt;</span> serverAddress <span class="ot">=</span> <span class="dt">SockAddr</span> <span class="dv">2291</span> (tupleToHostAddress (<span class="dv">127</span>,<span class="dv">0</span>,<span class="dv">0</span>,<span class="dv">1</span>))</a>
    <a class="sourceLine" id="cb2-5" title="5"><span class="op">&gt;</span> socket <span class="ot">&lt;-</span> socket <span class="dt">AF_INET</span> <span class="dt">Stream</span> defaultProtocol</a>
    <a class="sourceLine" id="cb2-6" title="6"><span class="op">&gt;</span> connect socket serverAddress</a>
    <a class="sourceLine" id="cb2-7" title="7"><span class="op">&gt;</span> send socket <span class="st">&quot;fish&quot;</span></a>
    <a class="sourceLine" id="cb2-8" title="8"><span class="op">&gt;</span> bs <span class="ot">&lt;-</span> recv socket <span class="dv">32</span></a>
    <a class="sourceLine" id="cb2-9" title="9"><span class="op">&gt;</span> close socket</a></code></pre></div>
    <p>To create a server socket is a bit more involved. The steps are the same up to the point where we allocate a network address structure. After that, the steps are:</p>
    <ul>
    <li>bind the server socket to a local port and address</li>
    <li>set the socket to start listening</li>
    <li>in a loop:
    <ul>
    <li>wait for the socket to <code>accept</code> connections</li>
    <li>recv/send to connections that arrive (possibly in a new thread)</li>
    </ul></li>
    </ul>
    <p>The code looks like this, without a loop:</p>
    <div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb3-1" title="1"><span class="op">&gt;</span> <span class="kw">import</span> <span class="dt">Network.Socket</span></a>
    <a class="sourceLine" id="cb3-2" title="2"><span class="op">&gt;</span> <span class="kw">import</span> <span class="kw">qualified</span> <span class="dt">Network.Socket.ByteString</span> <span class="kw">as</span> <span class="dt">NSB</span></a>
    <a class="sourceLine" id="cb3-3" title="3"></a>
    <a class="sourceLine" id="cb3-4" title="4"><span class="op">&gt;</span> serverAddress <span class="ot">=</span> <span class="dt">SockAddr</span> <span class="dv">2291</span> (tupleToHostAddress (<span class="dv">127</span>,<span class="dv">0</span>,<span class="dv">0</span>,<span class="dv">1</span>))</a>
    <a class="sourceLine" id="cb3-5" title="5"><span class="op">&gt;</span> socket <span class="ot">&lt;-</span> socket <span class="dt">AF_INET</span> <span class="dt">Stream</span> defaultProtocol</a>
    <a class="sourceLine" id="cb3-6" title="6"><span class="op">&gt;</span> bind socket serverAddress</a>
    <a class="sourceLine" id="cb3-7" title="7"><span class="op">&gt;</span> listen socket <span class="dv">1</span></a>
    <a class="sourceLine" id="cb3-8" title="8"><span class="op">&gt;</span> (client, _) <span class="ot">&lt;-</span> accept socket</a>
    <a class="sourceLine" id="cb3-9" title="9"><span class="op">&gt;</span> bs <span class="ot">&lt;-</span> recv client <span class="dv">32</span></a>
    <a class="sourceLine" id="cb3-10" title="10"><span class="op">&gt;</span> send client bs</a>
    <a class="sourceLine" id="cb3-11" title="11"><span class="op">&gt;</span> close client</a>
    <a class="sourceLine" id="cb3-12" title="12"><span class="op">&gt;</span> close socket</a></code></pre></div>
    <p>While seemingly relatively straight-forward (but perhaps only if one has read network socket documentation a lot), there’s some room for error. For example, one might:</p>
    <ul>
    <li>try to send/recv from a socket that is closed or unconnected</li>
    <li>try to listen on a server socket that is not yet bound</li>
    <li>try to accept connections on a server socket before starting to listen</li>
    </ul>
    <p>And if we consider the broader socket API, there’s also room for error around sockets that have shutdown their ability to send or receive. There’s also some very particular rules around sending or receiving with UDP sockets when using the <code>recvFrom</code>/<code>sendTo</code> interfaces.</p>
    <p>Lots of details. All documented in a few places. But!! We can do a little better with types. That’s my motivation for this effort, and the rest of this post goes into the design and implementation of such an API.</p>
    <h2 id="capturing-state-machine-assumptions-as-types">Capturing State (Machine) Assumptions as Types</h2>
    <p>Working with the socket API, there’s at least 4 bits of intrinsic state to them that determine how they work:</p>
    <ul>
    <li>status: unconnected, closed, connected, listening, bound, etc.</li>
    <li>protocol/type: stream/tcp, datagram/udp, etc.</li>
    <li>family: inetv4, inetv6, unix, etc.</li>
    <li>shutdown state: fully-open, blocked from sending, blocked from receiving, blocked</li>
    </ul>
    <p>Here’s a snippet of the current socket APIs type signatures:</p>
    <div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb4-1" title="1"><span class="ot">socket ::</span> <span class="dt">Family</span> <span class="ot">-&gt;</span> <span class="dt">SocketType</span> <span class="ot">-&gt;</span> <span class="dt">ProtocolNumber</span> <span class="ot">-&gt;</span> <span class="dt">IO</span> <span class="dt">Socket</span></a>
    <a class="sourceLine" id="cb4-2" title="2"><span class="ot">connect ::</span> <span class="dt">Socket</span> <span class="ot">-&gt;</span> <span class="dt">SockAddr</span> <span class="ot">-&gt;</span> <span class="dt">IO</span> ()</a>
    <a class="sourceLine" id="cb4-3" title="3"><span class="ot">bind ::</span> <span class="dt">Socket</span> <span class="ot">-&gt;</span> <span class="dt">SockAddr</span> <span class="ot">-&gt;</span> <span class="dt">IO</span> ()</a>
    <a class="sourceLine" id="cb4-4" title="4"><span class="ot">listen ::</span> <span class="dt">Socket</span> <span class="ot">-&gt;</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">IO</span> ()</a>
    <a class="sourceLine" id="cb4-5" title="5"><span class="ot">accept ::</span> <span class="dt">Socket</span> <span class="ot">-&gt;</span> <span class="dt">IO</span> (<span class="dt">Socket</span>, <span class="dt">SockAddr</span>)</a>
    <a class="sourceLine" id="cb4-6" title="6"><span class="ot">shutdown ::</span> <span class="dt">Socket</span> <span class="ot">-&gt;</span> <span class="dt">ShutdownCmd</span> <span class="ot">-&gt;</span> <span class="dt">IO</span> ()</a>
    <a class="sourceLine" id="cb4-7" title="7"><span class="ot">close ::</span> <span class="dt">Socket</span> <span class="ot">-&gt;</span> <span class="dt">IO</span> ()</a>
    <a class="sourceLine" id="cb4-8" title="8"><span class="ot">send ::</span> <span class="dt">Socket</span> <span class="ot">-&gt;</span> <span class="dt">ByteString</span> <span class="ot">-&gt;</span> <span class="dt">IO</span> <span class="dt">Int</span></a>
    <a class="sourceLine" id="cb4-9" title="9"><span class="ot">recv ::</span> <span class="dt">Socket</span> <span class="ot">-&gt;</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">IO</span> <span class="dt">ByteString</span></a>
    <a class="sourceLine" id="cb4-10" title="10"><span class="ot">sendTo ::</span> <span class="dt">Socket</span> <span class="ot">-&gt;</span> <span class="dt">ByteString</span> <span class="ot">-&gt;</span> <span class="dt">SockAddr</span> <span class="ot">-&gt;</span> <span class="dt">IO</span> <span class="dt">Int</span></a>
    <a class="sourceLine" id="cb4-11" title="11"><span class="ot">recvFrom ::</span> <span class="dt">Socket</span> <span class="ot">-&gt;</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">IO</span> (<span class="dt">ByteString</span>, <span class="dt">SockAddr</span>)</a></code></pre></div>
    <p>What I’d like to point out is that once we call <code>socket</code>, we have a <code>Socket</code>, but our types tell us nothing about that <code>Socket</code>. Further, when we call <code>connect</code> or <code>bind</code> or <code>listen</code> or <code>close</code>, we lose information. Changes are happening to that socket, but all we get back is an empty void, <code>IO ()</code>. We know an effect has happened! But - we forget the nature of that effect.</p>
    <p><code>IO ()</code>, for when you want to throw away information.</p>
    <p>Fortunately, Haskell is one of those languages where we can track these sorts of changes in the types themselves. So, what would a type-enhanced socket type look like?</p>
    <p>Here’s one possible design:</p>
    <div class="sourceCode" id="cb5"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb5-1" title="1"><span class="kw">import</span> <span class="kw">qualified</span> <span class="dt">Network.Socket</span> <span class="kw">as</span> <span class="dt">NS</span></a>
    <a class="sourceLine" id="cb5-2" title="2"></a>
    <a class="sourceLine" id="cb5-3" title="3"><span class="kw">newtype</span></a>
    <a class="sourceLine" id="cb5-4" title="4">  <span class="dt">SSocket</span> (<span class="ot">f ::</span> <span class="dt">SocketFamily</span>) (<span class="ot">p ::</span> <span class="dt">SocketProtocol</span>)</a>
    <a class="sourceLine" id="cb5-5" title="5">    (<span class="ot">s ::</span> <span class="dt">SocketStatus</span>) (<span class="ot">sh ::</span> <span class="dt">ShutdownStatus</span>)</a>
    <a class="sourceLine" id="cb5-6" title="6">  <span class="ot">=</span> <span class="dt">SSocket</span> <span class="dt">NS.Socket</span></a></code></pre></div>
    <p>It’s nothing more than a newtype wrapper around the socket library, with several phantom types. They’re so-called phantom types, because they don’t affect the in-memory or run-time representation of our data structures. They spookily vanish once the program begins to run.</p>
    <p>Now let’s look at the definitions for the each of these phantom types:</p>
    <div class="sourceCode" id="cb6"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb6-1" title="1"><span class="kw">data</span> <span class="dt">SocketFamily</span></a>
    <a class="sourceLine" id="cb6-2" title="2">  <span class="ot">=</span> <span class="dt">Unix</span></a>
    <a class="sourceLine" id="cb6-3" title="3">  <span class="op">|</span> <span class="dt">InetV4</span></a>
    <a class="sourceLine" id="cb6-4" title="4">  <span class="op">|</span> <span class="dt">InetV6</span></a>
    <a class="sourceLine" id="cb6-5" title="5"></a>
    <a class="sourceLine" id="cb6-6" title="6"><span class="kw">data</span> <span class="dt">SocketProtocol</span></a>
    <a class="sourceLine" id="cb6-7" title="7">  <span class="ot">=</span> <span class="dt">Tcp</span>  <span class="co">-- Stream</span></a>
    <a class="sourceLine" id="cb6-8" title="8">  <span class="op">|</span> <span class="dt">Udp</span>  <span class="co">-- Datagram</span></a>
    <a class="sourceLine" id="cb6-9" title="9"></a>
    <a class="sourceLine" id="cb6-10" title="10"><span class="kw">data</span> <span class="dt">SocketStatus</span></a>
    <a class="sourceLine" id="cb6-11" title="11">  <span class="ot">=</span> <span class="dt">Unconnected</span></a>
    <a class="sourceLine" id="cb6-12" title="12">  <span class="op">|</span> <span class="dt">Bound</span></a>
    <a class="sourceLine" id="cb6-13" title="13">  <span class="op">|</span> <span class="dt">Listening</span></a>
    <a class="sourceLine" id="cb6-14" title="14">  <span class="op">|</span> <span class="dt">Connected</span></a>
    <a class="sourceLine" id="cb6-15" title="15">  <span class="op">|</span> <span class="dt">Closed</span></a>
    <a class="sourceLine" id="cb6-16" title="16"></a>
    <a class="sourceLine" id="cb6-17" title="17"><span class="kw">data</span> <span class="dt">ShutdownStatus</span></a>
    <a class="sourceLine" id="cb6-18" title="18">  <span class="ot">=</span> <span class="dt">Available</span></a>
    <a class="sourceLine" id="cb6-19" title="19">  <span class="op">|</span> <span class="dt">CannotReceive</span></a>
    <a class="sourceLine" id="cb6-20" title="20">  <span class="op">|</span> <span class="dt">CannotSend</span></a>
    <a class="sourceLine" id="cb6-21" title="21">  <span class="op">|</span> <span class="dt">CannotSendOrReceive</span></a></code></pre></div>
    <p>Nothing too surprising yet. This is still pretty cozy Haskell. Now, how do we get from that data type definition to a typed socket API? There’s the challenge.</p>
    <p>Let’s consider the <code>socket</code> function. It’s original form is:</p>
    <div class="sourceCode" id="cb7"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb7-1" title="1"><span class="ot">socket ::</span> <span class="dt">Family</span> <span class="ot">-&gt;</span> <span class="dt">SocketType</span> <span class="ot">-&gt;</span> <span class="dt">ProtocolNumber</span> <span class="ot">-&gt;</span> <span class="dt">IO</span> <span class="dt">Socket</span></a></code></pre></div>
    <p>If we just map the original socket function over to the new socket type, it’d look something like:</p>
    <div class="sourceCode" id="cb8"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb8-1" title="1"><span class="ot">socket ::</span></a>
    <a class="sourceLine" id="cb8-2" title="2">  <span class="dt">Family</span> <span class="ot">-&gt;</span> <span class="dt">SocketType</span> <span class="ot">-&gt;</span> <span class="dt">ProtocolNumber</span></a>
    <a class="sourceLine" id="cb8-3" title="3">  <span class="ot">-&gt;</span> <span class="dt">IO</span> (<span class="dt">SSocket</span> f p s sh)</a></code></pre></div>
    <p>However, we know a bit more than that about the state of a newly created socket. This means we can refine our type signature a step further:</p>
    <div class="sourceCode" id="cb9"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb9-1" title="1"><span class="ot">socket ::</span></a>
    <a class="sourceLine" id="cb9-2" title="2">  <span class="dt">Family</span> <span class="ot">-&gt;</span> <span class="dt">SocketType</span> <span class="ot">-&gt;</span> <span class="dt">ProtocolNumber</span></a>
    <a class="sourceLine" id="cb9-3" title="3">  <span class="ot">-&gt;</span> <span class="dt">IO</span> (<span class="dt">SSocket</span> f p <span class="dt">&#39;Unconnected</span> <span class="dt">&#39;Available</span>)</a></code></pre></div>
    <p>Initializing a socket always gives one with a socket status of <code>Unconnected</code> and a shutdown status of <code>Available</code>. And so we initialize our socket state machine! We’re using <code>DataKinds</code> here to promote the data definitions to the type-level.</p>
    <p>So what would the implementation look like? Here it is:</p>
    <div class="sourceCode" id="cb10"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb10-1" title="1"><span class="ot">socket ::</span></a>
    <a class="sourceLine" id="cb10-2" title="2">  <span class="dt">Family</span> <span class="ot">-&gt;</span> <span class="dt">SocketType</span> <span class="ot">-&gt;</span> <span class="dt">ProtocolNumber</span></a>
    <a class="sourceLine" id="cb10-3" title="3">  <span class="ot">-&gt;</span> <span class="dt">IO</span> (<span class="dt">SSocket</span> f p <span class="dt">&#39;Unconnected</span> <span class="dt">&#39;Available</span>)</a>
    <a class="sourceLine" id="cb10-4" title="4">socket fam sockType protocolNum <span class="ot">=</span></a>
    <a class="sourceLine" id="cb10-5" title="5">  <span class="fu">return</span> (<span class="dt">SSocket</span> (socket fam sockType protocolNum))</a></code></pre></div>
    <p>That’s it!</p>
    <p>However, there’s two shortcomings here:</p>
    <ul>
    <li>we didn’t determine the family phantom type from the arguments</li>
    <li>we didn’t determine the protocol phantom from the arguments</li>
    </ul>
    <p>We should be able to do such a thing. And to do that, we turn to a technique known as singletons! In the next section, we’ll work through the machinery needed to connect types and values.</p>
    <h2 id="singletons-mean-1-to-1">Singletons Mean 1-to-1</h2>
    <p>Before we get to the details of the technique, we’ll start from the end. The final form of the <code>socket</code> function type signature looks like this:</p>
    <div class="sourceCode" id="cb11"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb11-1" title="1"><span class="ot">socket ::</span> <span class="kw">forall</span> f p<span class="op">.</span></a>
    <a class="sourceLine" id="cb11-2" title="2">  (<span class="dt">SockFam</span> f, <span class="dt">SockProto</span> p) <span class="ot">=&gt;</span> <span class="dt">IO</span> (<span class="dt">SSocket</span> f p <span class="dt">&#39;Unconnected</span> <span class="dt">&#39;Available</span>)</a></code></pre></div>
    <p>Since the last version presented, we’ve:</p>
    <ul>
    <li>removed all the function arguments</li>
    <li>added two constraints</li>
    </ul>
    <p>With singleton types, it was possible to do all this and still have a reasonable implementation. After all, if the user specifies what types they want for the family and protocol, shouldn’t we be able to produce the values that match that?</p>
    <p>That’s dependently-typed programming in Haskell.</p>
    <p>We’ll need four specific tools to connect types and values:</p>
    <ul>
    <li>GADTs: generalized algebraic data types - they help Haskell’s type inferencer solve for what we need</li>
    <li>DataKinds: lifting values types to the kind level</li>
    <li>Scoped Type Variables: being able to use types declared in the type signature as types in the implementation</li>
    <li>type classes: “generating” values from type</li>
    </ul>
    <p>Before we delve into the details, here’s the full implementation of the <code>socket</code> function in its final form:</p>
    <div class="sourceCode" id="cb12"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb12-1" title="1"><span class="ot">socket ::</span> <span class="kw">forall</span> f p<span class="op">.</span></a>
    <a class="sourceLine" id="cb12-2" title="2">  (<span class="dt">SockFam</span> f, <span class="dt">SockProto</span> p) <span class="ot">=&gt;</span> <span class="dt">IO</span> (<span class="dt">SSocket</span> f p <span class="dt">&#39;Unconnected</span> <span class="dt">&#39;Available</span>)</a>
    <a class="sourceLine" id="cb12-3" title="3">socket <span class="ot">=</span></a>
    <a class="sourceLine" id="cb12-4" title="4">  <span class="kw">let</span> x <span class="ot">=</span> socketFamily (<span class="ot">socketFamily1 ::</span> <span class="dt">SocketFamily1</span> f)</a>
    <a class="sourceLine" id="cb12-5" title="5">      y <span class="ot">=</span> socketProtocol (<span class="ot">sockProto1 ::</span> <span class="dt">SocketProtocol1</span> p)</a>
    <a class="sourceLine" id="cb12-6" title="6">  <span class="kw">in</span> <span class="dt">SSocket</span> <span class="op">&lt;$&gt;</span> (NS.socket x y NS.defaultProtocol)</a></code></pre></div>
    <p>I’ve at least six things to explain here:</p>
    <ul>
    <li><code>socketFamily</code></li>
    <li><code>socketFamily1</code></li>
    <li><code>SocketFamily1</code> (yeah, dependent types in Haskell are a party of repetition!)</li>
    <li><code>socketProtocol</code></li>
    <li><code>sockProto1</code></li>
    <li><code>SocketProtocol1</code> (we <strong>really</strong> need to convince the type-checker of things)</li>
    </ul>
    <p>Let’s start with all the socket family biz. Keep in mind, we want to go from a <strong>type</strong> <code>SocketFamily</code> given as <code>f</code> to a <strong>value</strong> <code>NS.SocketFamily</code> that the lower-level <code>socket</code> function can consume.</p>
    <p>First, a GADT mirror for the <code>SocketFamily</code> data type:</p>
    <div class="sourceCode" id="cb13"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb13-1" title="1"><span class="kw">data</span> <span class="dt">SocketFamily1</span> (<span class="ot">f ::</span> <span class="dt">SocketFamily</span>) <span class="kw">where</span></a>
    <a class="sourceLine" id="cb13-2" title="2">  <span class="dt">SUnix</span><span class="ot"> ::</span> <span class="dt">SocketFamily1</span> <span class="dt">&#39;Unix</span></a>
    <a class="sourceLine" id="cb13-3" title="3">  <span class="dt">SInetV4</span><span class="ot"> ::</span> <span class="dt">SocketFamily1</span> <span class="dt">&#39;InetV4</span></a>
    <a class="sourceLine" id="cb13-4" title="4">  <span class="dt">SInetV6</span><span class="ot"> ::</span> <span class="dt">SocketFamily1</span> <span class="dt">&#39;InetV6</span></a></code></pre></div>
    <p>This gives us values that connect the type-level <code>SocketFamily</code> to the value-level. That’s step one.</p>
    <p>The next step, a type class to give us a unique value from a <code>SocketFamily</code> type:</p>
    <div class="sourceCode" id="cb14"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb14-1" title="1"><span class="kw">class</span> <span class="dt">SockFam</span> (<span class="ot">f ::</span> <span class="dt">SocketFamily</span>) <span class="kw">where</span></a>
    <a class="sourceLine" id="cb14-2" title="2"><span class="ot">  socketFamily1 ::</span> <span class="dt">SocketFamily1</span> f</a>
    <a class="sourceLine" id="cb14-3" title="3"></a>
    <a class="sourceLine" id="cb14-4" title="4"><span class="kw">instance</span> <span class="dt">SockFam</span> <span class="dt">&#39;Unix</span></a>
    <a class="sourceLine" id="cb14-5" title="5">  <span class="kw">where</span> socketFamily1 <span class="ot">=</span> <span class="dt">SUnix</span></a>
    <a class="sourceLine" id="cb14-6" title="6"></a>
    <a class="sourceLine" id="cb14-7" title="7"><span class="kw">instance</span> <span class="dt">SockFam</span> <span class="dt">&#39;InetV4</span></a>
    <a class="sourceLine" id="cb14-8" title="8">  <span class="kw">where</span> socketFamily1 <span class="ot">=</span> <span class="dt">SInetV4</span></a>
    <a class="sourceLine" id="cb14-9" title="9"></a>
    <a class="sourceLine" id="cb14-10" title="10"><span class="kw">instance</span> <span class="dt">SockFam</span> <span class="dt">&#39;InetV6</span></a>
    <a class="sourceLine" id="cb14-11" title="11">  <span class="kw">where</span> socketFamily1 <span class="ot">=</span> <span class="dt">SInetV6</span></a></code></pre></div>
    <p>Between the GADT and the type-class, we’ve come most of the way. The final step, is a <em>forgetful</em> function giving us our <code>NS.SocketFamily</code>, so-called because it forgets its phantoms:</p>
    <div class="sourceCode" id="cb15"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb15-1" title="1"><span class="ot">socketFamily ::</span> <span class="dt">SocketFamily1</span> f <span class="ot">-&gt;</span> <span class="dt">NS.Family</span></a>
    <a class="sourceLine" id="cb15-2" title="2">socketFamily <span class="ot">=</span> \<span class="kw">case</span></a>
    <a class="sourceLine" id="cb15-3" title="3">  <span class="dt">SUnix</span> <span class="ot">-&gt;</span> <span class="dt">NS.AF_UNIX</span></a>
    <a class="sourceLine" id="cb15-4" title="4">  <span class="dt">SInetV4</span> <span class="ot">-&gt;</span> <span class="dt">NS.AF_INET</span></a>
    <a class="sourceLine" id="cb15-5" title="5">  <span class="dt">SInetV6</span> <span class="ot">-&gt;</span> <span class="dt">NS.AF_INET6</span></a></code></pre></div>
    <p>And that’s a singleton! The steps, in summary:</p>
    <ol type="1">
    <li>define a data type to represent your valid states, to be used as a kind</li>
    <li>define a GADT to mirror the type at the value-level, indexed by the kind</li>
    <li>define a type-class to give us one of those GADTs from a the type</li>
    <li>map from the GADT to whatever you’d like to map to</li>
    </ol>
    <p>This is the simplest version of the singleton technique. It involves a lot of repetitive code, to be sure. It’s also a topic of active research, with nifty ways of mirroring even more complex structures between the type- and value-levels.</p>
    <p>For more details and a couple of papers, check out the <a href="https://hackage.haskell.org/package/singletons">singletons</a> library, which uses template-haskell to automate a lot of this!</p>
    <p>Now, we look at the socket protocol side:</p>
    <div class="sourceCode" id="cb16"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb16-1" title="1"><span class="kw">data</span> <span class="dt">SocketProtocol1</span> (<span class="ot">p ::</span> <span class="dt">SocketProtocol</span>) <span class="kw">where</span></a>
    <a class="sourceLine" id="cb16-2" title="2">  <span class="dt">STcp</span><span class="ot"> ::</span> <span class="dt">SocketProtocol1</span> <span class="dt">&#39;Tcp</span></a>
    <a class="sourceLine" id="cb16-3" title="3">  <span class="dt">SUdp</span><span class="ot"> ::</span> <span class="dt">SocketProtocol1</span> <span class="dt">&#39;Udp</span></a>
    <a class="sourceLine" id="cb16-4" title="4"></a>
    <a class="sourceLine" id="cb16-5" title="5"><span class="kw">class</span> <span class="dt">SockProto</span> (<span class="ot">p ::</span> <span class="dt">SocketProtocol</span>) <span class="kw">where</span></a>
    <a class="sourceLine" id="cb16-6" title="6"><span class="ot">  sockProto1 ::</span> <span class="dt">SocketProtocol1</span> p</a>
    <a class="sourceLine" id="cb16-7" title="7"></a>
    <a class="sourceLine" id="cb16-8" title="8"><span class="kw">instance</span> <span class="dt">SockProto</span> <span class="dt">&#39;Tcp</span></a>
    <a class="sourceLine" id="cb16-9" title="9">  <span class="kw">where</span> sockProto1 <span class="ot">=</span> <span class="dt">STcp</span></a>
    <a class="sourceLine" id="cb16-10" title="10"></a>
    <a class="sourceLine" id="cb16-11" title="11"><span class="kw">instance</span> <span class="dt">SockProto</span> <span class="dt">&#39;Udp</span></a>
    <a class="sourceLine" id="cb16-12" title="12">  <span class="kw">where</span> sockProto1 <span class="ot">=</span> <span class="dt">SUdp</span></a>
    <a class="sourceLine" id="cb16-13" title="13"></a>
    <a class="sourceLine" id="cb16-14" title="14"><span class="ot">socketProtocol ::</span> <span class="dt">SocketProtocol1</span> p <span class="ot">-&gt;</span> <span class="dt">NS.SocketType</span></a>
    <a class="sourceLine" id="cb16-15" title="15">socketProtocol <span class="ot">=</span> \<span class="kw">case</span></a>
    <a class="sourceLine" id="cb16-16" title="16">  <span class="dt">STcp</span> <span class="ot">-&gt;</span> <span class="dt">NS.Stream</span></a>
    <a class="sourceLine" id="cb16-17" title="17">  <span class="dt">SUdp</span> <span class="ot">-&gt;</span> <span class="dt">NS.Datagram</span></a></code></pre></div>
    <p>It’s a nearly identical dance.</p>
    <p>With those two pieces, we’ve fully implemented <code>socket</code>:</p>
    <div class="sourceCode" id="cb17"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb17-1" title="1"><span class="ot">socket ::</span> <span class="kw">forall</span> f p<span class="op">.</span></a>
    <a class="sourceLine" id="cb17-2" title="2">  (<span class="dt">SockFam</span> f, <span class="dt">SockProto</span> p) <span class="ot">=&gt;</span> <span class="dt">IO</span> (<span class="dt">SSocket</span> f p <span class="dt">&#39;Unconnected</span> <span class="dt">&#39;Available</span>)</a>
    <a class="sourceLine" id="cb17-3" title="3">socket <span class="ot">=</span></a>
    <a class="sourceLine" id="cb17-4" title="4">  <span class="kw">let</span> x <span class="ot">=</span> socketFamily (<span class="ot">socketFamily1 ::</span> <span class="dt">SocketFamily1</span> f)</a>
    <a class="sourceLine" id="cb17-5" title="5">      y <span class="ot">=</span> socketProtocol (<span class="ot">sockProto1 ::</span> <span class="dt">SocketProtocol1</span> p)</a>
    <a class="sourceLine" id="cb17-6" title="6">  <span class="kw">in</span> <span class="dt">SSocket</span> <span class="op">&lt;$&gt;</span> (NS.socket x y NS.defaultProtocol)</a></code></pre></div>
    <p>That was An Effort.</p>
    <p>In the next section, we’ll look at the rest of the typed socket API.</p>
    <h2 id="coloring-in-the-state-machine">Coloring in the State Machine</h2>
    <p>So far, we have a nifty socket data type and a means to initialize our socket. However, we still want to like, be able to do a lot more with those sockets. After all, the goal is to have a usable sockets library!</p>
    <p>Here’s an overview of the remainder of the API that affects socket state. We’ll use this to detail the last few techniques:</p>
    <div class="sourceCode" id="cb18"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb18-1" title="1"><span class="ot">connect ::</span></a>
    <a class="sourceLine" id="cb18-2" title="2">  <span class="dt">SockAddr</span> f <span class="ot">-&gt;</span> <span class="dt">SSocket</span> f <span class="dt">&#39;Tcp</span> <span class="dt">&#39;Unconnected</span> sh</a>
    <a class="sourceLine" id="cb18-3" title="3">  <span class="ot">-&gt;</span> <span class="dt">IO</span> (<span class="dt">SSocket</span> f <span class="dt">&#39;Tcp</span> <span class="dt">&#39;Connected</span> <span class="dt">&#39;Available</span>)</a>
    <a class="sourceLine" id="cb18-4" title="4"></a>
    <a class="sourceLine" id="cb18-5" title="5"><span class="ot">bind ::</span> <span class="dt">SockAddr</span> f <span class="ot">-&gt;</span> <span class="dt">SSocket</span> f p <span class="dt">&#39;Unconnected</span> sh <span class="ot">-&gt;</span> <span class="dt">IO</span> (<span class="dt">SSocket</span> f p <span class="dt">&#39;Bound</span> sh)</a>
    <a class="sourceLine" id="cb18-6" title="6"></a>
    <a class="sourceLine" id="cb18-7" title="7"><span class="ot">listen ::</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">SSocket</span> f <span class="dt">&#39;Tcp</span> <span class="dt">&#39;Bound</span> sh <span class="ot">-&gt;</span> <span class="dt">IO</span> (<span class="dt">SSocket</span> f <span class="dt">&#39;Tcp</span> <span class="dt">&#39;Listening</span> sh)</a>
    <a class="sourceLine" id="cb18-8" title="8"></a>
    <a class="sourceLine" id="cb18-9" title="9"><span class="ot">accept ::</span> <span class="dt">SSocket</span> f <span class="dt">&#39;Tcp</span> <span class="dt">&#39;Listening</span> sh <span class="ot">-&gt;</span> <span class="dt">IO</span> (<span class="dt">SSocket</span> f <span class="dt">&#39;Tcp</span> <span class="dt">&#39;Connected</span> sh, <span class="dt">NS.SockAddr</span>)</a>
    <a class="sourceLine" id="cb18-10" title="10"></a>
    <a class="sourceLine" id="cb18-11" title="11"><span class="ot">close ::</span> <span class="dt">Closeable</span> s <span class="op">~</span> <span class="dt">&#39;True</span> <span class="ot">=&gt;</span></a>
    <a class="sourceLine" id="cb18-12" title="12">  <span class="dt">SSocket</span> f p s sh <span class="ot">-&gt;</span> <span class="dt">IO</span> (<span class="dt">SSocket</span> f p <span class="dt">&#39;Closed</span> sh)</a>
    <a class="sourceLine" id="cb18-13" title="13"></a>
    <a class="sourceLine" id="cb18-14" title="14"><span class="ot">shutdownReceive ::</span></a>
    <a class="sourceLine" id="cb18-15" title="15">  <span class="dt">CanShutdownReceive</span> sh s <span class="op">~</span> <span class="dt">&#39;True</span> <span class="ot">=&gt;</span></a>
    <a class="sourceLine" id="cb18-16" title="16">  <span class="dt">SSocket</span> f p s sh</a>
    <a class="sourceLine" id="cb18-17" title="17">  <span class="ot">-&gt;</span> <span class="dt">IO</span> (<span class="dt">SSocket</span> f p s (<span class="dt">Shutdown</span> sh <span class="dt">&#39;NS.ShutdownReceive</span>))</a>
    <a class="sourceLine" id="cb18-18" title="18"></a>
    <a class="sourceLine" id="cb18-19" title="19"><span class="ot">shutdownSend ::</span></a>
    <a class="sourceLine" id="cb18-20" title="20">  <span class="dt">CanShutdownSend</span> sh s <span class="op">~</span> <span class="dt">&#39;True</span> <span class="ot">=&gt;</span></a>
    <a class="sourceLine" id="cb18-21" title="21">  <span class="dt">SSocket</span> f p s sh</a>
    <a class="sourceLine" id="cb18-22" title="22">  <span class="ot">-&gt;</span> <span class="dt">IO</span> (<span class="dt">SSocket</span> f p s (<span class="dt">Shutdown</span> sh <span class="dt">&#39;NS.ShutdownSend</span>))</a>
    <a class="sourceLine" id="cb18-23" title="23"></a>
    <a class="sourceLine" id="cb18-24" title="24"><span class="ot">shutdownBoth ::</span></a>
    <a class="sourceLine" id="cb18-25" title="25">  <span class="dt">CanShutdownBoth</span> sh s <span class="op">~</span> <span class="dt">&#39;True</span> <span class="ot">=&gt;</span></a>
    <a class="sourceLine" id="cb18-26" title="26">  <span class="dt">SSocket</span> f p s sh</a>
    <a class="sourceLine" id="cb18-27" title="27">  <span class="ot">-&gt;</span> <span class="dt">IO</span> (<span class="dt">SSocket</span> f p s (<span class="dt">Shutdown</span> sh <span class="dt">&#39;NS.ShutdownBoth</span>))</a></code></pre></div>
    <p>The simplest operations here are <code>connect</code>, <code>listen</code>, <code>bind</code>, and <code>accept</code>. The implementations of these are strictly pass-throughs to the lower-level API, and all we do is update the types to reflect the changes. There’s still a bit of power here. For example, looking at <code>bind</code>, we require that a socket be in an <code>Unconnected</code> state before this’ll compile. This is where the safety of the API comes from - we place strong requirements on the data we introduce before we allow a change to be made.</p>
    <p>Let’s look at <code>close</code>:</p>
    <div class="sourceCode" id="cb19"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb19-1" title="1"><span class="ot">close ::</span> <span class="dt">Closeable</span> s <span class="op">~</span> <span class="dt">&#39;True</span> <span class="ot">=&gt;</span></a>
    <a class="sourceLine" id="cb19-2" title="2">  <span class="dt">SSocket</span> f p s sh <span class="ot">-&gt;</span> <span class="dt">IO</span> (<span class="dt">SSocket</span> f p <span class="dt">&#39;Closed</span> sh)</a></code></pre></div>
    <p>There’s two new things at work here:</p>
    <ol type="1">
    <li><code>Closeable</code>, a type family (a type function)</li>
    <li>an adorable squiggle that means - these two must be equal</li>
    </ol>
    <p><code>Closeable</code> itself is comfortable to write:</p>
    <div class="sourceCode" id="cb20"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb20-1" title="1"><span class="kw">type</span> <span class="kw">family</span> <span class="dt">Closeable</span> (<span class="ot">s ::</span> <span class="dt">SocketStatus</span>)<span class="ot"> ::</span> <span class="dt">Bool</span> <span class="kw">where</span></a>
    <a class="sourceLine" id="cb20-2" title="2">  <span class="dt">Closeable</span> <span class="dt">&#39;Unconnected</span> <span class="ot">=</span> <span class="dt">&#39;True</span></a>
    <a class="sourceLine" id="cb20-3" title="3">  <span class="dt">Closeable</span> <span class="dt">&#39;Bound</span> <span class="ot">=</span> <span class="dt">&#39;True</span></a>
    <a class="sourceLine" id="cb20-4" title="4">  <span class="dt">Closeable</span> <span class="dt">&#39;Listening</span> <span class="ot">=</span> <span class="dt">&#39;True</span></a>
    <a class="sourceLine" id="cb20-5" title="5">  <span class="dt">Closeable</span> <span class="dt">&#39;Connected</span> <span class="ot">=</span> <span class="dt">&#39;True</span></a>
    <a class="sourceLine" id="cb20-6" title="6">  <span class="dt">Closeable</span> <span class="dt">&#39;Closed</span> <span class="ot">=</span> <span class="dt">&#39;False</span></a></code></pre></div>
    <p>For every status a socket could be in, we indicate whether it makes sense to close it. Technically, there’s no harm in closing a socket that’s already been closed. But, we can prevent even that bit of waste by marking that as an invalid state.</p>
    <p>If we try to close a socket that’s already been closed, we’ll get this sort of error:</p>
    <div class="sourceCode" id="cb21"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb21-1" title="1"><span class="op">&gt;</span> tcp4Socket <span class="op">&gt;&gt;=</span> close <span class="op">&gt;&gt;=</span> close</a>
    <a class="sourceLine" id="cb21-2" title="2"></a>
    <a class="sourceLine" id="cb21-3" title="3"><span class="op">&lt;</span>interactive<span class="op">&gt;:</span><span class="dv">2</span><span class="op">:</span><span class="dv">26</span><span class="op">:</span> <span class="fu">error</span><span class="op">:</span></a>
    <a class="sourceLine" id="cb21-4" title="4">    • <span class="dt">Couldn&#39;t</span> match <span class="kw">type</span> ‘<span class="dt">&#39;False</span>’ with ‘<span class="dt">&#39;True</span>’</a>
    <a class="sourceLine" id="cb21-5" title="5">        arising from a use <span class="kw">of</span> ‘close’</a>
    <a class="sourceLine" id="cb21-6" title="6">    • <span class="dt">In</span> the second argument <span class="kw">of</span> ‘(<span class="op">&gt;&gt;=</span>)’, namely ‘close’</a>
    <a class="sourceLine" id="cb21-7" title="7">      <span class="dt">In</span> the expression<span class="op">:</span> tcp4Socket <span class="op">&gt;&gt;=</span> close <span class="op">&gt;&gt;=</span> close</a>
    <a class="sourceLine" id="cb21-8" title="8">      <span class="dt">In</span> an equation for ‘it’<span class="op">:</span> it <span class="ot">=</span> tcp4Socket <span class="op">&gt;&gt;=</span> close <span class="op">&gt;&gt;=</span> close</a></code></pre></div>
    <p>It is not <strong>at all</strong> a particularly helpful type error. As of GHC 8.0, there’s a means to make type errors more helpful, but that technique is not covered in this post. For now, it’ll suffice to say - the error is prevented and highlighted.</p>
    <p>To close up our discussion on <code>close</code>, here’s the full implementation (which mirrors <code>bind</code> and friends):</p>
    <div class="sourceCode" id="cb22"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb22-1" title="1"><span class="ot">close ::</span> <span class="dt">Closeable</span> s <span class="op">~</span> <span class="dt">&#39;True</span> <span class="ot">=&gt;</span></a>
    <a class="sourceLine" id="cb22-2" title="2">  <span class="dt">SSocket</span> f p s sh <span class="ot">-&gt;</span> <span class="dt">IO</span> (<span class="dt">SSocket</span> f p <span class="dt">&#39;Closed</span> sh)</a>
    <a class="sourceLine" id="cb22-3" title="3">close (<span class="dt">SSocket</span> s) <span class="ot">=</span> NS.close s <span class="op">&gt;&gt;</span> <span class="fu">return</span> (<span class="dt">SSocket</span> s)</a></code></pre></div>
    <p>Now, let’s talk about the shutdown commands:</p>
    <div class="sourceCode" id="cb23"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb23-1" title="1"><span class="ot">shutdownReceive ::</span></a>
    <a class="sourceLine" id="cb23-2" title="2">  <span class="dt">CanShutdownReceive</span> sh s <span class="op">~</span> <span class="dt">&#39;True</span> <span class="ot">=&gt;</span></a>
    <a class="sourceLine" id="cb23-3" title="3">  <span class="dt">SSocket</span> f p s sh</a>
    <a class="sourceLine" id="cb23-4" title="4">  <span class="ot">-&gt;</span> <span class="dt">IO</span> (<span class="dt">SSocket</span> f p s (<span class="dt">Shutdown</span> sh <span class="dt">&#39;NS.ShutdownReceive</span>))</a>
    <a class="sourceLine" id="cb23-5" title="5"></a>
    <a class="sourceLine" id="cb23-6" title="6"><span class="ot">shutdownSend ::</span></a>
    <a class="sourceLine" id="cb23-7" title="7">  <span class="dt">CanShutdownSend</span> sh s <span class="op">~</span> <span class="dt">&#39;True</span> <span class="ot">=&gt;</span></a>
    <a class="sourceLine" id="cb23-8" title="8">  <span class="dt">SSocket</span> f p s sh</a>
    <a class="sourceLine" id="cb23-9" title="9">  <span class="ot">-&gt;</span> <span class="dt">IO</span> (<span class="dt">SSocket</span> f p s (<span class="dt">Shutdown</span> sh <span class="dt">&#39;NS.ShutdownSend</span>))</a>
    <a class="sourceLine" id="cb23-10" title="10"></a>
    <a class="sourceLine" id="cb23-11" title="11"><span class="ot">shutdownBoth ::</span></a>
    <a class="sourceLine" id="cb23-12" title="12">  <span class="dt">CanShutdownBoth</span> sh s <span class="op">~</span> <span class="dt">&#39;True</span> <span class="ot">=&gt;</span></a>
    <a class="sourceLine" id="cb23-13" title="13">  <span class="dt">SSocket</span> f p s sh</a>
    <a class="sourceLine" id="cb23-14" title="14">  <span class="ot">-&gt;</span> <span class="dt">IO</span> (<span class="dt">SSocket</span> f p s (<span class="dt">Shutdown</span> sh <span class="dt">&#39;NS.ShutdownBoth</span>))</a></code></pre></div>
    <p>Four new type functions are introduced:</p>
    <ul>
    <li><code>CanShutdownReceive</code></li>
    <li><code>CanShutdownSend</code></li>
    <li><code>CanShutdownBoth</code></li>
    <li><code>Shutdown</code></li>
    </ul>
    <p>The techniques are very similar to what we did for <code>close</code>. Below are the relevant implementations for the first three type functions:</p>
    <div class="sourceCode" id="cb24"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb24-1" title="1"><span class="kw">type</span> <span class="kw">family</span> <span class="dt">CanShutdownReceive</span> (<span class="ot">sh ::</span> <span class="dt">ShutdownStatus</span>) (<span class="ot">s ::</span> <span class="dt">SocketStatus</span>)<span class="ot"> ::</span> <span class="dt">Bool</span> <span class="kw">where</span></a>
    <a class="sourceLine" id="cb24-2" title="2">  <span class="dt">CanShutdownReceive</span> <span class="dt">&#39;Available</span> <span class="dt">&#39;Connected</span> <span class="ot">=</span> <span class="dt">&#39;True</span></a>
    <a class="sourceLine" id="cb24-3" title="3">  <span class="dt">CanShutdownReceive</span> <span class="dt">&#39;CannotSend</span> <span class="dt">&#39;Connected</span> <span class="ot">=</span> <span class="dt">&#39;True</span></a>
    <a class="sourceLine" id="cb24-4" title="4">  <span class="dt">CanShutdownReceive</span> <span class="dt">&#39;Available</span> <span class="dt">&#39;Listening</span> <span class="ot">=</span> <span class="dt">&#39;True</span></a>
    <a class="sourceLine" id="cb24-5" title="5">  <span class="dt">CanShutdownReceive</span> <span class="dt">&#39;CannotSend</span> <span class="dt">&#39;Listening</span> <span class="ot">=</span> <span class="dt">&#39;True</span></a>
    <a class="sourceLine" id="cb24-6" title="6"></a>
    <a class="sourceLine" id="cb24-7" title="7"><span class="kw">type</span> <span class="kw">family</span> <span class="dt">CanShutdownSend</span> (<span class="ot">sh ::</span> <span class="dt">ShutdownStatus</span>) (<span class="ot">s ::</span> <span class="dt">SocketStatus</span>)<span class="ot">  ::</span> <span class="dt">Bool</span> <span class="kw">where</span></a>
    <a class="sourceLine" id="cb24-8" title="8">  <span class="dt">CanShutdownSend</span> <span class="dt">&#39;Available</span> <span class="dt">&#39;Connected</span> <span class="ot">=</span> <span class="dt">&#39;True</span></a>
    <a class="sourceLine" id="cb24-9" title="9">  <span class="dt">CanShutdownSend</span> <span class="dt">&#39;CannotSend</span> <span class="dt">&#39;Connected</span> <span class="ot">=</span> <span class="dt">&#39;True</span></a>
    <a class="sourceLine" id="cb24-10" title="10">  <span class="dt">CanShutdownSend</span> <span class="dt">&#39;Available</span> <span class="dt">&#39;Listening</span> <span class="ot">=</span> <span class="dt">&#39;True</span></a>
    <a class="sourceLine" id="cb24-11" title="11">  <span class="dt">CanShutdownSend</span> <span class="dt">&#39;CannotSend</span> <span class="dt">&#39;Listening</span> <span class="ot">=</span> <span class="dt">&#39;True</span></a>
    <a class="sourceLine" id="cb24-12" title="12"></a>
    <a class="sourceLine" id="cb24-13" title="13"><span class="kw">type</span> <span class="kw">family</span> <span class="dt">CanShutdownBoth</span> (<span class="ot">sh ::</span> <span class="dt">ShutdownStatus</span>) (<span class="ot">s ::</span> <span class="dt">SocketStatus</span>)<span class="ot">  ::</span> <span class="dt">Bool</span> <span class="kw">where</span></a>
    <a class="sourceLine" id="cb24-14" title="14">  <span class="dt">CanShutdownBoth</span> <span class="dt">&#39;Available</span> <span class="dt">&#39;Connected</span> <span class="ot">=</span> <span class="dt">&#39;True</span></a>
    <a class="sourceLine" id="cb24-15" title="15">  <span class="dt">CanShutdownBoth</span> <span class="dt">&#39;Available</span> <span class="dt">&#39;Listening</span> <span class="ot">=</span> <span class="dt">&#39;True</span></a></code></pre></div>
    <p>It turns out that trying to shut down sockets that aren’t in at least a connected or listening state will cause an exception. We’ve prevented that now.</p>
    <p>The last function is the one that performs the actual state transition:</p>
    <div class="sourceCode" id="cb25"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb25-1" title="1"><span class="kw">type</span> <span class="kw">family</span> <span class="dt">Shutdown</span> (<span class="ot">sh ::</span> <span class="dt">ShutdownStatus</span>) (<span class="ot">cmd ::</span> <span class="dt">NS.ShutdownCmd</span>)</a>
    <a class="sourceLine" id="cb25-2" title="2"><span class="ot">  ::</span> <span class="dt">ShutdownStatus</span> <span class="kw">where</span></a>
    <a class="sourceLine" id="cb25-3" title="3">  <span class="dt">Shutdown</span> <span class="dt">&#39;Available</span> <span class="dt">&#39;NS.ShutdownSend</span> <span class="ot">=</span> <span class="dt">&#39;CannotSend</span></a>
    <a class="sourceLine" id="cb25-4" title="4">  <span class="dt">Shutdown</span> <span class="dt">&#39;Available</span> <span class="dt">&#39;NS.ShutdownReceive</span> <span class="ot">=</span> <span class="dt">&#39;CannotReceive</span></a>
    <a class="sourceLine" id="cb25-5" title="5">  <span class="dt">Shutdown</span> <span class="dt">&#39;Available</span> <span class="dt">&#39;NS.ShutdownBoth</span> <span class="ot">=</span> <span class="dt">&#39;CannotSendOrReceive</span></a>
    <a class="sourceLine" id="cb25-6" title="6">  <span class="dt">Shutdown</span> <span class="dt">&#39;CannotSend</span> <span class="dt">&#39;NS.ShutdownReceive</span> <span class="ot">=</span> <span class="dt">&#39;CannotSendOrReceive</span></a>
    <a class="sourceLine" id="cb25-7" title="7">  <span class="dt">Shutdown</span> <span class="dt">&#39;CannotReceive</span> <span class="dt">&#39;NS.ShutdownSend</span> <span class="ot">=</span> <span class="dt">&#39;CannotSendOrReceive</span></a></code></pre></div>
    <p>Unapologetically, we leverage the types of the shutdown commands from the underlying network library to complete this type function, and also take the time to prevent all inefficient or invalid states.</p>
    <p>That’s the core of things! Next up, we’ll see how this affects the design of sending and receiving data over sockets.</p>
    <h2 id="sending-and-receiving-but-only-when-it-makes-sense">Sending and Receiving, but Only When it Makes Sense</h2>
    <p>The goal here is to only send or receive when it makes sense. In particular:</p>
    <ul>
    <li>a request to send/receive data over a disconnected TCP socket should never happen</li>
    <li>a request to sendTo/recvFrom should never happen over a connected socket</li>
    <li>we only send or receive if the relevant socket portion hasn’t been shutdown</li>
    </ul>
    <p>Here’s the API to achieve that:</p>
    <div class="sourceCode" id="cb26"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb26-1" title="1"><span class="ot">send ::</span> <span class="dt">CanSend</span> sh <span class="op">~</span> <span class="dt">&#39;True</span></a>
    <a class="sourceLine" id="cb26-2" title="2">  <span class="ot">=&gt;</span> <span class="dt">ByteString</span> <span class="ot">-&gt;</span> <span class="dt">SSocket</span> f <span class="dt">&#39;Tcp</span> <span class="dt">&#39;Connected</span> sh <span class="ot">-&gt;</span> <span class="dt">IO</span> <span class="dt">Int</span></a>
    <a class="sourceLine" id="cb26-3" title="3"></a>
    <a class="sourceLine" id="cb26-4" title="4"><span class="ot">recv ::</span> <span class="dt">CanReceive</span> sh <span class="op">~</span> <span class="dt">&#39;True</span></a>
    <a class="sourceLine" id="cb26-5" title="5">  <span class="ot">=&gt;</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">SSocket</span> f <span class="dt">&#39;Tcp</span> <span class="dt">&#39;Connected</span> sh <span class="ot">-&gt;</span> <span class="dt">IO</span> <span class="dt">ByteString</span></a>
    <a class="sourceLine" id="cb26-6" title="6"></a>
    <a class="sourceLine" id="cb26-7" title="7"><span class="ot">sendTo ::</span> <span class="dt">CanSend</span> sh <span class="op">~</span> <span class="dt">&#39;True</span></a>
    <a class="sourceLine" id="cb26-8" title="8">  <span class="ot">=&gt;</span> <span class="dt">ByteString</span> <span class="ot">-&gt;</span> <span class="dt">SockAddr</span> f <span class="ot">-&gt;</span> <span class="dt">SSocket</span> f <span class="dt">&#39;Udp</span> <span class="dt">&#39;Unconnected</span> sh <span class="ot">-&gt;</span> <span class="dt">IO</span> <span class="dt">Int</span></a>
    <a class="sourceLine" id="cb26-9" title="9"></a>
    <a class="sourceLine" id="cb26-10" title="10"><span class="ot">recvFrom ::</span> (<span class="dt">CanReceive</span> sh <span class="op">~</span> <span class="dt">&#39;True</span>, <span class="dt">SockFam</span> f)</a>
    <a class="sourceLine" id="cb26-11" title="11">  <span class="ot">=&gt;</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">SSocket</span> f <span class="dt">&#39;Udp</span> <span class="dt">&#39;Unconnected</span> sh <span class="ot">-&gt;</span> <span class="dt">IO</span> (<span class="dt">ByteString</span>, <span class="dt">SockAddr</span> f)</a></code></pre></div>
    <p><code>CanSend</code> and <code>CanReceive</code> are simple enough, and currently depend only on the socket shutdown status:</p>
    <div class="sourceCode" id="cb27"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb27-1" title="1"><span class="kw">type</span> <span class="kw">family</span> <span class="dt">CanSend</span> (<span class="ot">sh ::</span> <span class="dt">ShutdownStatus</span>)<span class="ot"> ::</span> <span class="dt">Bool</span> <span class="kw">where</span></a>
    <a class="sourceLine" id="cb27-2" title="2">  <span class="dt">CanSend</span> <span class="dt">&#39;Available</span> <span class="ot">=</span> <span class="dt">&#39;True</span></a>
    <a class="sourceLine" id="cb27-3" title="3">  <span class="dt">CanSend</span> <span class="dt">&#39;CannotReceive</span> <span class="ot">=</span> <span class="dt">&#39;True</span></a>
    <a class="sourceLine" id="cb27-4" title="4"></a>
    <a class="sourceLine" id="cb27-5" title="5"><span class="kw">type</span> <span class="kw">family</span> <span class="dt">CanReceive</span> (<span class="ot">sh ::</span> <span class="dt">ShutdownStatus</span>)<span class="ot"> ::</span> <span class="dt">Bool</span> <span class="kw">where</span></a>
    <a class="sourceLine" id="cb27-6" title="6">  <span class="dt">CanReceive</span> <span class="dt">&#39;Available</span> <span class="ot">=</span> <span class="dt">&#39;True</span></a>
    <a class="sourceLine" id="cb27-7" title="7">  <span class="dt">CanReceive</span> <span class="dt">&#39;CannotSend</span> <span class="ot">=</span> <span class="dt">&#39;True</span></a></code></pre></div>
    <p>The rest of the constraints are encoded in the type signature itself. Both the required protocol and the required socket state are specified, and only by passing in a socket with the appropriate state will a program compile.</p>
    <p>Type inference saves us all and makes this possible.</p>
    <p>As it’s given, this API is slightly over-preventative. Namely, it’s valid to use recvFrom/sendTo with a TCP socket, as long as it’s <strong>disconnected</strong>. Furthermore, it’s also fine to use send/recv with a UDP socket, also as long as it’s <strong>connected</strong>. The remaining cases throw errors. The nuanced version of this state machine gives us little gain, as it’s reasonable to expect to use TCP sockets mainly when connected and UDP sockets mainly when disconnected.</p>
    <p>I want to highlight one last thing before moving on to the next section. Take a look at <code>recvFrom</code> and <code>sendTo</code> and their <code>SockAddr</code> argument:</p>
    <div class="sourceCode" id="cb28"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb28-1" title="1"><span class="ot">sendTo ::</span> <span class="dt">CanSend</span> sh <span class="op">~</span> <span class="dt">&#39;True</span></a>
    <a class="sourceLine" id="cb28-2" title="2">  <span class="ot">=&gt;</span> <span class="dt">ByteString</span> <span class="ot">-&gt;</span> <span class="dt">SockAddr</span> f <span class="ot">-&gt;</span> <span class="dt">SSocket</span> f <span class="dt">&#39;Udp</span> <span class="dt">&#39;Unconnected</span> sh <span class="ot">-&gt;</span> <span class="dt">IO</span> <span class="dt">Int</span></a>
    <a class="sourceLine" id="cb28-3" title="3"></a>
    <a class="sourceLine" id="cb28-4" title="4"><span class="ot">recvFrom ::</span> (<span class="dt">CanReceive</span> sh <span class="op">~</span> <span class="dt">&#39;True</span>, <span class="dt">SockFam</span> f)</a>
    <a class="sourceLine" id="cb28-5" title="5">  <span class="ot">=&gt;</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">SSocket</span> f <span class="dt">&#39;Udp</span> <span class="dt">&#39;Unconnected</span> sh <span class="ot">-&gt;</span> <span class="dt">IO</span> (<span class="dt">ByteString</span>, <span class="dt">SockAddr</span> f)</a></code></pre></div>
    <p>This is a different structure than the one given by the underlying library with a touch of added safety.</p>
    <h2 id="safer-socket-address-initialization">Safer Socket Address Initialization</h2>
    <p>The underlying library uses the following socket address data structure:</p>
    <div class="sourceCode" id="cb29"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb29-1" title="1"><span class="kw">data</span> <span class="dt">SockAddr</span></a>
    <a class="sourceLine" id="cb29-2" title="2">  <span class="ot">=</span> <span class="dt">SockAddrInet</span> <span class="dt">PortNumber</span> <span class="dt">HostAddress</span></a>
    <a class="sourceLine" id="cb29-3" title="3">  <span class="op">|</span> <span class="dt">SockAddrInet6</span> <span class="dt">PortNumber</span> <span class="dt">FlowInfo</span> <span class="dt">HostAddress6</span> <span class="dt">ScopeID</span></a>
    <a class="sourceLine" id="cb29-4" title="4">  <span class="op">|</span> <span class="dt">SockAddrUnix</span> <span class="dt">String</span></a>
    <a class="sourceLine" id="cb29-5" title="5">  <span class="op">...</span></a></code></pre></div>
    <p>The problem here, is that there’s nothing preventing us from using a <code>SockAddrInet</code> with say, a Unix socket. Which, would throw an error. Or slightly more likely, an Inet6 structure with an Inet4 socket.</p>
    <p>To address this, the fix is simple enough - we use a GADT version!</p>
    <div class="sourceCode" id="cb30"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb30-1" title="1"><span class="kw">data</span> <span class="dt">SockAddr</span> (<span class="ot">f ::</span> <span class="dt">SocketFamily</span>) <span class="kw">where</span></a>
    <a class="sourceLine" id="cb30-2" title="2">  <span class="dt">SockAddrInet</span><span class="ot"> ::</span> <span class="dt">NS.PortNumber</span> <span class="ot">-&gt;</span> <span class="dt">NS.HostAddress</span></a>
    <a class="sourceLine" id="cb30-3" title="3">    <span class="ot">-&gt;</span> <span class="dt">SockAddr</span> <span class="dt">&#39;InetV4</span></a>
    <a class="sourceLine" id="cb30-4" title="4"></a>
    <a class="sourceLine" id="cb30-5" title="5">  <span class="dt">SockAddrInet6</span><span class="ot"> ::</span> <span class="dt">NS.PortNumber</span> <span class="ot">-&gt;</span> <span class="dt">NS.FlowInfo</span> <span class="ot">-&gt;</span> <span class="dt">NS.HostAddress6</span> <span class="ot">-&gt;</span> <span class="op">!</span><span class="dt">NS.ScopeID</span></a>
    <a class="sourceLine" id="cb30-6" title="6">    <span class="ot">-&gt;</span> <span class="dt">SockAddr</span> <span class="dt">&#39;InetV6</span></a>
    <a class="sourceLine" id="cb30-7" title="7"></a>
    <a class="sourceLine" id="cb30-8" title="8">  <span class="dt">SockAddrUnix</span><span class="ot"> ::</span> <span class="op">!</span>[<span class="dt">Char</span>]</a>
    <a class="sourceLine" id="cb30-9" title="9">    <span class="ot">-&gt;</span> <span class="dt">SockAddr</span> <span class="dt">&#39;Unix</span></a></code></pre></div>
    <p>The most important bit here is the SocketFamily phantom type and how it affects the return type at each branch of this GADT. We reuse the SocketFamily type from earlier so we can relate socket instances to socket address structures.</p>
    <p>Now if we look at a function like <code>bind</code>:</p>
    <div class="sourceCode" id="cb31"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb31-1" title="1"><span class="ot">bind ::</span> <span class="dt">SockAddr</span> f <span class="ot">-&gt;</span> <span class="dt">SSocket</span> f p <span class="dt">&#39;Unconnected</span> sh <span class="ot">-&gt;</span> <span class="dt">IO</span> (<span class="dt">SSocket</span> f p <span class="dt">&#39;Bound</span> sh)</a></code></pre></div>
    <p>It becomes impossible to pass in a socket address structure that isn’t of the same socket family as the socket being used. With that, our goal is achieved… mostly!</p>
    <p>There’s actually a hack I’d like to talk about here. Let’s look at <code>recvFrom</code>, including its implementation:</p>
    <div class="sourceCode" id="cb32"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb32-1" title="1"><span class="ot">recvFrom ::</span> (<span class="dt">CanReceive</span> sh <span class="op">~</span> <span class="dt">&#39;True</span>, <span class="dt">SockFam</span> f)</a>
    <a class="sourceLine" id="cb32-2" title="2">  <span class="ot">=&gt;</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">SSocket</span> f <span class="dt">&#39;Udp</span> <span class="dt">&#39;Unconnected</span> sh <span class="ot">-&gt;</span> <span class="dt">IO</span> (<span class="dt">ByteString</span>, <span class="dt">SockAddr</span> f)</a>
    <a class="sourceLine" id="cb32-3" title="3">recvFrom n (<span class="dt">SSocket</span> s) <span class="ot">=</span> <span class="kw">do</span></a>
    <a class="sourceLine" id="cb32-4" title="4">  (bs, sa) <span class="ot">&lt;-</span> NSB.recvFrom s n</a>
    <a class="sourceLine" id="cb32-5" title="5">  <span class="fu">return</span> (bs, fromNetworkSockAddr sa)</a></code></pre></div>
    <p>In every function except this one, the type parameter <code>f</code> occurs in the argument types. This made our lives easy - we could depend on the user to pass it what we needed without needing to communicate between the type and value levels.</p>
    <p>However, here, <code>recvFrom</code> returns a <code>SockAddr f</code>. Worse yet, the underlying <code>NSB.recvFrom</code> returns the original <code>NS.SockAddr</code> structure, with no type information!</p>
    <p>So we have to relate the two somehow, and convince the type-checker that it’s okay. This wasn’t easy, and I ended up with the following “proof”:</p>
    <div class="sourceCode" id="cb33"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb33-1" title="1"><span class="ot">fromNetworkSockAddr ::</span> <span class="kw">forall</span> f<span class="op">.</span> <span class="dt">SockFam</span> f <span class="ot">=&gt;</span> <span class="dt">NS.SockAddr</span> <span class="ot">-&gt;</span> <span class="dt">SockAddr</span> f</a>
    <a class="sourceLine" id="cb33-2" title="2">fromNetworkSockAddr netAddr <span class="ot">=</span> <span class="kw">case</span> (netAddr,<span class="ot"> socketFamily1 ::</span> <span class="dt">SocketFamily1</span> f) <span class="kw">of</span></a>
    <a class="sourceLine" id="cb33-3" title="3">  (<span class="dt">NS.SockAddrInet</span> p addr, <span class="dt">SInetV4</span>) <span class="ot">-&gt;</span> <span class="dt">SockAddrInet</span> p addr</a>
    <a class="sourceLine" id="cb33-4" title="4">  (<span class="dt">NS.SockAddrInet6</span> p f addr scope, <span class="dt">SInetV6</span>) <span class="ot">-&gt;</span> <span class="dt">SockAddrInet6</span> p f addr scope</a>
    <a class="sourceLine" id="cb33-5" title="5">  (<span class="dt">NS.SockAddrUnix</span> s, <span class="dt">SUnix</span>) <span class="ot">-&gt;</span> <span class="dt">SockAddrUnix</span> s</a>
    <a class="sourceLine" id="cb33-6" title="6">  _ <span class="ot">-&gt;</span> <span class="fu">error</span> <span class="st">&quot;impossible&quot;</span></a></code></pre></div>
    <p>I’ll explain. When doing a pattern match, Haskell’s type-checker is able to refine what it knows. From the left side of the pattern match arrow (including any type-level data), it’s able to learn a lot about what should be on the right. So, that’s the why of needing to generate a <code>SocketFamily1</code> object which carries the type-level data to associate the two - it convinces the type checker this all makes sense and we’re not just pulling a <code>SockAddrInet6 'Inet6</code> out of thin air.</p>
    <p>However, we aren’t able to convince the type-checker these are <strong>all</strong> the valid cases. It insists that we haven’t covered, say, <code>NS.SockAddrUnix, InetV4</code>, which we know make no sense, and the type-checker would even reject!</p>
    <p>So that’s that. A lil hack.</p>
    <p>With that out of the way, let’s talk about making this API nicer to use.</p>
    <h2 id="making-it-nicer-to-use">Making it Nicer to Use</h2>
    <p>Let’s take a look at the <code>socket</code> function:</p>
    <div class="sourceCode" id="cb34"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb34-1" title="1"><span class="ot">socket ::</span> <span class="kw">forall</span> f p<span class="op">.</span></a>
    <a class="sourceLine" id="cb34-2" title="2">  (<span class="dt">SockFam</span> f, <span class="dt">SockProto</span> p) <span class="ot">=&gt;</span> <span class="dt">IO</span> (<span class="dt">SSocket</span> f p <span class="dt">&#39;Unconnected</span> <span class="dt">&#39;Available</span>)</a></code></pre></div>
    <p>We’d use it look this to create a TCP4 client:</p>
    <div class="sourceCode" id="cb35"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb35-1" title="1">(<span class="ot">socket ::</span> <span class="dt">IO</span> (<span class="dt">SSocket</span> <span class="dt">&#39;InetV4</span> <span class="dt">&#39;Tcp</span> <span class="dt">&#39;Unconnected</span> <span class="dt">&#39;Available</span>))</a>
    <a class="sourceLine" id="cb35-2" title="2">  <span class="op">&gt;&gt;=</span> connect serverAddress</a>
    <a class="sourceLine" id="cb35-3" title="3">  <span class="op">&gt;&gt;=</span> <span class="op">...</span></a></code></pre></div>
    <p>Which is … actually really tedious. As a user, we have to remember to type all that out in the correct order, or get scolded by the compiler. It’s really cool that we don’t have to pass in any arguments to get the sort of socket we want, but it came at quite the cost in usability.</p>
    <p>We can do better - a lot better. I introduce several helper functions below:</p>
    <div class="sourceCode" id="cb36"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb36-1" title="1"><span class="ot">tcp4Socket ::</span> <span class="dt">IO</span> (<span class="dt">SSocket</span> <span class="dt">&#39;InetV4</span> <span class="dt">&#39;Tcp</span> <span class="dt">&#39;Unconnected</span> <span class="dt">&#39;Available</span>)</a>
    <a class="sourceLine" id="cb36-2" title="2">tcp4Socket <span class="ot">=</span> socket</a>
    <a class="sourceLine" id="cb36-3" title="3"></a>
    <a class="sourceLine" id="cb36-4" title="4"><span class="ot">tcp6Socket ::</span> <span class="dt">IO</span> (<span class="dt">SSocket</span> <span class="dt">&#39;InetV6</span> <span class="dt">&#39;Tcp</span> <span class="dt">&#39;Unconnected</span> <span class="dt">&#39;Available</span>)</a>
    <a class="sourceLine" id="cb36-5" title="5">tcp6Socket <span class="ot">=</span> socket</a>
    <a class="sourceLine" id="cb36-6" title="6"></a>
    <a class="sourceLine" id="cb36-7" title="7"><span class="ot">tcpUnixSocket ::</span> <span class="dt">IO</span> (<span class="dt">SSocket</span> <span class="dt">&#39;Unix</span> <span class="dt">&#39;Tcp</span> <span class="dt">&#39;Unconnected</span> <span class="dt">&#39;Available</span>)</a>
    <a class="sourceLine" id="cb36-8" title="8">tcpUnixSocket <span class="ot">=</span> socket</a>
    <a class="sourceLine" id="cb36-9" title="9"></a>
    <a class="sourceLine" id="cb36-10" title="10"><span class="ot">udp4Socket ::</span> <span class="dt">IO</span> (<span class="dt">SSocket</span> <span class="dt">&#39;InetV4</span> <span class="dt">&#39;Udp</span> <span class="dt">&#39;Unconnected</span> <span class="dt">&#39;Available</span>)</a>
    <a class="sourceLine" id="cb36-11" title="11">udp4Socket <span class="ot">=</span> socket</a>
    <a class="sourceLine" id="cb36-12" title="12"></a>
    <a class="sourceLine" id="cb36-13" title="13"><span class="ot">udp6Socket ::</span> <span class="dt">IO</span> (<span class="dt">SSocket</span> <span class="dt">&#39;InetV6</span> <span class="dt">&#39;Udp</span> <span class="dt">&#39;Unconnected</span> <span class="dt">&#39;Available</span>)</a>
    <a class="sourceLine" id="cb36-14" title="14">udp6Socket <span class="ot">=</span> socket</a>
    <a class="sourceLine" id="cb36-15" title="15"></a>
    <a class="sourceLine" id="cb36-16" title="16"><span class="ot">udpUnixSocket ::</span> <span class="dt">IO</span> (<span class="dt">SSocket</span> <span class="dt">&#39;Unix</span> <span class="dt">&#39;Udp</span> <span class="dt">&#39;Unconnected</span> <span class="dt">&#39;Available</span>)</a>
    <a class="sourceLine" id="cb36-17" title="17">udpUnixSocket <span class="ot">=</span> socket</a></code></pre></div>
    <p>Their implementations are the easiest - type-inference handles 99% of the work. We just need to be careful about the type signatures. And like magic, our client socket code now becomes much nicer:</p>
    <div class="sourceCode" id="cb37"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb37-1" title="1">tcp4Socket</a>
    <a class="sourceLine" id="cb37-2" title="2">  <span class="op">&gt;&gt;=</span> connect serverAddress</a>
    <a class="sourceLine" id="cb37-3" title="3">  <span class="op">&gt;&gt;=</span> <span class="op">...</span></a></code></pre></div>
    <p>Another usability pattern, is that we’d like functions to automatically create our sockets and also close them, even if errors come up. There’s a very powerful function hiding in Haskell’s <code>Control.Exception</code> standard prelude module: <a href="https://hackage.haskell.org/package/base-4.10.0.0/docs/Control-Exception.html#v:bracket">bracket</a>!</p>
    <div class="sourceCode" id="cb38"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb38-1" title="1"><span class="ot">bracket ::</span></a>
    <a class="sourceLine" id="cb38-2" title="2">  <span class="dt">IO</span> a           <span class="co">-- open/acquire a resource</span></a>
    <a class="sourceLine" id="cb38-3" title="3">  <span class="ot">-&gt;</span> (a <span class="ot">-&gt;</span> <span class="dt">IO</span> b) <span class="co">-- close/release the resource</span></a>
    <a class="sourceLine" id="cb38-4" title="4">  <span class="ot">-&gt;</span> (a <span class="ot">-&gt;</span> <span class="dt">IO</span> c) <span class="co">-- do some work with the resource</span></a>
    <a class="sourceLine" id="cb38-5" title="5">  <span class="dt">IO</span> c           <span class="co">-- return some value</span></a></code></pre></div>
    <p>Using <code>bracket</code>, we can write some pretty simple helpers:</p>
    <div class="sourceCode" id="cb39"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb39-1" title="1"><span class="co">-- the core bracket helper, written point-free style</span></a>
    <a class="sourceLine" id="cb39-2" title="2"><span class="ot">withSocket ::</span></a>
    <a class="sourceLine" id="cb39-3" title="3">  (<span class="dt">SockFam</span> f, <span class="dt">SockProto</span> p)</a>
    <a class="sourceLine" id="cb39-4" title="4">  <span class="ot">=&gt;</span> (<span class="dt">SSocket</span> f p <span class="dt">&#39;Unconnected</span> <span class="dt">&#39;Available</span> <span class="ot">-&gt;</span> <span class="dt">IO</span> a) <span class="ot">-&gt;</span> <span class="dt">IO</span> a</a>
    <a class="sourceLine" id="cb39-5" title="5">withSocket <span class="ot">=</span> bracket socket close</a>
    <a class="sourceLine" id="cb39-6" title="6"></a>
    <a class="sourceLine" id="cb39-7" title="7"><span class="co">-- expanding the point-free version:</span></a>
    <a class="sourceLine" id="cb39-8" title="8"><span class="co">-- withSocket f = bracket socket close f</span></a>
    <a class="sourceLine" id="cb39-9" title="9"></a>
    <a class="sourceLine" id="cb39-10" title="10"><span class="co">-- one of six helper functions, also point-free style</span></a>
    <a class="sourceLine" id="cb39-11" title="11"><span class="ot">withTcp4Socket ::</span> (<span class="dt">SSocket</span> <span class="dt">&#39;InetV4</span> <span class="dt">&#39;Tcp</span> <span class="dt">&#39;Unconnected</span> <span class="dt">&#39;Available</span> <span class="ot">-&gt;</span> <span class="dt">IO</span> a) <span class="ot">-&gt;</span> <span class="dt">IO</span> a</a>
    <a class="sourceLine" id="cb39-12" title="12">withTcp4Socket <span class="ot">=</span> withSocket</a></code></pre></div>
    <p>The way the <code>with</code> helpers work is like this:</p>
    <div class="sourceCode" id="cb40"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb40-1" title="1"><span class="co">-- serverAddress defined as earlier</span></a>
    <a class="sourceLine" id="cb40-2" title="2"><span class="ot">runRudeServer ::</span> <span class="dt">SSocket</span> <span class="dt">&#39;InetV4</span> <span class="dt">&#39;Tcp</span> <span class="dt">&#39;Unconnected</span> <span class="dt">&#39;Available</span> <span class="ot">-&gt;</span> <span class="dt">IO</span> ()</a>
    <a class="sourceLine" id="cb40-3" title="3">runRudeServer s <span class="ot">=</span> forever <span class="op">$</span> <span class="kw">do</span></a>
    <a class="sourceLine" id="cb40-4" title="4">  server <span class="ot">&lt;-</span> bind serverAddress s <span class="op">&gt;&gt;=</span> listen <span class="dv">1</span></a>
    <a class="sourceLine" id="cb40-5" title="5">  (client, _) <span class="ot">&lt;-</span> accept server</a>
    <a class="sourceLine" id="cb40-6" title="6">  _ <span class="ot">&lt;-</span> send <span class="st">&quot;bye&quot;</span> client</a>
    <a class="sourceLine" id="cb40-7" title="7">  close client</a>
    <a class="sourceLine" id="cb40-8" title="8"></a>
    <a class="sourceLine" id="cb40-9" title="9"><span class="co">-- guarantees that if the server ever crashes,</span></a>
    <a class="sourceLine" id="cb40-10" title="10"><span class="co">-- the socket will be closed</span></a>
    <a class="sourceLine" id="cb40-11" title="11">main <span class="ot">=</span> withTcp4Socket runRudeServer</a></code></pre></div>
    <p>In the next two sections, we’ll write a simple echo client and server using this library, and then test it all!</p>
    <h2 id="the-client-meows-and-the-server-echoes">The Client Meows and the Server Echoes</h2>
    <p>First, the client:</p>
    <div class="sourceCode" id="cb41"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb41-1" title="1"><span class="ot">{-# LANGUAGE OverloadedStrings #-}</span></a>
    <a class="sourceLine" id="cb41-2" title="2"><span class="ot">{-# LANGUAGE DataKinds #-}</span></a>
    <a class="sourceLine" id="cb41-3" title="3"><span class="kw">module</span> <span class="dt">Main</span> <span class="kw">where</span></a>
    <a class="sourceLine" id="cb41-4" title="4"></a>
    <a class="sourceLine" id="cb41-5" title="5"><span class="kw">import</span> <span class="dt">Network.Typed.Socket</span></a>
    <a class="sourceLine" id="cb41-6" title="6"></a>
    <a class="sourceLine" id="cb41-7" title="7"><span class="ot">meowClient ::</span> <span class="dt">SSocket</span> <span class="dt">&#39;InetV4</span> <span class="dt">&#39;Tcp</span> <span class="dt">&#39;Unconnected</span> <span class="dt">&#39;Available</span> <span class="ot">-&gt;</span> <span class="dt">IO</span> ()</a>
    <a class="sourceLine" id="cb41-8" title="8">meowClient s <span class="ot">=</span> <span class="kw">do</span></a>
    <a class="sourceLine" id="cb41-9" title="9">  client <span class="ot">&lt;-</span> connect serverAddress s</a>
    <a class="sourceLine" id="cb41-10" title="10">  send <span class="st">&quot;meow&quot;</span> client</a>
    <a class="sourceLine" id="cb41-11" title="11">  whatTheOtherCatSaid <span class="ot">&lt;-</span> recv <span class="dv">32</span> client</a>
    <a class="sourceLine" id="cb41-12" title="12">  <span class="fu">print</span> whatTheOtherCatSaid</a>
    <a class="sourceLine" id="cb41-13" title="13"></a>
    <a class="sourceLine" id="cb41-14" title="14"><span class="ot">main ::</span> <span class="dt">IO</span> ()</a>
    <a class="sourceLine" id="cb41-15" title="15">main <span class="ot">=</span> withTcp4Socket meowClient</a></code></pre></div>
    <p>Now the server:</p>
    <div class="sourceCode" id="cb42"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb42-1" title="1"><span class="ot">{-# LANGUAGE OverloadedStrings #-}</span></a>
    <a class="sourceLine" id="cb42-2" title="2"><span class="ot">{-# LANGUAGE DataKinds #-}</span></a>
    <a class="sourceLine" id="cb42-3" title="3"><span class="kw">module</span> <span class="dt">Main</span> <span class="kw">where</span></a>
    <a class="sourceLine" id="cb42-4" title="4"></a>
    <a class="sourceLine" id="cb42-5" title="5"><span class="kw">import</span> <span class="dt">Network.Typed.Socket</span></a>
    <a class="sourceLine" id="cb42-6" title="6"></a>
    <a class="sourceLine" id="cb42-7" title="7"><span class="ot">meowServer ::</span> <span class="dt">SSocket</span> <span class="dt">&#39;InetV4</span> <span class="dt">&#39;Tcp</span> <span class="dt">&#39;Unconnected</span> <span class="dt">&#39;Available</span> <span class="ot">-&gt;</span> <span class="dt">IO</span> ()</a>
    <a class="sourceLine" id="cb42-8" title="8">meowServer s <span class="ot">=</span> <span class="kw">do</span></a>
    <a class="sourceLine" id="cb42-9" title="9">  server <span class="ot">&lt;-</span> bind serverAddress s <span class="op">&gt;&gt;=</span> listen <span class="dv">1</span></a>
    <a class="sourceLine" id="cb42-10" title="10">  (client, _) <span class="ot">&lt;-</span> accept server</a>
    <a class="sourceLine" id="cb42-11" title="11">  <span class="fu">print</span> <span class="st">&quot;a cat arrives!&quot;</span></a>
    <a class="sourceLine" id="cb42-12" title="12">  whatTheNiceCatSaid <span class="ot">&lt;-</span> recv <span class="dv">32</span> client</a>
    <a class="sourceLine" id="cb42-13" title="13">  send whatTheNiceCatSaid</a>
    <a class="sourceLine" id="cb42-14" title="14"></a>
    <a class="sourceLine" id="cb42-15" title="15"><span class="ot">main ::</span> <span class="dt">IO</span> ()</a>
    <a class="sourceLine" id="cb42-16" title="16">main <span class="ot">=</span> withTcp4Socket meowServer</a></code></pre></div>
    <p>And that’s network programming in Haskell!</p>
    <h2 id="testing-it">Testing It</h2>
    <p>To go one step further, I’ll show how such a library could be tested. I used the echo server example above as a test case to make sure I didn’t break anything while doing the typed layer.</p>
    <p>I’ll make use of the nice <code>hspec</code> library.</p>
    <p>Conceptually, the test seems like it should be this simple:</p>
    <div class="sourceCode" id="cb43"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb43-1" title="1">  describe <span class="st">&quot;Network.Typed.Socket&quot;</span> <span class="op">$</span> <span class="kw">do</span></a>
    <a class="sourceLine" id="cb43-2" title="2"></a>
    <a class="sourceLine" id="cb43-3" title="3">    it <span class="st">&quot;can echo server (TCP)&quot;</span> <span class="op">$</span> <span class="kw">do</span></a>
    <a class="sourceLine" id="cb43-4" title="4">      withTcp4Socket <span class="op">$</span> \server <span class="ot">-&gt;</span> <span class="kw">do</span></a>
    <a class="sourceLine" id="cb43-5" title="5">        withTcp4Socket <span class="op">$</span> \client <span class="ot">-&gt;</span> <span class="kw">do</span></a>
    <a class="sourceLine" id="cb43-6" title="6">          runServer server</a>
    <a class="sourceLine" id="cb43-7" title="7">          runClient client</a></code></pre></div>
    <p>and as long as both finish, all’s well? Right?</p>
    <p>Herein we encounter all the loveliness and nuance of network programming. There’s immediately three things wrong with this test suite:</p>
    <ol type="1">
    <li>in <code>runServer</code>, we’ll block on <code>accept</code>. The client will never connect. The suite never ends. Eternal uncertainty!!</li>
    <li>Even if by bad magic we made it to <code>runClient</code>, we don’t actually check things are being echoed. Let’s fix that.</li>
    <li>Finally, between runs of our test suite, if any errors came up, our server would fail to rebind to that port and address because there’s a special <code>TIME_WAIT</code> mechanism where the operating system holds on to sockets after closure to ensure clean termination.</li>
    </ol>
    <p>Wow. Yeah. Welcome to network programming!</p>
    <p>I’ll skip ahead to the solutions, and just say that a bit of concurrency and setting a socket option goes most of the way:</p>
    <div class="sourceCode" id="cb44"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb44-1" title="1"><span class="ot">serverAddr ::</span> <span class="dt">SockAddr</span> <span class="dt">&#39;InetV4</span></a>
    <a class="sourceLine" id="cb44-2" title="2">serverAddr <span class="ot">=</span> <span class="dt">SockAddrInet</span> <span class="dv">2291</span> (NS.tupleToHostAddress (<span class="dv">127</span>,<span class="dv">0</span>,<span class="dv">0</span>,<span class="dv">1</span>))</a>
    <a class="sourceLine" id="cb44-3" title="3"></a>
    <a class="sourceLine" id="cb44-4" title="4"><span class="ot">runServer ::</span> <span class="dt">SSocket</span> <span class="dt">&#39;InetV4</span> <span class="dt">&#39;Tcp</span> <span class="dt">&#39;Unconnected</span> <span class="dt">&#39;Available</span> <span class="ot">-&gt;</span> <span class="dt">MVar</span> () <span class="ot">-&gt;</span> <span class="dt">IO</span> ()</a>
    <a class="sourceLine" id="cb44-5" title="5">runServer s serverWaitLock <span class="ot">=</span></a>
    <a class="sourceLine" id="cb44-6" title="6">  makeAddrReusable s</a>
    <a class="sourceLine" id="cb44-7" title="7">    <span class="op">&gt;&gt;=</span> bind serverAddr</a>
    <a class="sourceLine" id="cb44-8" title="8">    <span class="op">&gt;&gt;=</span> listen <span class="dv">1</span></a>
    <a class="sourceLine" id="cb44-9" title="9">    <span class="op">&gt;&gt;=</span> (\server <span class="ot">-&gt;</span> putMVar serverWaitLock () <span class="op">&gt;&gt;</span> serverTest server)</a>
    <a class="sourceLine" id="cb44-10" title="10"></a>
    <a class="sourceLine" id="cb44-11" title="11"><span class="ot">runClient ::</span> <span class="dt">SSocket</span> <span class="dt">&#39;InetV4</span> <span class="dt">&#39;Tcp</span> <span class="dt">&#39;Unconnected</span> <span class="dt">&#39;Available</span> <span class="ot">-&gt;</span> <span class="dt">IO</span> ()</a>
    <a class="sourceLine" id="cb44-12" title="12">runClient s <span class="ot">=</span> <span class="kw">do</span></a>
    <a class="sourceLine" id="cb44-13" title="13">  client <span class="ot">&lt;-</span> connect serverAddr s</a>
    <a class="sourceLine" id="cb44-14" title="14">  n <span class="ot">&lt;-</span> send <span class="st">&quot;fish&quot;</span> client</a>
    <a class="sourceLine" id="cb44-15" title="15">  bs <span class="ot">&lt;-</span> recv <span class="dv">32</span> client</a>
    <a class="sourceLine" id="cb44-16" title="16">  n <span class="ot">`shouldBe`</span> <span class="dv">4</span></a>
    <a class="sourceLine" id="cb44-17" title="17">  bs <span class="ot">`shouldBe`</span> <span class="st">&quot;fish&quot;</span></a>
    <a class="sourceLine" id="cb44-18" title="18"></a>
    <a class="sourceLine" id="cb44-19" title="19"><span class="ot">main ::</span> <span class="dt">IO</span> ()</a>
    <a class="sourceLine" id="cb44-20" title="20">main <span class="ot">=</span> hspec <span class="op">$</span> <span class="kw">do</span></a>
    <a class="sourceLine" id="cb44-21" title="21"></a>
    <a class="sourceLine" id="cb44-22" title="22">  describe <span class="st">&quot;Network.Typed.Socket&quot;</span> <span class="op">$</span> <span class="kw">do</span></a>
    <a class="sourceLine" id="cb44-23" title="23"></a>
    <a class="sourceLine" id="cb44-24" title="24">    it <span class="st">&quot;can echo server (TCP)&quot;</span> <span class="op">$</span> <span class="kw">do</span></a>
    <a class="sourceLine" id="cb44-25" title="25">      withTcp4Socket <span class="op">$</span> \server <span class="ot">-&gt;</span> <span class="kw">do</span></a>
    <a class="sourceLine" id="cb44-26" title="26">        withTcp4Socket <span class="op">$</span> \client <span class="ot">-&gt;</span> <span class="kw">do</span></a>
    <a class="sourceLine" id="cb44-27" title="27">          serverWaitLock <span class="ot">&lt;-</span> newEmptyMVar</a>
    <a class="sourceLine" id="cb44-28" title="28">          void (forkIO (runServer server serverWaitLock))</a>
    <a class="sourceLine" id="cb44-29" title="29">          void (takeMVar serverWaitLock)</a>
    <a class="sourceLine" id="cb44-30" title="30">          runClient client</a></code></pre></div>
    <p>That’s the whole test! The main changes needed were:</p>
    <ul>
    <li>adding a mutex (a Haskell MVar) to make the client wait til the server is ready</li>
    <li>creating the server on a separate thread</li>
    <li>setting an option that allows the server to rebind in future test suite runs</li>
    <li>checking that echoing actually works with <code>shouldBe</code> in the client</li>
    </ul>
    <h2 id="socket-options-and-socket-wishes">Socket Options and Socket Wishes</h2>
    <p>This brings us close to the end. Our last bit of adventure involves thinking about socket options. Turns out, there’s a lot of them!</p>
    <div class="sourceCode" id="cb45"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb45-1" title="1"><span class="kw">data</span> <span class="dt">SocketOption</span></a>
    <a class="sourceLine" id="cb45-2" title="2">  <span class="ot">=</span> <span class="dt">Debug</span></a>
    <a class="sourceLine" id="cb45-3" title="3">  <span class="op">|</span> <span class="dt">ReuseAddr</span></a>
    <a class="sourceLine" id="cb45-4" title="4">  <span class="op">|</span> <span class="dt">SType</span></a>
    <a class="sourceLine" id="cb45-5" title="5">  <span class="op">|</span> <span class="dt">SoError</span></a>
    <a class="sourceLine" id="cb45-6" title="6">  <span class="op">|</span> <span class="dt">DontRoute</span></a>
    <a class="sourceLine" id="cb45-7" title="7">  <span class="op">|</span> <span class="dt">Broadcast</span></a>
    <a class="sourceLine" id="cb45-8" title="8">  <span class="op">|</span> <span class="dt">SendBuffer</span></a>
    <a class="sourceLine" id="cb45-9" title="9">  <span class="op">|</span> <span class="dt">RecvBuffer</span></a>
    <a class="sourceLine" id="cb45-10" title="10">  <span class="op">|</span> <span class="dt">KeepAlive</span></a>
    <a class="sourceLine" id="cb45-11" title="11">  <span class="op">|</span> <span class="dt">OOBInline</span></a>
    <a class="sourceLine" id="cb45-12" title="12">  <span class="op">|</span> <span class="dt">TimeToLive</span></a>
    <a class="sourceLine" id="cb45-13" title="13">  <span class="op">|</span> <span class="dt">MaxSegment</span></a>
    <a class="sourceLine" id="cb45-14" title="14">  <span class="op">|</span> <span class="dt">NoDelay</span></a>
    <a class="sourceLine" id="cb45-15" title="15">  <span class="op">|</span> <span class="dt">Cork</span></a>
    <a class="sourceLine" id="cb45-16" title="16">  <span class="op">|</span> <span class="dt">Linger</span></a>
    <a class="sourceLine" id="cb45-17" title="17">  <span class="op">|</span> <span class="dt">ReusePort</span></a>
    <a class="sourceLine" id="cb45-18" title="18">  <span class="op">|</span> <span class="dt">RecvLowWater</span></a>
    <a class="sourceLine" id="cb45-19" title="19">  <span class="op">|</span> <span class="dt">SendLowWater</span></a>
    <a class="sourceLine" id="cb45-20" title="20">  <span class="op">|</span> <span class="dt">RecvTimeout</span></a>
    <a class="sourceLine" id="cb45-21" title="21">  <span class="op">|</span> <span class="dt">SendTimeout</span></a>
    <a class="sourceLine" id="cb45-22" title="22">  <span class="op">|</span> <span class="dt">UseLoopBack</span></a>
    <a class="sourceLine" id="cb45-23" title="23">  <span class="op">|</span> <span class="dt">UserTimeout</span></a>
    <a class="sourceLine" id="cb45-24" title="24">  <span class="op">|</span> <span class="dt">IPv6Only</span></a></code></pre></div>
    <p>Here’s the typed socket function for setting options as of right now:</p>
    <div class="sourceCode" id="cb46"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb46-1" title="1"><span class="ot">setSocketOption ::</span> <span class="dt">NS.SocketOption</span> <span class="ot">-&gt;</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">SSocket</span> f p s sh <span class="ot">-&gt;</span> <span class="dt">IO</span> (<span class="dt">SSocket</span> f p s sh)</a></code></pre></div>
    <p>As you can see, we’re not doing any of the property tracking in the types we did with socket status, protocol, and family. We’re just, running the effects.</p>
    <p>The problem here is more complicated, because we want to track all the options we’ve enabled so far, which would be a type-level list.</p>
    <p>Something like:</p>
    <div class="sourceCode" id="cb47"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb47-1" title="1"><span class="ot">setSocketOption ::</span> <span class="dt">ValidToSetOption</span> opts <span class="op">~</span> <span class="dt">&#39;True</span></a>
    <a class="sourceLine" id="cb47-2" title="2">  <span class="ot">=&gt;</span> <span class="dt">NS.SocketOption</span> <span class="ot">-&gt;</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">SSocket</span> f p s sh opts</a>
    <a class="sourceLine" id="cb47-3" title="3">  <span class="ot">-&gt;</span> <span class="dt">IO</span> (<span class="dt">SSocket</span> f p s sh (<span class="dt">SetOpt</span> <span class="dt">NS.SocketOption</span> opts))</a></code></pre></div>
    <p>This is not a completely terrible endeavor just yet. It becomes harder if we’d like to also track the values we set options to, and to possibly reconsider how this interacts with the rest of our current API.</p>
    <p>Some options are boolean-valued (on, or off). Some take integers. Some take time-structures. It’s an adventure for another time!</p>
    <h2 id="lacking-linearity-leaves-limitations">Lacking Linearity Leaves Limitations</h2>
    <p>We catch a wide class of errors with this new API design. However, there’s still one major weakness to this approach because of the limitations of the type system we’re working in.</p>
    <p>What happens if we throw some threading into the mix??</p>
    <div class="sourceCode" id="cb48"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb48-1" title="1">g server <span class="ot">=</span> <span class="kw">do</span></a>
    <a class="sourceLine" id="cb48-2" title="2">  forkIO (runServer server)</a>
    <a class="sourceLine" id="cb48-3" title="3">  close server <span class="co">-- oh no</span></a>
    <a class="sourceLine" id="cb48-4" title="4"></a>
    <a class="sourceLine" id="cb48-5" title="5">runServer server <span class="ot">=</span> forever <span class="op">$</span> <span class="kw">do</span></a>
    <a class="sourceLine" id="cb48-6" title="6">  <span class="co">-- do cool server stuff, I hope</span></a></code></pre></div>
    <p>We were able to close the server socket in the main thread, and as a result, the type-state will be inconsistent in the child thread. The server will actually be closed but will have a type like <code>SSocket f p 'Listening sh</code>.</p>
    <p>Welp.</p>
    <p>This is a limitation of the type-system, and there’s naught we can to help the compiler help us.</p>
    <p>Fortunately, extensions to the Haskell type-system are being worked on that will allow it to encode these sorts of constraints. Constraints that make it possible to say, “I’ve handed this resource off to another thread. Please make no further modifications on this thread!”. That’s linear types - things can be “consumed”.</p>
    <p>Here’s the haskell <a href="https://arxiv.org/pdf/1710.09756.pdf">paper</a> detailing how this system would work.</p>
    <h2 id="closing">Closing</h2>
    <p>And that’s that! Phantom types, singletons, to the type-level and back to the value-level again.</p>
    <p>If you’re interested in seeing the full implementation, you can check it out on <a href="https://gitlab.com/queertypes/linear-socket">gitlab</a>.</p>
    <p>I’m looking for new work, so send me an email if you’d like to work with me on making reliable systems for all.</p>
    <p>Thanks for reading!</p>]]></summary>
</entry>
<entry>
  <title>Applicatives and Alternatives</title>
  <link href="https://queertypes.com/posts/59-applicatives-alternatives.html" />
  <id>https://queertypes.com/posts/59-applicatives-alternatives.html</id>
  <published>2017-04-21T00:00:00Z</published>
  <updated>2017-04-21T00:00:00Z</updated>
  <summary type="html"><![CDATA[<h2 id="introduction">Introduction</h2>
  <p>This post is a tutorial on applicative functors (applicatives) and alternatives - what they are, how they’re used, and how to build an intuition for them.</p>
  <p>It’s going to be extremely practical. It’s going to be all Haskell. We’ll build stuff along the way so that it makes more sense.</p>
  <p>The following imports are assumed to be in scope for this post:</p>
  <div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb1-1" title="1"><span class="kw">import</span> <span class="dt">Control.Applicative</span></a>
  <a class="sourceLine" id="cb1-2" title="2"><span class="kw">import</span> <span class="dt">Data.Monoid</span></a></code></pre></div>
  <p>All code used in this post is available <a href="https://gitlab.com/queertypes/read-review-refactor/tree/develop/code/59-app-alt">here</a>.</p>
  <p>Let’s start with some informal definitions.</p>
  <p>An applicative is an abstraction that lets us express function composition in a context. Applicative composition only succeeds if each independent component in the computation succeeds. Rules for what success looks like vary from context to context. Its interface looks like this.</p>
  <div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb2-1" title="1"><span class="kw">class</span> <span class="dt">Functor</span> f <span class="ot">=&gt;</span> <span class="dt">Applicative</span> (<span class="ot">f ::</span> <span class="op">*</span> <span class="ot">-&gt;</span> <span class="op">*</span>) <span class="kw">where</span></a>
  <a class="sourceLine" id="cb2-2" title="2"><span class="ot">  pure  ::</span> a <span class="ot">-&gt;</span> f a</a>
  <a class="sourceLine" id="cb2-3" title="3"><span class="ot">  (&lt;*&gt;) ::</span> f (a <span class="ot">-&gt;</span> b) <span class="ot">-&gt;</span> f a <span class="ot">-&gt;</span> f b</a></code></pre></div>
  <p>Notably, all applicative functors must also be functors. We won’t cover functors here, but it suffices to say that they’re an abstraction for applying context-free functions to context-embedded values.</p>
  <p><code>pure</code> is sometimes called <code>lift</code> because it lifts a pure value of type <code>a</code> into the context <code>f</code>. <code>&lt;*&gt;</code> is our applicative composition operator. It allows us to chain together contextual computations.</p>
  <p>An alternative is an abstraction that builds on applicatives. Like it sounds, it allows to express the idea of trying something else in case something fails. Composition of alternatives take the first success. Its interface looks like this:</p>
  <div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb3-1" title="1"><span class="kw">class</span> <span class="dt">Applicative</span> f <span class="ot">=&gt;</span> <span class="dt">Alternative</span> (<span class="ot">f ::</span> <span class="op">*</span> <span class="ot">-&gt;</span> <span class="op">*</span>) <span class="kw">where</span></a>
  <a class="sourceLine" id="cb3-2" title="2"><span class="ot">  empty ::</span> f a</a>
  <a class="sourceLine" id="cb3-3" title="3"><span class="ot">  (&lt;|&gt;) ::</span> f a <span class="ot">-&gt;</span> f a <span class="ot">-&gt;</span> f a</a></code></pre></div>
  <p><code>empty</code> is our failure value, to represent a failure of all alternatives. <code>&lt;|&gt;</code> is our composition operator, for chaining together alternatives.</p>
  <p>With those definitions out of the way -</p>
  <p>Let’s build our first applicative! To do so, we’ll look at the <code>Option</code> type.</p>
  <h2 id="option-types-optional-contexts">Option Types; Optional Contexts</h2>
  <p>An <code>Option</code> or <code>Maybe</code> type allows us to express the idea of nullability safely. It looks like this:</p>
  <div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb4-1" title="1"><span class="kw">data</span> <span class="dt">Option</span> a</a>
  <a class="sourceLine" id="cb4-2" title="2">  <span class="ot">=</span> <span class="dt">Some</span> a</a>
  <a class="sourceLine" id="cb4-3" title="3">  <span class="op">|</span> <span class="dt">None</span></a>
  <a class="sourceLine" id="cb4-4" title="4">  <span class="kw">deriving</span> <span class="dt">Show</span></a></code></pre></div>
  <p>We either have <code>Some</code> value of type <code>a</code> or we have <code>None</code>.</p>
  <p>Here’s the applicative (and functor) implementation for <code>Option</code>:</p>
  <div class="sourceCode" id="cb5"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb5-1" title="1"><span class="kw">instance</span> <span class="dt">Functor</span> <span class="dt">Option</span> <span class="kw">where</span></a>
  <a class="sourceLine" id="cb5-2" title="2">  <span class="fu">fmap</span> f (<span class="dt">Some</span> a) <span class="ot">=</span> <span class="dt">Some</span> (f a)</a>
  <a class="sourceLine" id="cb5-3" title="3">  <span class="fu">fmap</span> _ <span class="dt">None</span>     <span class="ot">=</span> <span class="dt">None</span></a>
  <a class="sourceLine" id="cb5-4" title="4"></a>
  <a class="sourceLine" id="cb5-5" title="5"><span class="kw">instance</span> <span class="dt">Applicative</span> <span class="dt">Option</span> <span class="kw">where</span></a>
  <a class="sourceLine" id="cb5-6" title="6">  <span class="fu">pure</span> <span class="ot">=</span> <span class="dt">Some</span></a>
  <a class="sourceLine" id="cb5-7" title="7">  <span class="dt">Some</span> f <span class="op">&lt;*&gt;</span> <span class="dt">Some</span> a <span class="ot">=</span> <span class="dt">Some</span> (f a)</a>
  <a class="sourceLine" id="cb5-8" title="8">  <span class="dt">None</span>   <span class="op">&lt;*&gt;</span> <span class="dt">Some</span> _ <span class="ot">=</span> <span class="dt">None</span></a>
  <a class="sourceLine" id="cb5-9" title="9">  <span class="dt">Some</span> _ <span class="op">&lt;*&gt;</span> <span class="dt">None</span>   <span class="ot">=</span> <span class="dt">None</span></a>
  <a class="sourceLine" id="cb5-10" title="10">  <span class="dt">None</span>   <span class="op">&lt;*&gt;</span> <span class="dt">None</span>   <span class="ot">=</span> <span class="dt">None</span></a></code></pre></div>
  <p>The only way a computation for <code>Option</code> can succeed is if we have <code>Some</code> value along all paths. The moment we encounter a <code>None</code>, we abort.</p>
  <p>Let’s look at the implementation of <code>Alternative</code> for <code>Option</code> now:</p>
  <div class="sourceCode" id="cb6"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb6-1" title="1"><span class="kw">instance</span> <span class="dt">Alternative</span> <span class="dt">Option</span> <span class="kw">where</span></a>
  <a class="sourceLine" id="cb6-2" title="2">  empty <span class="ot">=</span> <span class="dt">None</span></a>
  <a class="sourceLine" id="cb6-3" title="3">  <span class="dt">Some</span> a <span class="op">&lt;|&gt;</span> <span class="dt">Some</span> _ <span class="ot">=</span> <span class="dt">Some</span> a</a>
  <a class="sourceLine" id="cb6-4" title="4">  <span class="dt">Some</span> a <span class="op">&lt;|&gt;</span> <span class="dt">None</span>   <span class="ot">=</span> <span class="dt">Some</span> a</a>
  <a class="sourceLine" id="cb6-5" title="5">  <span class="dt">None</span>   <span class="op">&lt;|&gt;</span> <span class="dt">Some</span> a <span class="ot">=</span> <span class="dt">Some</span> a</a>
  <a class="sourceLine" id="cb6-6" title="6">  <span class="dt">None</span>   <span class="op">&lt;|&gt;</span> <span class="dt">None</span>   <span class="ot">=</span> <span class="dt">None</span></a></code></pre></div>
  <p>In this case, we succeed as long as some computational path results in <code>Some</code>. We fail only if all paths fail.</p>
  <p>I’ll provide some examples next. A few notes:</p>
  <ul>
  <li><code>&lt;$&gt;</code> is <code>fmap</code>, or, the means to apply a pure function in a context</li>
  <li><code>:{</code> is used in the Haskell REPL to indicate multi-line input</li>
  <li><code>:}</code> terminates multi-line input</li>
  </ul>
  <p>Here’s how what we’ve written looks like in use:</p>
  <div class="sourceCode" id="cb7"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb7-1" title="1"><span class="op">&gt;</span> (<span class="op">+</span>) <span class="op">&lt;$&gt;</span> <span class="dt">Some</span> <span class="dv">1</span> <span class="op">&lt;*&gt;</span> <span class="dt">Some</span> <span class="dv">2</span></a>
  <a class="sourceLine" id="cb7-2" title="2"><span class="dt">Some</span> <span class="dv">3</span></a>
  <a class="sourceLine" id="cb7-3" title="3"></a>
  <a class="sourceLine" id="cb7-4" title="4"><span class="op">&gt;</span> (<span class="op">+</span>) <span class="ot">`fmap`</span> <span class="dt">Some</span> <span class="dv">1</span> <span class="op">&lt;*&gt;</span> <span class="dt">Some</span> <span class="dv">2</span></a>
  <a class="sourceLine" id="cb7-5" title="5"><span class="dt">Some</span> <span class="dv">3</span></a>
  <a class="sourceLine" id="cb7-6" title="6"></a>
  <a class="sourceLine" id="cb7-7" title="7"><span class="op">&gt;</span> (<span class="op">+</span>) <span class="op">&lt;$&gt;</span> <span class="dt">Some</span> <span class="dv">1</span> <span class="op">&lt;*&gt;</span> <span class="dt">None</span></a>
  <a class="sourceLine" id="cb7-8" title="8"><span class="dt">None</span></a>
  <a class="sourceLine" id="cb7-9" title="9"></a>
  <a class="sourceLine" id="cb7-10" title="10"><span class="op">&gt;</span> add3 a b c <span class="ot">=</span> a <span class="op">+</span> b <span class="op">+</span> c</a>
  <a class="sourceLine" id="cb7-11" title="11"><span class="op">&gt;</span> <span class="op">:</span>t add3</a>
  <a class="sourceLine" id="cb7-12" title="12"><span class="ot">add3 ::</span> <span class="dt">Num</span> a <span class="ot">=&gt;</span> a <span class="ot">-&gt;</span> a <span class="ot">-&gt;</span> a <span class="ot">-&gt;</span> a</a>
  <a class="sourceLine" id="cb7-13" title="13"></a>
  <a class="sourceLine" id="cb7-14" title="14"><span class="op">&gt;</span> add3 <span class="op">&lt;$&gt;</span> <span class="dt">Some</span> <span class="dv">1</span> <span class="op">&lt;*&gt;</span> <span class="dt">Some</span> <span class="dv">2</span> <span class="op">&lt;*&gt;</span> <span class="dt">Some</span> <span class="dv">3</span></a>
  <a class="sourceLine" id="cb7-15" title="15"><span class="dt">Some</span> <span class="dv">6</span></a>
  <a class="sourceLine" id="cb7-16" title="16"></a>
  <a class="sourceLine" id="cb7-17" title="17"><span class="op">&gt;</span> add3 <span class="op">&lt;$&gt;</span> <span class="dt">Some</span> <span class="dv">1</span> <span class="op">&lt;*&gt;</span> <span class="dt">None</span> <span class="op">&lt;*&gt;</span> <span class="dt">Some</span> <span class="dv">3</span></a>
  <a class="sourceLine" id="cb7-18" title="18"><span class="dt">None</span></a>
  <a class="sourceLine" id="cb7-19" title="19"></a>
  <a class="sourceLine" id="cb7-20" title="20"><span class="op">&gt;</span> <span class="dt">None</span> <span class="op">&lt;|&gt;</span> <span class="dt">Some</span> <span class="dv">1</span></a>
  <a class="sourceLine" id="cb7-21" title="21"><span class="dt">Some</span> <span class="dv">1</span></a>
  <a class="sourceLine" id="cb7-22" title="22"></a>
  <a class="sourceLine" id="cb7-23" title="23"><span class="op">&gt;</span> <span class="dt">None</span> <span class="op">&lt;|&gt;</span> <span class="dt">None</span></a>
  <a class="sourceLine" id="cb7-24" title="24"><span class="dt">None</span></a>
  <a class="sourceLine" id="cb7-25" title="25"></a>
  <a class="sourceLine" id="cb7-26" title="26"><span class="op">&gt;</span> <span class="dt">None</span> <span class="op">&lt;|&gt;</span> <span class="dt">None</span> <span class="op">&lt;|&gt;</span> <span class="dt">Some</span> <span class="dv">3</span></a>
  <a class="sourceLine" id="cb7-27" title="27"><span class="dt">Some</span> <span class="dv">3</span></a>
  <a class="sourceLine" id="cb7-28" title="28"></a>
  <a class="sourceLine" id="cb7-29" title="29"><span class="op">&gt;</span> <span class="op">:</span>{</a>
  <a class="sourceLine" id="cb7-30" title="30"><span class="op">|</span>     (<span class="op">+</span>) <span class="op">&lt;$&gt;</span> <span class="dt">None</span>   <span class="op">&lt;*&gt;</span> <span class="dt">Some</span> <span class="dv">1</span></a>
  <a class="sourceLine" id="cb7-31" title="31"><span class="op">|</span> <span class="op">&lt;|&gt;</span> (<span class="op">*</span>) <span class="op">&lt;$&gt;</span> <span class="dt">Some</span> <span class="dv">5</span> <span class="op">&lt;*&gt;</span> <span class="dt">Some</span> <span class="dv">10</span></a>
  <a class="sourceLine" id="cb7-32" title="32"><span class="op">|</span> <span class="op">:</span>}</a>
  <a class="sourceLine" id="cb7-33" title="33"><span class="dt">Some</span> <span class="dv">50</span></a>
  <a class="sourceLine" id="cb7-34" title="34"></a>
  <a class="sourceLine" id="cb7-35" title="35"><span class="op">&gt;</span> <span class="op">:</span>(</a>
  <a class="sourceLine" id="cb7-36" title="36"><span class="op">|</span>     (<span class="op">*</span>) <span class="op">&lt;$&gt;</span> <span class="dt">Some</span> <span class="dv">8</span> <span class="op">&lt;*&gt;</span> <span class="dt">Some</span> <span class="dv">5</span></a>
  <a class="sourceLine" id="cb7-37" title="37"><span class="op">|</span> <span class="op">&lt;|&gt;</span> <span class="dt">None</span></a>
  <a class="sourceLine" id="cb7-38" title="38"><span class="op">|</span> <span class="op">:</span>}</a>
  <a class="sourceLine" id="cb7-39" title="39"><span class="dt">Some</span> <span class="dv">40</span></a></code></pre></div>
  <p>The examples above all use integers, but we could easily use any other type. Furthermore, notice that we can chain as many computations as we need, like with <code>add3</code>. This is much more convenient than pattern matching on the spot every time, like:</p>
  <div class="sourceCode" id="cb8"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb8-1" title="1"><span class="ot">add3OptLengthy ::</span> <span class="dt">Num</span> a <span class="ot">=&gt;</span> <span class="dt">Option</span> a <span class="ot">-&gt;</span> <span class="dt">Option</span> a <span class="ot">-&gt;</span> <span class="dt">Option</span> a <span class="ot">-&gt;</span> <span class="dt">Option</span> a</a>
  <a class="sourceLine" id="cb8-2" title="2">add3OptLengthy a b c <span class="ot">=</span></a>
  <a class="sourceLine" id="cb8-3" title="3">  <span class="kw">case</span> a <span class="kw">of</span></a>
  <a class="sourceLine" id="cb8-4" title="4">    <span class="dt">None</span> <span class="ot">-&gt;</span> <span class="dt">None</span></a>
  <a class="sourceLine" id="cb8-5" title="5">    (<span class="dt">Some</span> a&#39;) <span class="ot">-&gt;</span> <span class="kw">case</span> b <span class="kw">of</span></a>
  <a class="sourceLine" id="cb8-6" title="6">      <span class="dt">None</span> <span class="ot">-&gt;</span> <span class="dt">None</span></a>
  <a class="sourceLine" id="cb8-7" title="7">      (<span class="dt">Some</span> b&#39;) <span class="ot">-&gt;</span> <span class="kw">case</span> c <span class="kw">of</span></a>
  <a class="sourceLine" id="cb8-8" title="8">        <span class="dt">None</span> <span class="ot">-&gt;</span> <span class="dt">None</span></a>
  <a class="sourceLine" id="cb8-9" title="9">        (<span class="dt">Some</span> c&#39;) <span class="ot">-&gt;</span> <span class="dt">Some</span> (add3 a&#39; b&#39; c&#39;)</a></code></pre></div>
  <p>It is also less error-prone that using if-expressions, like so:</p>
  <div class="sourceCode" id="cb9"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb9-1" title="1"><span class="ot">isNone ::</span> <span class="dt">Option</span> a <span class="ot">-&gt;</span> <span class="dt">Bool</span></a>
  <a class="sourceLine" id="cb9-2" title="2">isNone <span class="dt">None</span> <span class="ot">=</span> <span class="dt">True</span></a>
  <a class="sourceLine" id="cb9-3" title="3">isNone _ <span class="ot">=</span> <span class="dt">False</span></a>
  <a class="sourceLine" id="cb9-4" title="4"></a>
  <a class="sourceLine" id="cb9-5" title="5"><span class="co">-- dangerous</span></a>
  <a class="sourceLine" id="cb9-6" title="6"><span class="ot">getOption ::</span> <span class="dt">Option</span> a <span class="ot">-&gt;</span> a</a>
  <a class="sourceLine" id="cb9-7" title="7">getOption (<span class="dt">Some</span> a) <span class="ot">=</span> a</a>
  <a class="sourceLine" id="cb9-8" title="8">getOption <span class="dt">None</span> <span class="ot">=</span> <span class="fu">error</span> <span class="st">&quot;oh no crash&quot;</span></a>
  <a class="sourceLine" id="cb9-9" title="9"></a>
  <a class="sourceLine" id="cb9-10" title="10"><span class="ot">add3OptUnsafe ::</span> <span class="dt">Num</span> a <span class="ot">=&gt;</span> <span class="dt">Option</span> a <span class="ot">-&gt;</span> <span class="dt">Option</span> a <span class="ot">-&gt;</span> <span class="dt">Option</span> a <span class="ot">-&gt;</span> <span class="dt">Option</span> a</a>
  <a class="sourceLine" id="cb9-11" title="11">add3OptUnsafe a b c <span class="ot">=</span></a>
  <a class="sourceLine" id="cb9-12" title="12">  <span class="kw">if</span> isNone a</a>
  <a class="sourceLine" id="cb9-13" title="13">  <span class="kw">then</span> <span class="dt">None</span></a>
  <a class="sourceLine" id="cb9-14" title="14">  <span class="kw">else</span> <span class="kw">if</span> isNone b</a>
  <a class="sourceLine" id="cb9-15" title="15">       <span class="kw">then</span> <span class="dt">None</span></a>
  <a class="sourceLine" id="cb9-16" title="16">       <span class="kw">else</span> <span class="kw">if</span> isNone c</a>
  <a class="sourceLine" id="cb9-17" title="17">            <span class="kw">then</span> <span class="dt">None</span></a>
  <a class="sourceLine" id="cb9-18" title="18">            <span class="kw">else</span> <span class="dt">Some</span> (add3 (getOption a) (getOption b) (getOption c))</a></code></pre></div>
  <p>Alternatives and applicatives (and functors!) capture this logic for us so we don’t have to repeat ourselves. All of the above to say:</p>
  <div class="sourceCode" id="cb10"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb10-1" title="1"><span class="op">&gt;</span> f3 a b c <span class="ot">=</span> add3 <span class="op">&lt;$&gt;</span> a <span class="op">&lt;*&gt;</span> b <span class="op">&lt;*&gt;</span> c</a>
  <a class="sourceLine" id="cb10-2" title="2"><span class="op">&gt;</span> <span class="op">:</span>t f3</a>
  <a class="sourceLine" id="cb10-3" title="3"><span class="dt">Num</span> a <span class="ot">=&gt;</span> <span class="dt">Option</span> a <span class="ot">-&gt;</span> <span class="dt">Option</span> a <span class="ot">-&gt;</span> <span class="dt">Option</span> a <span class="ot">-&gt;</span> <span class="dt">Option</span> a</a></code></pre></div>
  <p>Let’s look at a new context type now: <code>Xor</code></p>
  <h2 id="xor---success-or-failing-with-informational-errors">Xor - Success or Failing With Informational Errors</h2>
  <p><code>Xor</code> (or it’s Haskell analogue, <code>Either</code>) is a type used to store simple error information in the case of a failure. It looks like this:</p>
  <div class="sourceCode" id="cb11"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb11-1" title="1"><span class="kw">data</span> <span class="dt">Xor</span> a b</a>
  <a class="sourceLine" id="cb11-2" title="2">  <span class="ot">=</span> <span class="dt">XLeft</span> a</a>
  <a class="sourceLine" id="cb11-3" title="3">  <span class="op">|</span> <span class="dt">XRight</span> b</a>
  <a class="sourceLine" id="cb11-4" title="4">  <span class="kw">deriving</span> <span class="dt">Show</span></a></code></pre></div>
  <p>The <code>XLeft</code> branch is typically used to capture an error. The <code>XRight</code> branch is our success path.</p>
  <p>Here’s the implementation of <code>Applicative</code> for <code>Xor</code>:</p>
  <div class="sourceCode" id="cb12"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb12-1" title="1"><span class="kw">instance</span> <span class="dt">Functor</span> (<span class="dt">Xor</span> a) <span class="kw">where</span></a>
  <a class="sourceLine" id="cb12-2" title="2">  <span class="fu">fmap</span> f (<span class="dt">XRight</span> a) <span class="ot">=</span> <span class="dt">XRight</span> (f a)</a>
  <a class="sourceLine" id="cb12-3" title="3">  <span class="fu">fmap</span> _ (<span class="dt">XLeft</span> a)  <span class="ot">=</span> <span class="dt">XLeft</span> a</a>
  <a class="sourceLine" id="cb12-4" title="4"></a>
  <a class="sourceLine" id="cb12-5" title="5"><span class="kw">instance</span> <span class="dt">Applicative</span> (<span class="dt">Xor</span> a) <span class="kw">where</span></a>
  <a class="sourceLine" id="cb12-6" title="6">  <span class="fu">pure</span> <span class="ot">=</span> <span class="dt">XRight</span></a>
  <a class="sourceLine" id="cb12-7" title="7">  <span class="dt">XRight</span> f <span class="op">&lt;*&gt;</span> <span class="dt">XRight</span> a <span class="ot">=</span> <span class="dt">XRight</span> (f a)</a>
  <a class="sourceLine" id="cb12-8" title="8">  <span class="dt">XRight</span> _ <span class="op">&lt;*&gt;</span> <span class="dt">XLeft</span> a  <span class="ot">=</span> <span class="dt">XLeft</span> a</a>
  <a class="sourceLine" id="cb12-9" title="9">  <span class="dt">XLeft</span> a  <span class="op">&lt;*&gt;</span> <span class="dt">XRight</span> _ <span class="ot">=</span> <span class="dt">XLeft</span> a</a>
  <a class="sourceLine" id="cb12-10" title="10">  <span class="dt">XLeft</span> a  <span class="op">&lt;*&gt;</span> <span class="dt">XLeft</span> _  <span class="ot">=</span> <span class="dt">XLeft</span> a <span class="co">-- choose the first error to short-circuit</span></a></code></pre></div>
  <p>As before, the only way to get to a success is to have successes all along the way.</p>
  <p><code>Xor</code> does not admit an <code>Alternative</code> instance. This is because, there is no reasonable definition of <code>Alternative</code>’s <code>empty</code>. If the left-branch type <code>a</code> was a <code>Monoid</code>, then it’d be possible to define, but it still wouldn’t be very interesting. We’ll elide that for now and show a more reasonable error-capturing type in the next section that allows for alternatives.</p>
  <p>For the examples involving <code>Xor</code>, we’ll first define a simple error data type with a few helper functions:</p>
  <div class="sourceCode" id="cb13"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb13-1" title="1"><span class="kw">data</span> <span class="dt">NumberError</span></a>
  <a class="sourceLine" id="cb13-2" title="2">  <span class="ot">=</span> <span class="dt">NumberTooBig</span></a>
  <a class="sourceLine" id="cb13-3" title="3">  <span class="op">|</span> <span class="dt">NumberTooSmall</span></a>
  <a class="sourceLine" id="cb13-4" title="4">  <span class="op">|</span> <span class="dt">NumberNotAllowed</span></a>
  <a class="sourceLine" id="cb13-5" title="5">  <span class="kw">deriving</span> <span class="dt">Show</span></a>
  <a class="sourceLine" id="cb13-6" title="6"></a>
  <a class="sourceLine" id="cb13-7" title="7"><span class="ot">validateNumber ::</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">Xor</span> <span class="dt">NumberError</span> <span class="dt">Int</span></a>
  <a class="sourceLine" id="cb13-8" title="8">validateNumber x</a>
  <a class="sourceLine" id="cb13-9" title="9">  <span class="op">|</span> x <span class="op">&gt;</span> <span class="dv">500</span> <span class="ot">=</span> <span class="dt">XLeft</span> <span class="dt">NumberTooBig</span></a>
  <a class="sourceLine" id="cb13-10" title="10">  <span class="op">|</span> x <span class="op">&lt;</span> <span class="dv">30</span> <span class="ot">=</span> <span class="dt">XLeft</span> <span class="dt">NumberTooSmall</span></a>
  <a class="sourceLine" id="cb13-11" title="11">  <span class="op">|</span> x <span class="ot">`elem`</span> [<span class="dv">42</span>, <span class="dv">69</span>, <span class="dv">420</span>] <span class="ot">=</span> <span class="dt">XLeft</span> <span class="dt">NumberNotAllowed</span></a>
  <a class="sourceLine" id="cb13-12" title="12">  <span class="op">|</span> <span class="fu">otherwise</span> <span class="ot">=</span> <span class="dt">XRight</span> x</a></code></pre></div>
  <p>Now for some examples:</p>
  <div class="sourceCode" id="cb14"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb14-1" title="1"><span class="op">&gt;</span> validateNumber <span class="dv">32</span></a>
  <a class="sourceLine" id="cb14-2" title="2"><span class="dt">XRight</span> <span class="dv">32</span></a>
  <a class="sourceLine" id="cb14-3" title="3"></a>
  <a class="sourceLine" id="cb14-4" title="4"><span class="op">&gt;</span> validateNumber <span class="dv">69</span></a>
  <a class="sourceLine" id="cb14-5" title="5"><span class="dt">XLeft</span> <span class="dt">NumberNotAllowed</span></a>
  <a class="sourceLine" id="cb14-6" title="6"></a>
  <a class="sourceLine" id="cb14-7" title="7"><span class="op">&gt;</span> validateNumber <span class="dv">501</span></a>
  <a class="sourceLine" id="cb14-8" title="8"><span class="dt">XLeft</span> <span class="dt">NumberTooBig</span></a>
  <a class="sourceLine" id="cb14-9" title="9"></a>
  <a class="sourceLine" id="cb14-10" title="10"><span class="op">&gt;</span> validateNumber <span class="dv">29</span></a>
  <a class="sourceLine" id="cb14-11" title="11"><span class="dt">XLeft</span> <span class="dt">NumberTooSmall</span></a>
  <a class="sourceLine" id="cb14-12" title="12"></a>
  <a class="sourceLine" id="cb14-13" title="13"><span class="op">&gt;</span> (<span class="op">+</span>) <span class="op">&lt;$&gt;</span> validateNumber <span class="dv">31</span> <span class="op">&lt;*&gt;</span> validateNumber <span class="dv">33</span></a>
  <a class="sourceLine" id="cb14-14" title="14"><span class="dt">XRight</span> <span class="dv">64</span></a>
  <a class="sourceLine" id="cb14-15" title="15"></a>
  <a class="sourceLine" id="cb14-16" title="16"><span class="op">&gt;</span> (<span class="op">+</span>) <span class="op">&lt;$&gt;</span> validateNumber <span class="dv">31</span> <span class="op">&lt;*&gt;</span> validateNumber <span class="dv">42</span></a>
  <a class="sourceLine" id="cb14-17" title="17"><span class="dt">XLeft</span> <span class="dt">NumberNotAllowed</span></a></code></pre></div>
  <p>Next, let’s look at an extension to the <code>Xor</code> type - the <code>Validation</code> type.</p>
  <h2 id="validation---keeping-track-of-all-the-errors">Validation - Keeping Track of All The Errors</h2>
  <p><code>Validation</code> is a very handy type for any form of validation where you’d like to keep track of all errors that occur, instead of throwing them away. It looks fairly similar to <code>Xor</code>, even:</p>
  <div class="sourceCode" id="cb15"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb15-1" title="1"><span class="kw">data</span> <span class="dt">Validation</span> a b</a>
  <a class="sourceLine" id="cb15-2" title="2">  <span class="ot">=</span> <span class="dt">Failure</span> a</a>
  <a class="sourceLine" id="cb15-3" title="3">  <span class="op">|</span> <span class="dt">Success</span> b</a>
  <a class="sourceLine" id="cb15-4" title="4">  <span class="kw">deriving</span> <span class="dt">Show</span></a></code></pre></div>
  <p>Exchange <code>Validation</code> with <code>Xor</code>, <code>Failure</code> with <code>XLeft</code>, and <code>Success</code> with <code>XRight</code> and we’re right back to the last section! However, we’ll see that how we implement <code>Applicative</code> and <code>Alternative</code> can make a big difference.</p>
  <p>Here’s the <code>Applicative</code> implementation:</p>
  <div class="sourceCode" id="cb16"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb16-1" title="1"><span class="kw">instance</span> <span class="dt">Functor</span> (<span class="dt">Validation</span> a) <span class="kw">where</span></a>
  <a class="sourceLine" id="cb16-2" title="2">  <span class="fu">fmap</span> f (<span class="dt">Success</span> a) <span class="ot">=</span> <span class="dt">Success</span> (f a)</a>
  <a class="sourceLine" id="cb16-3" title="3">  <span class="fu">fmap</span> _ (<span class="dt">Failure</span> a) <span class="ot">=</span> <span class="dt">Failure</span> a</a>
  <a class="sourceLine" id="cb16-4" title="4"></a>
  <a class="sourceLine" id="cb16-5" title="5"><span class="co">-- accumulating errors</span></a>
  <a class="sourceLine" id="cb16-6" title="6"><span class="kw">instance</span> <span class="dt">Monoid</span> a <span class="ot">=&gt;</span> <span class="dt">Applicative</span> (<span class="dt">Validation</span> a) <span class="kw">where</span></a>
  <a class="sourceLine" id="cb16-7" title="7">  <span class="fu">pure</span> <span class="ot">=</span> <span class="dt">Success</span></a>
  <a class="sourceLine" id="cb16-8" title="8">  <span class="dt">Success</span> f <span class="op">&lt;*&gt;</span> <span class="dt">Success</span> a <span class="ot">=</span> <span class="dt">Success</span> (f a)</a>
  <a class="sourceLine" id="cb16-9" title="9">  <span class="dt">Failure</span> a <span class="op">&lt;*&gt;</span> <span class="dt">Success</span> _ <span class="ot">=</span> <span class="dt">Failure</span> a</a>
  <a class="sourceLine" id="cb16-10" title="10">  <span class="dt">Success</span> _ <span class="op">&lt;*&gt;</span> <span class="dt">Failure</span> a <span class="ot">=</span> <span class="dt">Failure</span> a</a>
  <a class="sourceLine" id="cb16-11" title="11">  <span class="dt">Failure</span> a <span class="op">&lt;*&gt;</span> <span class="dt">Failure</span> b <span class="ot">=</span> <span class="dt">Failure</span> (a <span class="op">&lt;&gt;</span> b)</a></code></pre></div>
  <p>The biggest change so far is that we require the error type to implement the <code>Monoid</code> interface, which, expresses things that can be concatenated/merged/etc via the <code>&lt;&gt;</code> operator. Instead of keeping only the first error encounterd, we keep them all. We’ll see how this works more closely with some examples soon.</p>
  <p>Here’s the implementation for <code>Alternative</code>:</p>
  <div class="sourceCode" id="cb17"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb17-1" title="1"><span class="kw">instance</span> <span class="dt">Monoid</span> a <span class="ot">=&gt;</span> <span class="dt">Alternative</span> (<span class="dt">Validation</span> a) <span class="kw">where</span></a>
  <a class="sourceLine" id="cb17-2" title="2">  empty <span class="ot">=</span> <span class="dt">Failure</span> <span class="fu">mempty</span></a>
  <a class="sourceLine" id="cb17-3" title="3">  <span class="dt">Success</span> a <span class="op">&lt;|&gt;</span> <span class="dt">Success</span> _ <span class="ot">=</span> <span class="dt">Success</span> a</a>
  <a class="sourceLine" id="cb17-4" title="4">  <span class="dt">Success</span> a <span class="op">&lt;|&gt;</span> <span class="dt">Failure</span> _ <span class="ot">=</span> <span class="dt">Success</span> a</a>
  <a class="sourceLine" id="cb17-5" title="5">  <span class="dt">Failure</span> _ <span class="op">&lt;|&gt;</span> <span class="dt">Success</span> a <span class="ot">=</span> <span class="dt">Success</span> a</a>
  <a class="sourceLine" id="cb17-6" title="6">  <span class="dt">Failure</span> _ <span class="op">&lt;|&gt;</span> <span class="dt">Failure</span> a <span class="ot">=</span> <span class="dt">Failure</span> a</a></code></pre></div>
  <p>Nothing changed here compared to <code>Xor</code>.</p>
  <p>Before getting to examples, let’s define some convenient functions around our <code>NumberError</code> data type from before:</p>
  <div class="sourceCode" id="cb18"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb18-1" title="1"><span class="ot">numberTooSmall ::</span> <span class="dt">Validation</span> [<span class="dt">NumberError</span>] a</a>
  <a class="sourceLine" id="cb18-2" title="2">numberTooSmall <span class="ot">=</span> <span class="dt">Failure</span> [<span class="dt">NumberTooSmall</span>]</a>
  <a class="sourceLine" id="cb18-3" title="3"></a>
  <a class="sourceLine" id="cb18-4" title="4"><span class="ot">numberTooBig ::</span> <span class="dt">Validation</span> [<span class="dt">NumberError</span>] a</a>
  <a class="sourceLine" id="cb18-5" title="5">numberTooBig <span class="ot">=</span> <span class="dt">Failure</span> [<span class="dt">NumberTooBig</span>]</a>
  <a class="sourceLine" id="cb18-6" title="6"></a>
  <a class="sourceLine" id="cb18-7" title="7"><span class="ot">numberNotAllowed ::</span> <span class="dt">Validation</span> [<span class="dt">NumberError</span>] a</a>
  <a class="sourceLine" id="cb18-8" title="8">numberNotAllowed <span class="ot">=</span> <span class="dt">Failure</span> [<span class="dt">NumberNotAllowed</span>]</a>
  <a class="sourceLine" id="cb18-9" title="9"></a>
  <a class="sourceLine" id="cb18-10" title="10"><span class="ot">validateNumberV ::</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">Validation</span> [<span class="dt">NumberError</span>] <span class="dt">Int</span></a>
  <a class="sourceLine" id="cb18-11" title="11">validateNumberV x</a>
  <a class="sourceLine" id="cb18-12" title="12">  <span class="op">|</span> x <span class="op">&gt;</span> <span class="dv">500</span> <span class="ot">=</span> numberTooBig</a>
  <a class="sourceLine" id="cb18-13" title="13">  <span class="op">|</span> x <span class="op">&lt;</span> <span class="dv">30</span> <span class="ot">=</span> numberTooSmall</a>
  <a class="sourceLine" id="cb18-14" title="14">  <span class="op">|</span> x <span class="ot">`elem`</span> [<span class="dv">42</span>, <span class="dv">69</span>, <span class="dv">420</span>] <span class="ot">=</span> numberNotAllowed</a>
  <a class="sourceLine" id="cb18-15" title="15">  <span class="op">|</span> <span class="fu">otherwise</span> <span class="ot">=</span> <span class="dt">Success</span> x</a></code></pre></div>
  <p>And some examples:</p>
  <div class="sourceCode" id="cb19"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb19-1" title="1"><span class="op">&gt;</span> validateNumberV <span class="dv">32</span></a>
  <a class="sourceLine" id="cb19-2" title="2"><span class="dt">Success</span> <span class="dv">32</span></a>
  <a class="sourceLine" id="cb19-3" title="3"></a>
  <a class="sourceLine" id="cb19-4" title="4"><span class="op">&gt;</span> validateNumberV <span class="dv">69</span></a>
  <a class="sourceLine" id="cb19-5" title="5"><span class="dt">Failure</span> [<span class="dt">NumberNotAllowed</span>]</a>
  <a class="sourceLine" id="cb19-6" title="6"></a>
  <a class="sourceLine" id="cb19-7" title="7"><span class="op">&gt;</span> add3V a b c <span class="ot">=</span> a <span class="op">+</span> b <span class="op">+</span> c</a>
  <a class="sourceLine" id="cb19-8" title="8"></a>
  <a class="sourceLine" id="cb19-9" title="9"><span class="op">&gt;</span> add3V <span class="op">&lt;$&gt;</span> validateNumberV <span class="dv">32</span> <span class="op">&lt;*&gt;</span> validateNumberV <span class="dv">33</span> <span class="op">&lt;*&gt;</span> validateNumberV <span class="dv">34</span></a>
  <a class="sourceLine" id="cb19-10" title="10"><span class="dt">Success</span> <span class="dv">99</span></a>
  <a class="sourceLine" id="cb19-11" title="11"></a>
  <a class="sourceLine" id="cb19-12" title="12"><span class="op">&gt;</span> add3V <span class="op">&lt;$&gt;</span> validateNumberV <span class="dv">42</span> <span class="op">&lt;*&gt;</span> validateNumberV <span class="dv">69</span> <span class="op">&lt;*&gt;</span> validateNumberV <span class="dv">420</span></a>
  <a class="sourceLine" id="cb19-13" title="13"><span class="dt">Failure</span> [<span class="dt">NumberNotAllowed</span>, <span class="dt">NumberNotAllowed</span>, <span class="dt">NumberNotAllowed</span>]</a>
  <a class="sourceLine" id="cb19-14" title="14"></a>
  <a class="sourceLine" id="cb19-15" title="15"><span class="op">&gt;</span> add3V <span class="op">&lt;$&gt;</span> validateNumberV <span class="dv">42</span> <span class="op">&lt;*&gt;</span> validateNumberV <span class="dv">10</span> <span class="op">&lt;*&gt;</span> validateNumberV <span class="dv">50</span></a>
  <a class="sourceLine" id="cb19-16" title="16"><span class="dt">Failure</span> [<span class="dt">NumberNotAllowed</span>, <span class="dt">NumberTooSmall</span>]</a>
  <a class="sourceLine" id="cb19-17" title="17"></a>
  <a class="sourceLine" id="cb19-18" title="18"><span class="op">&gt;</span> <span class="op">:</span>{</a>
  <a class="sourceLine" id="cb19-19" title="19">      (<span class="op">+</span>) <span class="op">&lt;$&gt;</span> validateNumberV <span class="dv">42</span> <span class="op">&lt;*&gt;</span> validateNumberV <span class="dv">10</span></a>
  <a class="sourceLine" id="cb19-20" title="20">  <span class="op">&lt;|&gt;</span> (<span class="op">+</span>) <span class="op">&lt;$&gt;</span> validateNumberV <span class="dv">41</span> <span class="op">&lt;*&gt;</span> validateNumberV <span class="dv">31</span></a>
  <a class="sourceLine" id="cb19-21" title="21">  <span class="op">:</span>}</a>
  <a class="sourceLine" id="cb19-22" title="22"><span class="dt">Success</span> <span class="dv">72</span></a></code></pre></div>
  <p>These examples are mostly silly, mainly for showing the mechanics, but the <code>Validation</code> type is incredibly useful for giving thorough feedback to users when validating complex input. It often comes up in the context of web form validation (if working in a language like Purescript) where you want to check that all fields have sensible values, and if not, reporting what fields aren’t valid and why.</p>
  <p>I’ll list two implementations below:</p>
  <ul>
  <li>Haskell <a href="https://hackage.haskell.org/package/validation">validation</a></li>
  <li>Purescript <a href="https://pursuit.purescript.org/packages/purescript-validation/3.0.0">validation</a></li>
  </ul>
  <h2 id="conclusion">Conclusion</h2>
  <p>As this post has already gotten kind of long, I’ll leave several other amazing bits out of it for this time. The most useful bits I’m not bringing up, are that applicatives and alternatives can be used for:</p>
  <ul>
  <li>parsing
  <ul>
  <li>text and bytestring formats: <a href="https://hackage.haskell.org/package/attoparsec">attoparsec</a></li>
  <li>complex text parsing, say, for programming languages: <a href="https://hackage.haskell.org/package/megaparsec">megaparsec</a></li>
  <li>JSON: <a href="https://hackage.haskell.org/package/aeson">aeson</a></li>
  <li>database fields: <a href="https://hackage.haskell.org/package/postgresql-simple">postgresql-simple</a></li>
  <li>command line options: <a href="https://github.com/pcapriotti/optparse-applicative">optparse-applicative</a></li>
  <li>HTTP query params: <a href="https://gitlab.com/queertypes/wai-request-spec">wai-request-spec</a>
  <ul>
  <li>Though the <a href="https://haskell-servant.readthedocs.io/en/stable/tutorial/index.html">servant</a> package does this even more nicely with fancier techniques</li>
  </ul></li>
  </ul></li>
  <li>sequencing parallel code: <a href="https://hackage.haskell.org/package/parallel">parallel</a></li>
  <li>sequencing concurrent code: <a href="https://hackage.haskell.org/package/async-2.1.1.1/docs/Control-Concurrent-Async.html">async</a></li>
  <li>even more</li>
  </ul>
  <p>As a small example of applicatives and alternatives for parsing, here’s a bit of code from one of my projects that uses the attoparsec library noted above:</p>
  <div class="sourceCode" id="cb20"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb20-1" title="1"><span class="co">-- #SELECTABLE:YES</span></a>
  <a class="sourceLine" id="cb20-2" title="2"><span class="ot">parseSelectable ::</span> <span class="dt">Parser</span> <span class="dt">Selectable</span></a>
  <a class="sourceLine" id="cb20-3" title="3">parseSelectable <span class="ot">=</span></a>
  <a class="sourceLine" id="cb20-4" title="4">  <span class="dt">Selectable</span> <span class="op">&lt;$&gt;</span> (startTag <span class="op">*&gt;</span> parseSelect)</a>
  <a class="sourceLine" id="cb20-5" title="5">  <span class="kw">where</span><span class="ot"> startTag ::</span> <span class="dt">Parser</span> <span class="dt">Char</span></a>
  <a class="sourceLine" id="cb20-6" title="6">        startTag <span class="ot">=</span> pound <span class="op">*&gt;</span> stringCI (tagBytes <span class="dt">TNSelectable</span>) <span class="op">*&gt;</span> char <span class="ch">&#39;:&#39;</span></a>
  <a class="sourceLine" id="cb20-7" title="7"></a>
  <a class="sourceLine" id="cb20-8" title="8"><span class="ot">        parseSelect ::</span> <span class="dt">Parser</span> <span class="dt">Bool</span></a>
  <a class="sourceLine" id="cb20-9" title="9">        parseSelect <span class="ot">=</span></a>
  <a class="sourceLine" id="cb20-10" title="10">          (stringCI <span class="st">&quot;YES&quot;</span> <span class="op">$&gt;</span> <span class="dt">True</span>) <span class="op">&lt;|&gt;</span> (stringCI <span class="st">&quot;NO&quot;</span> <span class="op">$&gt;</span> <span class="dt">False</span>)</a></code></pre></div>
  <p>The goal is to parse a string that looks like <code>"#SELECTABLE:YES"</code> or <code>"#SELECTABLE:NO"</code>, and having done so, wrap it in a type that differentiates it from a plain boolean type.</p>
  <p><code>*&gt;</code> is an applicative operator that performs parse sequencing, but discards the left-hand side. <code>$&gt;</code> is a functor operator that performs the effect to the left, and if successful, lifts the right-hand side into the current context.</p>
  <p><code>startTag</code> matches the <code>"#SELECTABLE:</code> portion, and <code>parseSelect</code> looks for either a <code>"yes"</code> or a <code>"no"</code> in a case-insensitive fashion.</p>
  <p>Probably the most interesting bit here is that I had the ability to specify alternative, valid parses using <code>&lt;|&gt;</code> in <code>parseSelect</code>. This comes in handy for parsing any sort of enumerated type, say, the error return code from a web API you’re consuming.</p>
  <h2 id="concluding-for-real">Concluding For Real</h2>
  <p>Anyway, that’s all! I hope this helped clarify what applicatives and alternatives are, and how you might use them. Even though this post is Haskell-centric, the core ideas can be expressed in other programming languages. Ultimately, applicatives and alternatives are just a safe, broad interface for composing computations with context and assigning rules to those contexts.</p>
  <h2 id="updatesedits">Updates/Edits</h2>
  <ul>
  <li>May 9, 2017: <a href="https://gitlab.com/queertypes/read-review-refactor/commit/e00cd8e6b364b02f80510b3845bf74577a247c84">changes</a></li>
  </ul>]]></summary>
</entry>
<entry>
  <title>Reading Types (in progress!!)</title>
  <link href="https://queertypes.com/posts/58-reading-types.html" />
  <id>https://queertypes.com/posts/58-reading-types.html</id>
  <published>2017-04-21T00:00:00Z</published>
  <updated>2017-04-21T00:00:00Z</updated>
  <summary type="html"><![CDATA[<p>This post is about reading type signatures. It assumes you’ve never written Haskell (or a similar language) before. If you’ve written in such a language before, it’s likely this material will be familiar to you. This post is not for you.</p>
  <p>It doesn’t assume you’ve used a typed programming language before. I’ll define things carefully and take care with jargon. I’ll give priority to concrete examples, so that you can use this to build things. It’s important to me that this can be a post you can come back to if you want to get more comfortable with typed programming and have the time and energy for it.</p>
  <p>Types and type systems are powerful tools, and I want to make them a little easier to use. They’re great for communication, for expression, for correctness, for program maintenance, and peformance. The problem is, how do we use them well?</p>
  <p>All examples will be given using the Haskell programming language. It’s pretty easy to install nowadays, and the syntax is pretty clean for talking about type system things.</p>
  <h2 id="what-are-types">What are types?</h2>
  <p>Types are, a lot of things. If I were to give an overarching definition, they’re at the very least, a way to describe the shapes of data and programs.</p>
  <p>There’s a lot of context, often times left unsaid when folks talk about “types”, unqualified.</p>
  <p>Some people talk about types and think of <strong>storage</strong> types. These describe how much space certain pieces of program take up in memory, and possibly, how that data is interpreted. Think: integers, longs, unsigned integers, floats, arrays, C.</p>
  <p>Some people talk about types, and object-oriented design comes to mind. These sorts of types are one system for classifying data that mixes state, scope, and behavior together, and talks about types in terms of hierarchies of capabilities. These are known as <strong>sub-type</strong> systems. Think: Java, C Sharp, classes. This post is not about these systems.</p>
  <p>Others still, talk about functional programming, and algebraic types, and a bunch of other things, sometimes of a very mathematical nature. There’s talk of how to compose things, how functions interact, and about side-effects and types to describe them. Think: Haskell, Elm, monads, and the like. This post is about these sorts of type systems, and how to understand and think about them. Let’s call them, <strong>algebraic data</strong> types.</p>
  <p>Further still, some folks talk about types for proving whole programs adhere to specs. At this point, we’ve wandered into <strong>dependent</strong> types, provers, and type theory. While these systems can be extremely expressive, they’re outside the scope of this post. We’re starting small, to be able to read and make sense of things we can use right now.</p>
  <p>There’s also the distinction between <strong>dynamic</strong> and <strong>static</strong> types. When I talk about dynamic type systems here, I’m talking about Python, Ruby, Racket, Javascript, Erlang, and others like them, who check the types of data at run-time. Some are more strict than others. Static type systems run all their checks at compile time, before a program is ever run. That’s what languages like Java, C, C++, Rust, Haskell, and OCaml do. Like the dynamic case, there’s sharp differences about what sorts of things each of these languages can check, some strictly more expressive than others.</p>
  <p>Speaking of expressivity - I use this term in a strict sense, which I’ll make more clear with the rest of this post, this post about reading and understanding types. An expressive type system is one that let’s you say more about your data, without much hassle. These are the best sorts of type systems for communicating intent about your programs and data.</p>
  <p>That’s “types”. “Types”, as it turns out, can mean a lot of different things to different people.</p>
  <h2 id="type-signatures-what-can-it-do">Type signatures: what can it do?</h2>
  <p>Given all the definitions up to this point, let’s talk about something more concrete: type signatures.</p>
  <p>A type signature is a description of a function:</p>
  <ul>
  <li>what it’s name is</li>
  <li>what parameters it takes and their shapes</li>
  <li>the shape of what it returns</li>
  </ul>
  <p>A lot of information can be packed into a small space. Type signatures are the first place to look to try to understand a function.</p>
  <p>Type signatures are the main thing we’ll be working to understand how to read.</p>
  <p>Here’s a type signature now:</p>
  <div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb1-1" title="1"><span class="ot">add ::</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">Int</span></a></code></pre></div>
  <p><code>add</code> is a function that takes two <code>Int</code> arguments and returns an <code>Int</code>. More generally:</p>
  <div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb2-1" title="1"><span class="ot">  someFunction ::</span>  <span class="co">-- function name</span></a>
  <a class="sourceLine" id="cb2-2" title="2">    a <span class="ot">-&gt;</span> b <span class="ot">-&gt;</span> c    <span class="co">-- function parameters/arguments</span></a>
  <a class="sourceLine" id="cb2-3" title="3">    <span class="ot">-&gt;</span> d           <span class="co">-- return type</span></a></code></pre></div>
  <p>Of course, different languages have different ways of expressing types signatures. Here’s <code>add</code> in C:</p>
  <div class="sourceCode" id="cb3"><pre class="sourceCode c"><code class="sourceCode c"><a class="sourceLine" id="cb3-1" title="1"><span class="dt">int</span> add(<span class="dt">int</span> x, <span class="dt">int</span> y);</a></code></pre></div>
  <p>Here’s <code>add</code> in Scala:</p>
  <div class="sourceCode" id="cb4"><pre class="sourceCode scala"><code class="sourceCode scala"><a class="sourceLine" id="cb4-1" title="1"><span class="kw">def</span> <span class="fu">add</span>(x: Int, y: Int): Int</a></code></pre></div>
  <p>We’ll look at far more interesting type signatures later as we build on what we know.</p>
  <h2 id="simple-and-primitive-types">Simple and primitive types</h2>
  <p>Primitive types are those provided by a language that are closest to what might be stored on a machine. Things like:</p>
  <ul>
  <li>integral types: 1, -1, 10</li>
  <li>decimal types, doubles and floats: 1.0, 2.31212, 3.1415926</li>
  <li>text: “cat”, “girls”, “are”, “great”, “actually”, “”</li>
  <li>bytes: [0x01, 0x05, 0x19]</li>
  <li>bool: true, false</li>
  <li>unit: () – literally the type that only has one value, ()</li>
  </ul>
  <p>In Haskell, these types are:</p>
  <ul>
  <li>Int</li>
  <li>Integer (to represent numbers that are too big to fit in a register)</li>
  <li>Float</li>
  <li>Double</li>
  <li>String (sort of - it’s a doubly-linked list of characters)</li>
  <li>Text</li>
  <li>ByteString</li>
  <li>Bool</li>
  <li>()</li>
  </ul>
  <h2 id="function-types-passing-around-functions">Function types: passing around functions</h2>
  <p>There are also types that can express functions, specifically, functions as arguments to other functions.</p>
  <p>Let’s look at an example:</p>
  <div class="sourceCode" id="cb5"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb5-1" title="1"><span class="ot">addAndShow ::</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> (<span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">String</span>) <span class="ot">-&gt;</span> <span class="dt">String</span></a>
  <a class="sourceLine" id="cb5-2" title="2"><span class="fu">showInt</span><span class="ot"> ::</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">String</span></a></code></pre></div>
  <p>This is almost <code>add</code>, but with more power. It now takes a function paramter that let’s us turn an integer into a string. Looking immediately below, we have such a function handy. Let’s go even more concrete for a moment. Here’s the <strong>definition</strong> of <code>addAndShow</code>:</p>
  <div class="sourceCode" id="cb6"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb6-1" title="1">addAndShow x y showFn <span class="ot">=</span> showFn (x <span class="op">+</span> y)</a></code></pre></div>
  <p>That’s all there is to that one. Below, I’ll show what a definition of <code>showInt</code> might look like. A few notes before I get to it:</p>
  <ul>
  <li>such a function is already provided by the standard library: <code>show</code></li>
  <li>it introduces a lot of new syntax</li>
  <li>the implementation is recursive</li>
  <li>it’s not necessary to understand what function types mean</li>
  </ul>
  <p>With that said, for the curious:</p>
  <div class="sourceCode" id="cb7"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb7-1" title="1"><span class="fu">showInt</span><span class="ot"> ::</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">String</span></a>
  <a class="sourceLine" id="cb7-2" title="2"><span class="fu">showInt</span> n <span class="ot">=</span></a>
  <a class="sourceLine" id="cb7-3" title="3">    <span class="kw">let</span> n&#39; <span class="ot">=</span> <span class="fu">abs</span> n <span class="kw">in</span></a>
  <a class="sourceLine" id="cb7-4" title="4">    <span class="kw">let</span> ret <span class="ot">=</span> go (n&#39; <span class="ot">`div`</span> <span class="dv">10</span>) (n&#39; <span class="ot">`mod`</span> <span class="dv">10</span>) [] <span class="kw">in</span></a>
  <a class="sourceLine" id="cb7-5" title="5">    <span class="kw">if</span> n <span class="op">&lt;</span> <span class="dv">0</span> <span class="kw">then</span> <span class="ch">&#39;-&#39;</span> <span class="op">:</span> ret <span class="kw">else</span> ret</a>
  <a class="sourceLine" id="cb7-6" title="6"></a>
  <a class="sourceLine" id="cb7-7" title="7">  <span class="kw">where</span><span class="ot"> go ::</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">String</span> <span class="ot">-&gt;</span> <span class="dt">String</span></a>
  <a class="sourceLine" id="cb7-8" title="8">        go leftDigits rightDigit acc</a>
  <a class="sourceLine" id="cb7-9" title="9">          <span class="op">|</span> leftDigits <span class="op">==</span> <span class="dv">0</span> <span class="op">&amp;&amp;</span> rightDigit <span class="op">==</span> <span class="dv">0</span> <span class="ot">=</span> acc</a>
  <a class="sourceLine" id="cb7-10" title="10">          <span class="op">|</span> <span class="fu">otherwise</span> <span class="ot">=</span> go (leftDigits <span class="ot">`div`</span> <span class="dv">10</span>) (leftDigits <span class="ot">`mod`</span> <span class="dv">10</span>) (toChar rightDigit <span class="op">:</span> acc)</a>
  <a class="sourceLine" id="cb7-11" title="11"></a>
  <a class="sourceLine" id="cb7-12" title="12"><span class="ot">        toChar ::</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">Char</span></a>
  <a class="sourceLine" id="cb7-13" title="13">        toChar x <span class="ot">=</span> <span class="kw">case</span> x <span class="kw">of</span></a>
  <a class="sourceLine" id="cb7-14" title="14">          <span class="dv">0</span> <span class="ot">-&gt;</span> <span class="ch">&#39;0&#39;</span></a>
  <a class="sourceLine" id="cb7-15" title="15">          <span class="dv">1</span> <span class="ot">-&gt;</span> <span class="ch">&#39;1&#39;</span></a>
  <a class="sourceLine" id="cb7-16" title="16">          <span class="dv">2</span> <span class="ot">-&gt;</span> <span class="ch">&#39;2&#39;</span></a>
  <a class="sourceLine" id="cb7-17" title="17">          <span class="dv">3</span> <span class="ot">-&gt;</span> <span class="ch">&#39;3&#39;</span></a>
  <a class="sourceLine" id="cb7-18" title="18">          <span class="dv">4</span> <span class="ot">-&gt;</span> <span class="ch">&#39;4&#39;</span></a>
  <a class="sourceLine" id="cb7-19" title="19">          <span class="dv">5</span> <span class="ot">-&gt;</span> <span class="ch">&#39;5&#39;</span></a>
  <a class="sourceLine" id="cb7-20" title="20">          <span class="dv">6</span> <span class="ot">-&gt;</span> <span class="ch">&#39;6&#39;</span></a>
  <a class="sourceLine" id="cb7-21" title="21">          <span class="dv">7</span> <span class="ot">-&gt;</span> <span class="ch">&#39;7&#39;</span></a>
  <a class="sourceLine" id="cb7-22" title="22">          <span class="dv">8</span> <span class="ot">-&gt;</span> <span class="ch">&#39;8&#39;</span></a>
  <a class="sourceLine" id="cb7-23" title="23">          <span class="dv">9</span> <span class="ot">-&gt;</span> <span class="ch">&#39;9&#39;</span></a>
  <a class="sourceLine" id="cb7-24" title="24">          _ <span class="ot">-&gt;</span> <span class="fu">error</span> <span class="st">&quot;out of bounds - toChar, Int not between 0 - 9&quot;</span></a></code></pre></div>
  <p>It is important to note also that at this point, we don’t have enough tools to give really precise types. Notably, let’s look at another function that fits <code>(Int -&gt; String)</code>, but doesn’t do what we’d hope:</p>
  <div class="sourceCode" id="cb8"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb8-1" title="1"><span class="ot">showInt2 ::</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">String</span></a>
  <a class="sourceLine" id="cb8-2" title="2">showInt2 _ <span class="ot">=</span> <span class="st">&quot;2&quot;</span></a></code></pre></div>
  <p>This definition <strong>does</strong> satisfy the given type signature. Sure. But! This warning is here specifically to emphasize the limits of what we can express at this point. We know that:</p>
  <ul>
  <li>we have one and only one argument: an <code>Int</code></li>
  <li>it returns a <code>String</code> - never anything else</li>
  <li>no side-effects will be performed (more on that later)</li>
  </ul>
  <p>Reading type signatures and designing types is every bit as much about what functions can do as it is about what functions <strong>cannot</strong> do. Keep this in mind as we proceed.</p>
  <p>Let’s expand what we can express with types in the next section.</p>
  <h2 id="product-types-records-structs-and-more">Product types: records, structs, and more</h2>
  <h2 id="sum-types-tagged-variants-enumerations-and-more">Sum types: tagged variants, enumerations, and more</h2>
  <h2 id="composing-types-sums-and-products-together">Composing types: sums and products together</h2>
  <h2 id="type-inference-the-compiler-can-work-with-you">Type inference: the compiler can work with you</h2>
  <h2 id="polymorphism-types-for-more-general-functions">Polymorphism: types for more general functions</h2>
  <h2 id="an-example-types-for-optional-data">An example: types for optional data</h2>
  <h2 id="another-example-types-for-tracking-success-and-failure">Another example: types for tracking success and failure</h2>
  <h2 id="adts-abstract-and-algebraic">ADTs: abstract and algebraic</h2>
  <h2 id="recursive-types">Recursive types</h2>
  <h2 id="another-kind-of-polymorphism-traits-and-typeclasses">Another kind of polymorphism: traits and typeclasses</h2>
  <h2 id="types-for-tracking-effects">Types for tracking effects</h2>
  <h2 id="types-of-a-higher-kind-or-type-signatures-for-types">Types of a higher kind, or, type signatures for types</h2>
  <h2 id="writing-types-to-be-read---communicating-effectively">Writing types to be read - communicating effectively</h2>
  <h2 id="where-to-next">Where to next?</h2>]]></summary>
</entry>
<entry>
  <title>Smaller Types, Happier Programs</title>
  <link href="https://queertypes.com/posts/57-small-types.html" />
  <id>https://queertypes.com/posts/57-small-types.html</id>
  <published>2017-01-06T00:00:00Z</published>
  <updated>2017-01-06T00:00:00Z</updated>
  <summary type="html"><![CDATA[<p>This post will be all about a simple technique for making programs a little easier to maintain. It’s all about the size of types.</p>
  <p>Most code examples will be given using Haskell. When an example uses another programming language, I’ll call that out before showing the example.</p>
  <p>The advice is focused on languages that have a compile-time type-check step, but can be informally adopted to make maintenance of dynamically-typed programs (Javascript, Python, Ruby, etc.) a little easier.</p>
  <h2 id="the-size-of-types">The Size of Types</h2>
  <p>When I talk about the size of types, I’m talking about how many values a given type can represent. A few examples:</p>
  <ul>
  <li>boolean: true or false, a type of size 2</li>
  <li>32-bit integer: 2^32 possible values, a type of size 2^32</li>
  <li>a string, encoded as a byte array: 2^8 * n, n being the length of the string
  <ul>
  <li>a type of infinite size, for our calculations</li>
  </ul></li>
  </ul>
  <p>These examples are all primitive types. Fortunately, most programming languages offer at least some form of abstraction over these primitive types. Leveraging those abstractions will allow us to craft types that are smaller than the primitive types, and can therefore more accurately describe our domain of interest.</p>
  <p>In particular, I want to talk about algebraic data types. They’re called as such because in a sense I’ll make clear soon, they allow us to add to and multiply a type space to meet our needs as precisely as possible.</p>
  <p>For example, a sum type <code>Color</code>:</p>
  <div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb1-1" title="1"><span class="kw">data</span> <span class="dt">Color</span> <span class="ot">=</span> <span class="dt">Red</span> <span class="op">|</span> <span class="dt">Green</span> <span class="op">|</span> <span class="dt">Blue</span></a></code></pre></div>
  <p>This type has 3 values, or variants: <code>Red</code>, <code>Green</code>, <code>Blue</code>. It’s a sum type, because each variant adds 1 one more value to the size of the type. With that, <code>Color</code> is a type of size 3. This is a reliable fact as long as implicit conversion isn’t something to watch for in your programming language of choice. More on that later.</p>
  <p>In Scala, this might look like:</p>
  <div class="sourceCode" id="cb2"><pre class="sourceCode scala"><code class="sourceCode scala"><a class="sourceLine" id="cb2-1" title="1"><span class="kw">sealed</span> <span class="kw">trait</span> Color</a>
  <a class="sourceLine" id="cb2-2" title="2"><span class="kw">final</span> <span class="kw">case</span> <span class="kw">object</span> Red <span class="kw">extends</span> Color</a>
  <a class="sourceLine" id="cb2-3" title="3"><span class="kw">final</span> <span class="kw">case</span> <span class="kw">object</span> Green <span class="kw">extends</span> Color</a>
  <a class="sourceLine" id="cb2-4" title="4"><span class="kw">final</span> <span class="kw">case</span> <span class="kw">object</span> Blue <span class="kw">extends</span> Color</a></code></pre></div>
  <p>Most mainstream languages have basic support for sum types, via enumerations. However, sum types become particularly interesting when used with product types. A product type might look something like:</p>
  <div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb3-1" title="1"><span class="kw">data</span> <span class="dt">AreaColor</span> <span class="ot">=</span> <span class="dt">AreaColor</span> {<span class="ot"> inverted ::</span> <span class="dt">Bool</span>,<span class="ot"> color ::</span> <span class="dt">Color</span> }</a>
  <a class="sourceLine" id="cb3-2" title="2"><span class="co">-- basic shape: AreaColor Bool Color</span></a></code></pre></div>
  <p>An <code>AreaColor</code> has 2 * 3 possible values. Let’s enumerate them.</p>
  <div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb4-1" title="1"><span class="op">&gt;</span> <span class="kw">let</span> ct0 <span class="ot">=</span> <span class="dt">AreaColor</span> <span class="dt">True</span> <span class="dt">Red</span></a>
  <a class="sourceLine" id="cb4-2" title="2"><span class="op">&gt;</span> <span class="kw">let</span> ct1 <span class="ot">=</span> <span class="dt">AreaColor</span> <span class="dt">True</span> <span class="dt">Green</span></a>
  <a class="sourceLine" id="cb4-3" title="3"><span class="op">&gt;</span> <span class="kw">let</span> ct2 <span class="ot">=</span> <span class="dt">AreaColor</span> <span class="dt">True</span> <span class="dt">Blue</span></a>
  <a class="sourceLine" id="cb4-4" title="4"><span class="op">&gt;</span> <span class="kw">let</span> cf0 <span class="ot">=</span> <span class="dt">AreaColor</span> <span class="dt">False</span> <span class="dt">Red</span></a>
  <a class="sourceLine" id="cb4-5" title="5"><span class="op">&gt;</span> <span class="kw">let</span> cf1 <span class="ot">=</span> <span class="dt">AreaColor</span> <span class="dt">False</span> <span class="dt">Green</span></a>
  <a class="sourceLine" id="cb4-6" title="6"><span class="op">&gt;</span> <span class="kw">let</span> cf2 <span class="ot">=</span> <span class="dt">AreaColor</span> <span class="dt">False</span> <span class="dt">Blue</span></a></code></pre></div>
  <p>Most mainstream languages have excellent support for product types. You may have heard them referred to by the names, “struct”, “record”, or the like. The components of the product type multiply together to give the new type size.</p>
  <p>Here’s where most mainstream programming languages falter: being able to combine sum and product types conveniently.</p>
  <p>Here’s an example of the two used together:</p>
  <div class="sourceCode" id="cb5"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb5-1" title="1"><span class="kw">data</span> <span class="dt">PaletteCommand</span></a>
  <a class="sourceLine" id="cb5-2" title="2">  <span class="ot">=</span> <span class="dt">ClearPalette</span> <span class="co">-- 1</span></a>
  <a class="sourceLine" id="cb5-3" title="3">  <span class="op">|</span> <span class="dt">AddColor</span> <span class="dt">Color</span> <span class="co">-- 3</span></a>
  <a class="sourceLine" id="cb5-4" title="4">  <span class="op">|</span> <span class="dt">SwapColor</span> <span class="dt">Color</span> <span class="dt">Color</span> <span class="co">-- 3 * 3</span></a>
  <a class="sourceLine" id="cb5-5" title="5">  <span class="op">|</span> <span class="dt">InvertPalette</span> <span class="co">-- 1</span></a>
  <a class="sourceLine" id="cb5-6" title="6">  <span class="co">-- total size: 1 + 3 + 9 + 1 = 14</span></a></code></pre></div>
  <p><code>PaletteCommand</code> is a toy example of the sorts of concepts that can be expressed when you have algebraic data types.</p>
  <p>It’s very powerful being able to conveniently combine sum and product types. So much so, that without this ability, it becomes very easy for the complexity of a program to multiply. This is what I meant when I said 2 years ago that “In languages lacking sum types, the complexity of a program can only multiply”. <a href="https://twitter.com/queertypes/status/588020984856367105">ref</a></p>
  <p>Now we have just enough of the basics of calculating the size of types so that we can talk about dealing with how to resolve design problems.</p>
  <h2 id="stringly-typed-and-work-arounds">Stringly-Typed and Work Arounds</h2>
  <p>One of the most common design problems that one might encounter when working with existing code is that everything will be expressed and encoded as a string, e.g., a so-called stringly-typed code base. This can happen in any programming language. After all, strings are nearly always provided as a primitive type. It might come up in the context of retrieving data from a database to affect control flows, as messages from a queuing system, as commands from a network-connected client, or even as commands from a command line interface.</p>
  <p>Below, I’ll show some of the trouble with relying on strings to handle program control flow.</p>
  <p>Let’s write a function, <code>colorToInt</code>, that given a string, returns a numeric representation of that color:</p>
  <div class="sourceCode" id="cb6"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb6-1" title="1"><span class="ot">colorToInt ::</span> <span class="dt">String</span> <span class="ot">-&gt;</span> <span class="dt">Int</span></a>
  <a class="sourceLine" id="cb6-2" title="2">colorToInt c <span class="ot">=</span></a>
  <a class="sourceLine" id="cb6-3" title="3">  <span class="kw">case</span> c <span class="kw">of</span></a>
  <a class="sourceLine" id="cb6-4" title="4">    <span class="st">&quot;red&quot;</span> <span class="ot">-&gt;</span> <span class="dv">0</span></a>
  <a class="sourceLine" id="cb6-5" title="5">    <span class="st">&quot;green&quot;</span> <span class="ot">-&gt;</span> <span class="dv">1</span></a>
  <a class="sourceLine" id="cb6-6" title="6">    <span class="st">&quot;blue&quot;</span> <span class="ot">-&gt;</span> <span class="dv">2</span></a></code></pre></div>
  <p>And an example of where this goes wrong:</p>
  <div class="sourceCode" id="cb7"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb7-1" title="1">main <span class="ot">=</span> <span class="fu">print</span> (colorToInt <span class="st">&quot;Red&quot;</span>)</a></code></pre></div>
  <p>This will raise an exception. Our <code>colorToInt</code> function doesn’t know how to handle <code>"Red"</code>. A first-pass at fixing this might be something like:</p>
  <div class="sourceCode" id="cb8"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb8-1" title="1"><span class="co">-- toLower: String -&gt; String, makes it lower-case</span></a>
  <a class="sourceLine" id="cb8-2" title="2"></a>
  <a class="sourceLine" id="cb8-3" title="3"><span class="ot">colorToInt2 ::</span> <span class="dt">String</span> <span class="ot">-&gt;</span> <span class="dt">Int</span></a>
  <a class="sourceLine" id="cb8-4" title="4">colorToInt2 c <span class="ot">=</span></a>
  <a class="sourceLine" id="cb8-5" title="5">  <span class="kw">case</span> <span class="fu">toLower</span> c <span class="kw">of</span></a>
  <a class="sourceLine" id="cb8-6" title="6">    <span class="st">&quot;red&quot;</span> <span class="ot">-&gt;</span> <span class="dv">0</span></a>
  <a class="sourceLine" id="cb8-7" title="7">    <span class="st">&quot;green&quot;</span> <span class="ot">-&gt;</span> <span class="dv">1</span></a>
  <a class="sourceLine" id="cb8-8" title="8">    <span class="st">&quot;blue&quot;</span> <span class="ot">-&gt;</span> <span class="dv">2</span></a>
  <a class="sourceLine" id="cb8-9" title="9">    _ <span class="ot">-&gt;</span> <span class="dv">3</span></a></code></pre></div>
  <p>Now, our former example works alright. But what about:</p>
  <div class="sourceCode" id="cb9"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb9-1" title="1">main <span class="ot">=</span> <span class="kw">do</span></a>
  <a class="sourceLine" id="cb9-2" title="2">  <span class="fu">print</span> (colorToInt2 <span class="st">&quot;Reed&quot;</span>)</a>
  <a class="sourceLine" id="cb9-3" title="3">  <span class="fu">print</span> (colorToInt2 <span class="st">&quot;Bleu&quot;</span>)</a>
  <a class="sourceLine" id="cb9-4" title="4">  <span class="fu">print</span> (colorToInt2 <span class="st">&quot;Gren&quot;</span>)</a></code></pre></div>
  <p>These all return <code>3</code>, which amounts to an unknown color value. A third and common pass might throw an exception:</p>
  <div class="sourceCode" id="cb10"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb10-1" title="1"><span class="ot">colorToInt3 ::</span> <span class="dt">String</span> <span class="ot">-&gt;</span> <span class="dt">Int</span></a>
  <a class="sourceLine" id="cb10-2" title="2">colorToInt3 c <span class="ot">=</span></a>
  <a class="sourceLine" id="cb10-3" title="3">  <span class="kw">case</span> <span class="fu">toLower</span> c <span class="kw">of</span></a>
  <a class="sourceLine" id="cb10-4" title="4">    <span class="st">&quot;red&quot;</span> <span class="ot">-&gt;</span> <span class="dv">0</span></a>
  <a class="sourceLine" id="cb10-5" title="5">    <span class="st">&quot;green&quot;</span> <span class="ot">-&gt;</span> <span class="dv">1</span></a>
  <a class="sourceLine" id="cb10-6" title="6">    <span class="st">&quot;blue&quot;</span> <span class="ot">-&gt;</span> <span class="dv">2</span></a>
  <a class="sourceLine" id="cb10-7" title="7">    _ <span class="ot">-&gt;</span> <span class="fu">error</span> (<span class="st">&quot;unknown color &quot;</span> <span class="op">++</span> c)</a></code></pre></div>
  <p>This will catch all typos. But - how does it fare in the face of program updates?</p>
  <p>Let’s assume we have a <code>morbidify</code> function defined deep in our system. It’s worked for a long time, and no one’s thought about it for months because there’s been other priorities in mind.</p>
  <div class="sourceCode" id="cb11"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb11-1" title="1"><span class="ot">morbidify ::</span> <span class="dt">String</span> <span class="ot">-&gt;</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">DbConnection</span> <span class="ot">-&gt;</span> <span class="dt">IO</span> ()</a>
  <a class="sourceLine" id="cb11-2" title="2">morbidify c userId databaseConn <span class="ot">=</span> <span class="kw">do</span></a>
  <a class="sourceLine" id="cb11-3" title="3">  <span class="kw">let</span> colorValue <span class="ot">=</span> colorToInt3 c</a>
  <a class="sourceLine" id="cb11-4" title="4">  setFaveUserColor c userId databaseConn</a></code></pre></div>
  <p>New requirements come in: Red in no longer supported and now we need to add Cyan.</p>
  <p>We remember to update <code>colorToInt3</code>:</p>
  <div class="sourceCode" id="cb12"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb12-1" title="1"><span class="ot">colorToInt3 ::</span> <span class="dt">String</span> <span class="ot">-&gt;</span> <span class="dt">Int</span></a>
  <a class="sourceLine" id="cb12-2" title="2">colorToInt3 c <span class="ot">=</span></a>
  <a class="sourceLine" id="cb12-3" title="3">  <span class="kw">case</span> <span class="fu">toLower</span> c <span class="kw">of</span></a>
  <a class="sourceLine" id="cb12-4" title="4">    <span class="st">&quot;cyan&quot;</span> <span class="ot">-&gt;</span> <span class="dv">0</span></a>
  <a class="sourceLine" id="cb12-5" title="5">    <span class="st">&quot;green&quot;</span> <span class="ot">-&gt;</span> <span class="dv">1</span></a>
  <a class="sourceLine" id="cb12-6" title="6">    <span class="st">&quot;blue&quot;</span> <span class="ot">-&gt;</span> <span class="dv">2</span></a>
  <a class="sourceLine" id="cb12-7" title="7">    _ <span class="ot">-&gt;</span> <span class="fu">error</span> (<span class="st">&quot;unknown color &quot;</span> <span class="op">++</span> c)</a></code></pre></div>
  <p>But somewhere in the system, the following call occurs:</p>
  <div class="sourceCode" id="cb13"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb13-1" title="1"><span class="op">...</span></a>
  <a class="sourceLine" id="cb13-2" title="2">morbidify <span class="st">&quot;red&quot;</span> userId dbConn</a>
  <a class="sourceLine" id="cb13-3" title="3"><span class="op">...</span></a></code></pre></div>
  <p>Suddenly, this call will start to fail. Given the nature of the code, one that performs a side-effect on behalf of the system, it is very unlikely that tests will have been written for this. If we’re somewhat fortunate, someone thought to add some logging to <code>morbidify</code>, so at least the problem will be detected and can be explained. If we’re not so lucky…</p>
  <p>Well, the good news is we can do much better than this. Let’s talk about it next.</p>
  <h2 id="a-solution-smol-types">A Solution: Smol Types</h2>
  <p>Many of the problems we ran into the last section were because the size of string types is far too large. Let’s see how using sum types can make the situation better. First, we start with a few definitions:</p>
  <div class="sourceCode" id="cb14"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb14-1" title="1"><span class="kw">data</span> <span class="dt">Color</span> <span class="ot">=</span> <span class="dt">Red</span> <span class="op">|</span> <span class="dt">Green</span> <span class="op">|</span> <span class="dt">Blue</span></a>
  <a class="sourceLine" id="cb14-2" title="2"></a>
  <a class="sourceLine" id="cb14-3" title="3"><span class="ot">colorToInt4 ::</span> <span class="dt">Color</span> <span class="ot">-&gt;</span> <span class="dt">Int</span></a>
  <a class="sourceLine" id="cb14-4" title="4">colorToInt4 c <span class="ot">=</span></a>
  <a class="sourceLine" id="cb14-5" title="5">  <span class="kw">case</span> c <span class="kw">of</span></a>
  <a class="sourceLine" id="cb14-6" title="6">    <span class="dt">Red</span> <span class="ot">-&gt;</span> <span class="dv">0</span></a>
  <a class="sourceLine" id="cb14-7" title="7">    <span class="dt">Green</span> <span class="ot">-&gt;</span> <span class="dv">1</span></a>
  <a class="sourceLine" id="cb14-8" title="8">    <span class="dt">Blue</span> <span class="ot">-&gt;</span> <span class="dv">2</span></a></code></pre></div>
  <p>Let’s play around with this a little. First, our failing examples from before:</p>
  <div class="sourceCode" id="cb15"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb15-1" title="1">main <span class="ot">=</span> <span class="fu">print</span> (colorToInt4 <span class="st">&quot;Red&quot;</span>)</a></code></pre></div>
  <p>This gives us a compile-time error, noting that we expect a <code>Color</code> but got a <code>String</code></p>
  <div class="sourceCode" id="cb16"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb16-1" title="1">main <span class="ot">=</span> <span class="fu">print</span> (colorToInt4 <span class="dt">Reed</span>)</a></code></pre></div>
  <p>This gives us a compile-time error, noting that the value <code>Reed</code> is unknown.</p>
  <p>Let’s add support for <code>Cyan</code>:</p>
  <div class="sourceCode" id="cb17"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb17-1" title="1"><span class="kw">data</span> <span class="dt">Color</span> <span class="ot">=</span> <span class="dt">Red</span> <span class="op">|</span> <span class="dt">Green</span> <span class="op">|</span> <span class="dt">Blue</span> <span class="op">|</span> <span class="dt">Cyan</span></a></code></pre></div>
  <p>And a small test program:</p>
  <div class="sourceCode" id="cb18"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb18-1" title="1">main <span class="ot">=</span> <span class="fu">print</span> (colorToInt4 <span class="dt">Cyan</span>)</a></code></pre></div>
  <p>This gives us a compile-time <strong>warning</strong> that <code>Cyan</code> wasn’t handled in <code>colorToInt4</code>. This is a cool thing known as <strong>exhaustivity analysis</strong>, a super helpful thing a few languages containing full support for sum types can do.</p>
  <p>Let’s remove support for <code>Red</code> and <code>Cyan</code>:</p>
  <div class="sourceCode" id="cb19"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb19-1" title="1"><span class="kw">data</span> <span class="dt">Color</span> <span class="ot">=</span> <span class="dt">Green</span> <span class="op">|</span> <span class="dt">Blue</span></a>
  <a class="sourceLine" id="cb19-2" title="2"></a>
  <a class="sourceLine" id="cb19-3" title="3">main <span class="ot">=</span> <span class="fu">print</span> (colorToInt4 <span class="dt">Cyan</span>)</a></code></pre></div>
  <p>This will give us two compile-time errors:</p>
  <ul>
  <li>we mention <code>Cyan</code> in <code>main</code> when we’ve never defined it</li>
  <li>we mention <code>Red</code> in <code>colorToInt4</code> when we’ve not defined it</li>
  </ul>
  <p>This is the basic premise behind using small types as a tool for making programs easier to maintain. If the size of our types is as small as possible, while still fully expressing our domain, errors cannot be represented.</p>
  <p>Now let’s talk about caveats and things to watch for.</p>
  <h2 id="implicits-ruin-everything">Implicits Ruin Everything</h2>
  <p>Implicit conversion. Sometimes, we try as hard as we can, and the programming languages we use try to be as helpful as they can, and things Just Go Wrong.</p>
  <p>This is particularly notable in Java and Scala, where <code>toString</code> is defined over all types.</p>
  <p>The problem arises when interfacing with legacy code, where most of the types are still the infinitely-sized <code>String</code> instead of our constrained <code>Color</code>. We might pass in a <code>Color</code> to such a legacy function, and expect the compiler to flag us somehow, but it silently gets converted to a String. Where we expected to be able to reason about a type that is at most size 3, we’re back to the land of infinites.</p>
  <p>Another problematic conversion that is fairly common, is one of most primitive types to <code>Bool</code>. This will sometimes come up in control paths, where an if-statement is used to determine what’s to be done next, rather than a switch. The problem here is that we’re defined fine-grained control with our type, say <code>Color</code>, of size 3, but we’ve shrunken down to something that can only express 2 paths, <code>Bool</code>.</p>
  <p>Another common implicit conversion to watch for is that of enumeration to integer. This happens in C, and when using regular enumerations (rather than <code>enum class</code>) in C++.</p>
  <p>For languages (frequently object-oriented languages) that have a notion of value and reference types, and of nulls, beware of the possibility of <code>null</code> inhabiting your type. It is entirely possible in <code>Scala</code> and <code>Java</code> for <code>null</code> to be a member of our <code>Color</code> type, and still cause us trouble.</p>
  <p>There’s a few other forms of implicit conversion to worry about. It all depends on your domain and the programming language you’re working with. They’re beyond the scope of this post.</p>
  <p>In sum, watch out for:</p>
  <ul>
  <li>implicit string conversions, particularly in Java and Scala</li>
  <li>implicit conversion to Bool at control paths</li>
  <li>implicit conversion from enumeration to integer, particularly in C and C++</li>
  <li>nulls, because they break everything</li>
  </ul>
  <p>It’s worthwhile to keep in mind the danger of implicit conversions when relying on the size of types to design your programs.</p>
  <h2 id="in-practice">In Practice</h2>
  <p>To wrap up, I’ll leave you with a few practical tips.</p>
  <p>Make the types at the core of your program as small as possible.</p>
  <p>The best time to shrink a type is as early as possible. By this I mean, if you’re interfacing with the outside world, say, reading an environment variable or parsing some user input, that is the time to determine whether the input is valid and make a value of your small type or return some form of error. <code>Either</code> and <code>Maybe</code> types are lovely here.</p>
  <p>The best time to grow your types again is when outputing at the boundaries of your programs. Serialize them as needed, and send them off to the database as a String field or as a response to a client command as a part of a JSON blob.</p>
  <p>Sometimes, there’ll be data that really needs to be a <code>String</code> or an <code>Int</code>, and can’t be shrunk. This will be safe to do as long as no control paths depend on the shapes of the values contained in the <code>String</code> or <code>Int</code>. If you notice the flow of your program start to depend on what’s in those strings or integers, it might be time to consider shrinking to a more representative type.</p>
  <p>That’s the basic idea.</p>
  <h2 id="topics-not-covered">Topics Not Covered</h2>
  <p>We’ve covered a lot in this post. However, there’s still a whole lot more to explore when it comes to using the size of types to design programs. Here’s a selection of things I didn’t cover that might be of interest:</p>
  <ul>
  <li>size of polymorphic types:</li>
  </ul>
  <div class="sourceCode" id="cb20"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb20-1" title="1"><span class="ot">f ::</span> (b <span class="ot">-&gt;</span> c) <span class="ot">-&gt;</span> (a <span class="ot">-&gt;</span> b) <span class="ot">-&gt;</span> (a <span class="ot">-&gt;</span> c)</a></code></pre></div>
  <ul>
  <li>size of functions and their compositions</li>
  <li>size of subtypes (Scala, Java, etc.):</li>
  </ul>
  <div class="sourceCode" id="cb21"><pre class="sourceCode scala"><code class="sourceCode scala"><a class="sourceLine" id="cb21-1" title="1">ExtendedColor &lt;: Color</a></code></pre></div>
  <ul>
  <li>size of constrained types:</li>
  </ul>
  <div class="sourceCode" id="cb22"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb22-1" title="1">(<span class="dt">Show</span> a, <span class="dt">Random</span> a) <span class="ot">=&gt;</span> a <span class="ot">-&gt;</span> <span class="dt">Maybe</span> a</a></code></pre></div>
  <ul>
  <li>typed holes</li>
  <li>limits of exhaustivity analysis</li>
  <li>reasoning about the size of effectful functions:</li>
  </ul>
  <div class="sourceCode" id="cb23"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb23-1" title="1"><span class="ot">g ::</span> <span class="dt">Color</span> <span class="ot">-&gt;</span> <span class="dt">Url</span> <span class="ot">-&gt;</span> <span class="dt">Database</span> <span class="dt">Response</span></a></code></pre></div>
  <ul>
  <li>using the size of types for automated linting and static analysis</li>
  </ul>
  <p>Anyway, enjoy your small types. ❤</p>]]></summary>
</entry>
<entry>
  <title>Writing a Search DSL, Part 1</title>
  <link href="https://queertypes.com/posts/56-writing-a-search-dsl-1.html" />
  <id>https://queertypes.com/posts/56-writing-a-search-dsl-1.html</id>
  <published>2016-01-27T00:00:00Z</published>
  <updated>2016-01-27T00:00:00Z</updated>
  <summary type="html"><![CDATA[<p>Hi, readers!</p>
  <p>This will be a series of posts covering the development of a Domain Specific Language for search. I’ll dive into the design, testing, and implementation of parsing and evaluation. This will include both pure evaluation and compilation to express queries for an external data store (*SQL, Elastic Search, etc.).</p>
  <p>In part 1, I’ll cover why someone might want to use a DSL, as well as the design and implementation of the DSL core.</p>
  <h2 id="motivation">Motivation</h2>
  <p>Why would you want to implement a DSL?</p>
  <p>Focusing on search, you might want to expose search functionality to users. This might be a public-facing, HTTP API that allows users to look up other user details and posts, like Twitter’s <a href="https://dev.twitter.com/rest/public/search">Search API</a>. You might be part of a team at a large Cloud Services provider, putting together a search engine for users to review logs regarding infrastrcuture. This might an internal feature, an enhancement to existing developer tools to facilitate handling customer support requests.</p>
  <p>Search is a wide space!</p>
  <p>The main reasons to pursue a DSL as a solution are:</p>
  <ul>
  <li>You need a precise language for describing allowed operations</li>
  <li>The language must be expressive and relevant</li>
  <li>Extensions to the language should be possible to meet changing needs</li>
  <li>The system should be helpful, letting users know if something was incorrect</li>
  <li>The backend should be swappable</li>
  <li>The frontend should be swappable</li>
  <li>The system needs to be well understood, with no edge cases</li>
  </ul>
  <p>Essentially, DSL approaches to solving problems bring the capabilities of language design theory to your problem space.</p>
  <p>It’s notable that command line tools like <strong>grep</strong>, <strong>sed</strong>, <strong>awk</strong>, and <strong>find</strong> are themselves DSLs. A surprising number of problems can be approached with language design techniques in mind.</p>
  <p>While there are many benefits to such an approach, it is not without costs. The primary costs are knowledge and time - knowing enough about language design, parsing, and the domain to bring together the pieces, and having enough time to make it all work.</p>
  <p>This series of posts aims to address the knowledge part of the cost, by demonstrating what this process looks like from start to finish.</p>
  <p>Let’s get into it!</p>
  <h2 id="the-language">The Language</h2>
  <p>We’re going to start with a simple search language and build on it in later posts. Let’s first specify a few parts of the language, enough to perform some interesting searches:</p>
  <ul>
  <li>operations: <code>&lt;</code>, <code>&gt;</code>, <code>==</code>, <code>&gt;=</code>, <code>&lt;=</code></li>
  <li>types: <code>int</code>, <code>bool</code>, <code>str</code></li>
  <li>variables/identifiers</li>
  <li>basic type-operation constraints: <code>Ordering</code>, <code>Equality</code></li>
  <li>logical combinators: <code>and</code></li>
  </ul>
  <p>This allows for expressions like:</p>
  <pre><code>confidential == false
  count &gt; 10 and ip == &quot;10.0.8.0&quot;
  date &gt;= &quot;2010-10-12&quot; and date &lt;= &quot;2010-11-12&quot;</code></pre>
  <p>Future extensions to the language could happen along any of the above axes: more operations, more types, more type constraints, more logical combinators.</p>
  <p>For a convenient implementation, the limiting factor is how much the host programming language’s type system can accommodate. I’ll be using Haskell in this series, but any language that allows for algebraic data types should be enough to reproduce this work. It’s entirely possible to develop a DSL solution in other languages, but there’ll be more testing to do.</p>
  <p>Let’s implement the language core.</p>
  <p>We’d like to support all of <code>&lt;</code>, <code>&gt;</code>, <code>==</code>, <code>&gt;=</code>, <code>&lt;=</code>. Let’s add those in:</p>
  <div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb2-1" title="1"><span class="kw">data</span> <span class="dt">Op</span></a>
  <a class="sourceLine" id="cb2-2" title="2">  <span class="ot">=</span> <span class="dt">Lt</span>   <span class="co">-- &lt;</span></a>
  <a class="sourceLine" id="cb2-3" title="3">  <span class="op">|</span> <span class="dt">Gt</span>   <span class="co">-- &gt;</span></a>
  <a class="sourceLine" id="cb2-4" title="4">  <span class="op">|</span> <span class="dt">Eq</span>   <span class="co">-- ==</span></a>
  <a class="sourceLine" id="cb2-5" title="5">  <span class="op">|</span> <span class="dt">Gte</span>  <span class="co">-- &gt;=</span></a>
  <a class="sourceLine" id="cb2-6" title="6">  <span class="op">|</span> <span class="dt">Lte</span>  <span class="co">-- &lt;=</span></a>
  <a class="sourceLine" id="cb2-7" title="7">    <span class="kw">deriving</span> <span class="dt">Eq</span></a></code></pre></div>
  <p>Next, we’ll add support for types and values:</p>
  <div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb3-1" title="1"><span class="kw">data</span> <span class="dt">Type</span></a>
  <a class="sourceLine" id="cb3-2" title="2">  <span class="ot">=</span> <span class="dt">IntType</span></a>
  <a class="sourceLine" id="cb3-3" title="3">  <span class="op">|</span> <span class="dt">StringType</span></a>
  <a class="sourceLine" id="cb3-4" title="4">  <span class="op">|</span> <span class="dt">BoolType</span></a>
  <a class="sourceLine" id="cb3-5" title="5">    <span class="kw">deriving</span> <span class="dt">Eq</span></a>
  <a class="sourceLine" id="cb3-6" title="6"></a>
  <a class="sourceLine" id="cb3-7" title="7"><span class="kw">data</span> <span class="dt">Literal</span></a>
  <a class="sourceLine" id="cb3-8" title="8">  <span class="ot">=</span> <span class="dt">IntLit</span> <span class="dt">Int</span></a>
  <a class="sourceLine" id="cb3-9" title="9">  <span class="op">|</span> <span class="dt">StringLit</span> <span class="dt">Text</span></a>
  <a class="sourceLine" id="cb3-10" title="10">  <span class="op">|</span> <span class="dt">BoolLit</span> <span class="dt">Bool</span></a>
  <a class="sourceLine" id="cb3-11" title="11">    <span class="kw">deriving</span> <span class="dt">Eq</span></a></code></pre></div>
  <p>Notice that we’re maintaining both type-level and value-level information, and maintain a distinction between the two levels.</p>
  <p>Next up, support for variables:</p>
  <div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb4-1" title="1"><span class="kw">type</span> <span class="dt">Var</span> <span class="ot">=</span> <span class="dt">Text</span></a>
  <a class="sourceLine" id="cb4-2" title="2"><span class="kw">type</span> <span class="dt">Field</span> <span class="ot">=</span> (<span class="dt">Var</span>, <span class="dt">Type</span>)</a>
  <a class="sourceLine" id="cb4-3" title="3"><span class="kw">type</span> <span class="dt">Env</span> <span class="ot">=</span> [<span class="dt">Field</span>]</a></code></pre></div>
  <p>This is a fairly straightforward approach. We don’t have to worry about bindings or scope. We leave the declaration of allowed variables to the search engine provider, to be exposed via documentation.</p>
  <p>Next up: type constraints!</p>
  <div class="sourceCode" id="cb5"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb5-1" title="1"><span class="kw">data</span> <span class="dt">Supports</span></a>
  <a class="sourceLine" id="cb5-2" title="2">  <span class="ot">=</span> <span class="dt">Equality</span></a>
  <a class="sourceLine" id="cb5-3" title="3">  <span class="op">|</span> <span class="dt">Ordering</span></a>
  <a class="sourceLine" id="cb5-4" title="4">    <span class="kw">deriving</span> (<span class="dt">Eq</span>, <span class="dt">Show</span>)</a>
  <a class="sourceLine" id="cb5-5" title="5"></a>
  <a class="sourceLine" id="cb5-6" title="6"><span class="ot">supports ::</span> <span class="dt">Type</span> <span class="ot">-&gt;</span> [<span class="dt">Supports</span>]</a>
  <a class="sourceLine" id="cb5-7" title="7">supports <span class="dt">IntType</span> <span class="ot">=</span> [<span class="dt">Equality</span>, <span class="dt">Ordering</span>]</a>
  <a class="sourceLine" id="cb5-8" title="8">supports <span class="dt">StringType</span> <span class="ot">=</span> [<span class="dt">Equality</span>, <span class="dt">Ordering</span>]</a>
  <a class="sourceLine" id="cb5-9" title="9">supports <span class="dt">BoolType</span> <span class="ot">=</span> [<span class="dt">Equality</span>]</a>
  <a class="sourceLine" id="cb5-10" title="10"></a>
  <a class="sourceLine" id="cb5-11" title="11"><span class="ot">needs ::</span> <span class="dt">Op</span> <span class="ot">-&gt;</span> [<span class="dt">Supports</span>]</a>
  <a class="sourceLine" id="cb5-12" title="12">needs <span class="dt">Lt</span> <span class="ot">=</span> [<span class="dt">Ordering</span>]</a>
  <a class="sourceLine" id="cb5-13" title="13">needs <span class="dt">Gt</span> <span class="ot">=</span> [<span class="dt">Ordering</span>]</a>
  <a class="sourceLine" id="cb5-14" title="14">needs <span class="dt">Eq</span> <span class="ot">=</span> [<span class="dt">Equality</span>]</a>
  <a class="sourceLine" id="cb5-15" title="15">needs <span class="dt">Gte</span> <span class="ot">=</span> [<span class="dt">Ordering</span>, <span class="dt">Equality</span>]</a>
  <a class="sourceLine" id="cb5-16" title="16">needs <span class="dt">Lte</span> <span class="ot">=</span> [<span class="dt">Ordering</span>, <span class="dt">Equality</span>]</a>
  <a class="sourceLine" id="cb5-17" title="17"></a>
  <a class="sourceLine" id="cb5-18" title="18"><span class="co">-- &gt; IntType `can` Lt</span></a>
  <a class="sourceLine" id="cb5-19" title="19"><span class="co">-- True</span></a>
  <a class="sourceLine" id="cb5-20" title="20"><span class="ot">can ::</span> <span class="dt">Type</span> <span class="ot">-&gt;</span> <span class="dt">Op</span> <span class="ot">-&gt;</span> <span class="dt">Bool</span></a>
  <a class="sourceLine" id="cb5-21" title="21">can t op <span class="ot">=</span> <span class="fu">null</span> (needs op \\ supports t)</a></code></pre></div>
  <p>Next, the logical combinator <code>and</code>. I’ll also introduce how binary operations are represented at this stage:</p>
  <div class="sourceCode" id="cb6"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb6-1" title="1"><span class="kw">data</span> <span class="dt">Expr</span></a>
  <a class="sourceLine" id="cb6-2" title="2">  <span class="ot">=</span> <span class="dt">And</span> <span class="dt">Expr</span> <span class="dt">Expr</span></a>
  <a class="sourceLine" id="cb6-3" title="3">  <span class="op">|</span> <span class="dt">BinOpL</span> <span class="dt">Op</span> <span class="dt">Var</span> <span class="dt">Literal</span></a>
  <a class="sourceLine" id="cb6-4" title="4">  <span class="op">|</span> <span class="dt">BinOpR</span> <span class="dt">Op</span> <span class="dt">Literal</span> <span class="dt">Var</span></a>
  <a class="sourceLine" id="cb6-5" title="5">    <span class="kw">deriving</span> (<span class="dt">Show</span>, <span class="dt">Eq</span>)</a></code></pre></div>
  <p>This little recursive data type captures the essence of our language. Let’s break it down:</p>
  <ul>
  <li><code>And Expr Expr</code>: <code>and</code> combines two expressions</li>
  <li><code>BinOpL Op Var Literal</code>: a binary <code>Op</code> with a <code>Var</code> on the Left and a <code>Literal</code></li>
  <li><code>BinOpR Op Literal Var</code>: a binary <code>Op</code> with a <code>Var</code> on the Right and a <code>Literal</code></li>
  </ul>
  <p>With this construction, we’ve ruled out some cases:</p>
  <ul>
  <li>An expression can never consist of two literals: <code>1 &lt; 2</code></li>
  <li>An expression can never consist of two vars: <code>date == date</code>, <code>ip &gt; date</code></li>
  <li>A single expression will never have more than one operator</li>
  </ul>
  <p>Here’s the language as a whole, for convenient reading:</p>
  <div class="sourceCode" id="cb7"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb7-1" title="1"><span class="kw">data</span> <span class="dt">Op</span></a>
  <a class="sourceLine" id="cb7-2" title="2">  <span class="ot">=</span> <span class="dt">Lt</span>   <span class="co">-- &lt;</span></a>
  <a class="sourceLine" id="cb7-3" title="3">  <span class="op">|</span> <span class="dt">Gt</span>   <span class="co">-- &gt;</span></a>
  <a class="sourceLine" id="cb7-4" title="4">  <span class="op">|</span> <span class="dt">Eq</span>   <span class="co">-- ==</span></a>
  <a class="sourceLine" id="cb7-5" title="5">  <span class="op">|</span> <span class="dt">Gte</span>  <span class="co">-- &gt;=</span></a>
  <a class="sourceLine" id="cb7-6" title="6">  <span class="op">|</span> <span class="dt">Lte</span>  <span class="co">-- &lt;=</span></a>
  <a class="sourceLine" id="cb7-7" title="7">    <span class="kw">deriving</span> <span class="dt">Eq</span></a>
  <a class="sourceLine" id="cb7-8" title="8"></a>
  <a class="sourceLine" id="cb7-9" title="9"><span class="kw">data</span> <span class="dt">Type</span></a>
  <a class="sourceLine" id="cb7-10" title="10">  <span class="ot">=</span> <span class="dt">IntType</span></a>
  <a class="sourceLine" id="cb7-11" title="11">  <span class="op">|</span> <span class="dt">StringType</span></a>
  <a class="sourceLine" id="cb7-12" title="12">  <span class="op">|</span> <span class="dt">BoolType</span></a>
  <a class="sourceLine" id="cb7-13" title="13">    <span class="kw">deriving</span> <span class="dt">Eq</span></a>
  <a class="sourceLine" id="cb7-14" title="14"></a>
  <a class="sourceLine" id="cb7-15" title="15"><span class="kw">data</span> <span class="dt">Literal</span></a>
  <a class="sourceLine" id="cb7-16" title="16">  <span class="ot">=</span> <span class="dt">IntLit</span> <span class="dt">Int</span></a>
  <a class="sourceLine" id="cb7-17" title="17">  <span class="op">|</span> <span class="dt">StringLit</span> <span class="dt">Text</span></a>
  <a class="sourceLine" id="cb7-18" title="18">  <span class="op">|</span> <span class="dt">BoolLit</span> <span class="dt">Bool</span></a>
  <a class="sourceLine" id="cb7-19" title="19">    <span class="kw">deriving</span> <span class="dt">Eq</span></a>
  <a class="sourceLine" id="cb7-20" title="20"></a>
  <a class="sourceLine" id="cb7-21" title="21"><span class="kw">data</span> <span class="dt">Supports</span></a>
  <a class="sourceLine" id="cb7-22" title="22">  <span class="ot">=</span> <span class="dt">Equality</span></a>
  <a class="sourceLine" id="cb7-23" title="23">  <span class="op">|</span> <span class="dt">Ordering</span></a>
  <a class="sourceLine" id="cb7-24" title="24">    <span class="kw">deriving</span> (<span class="dt">Eq</span>, <span class="dt">Show</span>)</a>
  <a class="sourceLine" id="cb7-25" title="25"></a>
  <a class="sourceLine" id="cb7-26" title="26"><span class="ot">supports ::</span> <span class="dt">Type</span> <span class="ot">-&gt;</span> [<span class="dt">Supports</span>]</a>
  <a class="sourceLine" id="cb7-27" title="27">supports <span class="dt">IntType</span> <span class="ot">=</span> [<span class="dt">Equality</span>, <span class="dt">Ordering</span>]</a>
  <a class="sourceLine" id="cb7-28" title="28">supports <span class="dt">StringType</span> <span class="ot">=</span> [<span class="dt">Equality</span>, <span class="dt">Ordering</span>]</a>
  <a class="sourceLine" id="cb7-29" title="29">supports <span class="dt">BoolType</span> <span class="ot">=</span> [<span class="dt">Equality</span>]</a>
  <a class="sourceLine" id="cb7-30" title="30"></a>
  <a class="sourceLine" id="cb7-31" title="31"><span class="ot">needs ::</span> <span class="dt">Op</span> <span class="ot">-&gt;</span> [<span class="dt">Supports</span>]</a>
  <a class="sourceLine" id="cb7-32" title="32">needs <span class="dt">Lt</span> <span class="ot">=</span> [<span class="dt">Ordering</span>]</a>
  <a class="sourceLine" id="cb7-33" title="33">needs <span class="dt">Gt</span> <span class="ot">=</span> [<span class="dt">Ordering</span>]</a>
  <a class="sourceLine" id="cb7-34" title="34">needs <span class="dt">Eq</span> <span class="ot">=</span> [<span class="dt">Equality</span>]</a>
  <a class="sourceLine" id="cb7-35" title="35">needs <span class="dt">Gte</span> <span class="ot">=</span> [<span class="dt">Ordering</span>, <span class="dt">Equality</span>]</a>
  <a class="sourceLine" id="cb7-36" title="36">needs <span class="dt">Lte</span> <span class="ot">=</span> [<span class="dt">Ordering</span>, <span class="dt">Equality</span>]</a>
  <a class="sourceLine" id="cb7-37" title="37"></a>
  <a class="sourceLine" id="cb7-38" title="38"><span class="co">-- &gt; IntType `can` Lt</span></a>
  <a class="sourceLine" id="cb7-39" title="39"><span class="co">-- True</span></a>
  <a class="sourceLine" id="cb7-40" title="40"><span class="ot">can ::</span> <span class="dt">Type</span> <span class="ot">-&gt;</span> <span class="dt">Op</span> <span class="ot">-&gt;</span> <span class="dt">Bool</span></a>
  <a class="sourceLine" id="cb7-41" title="41">can t op <span class="ot">=</span> <span class="fu">null</span> (needs op \\ supports t)</a>
  <a class="sourceLine" id="cb7-42" title="42"></a>
  <a class="sourceLine" id="cb7-43" title="43"><span class="kw">data</span> <span class="dt">Expr</span></a>
  <a class="sourceLine" id="cb7-44" title="44">  <span class="ot">=</span> <span class="dt">And</span> <span class="dt">Expr</span> <span class="dt">Expr</span></a>
  <a class="sourceLine" id="cb7-45" title="45">  <span class="op">|</span> <span class="dt">BinOpL</span> <span class="dt">Op</span> <span class="dt">Var</span> <span class="dt">Literal</span></a>
  <a class="sourceLine" id="cb7-46" title="46">  <span class="op">|</span> <span class="dt">BinOpR</span> <span class="dt">Op</span> <span class="dt">Literal</span> <span class="dt">Var</span></a>
  <a class="sourceLine" id="cb7-47" title="47">    <span class="kw">deriving</span> (<span class="dt">Show</span>, <span class="dt">Eq</span>)</a></code></pre></div>
  <h2 id="closing">Closing</h2>
  <p>That’s all I’m going to cover for now - the core of the language. In the next post, I’ll build a parser for this language and write some tests for that parser.</p>
  <p>If you’d like to get a head start, the source for the search DSL is availaible in this GitLab <a href="https://gitlab.com/queertypes/search-dsl/">repo</a>.</p>
  <p>Thanks for reading!</p>
  <h2 id="contributing">Contributing</h2>
  <p>If you enjoy my works:</p>
  <ul>
  <li>I’m currently available for hire: <a href="https://queertypes.com/files/resume.pdf">resume</a></li>
  <li>I welcome donations to my <a href="https://www.patreon.com/queertypes?ty=h">Patreon</a></li>
  </ul>]]></summary>
</entry>
<entry>
  <title>Terms and Boundaries for my Works</title>
  <link href="https://queertypes.com/posts/55-queer-types-terms.html" />
  <id>https://queertypes.com/posts/55-queer-types-terms.html</id>
  <published>2015-10-17T00:00:00Z</published>
  <updated>2015-10-17T00:00:00Z</updated>
  <summary type="html"><![CDATA[<p>This is a short document highlighting what I can do, what I am willing to do, and under what terms.</p>
  <p>If you’d like to help me, please consider donating to my <a href="https://www.patreon.com/queertypes?ty=h">Patreon</a>.</p>
  <h2 id="what-i-can-do">What I Can Do</h2>
  <p>I write Haskell, primarily. I’ve written quite a bit of it. You can see a sampling of that work on my <a href="https://gitlab.com/u/queertypes">Gitlab</a>. You may also turn to my <a href="https://queertypes.com/files/resume.pdf">resume</a> for additional pointers.</p>
  <p>I specialize in backend and systems work. I’m comfortable with implementing components of projects, as well as full projects.</p>
  <p>I can also teach, write, and draw. I use a combination of those skills to communicate, express ideas, and make concepts more accessible.</p>
  <p>I am an intersectional feminist. This means that my works try to take into account the needs of all people, but especially those that are being hurt by our current systems.</p>
  <h2 id="terms-of-my-works">Terms of my Works</h2>
  <ul>
  <li>I will not work on secret or proprietary software</li>
  <li>I will not work on secret or proprietary research</li>
  <li>I will not patent my original works</li>
  <li>I will enforce a <a href="http://contributor-covenant.org/">code of conduct</a> on my projects</li>
  <li>I will work on software projects available to all</li>
  <li>I will work on software with licensing approved by <a href="http://opensource.org/licenses">OSI</a></li>
  <li>I will release my own software under such licenses</li>
  <li>I will create other works under the <a href="http://creativecommons.org/licenses/by/4.0/">CC-BY</a> license</li>
  <li>I may refuse to work on projects with toxic maintainers or communities</li>
  <li>Consent first: our terms must be mutually agreeable</li>
  <li>I expect to be paid for my labor</li>
  <li>None of the above is negotiable</li>
  </ul>]]></summary>
</entry>
<entry>
  <title>Notes on -  Name and Marker Change in Austin, TX</title>
  <link href="https://queertypes.com/posts/54-name-marker-change-austin-tx.html" />
  <id>https://queertypes.com/posts/54-name-marker-change-austin-tx.html</id>
  <published>2015-10-16T00:00:00Z</published>
  <updated>2015-10-16T00:00:00Z</updated>
  <summary type="html"><![CDATA[<p>I went to a free workshop today focused on the process needed to change one’s name and gender marker in Austin, TX. It turned out that it was a bit more than that! The very brief of it:</p>
  <ul>
  <li>They gave a short overview of the program</li>
  <li>They paired me with legal students</li>
  <li>They began the process of filling out the relevant petitions</li>
  <li>They made copies of necessary documents</li>
  <li>They gave me a take-home list of docs I still needed</li>
  <li>They’ll keep in touch during the process and help guide it to completion</li>
  </ul>
  <p>I asked about support for non-binary gender identities. As I expected, there is no such support in the United States of America. You pretty much choose M or F. It’s amazing how much pressure this puts on individuals to “sort” into the binary. More on that some other time.</p>
  <p>Here’s the sorts of documents needed for completing the process:</p>
  <ul>
  <li>Social security card</li>
  <li>Birth certificate</li>
  <li>Letter from therapist</li>
  <li>Letter from physician</li>
  <li>State issued ID</li>
  <li>Fingerprint card</li>
  </ul>
  <p>There seems to be additional documentation needed if you’ve changed your name before or if you’ve got a criminal record.</p>
  <p>They reassured listeners that the judges in Austin, TX would not have an adversarial hearing. That’s to say, they won’t ask you why you’re doing this, what your gender is, etc. They’ll review your petition beforehand, and then you just need to be present in order to answer questions about missing documentation or documentation that needs clarification.</p>
  <p>Another detail: as a resident of TX, you can choose any county in TX to undertake the procedure. Austin, TX rates favorably, based on information I overheard at the workshop. Also, if you were born in TX, but then move to another state, you can still complete this process in TX, even as a non-resident.</p>
  <p>The expected fees for the full process are around $300-$400. These fees can be waived if you show that you’d be unable to pay them.</p>
  <p>The most tedious process really seems to be about the aftermath - everything <strong>ELSE</strong> you need to change after getting legal recognition from the local government. For me, that amounts to at least:</p>
  <ul>
  <li>Marriage license</li>
  <li>Lease</li>
  <li>Vehicle registration</li>
  <li>Social security</li>
  <li>Driver’s license</li>
  <li>Business ownership</li>
  <li>Bank accounts</li>
  <li>Credit reporting agencies</li>
  </ul>
  <p>That’s the gist of things. <a href="http://www.theqaustin.org/">The Q</a> hosted the workshop, and they seem pretty great.</p>
  <p>So with that, my name will be Allele Dev in probably about three to four months, and my gender marker will be F.</p>]]></summary>
</entry>
<entry>
  <title>Queer Types -  The Business</title>
  <link href="https://queertypes.com/posts/53-announcing-queer-types.html" />
  <id>https://queertypes.com/posts/53-announcing-queer-types.html</id>
  <published>2015-10-16T00:00:00Z</published>
  <updated>2015-10-16T00:00:00Z</updated>
  <summary type="html"><![CDATA[<p>I started my own business yesterday: Queer Types! I’m writing about it here to tell the story of how Queer Types came to be, and what it is.</p>
  <h2 id="tech-capitalism-do-you-wanna-have-a-bad-time">Tech &amp; Capitalism: do you wanna have a bad time?</h2>
  <p>I’ve worked in tech for almost three years now. The first year was the best, and things gradually went down hill from there. A few factors were at play in that decline.</p>
  <p>My awareness of myself and the world around me was <strong>really</strong> low in 2012. Honestly, I didn’t know much about who I was, what I believed in, or where I was going. I just knew I had to get out of college, and that getting paid was a Good Thing.</p>
  <p>At the time, I had no idea I was trans. I didn’t even know it was a thing I could be. I’m suspect I had some white, male-passing privilege helping me along. By the time April 2014 rolled around, I knew I was trans - I was out soon after that. Things gradually got worse at work, as I presented more and more femme, especially in 2014. 2015 was better, in this regard. Still, for every job after my first, I was the only woman on the teams I was assigned to. It’s really alienating to be the only trans person, the only woman, the only X - in any team situation.</p>
  <p>Awareness - that spiked pretty quickly after I realized I was trans. I learned about all the different ways people are marginalized and oppressed, that there are irrefutable, intersectional systems that keep people down. <a href="https://twitter.com/">Twitter</a> played a significant role in my awareness increasing over time. I saw painful situations unfolding in real time. I listened carefully to people expressing their lived experiences. The first few months of analyzing my privilege were <strong>very</strong> uncomfortable. It still can be uncomfortable when I run into something I haven’t introspected on before. That got easier. It gets easier.</p>
  <p>I couldn’t help but see those structures of oppression paralleled in every job I had. Expectations of long hours, more hours than my health could handle. Credit being taken by company “leadership” for the efforts of the workers. Blame being put on the workers when things didn’t work out as planned. Punishment for the workers when they spoke up about things being shitty, unrealistic, or unreasonable. I didn’t spend much time blaming individual actors, except for a few particularly toxic examples. The non-toxic participants were caught up in these sytems, and they need to make a living, too. My goals were fundamentally misaligned with things like <em>Delivering Business Value</em>, <em>Minimum Viable Products</em>, <em>Profits</em>. I’m not suited to quietly working in companies that want to keep to the exploitative status quo.</p>
  <p>In a sense, all that time I’d been working (n+1) jobs: the 1 salaried, the remaining <strong>n</strong>, unpaid labor. Things like: writing articles about <a href="https://queertypes.com/">tech</a>, taking <strong>extra</strong> time and care to ensure the things I made in my day job were of high quality and maintainable, publishing and maintaining open source <a href="https://gitlab.com/u/queertypes">libraries</a>, crafting <a href="https://gitlab.com/queertypes/type-assisted-speed-runs">tutorials</a>, following and reporting on tech trends. Others have written about all this <a href="http://www.ashedryden.com/blog/the-ethics-of-unpaid-labor-and-the-oss-community">before</a>, several <a href="https://modelviewculture.com/pieces/what-your-open-source-culture-really-says-part-one">people</a>, at different <a href="https://modelviewculture.com/pieces/you-say-you-want-diversity-but-we-cant-even-get-internships">times</a> - Unpaid Labor.</p>
  <p>Taking inspiration from articles like <a href="https://modelviewculture.com/news/lets-talk-about-pay">this one</a> on wages in tech, or <a href="https://modelviewculture.com/pieces/giveyourmoneytowomen-the-end-game-of-capitalism">this one</a>, and realizing I couldn’t end Capitalism over night or escape it, I decided to seek my own path. I wanted to find a way to: work on my terms, with my ethics and capabilities in mind, and make it sustainable. If anything, that’s the origin story for Queer Types, at least three years in the making.</p>
  <h2 id="queer-types-setting-goals-and-boundaries">Queer Types: Setting Goals and Boundaries</h2>
  <p>I want to use the skills I have to help make the world a better place. I also want it to be on my terms.</p>
  <p>Some terms I’ve set for myself:</p>
  <ul>
  <li>I will not work on secret or proprietary software</li>
  </ul>
  <p>I’ve spent enough time rebuilding CRUD app #37. At large, it’s also notable that we have more than 5 SQL databases, more than 4 web browsers, probably more than 20 text editors, and so on. There’s a lot of repetition, and for what?</p>
  <p>I can understand the need for customization and tailoring tools for specific problems. However, what we have as a consequence of copyright and profit-driven-development is a rebuilding of the same fundamental things, over and over again. This is not a technical problem, this is a socioeconomic problem.</p>
  <p>I want to avoid contributing to that senseless duplication.</p>
  <ul>
  <li>I will not work on secret or proprietary research</li>
  </ul>
  <p>Similarly, for research. Knowledge should flow as freely as possible. It’s easier than ever now, too.</p>
  <ul>
  <li>I <strong>will</strong> work on software projects available to all</li>
  </ul>
  <p>Generally, this means that the source is available and the <a href="http://opensource.org/licenses">licensing</a> aligns with the goal of keeping the projects available to all.</p>
  <ul>
  <li>I <strong>will</strong> make all my written works freely available</li>
  </ul>
  <p>As with software. Yes, this includes books I intend to write.</p>
  <ul>
  <li>I expect to be paid</li>
  </ul>
  <p>See the origin story in the previous section. That I want to provide freely available works doesn’t mean I’ll allow myself to be exploited. I have to live, too. I do not condone Unpaid Labor, <strong>especially</strong> in a society that still thinks it’s right for people to have to Earn Their Right to Live.</p>
  <ul>
  <li>Consent first</li>
  </ul>
  <p>I’ll be working with others. I’ll respect their terms, too, and if they align, we can work together.</p>
  <h2 id="queer-types-what-will-it-do">Queer Types: What Will it Do</h2>
  <p>Phrased differently, what can I do? I can write software, technical articles, tutorials, and books. I can also teach, make art, and consult.</p>
  <p>So, keeping the terms and boundaries above in mind, I hope to offer the following services through Queer Types:</p>
  <ul>
  <li>Technical writing</li>
  <li>Technical workshops</li>
  <li>Written tutorials</li>
  <li>Software: implementation, design, testing, benchmarking</li>
  <li>Drawing: cute things, technical things, cute &amp; technical things</li>
  <li>Awareness &amp; advocacy</li>
  </ul>
  <p>Notably, for drawings, this would go great with Patreon-acquired funding as a reward. You like what I do, I write you a small program, and then send you a hand-made drawing involving that program. We celebrate, because things are cute, and we both learn things. This line of thinking was inspired by <a href="https://twitter.com/sailorhg">sailorhg</a>’s work on <a href="http://shop.bubblesort.io/">Bubble Sort</a> zines. There’s no reason tech can’t be cute. ❤</p>
  <p>On tech work specifically, I have no intention of covering the full software market. I know my specialties and preferences. I love typed, functional programming. I like backend and systems work, and I want to like frontend work. Technology-wise, this amounts to work involving some mix of:</p>
  <ul>
  <li>Linux</li>
  <li>Haskell</li>
  <li>PureScript</li>
  <li>Idris</li>
  <li>Rust, maybe</li>
  </ul>
  <p>Projects like <a href="https://github.com/purescript/purescript/issues/1523">Support JSON errors</a> for PureScript (or other error reporting enhancements), improvements to <a href="https://github.com/haskell/cabal/issues">cabal</a> or <a href="https://github.com/commercialhaskell/stack/issues">stack</a>, and other infrastructural/library work around any of the above languages, would be suitable for me.</p>
  <h2 id="finance-models-attempting-ethical-sustainability">Finance Models: Attempting Ethical Sustainability</h2>
  <p>So then, what do I do, and how do I maintain my terms and beliefs while I’m at it? It’s still a bit of an open problem, to be honest. My first two plans, both with short-comings:</p>
  <ul>
  <li>Start a Patreon for my month-to-month efforts</li>
  <li>Start a KickStarter/IndieGogo campaign for book work</li>
  </ul>
  <p>Patreon mostly works, especially for requesting donations in exchange for cute things. The primary shortcoming is in the allocation of my resources (hours I can work per month) to various “rewards” tiers for larger scope work.</p>
  <p>For example, I can create a reward tier for $1200/month that serves as a promise to a particular funder that I will work on a mutually-agreed project for 4 days. If I introduced more levels (more money, more promised days), the limit system fails me, because it works in terms of rewards given, rather than the global hours-I-can-work-per-month resource. Example:</p>
  <ul>
  <li>10 slots: $600/month for 2d of project work</li>
  <li>5 slots: $1200/month for 4d of project work</li>
  </ul>
  <p>I can end up overcommitted with:</p>
  <ul>
  <li>4 instances of $600/month: 8d of work</li>
  <li>4 instances of $1200/month: 20d of work</li>
  <li>Total: 28d of work</li>
  </ul>
  <p>Also, things like:</p>
  <ul>
  <li>Negotiating projects with potential funders (accept/reject/clarify/allocate)</li>
  <li>Scheduling/managing projects (in progress/outcome reporting/pending)</li>
  </ul>
  <p>would have to happen out-of-band. Might be a worthwhile weekend project to scrap together a site that interacts with a service like Stripe for payment processing but also gives me (and funders) the ability to visibly negotiate and track projects. I dunno. Open problem!</p>
  <p>The IndieGogo campaign may be the best short term path. I have plans to write an accessible book on <strong>Haskell Web Development</strong>. I’d ask for $20000 as a minimum marker for writing such a book over a period of 3 months. That’d cover the cost of living for longer than 3 months, and there’d likely be weekly chapter releases, so funders can watch the book grow over time. Stretch goals would cover additional topics, additional mediums (screen casts?), and possibly an additional book (anything past the $60,000 mark).</p>
  <p>Keeping with the previous section on my terms, the book would be freely available. It’d be akin to <a href="http://book.realworldhaskell.org/">Real World Haskell</a> or <a href="https://leanpub.com/purescript">PureScript by Example</a> - people can choose to pay for print copies, and Amazon/LeanPub/etc. can provide them, but the sources to build PDFs/etc. and run code will always be available in some source control repo. Also akin to the Bandcamp model for artists, where some works are offered on a “Name Your Price” basis: you can download it for free if you like, but if you’re able, donations are welcome.</p>
  <p>Another possibility for funding would be to look towards something like <a href="https://rubytogether.org/">Ruby Together</a>. It seems that the <a href="http://industry.haskell.org/">Industrial Haskell Group</a> is the closest analogue to this in Haskell land. It doesn’t sound anywhere near as appealing or welcoming as the Ruby Together effort. Maybe there’s room for another such organization? I don’t know. More open problems!</p>
  <h2 id="closing">Closing</h2>
  <p>That’s the gist of it. I don’t want to work in our current systems, I want to set my boundaries, and I have a plan.</p>
  <p>Next steps: figure out funding. You’ll all hear about that part pretty soon.</p>
  <p>Long term goals: end capitalism?</p>]]></summary>
</entry>
<entry>
  <title>GHC base Library Changes -  7.8 to 7.10</title>
  <link href="https://queertypes.com/posts/52-ghc-changes.html" />
  <id>https://queertypes.com/posts/52-ghc-changes.html</id>
  <published>2015-10-04T00:00:00Z</published>
  <updated>2015-10-04T00:00:00Z</updated>
  <summary type="html"><![CDATA[<p>While organizing old notes, I happened upon a file titled <code>ghc-base-78-to-710.diff</code>. The information inside turned out to be pretty interesting. I figured I’d throw it up here in case it’s of use to anyone.</p>
  <p>Below is a summary of all the changes to the <code>base</code> library from GHC 7.8 to 7.10, by module, in diff format:</p>
  <div class="sourceCode" id="cb1"><pre class="sourceCode diff"><code class="sourceCode diff"><a class="sourceLine" id="cb1-1" title="1">Control.Monad</a>
  <a class="sourceLine" id="cb1-2" title="2"><span class="va">+ (&lt;$!&gt;) :: Monad m =&gt; (a -&gt; b) -&gt; m a -&gt; m b</span></a>
  <a class="sourceLine" id="cb1-3" title="3"><span class="st">- class Monad m</span></a>
  <a class="sourceLine" id="cb1-4" title="4"><span class="va">+ class Applicative m =&gt; Monad m</span></a>
  <a class="sourceLine" id="cb1-5" title="5"><span class="st">- class Monad m =&gt; MonadPlus m</span></a>
  <a class="sourceLine" id="cb1-6" title="6"><span class="va">+ class (Alternative m, Monad m) =&gt; MonadPlus m</span></a>
  <a class="sourceLine" id="cb1-7" title="7"><span class="st">- foldM :: Monad m =&gt; (a -&gt; b -&gt; m a) -&gt; a -&gt; [b] -&gt; m a</span></a>
  <a class="sourceLine" id="cb1-8" title="8"><span class="va">+ foldM :: (Foldable t, Monad m) =&gt; (b -&gt; a -&gt; m b) -&gt; b -&gt; t a -&gt; m b</span></a>
  <a class="sourceLine" id="cb1-9" title="9"><span class="st">- foldM_ :: Monad m =&gt; (a -&gt; b -&gt; m a) -&gt; a -&gt; [b] -&gt; m ()</span></a>
  <a class="sourceLine" id="cb1-10" title="10"><span class="va">+ foldM_ :: (Foldable t, Monad m) =&gt; (b -&gt; a -&gt; m b) -&gt; b -&gt; t a -&gt; m ()</span></a>
  <a class="sourceLine" id="cb1-11" title="11"><span class="st">- forM :: Monad m =&gt; [a] -&gt; (a -&gt; m b) -&gt; m [b]</span></a>
  <a class="sourceLine" id="cb1-12" title="12"><span class="va">+ forM :: (Traversable t, Monad m) =&gt; t a -&gt; (a -&gt; m b) -&gt; m (t b)</span></a>
  <a class="sourceLine" id="cb1-13" title="13"><span class="st">- forM_ :: Monad m =&gt; [a] -&gt; (a -&gt; m b) -&gt; m ()</span></a>
  <a class="sourceLine" id="cb1-14" title="14"><span class="va">+ forM_ :: (Foldable t, Monad m) =&gt; t a -&gt; (a -&gt; m b) -&gt; m ()</span></a>
  <a class="sourceLine" id="cb1-15" title="15"><span class="st">- guard :: MonadPlus m =&gt; Bool -&gt; m ()</span></a>
  <a class="sourceLine" id="cb1-16" title="16"><span class="va">+ guard :: (Alternative f) =&gt; Bool -&gt; f ()</span></a>
  <a class="sourceLine" id="cb1-17" title="17"><span class="st">- mapM :: Monad m =&gt; (a -&gt; m b) -&gt; [a] -&gt; m [b]</span></a>
  <a class="sourceLine" id="cb1-18" title="18"><span class="va">+ mapM :: (Traversable t, Monad m) =&gt; (a -&gt; m b) -&gt; t a -&gt; m (t b)</span></a>
  <a class="sourceLine" id="cb1-19" title="19"><span class="st">- mapM_ :: Monad m =&gt; (a -&gt; m b) -&gt; [a] -&gt; m ()</span></a>
  <a class="sourceLine" id="cb1-20" title="20"><span class="va">+ mapM_ :: (Foldable t, Monad m) =&gt; (a -&gt; m b) -&gt; t a -&gt; m ()</span></a>
  <a class="sourceLine" id="cb1-21" title="21"><span class="st">- msum :: MonadPlus m =&gt; [m a] -&gt; m a</span></a>
  <a class="sourceLine" id="cb1-22" title="22"><span class="va">+ msum :: (Foldable t, MonadPlus m) =&gt; t (m a) -&gt; m a</span></a>
  <a class="sourceLine" id="cb1-23" title="23"><span class="st">- sequence :: Monad m =&gt; [m a] -&gt; m [a]</span></a>
  <a class="sourceLine" id="cb1-24" title="24"><span class="va">+ sequence :: (Traversable t, Monad m) =&gt; t (m a) -&gt; m (t a)</span></a>
  <a class="sourceLine" id="cb1-25" title="25"><span class="st">- sequence_ :: Monad m =&gt; [m a] -&gt; m ()</span></a>
  <a class="sourceLine" id="cb1-26" title="26"><span class="va">+ sequence_ :: (Foldable t, Monad m) =&gt; t (m a) -&gt; m ()</span></a>
  <a class="sourceLine" id="cb1-27" title="27"><span class="st">- unless :: Monad m =&gt; Bool -&gt; m () -&gt; m ()</span></a>
  <a class="sourceLine" id="cb1-28" title="28"><span class="va">+ unless :: (Applicative f) =&gt; Bool -&gt; f () -&gt; f ()</span></a>
  <a class="sourceLine" id="cb1-29" title="29"><span class="st">- when :: Monad m =&gt; Bool -&gt; m () -&gt; m ()</span></a>
  <a class="sourceLine" id="cb1-30" title="30"><span class="va">+ when :: (Applicative f) =&gt; Bool -&gt; f () -&gt; f ()</span></a>
  <a class="sourceLine" id="cb1-31" title="31"></a>
  <a class="sourceLine" id="cb1-32" title="32">Data.Bits</a>
  <a class="sourceLine" id="cb1-33" title="33"><span class="va">+ countLeadingZeros :: FiniteBits b =&gt; b -&gt; Int</span></a>
  <a class="sourceLine" id="cb1-34" title="34"><span class="va">+ countTrailingZeros :: FiniteBits b =&gt; b -&gt; Int</span></a>
  <a class="sourceLine" id="cb1-35" title="35"><span class="va">+ toIntegralSized :: (Integral a, Integral b, Bits a, Bits b) =&gt; a -&gt; Maybe b</span></a>
  <a class="sourceLine" id="cb1-36" title="36"></a>
  <a class="sourceLine" id="cb1-37" title="37">Data.Monoid</a>
  <a class="sourceLine" id="cb1-38" title="38"><span class="va">+ Alt :: f a -&gt; Alt f a</span></a>
  <a class="sourceLine" id="cb1-39" title="39"><span class="va">+ getAlt :: Alt f a -&gt; f a</span></a>
  <a class="sourceLine" id="cb1-40" title="40"><span class="va">+ newtype Alt f a</span></a>
  <a class="sourceLine" id="cb1-41" title="41"></a>
  <a class="sourceLine" id="cb1-42" title="42">Data.List</a>
  <a class="sourceLine" id="cb1-43" title="43"><span class="st">- all :: (a -&gt; Bool) -&gt; [a] -&gt; Bool</span></a>
  <a class="sourceLine" id="cb1-44" title="44"><span class="va">+ all :: Foldable t =&gt; (a -&gt; Bool) -&gt; t a -&gt; Bool</span></a>
  <a class="sourceLine" id="cb1-45" title="45"><span class="st">- and :: [Bool] -&gt; Bool</span></a>
  <a class="sourceLine" id="cb1-46" title="46"><span class="va">+ and :: Foldable t =&gt; t Bool -&gt; Bool</span></a>
  <a class="sourceLine" id="cb1-47" title="47"><span class="st">- any :: (a -&gt; Bool) -&gt; [a] -&gt; Bool</span></a>
  <a class="sourceLine" id="cb1-48" title="48"><span class="va">+ any :: Foldable t =&gt; (a -&gt; Bool) -&gt; t a -&gt; Bool</span></a>
  <a class="sourceLine" id="cb1-49" title="49"><span class="st">- concat :: [[a]] -&gt; [a]</span></a>
  <a class="sourceLine" id="cb1-50" title="50"><span class="va">+ concat :: Foldable t =&gt; t [a] -&gt; [a]</span></a>
  <a class="sourceLine" id="cb1-51" title="51"><span class="st">- concatMap :: (a -&gt; [b]) -&gt; [a] -&gt; [b]</span></a>
  <a class="sourceLine" id="cb1-52" title="52"><span class="va">+ concatMap :: Foldable t =&gt; (a -&gt; [b]) -&gt; t a -&gt; [b]</span></a>
  <a class="sourceLine" id="cb1-53" title="53"><span class="st">- elem :: Eq a =&gt; a -&gt; [a] -&gt; Bool</span></a>
  <a class="sourceLine" id="cb1-54" title="54"><span class="va">+ elem :: (Foldable t, Eq a) =&gt; a -&gt; t a -&gt; Bool</span></a>
  <a class="sourceLine" id="cb1-55" title="55"><span class="st">- find :: (a -&gt; Bool) -&gt; [a] -&gt; Maybe a</span></a>
  <a class="sourceLine" id="cb1-56" title="56"><span class="va">+ find :: Foldable t =&gt; (a -&gt; Bool) -&gt; t a -&gt; Maybe a</span></a>
  <a class="sourceLine" id="cb1-57" title="57"><span class="st">- foldl :: (b -&gt; a -&gt; b) -&gt; b -&gt; [a] -&gt; b</span></a>
  <a class="sourceLine" id="cb1-58" title="58"><span class="va">+ foldl :: Foldable t =&gt; (b -&gt; a -&gt; b) -&gt; b -&gt; t a -&gt; b</span></a>
  <a class="sourceLine" id="cb1-59" title="59"><span class="st">- foldl&#39; :: (b -&gt; a -&gt; b) -&gt; b -&gt; [a] -&gt; b</span></a>
  <a class="sourceLine" id="cb1-60" title="60"><span class="va">+ foldl&#39; :: Foldable t =&gt; (b -&gt; a -&gt; b) -&gt; b -&gt; t a -&gt; b</span></a>
  <a class="sourceLine" id="cb1-61" title="61"><span class="st">- foldl1 :: (a -&gt; a -&gt; a) -&gt; [a] -&gt; a</span></a>
  <a class="sourceLine" id="cb1-62" title="62"><span class="va">+ foldl1 :: Foldable t =&gt; (a -&gt; a -&gt; a) -&gt; t a -&gt; a</span></a>
  <a class="sourceLine" id="cb1-63" title="63"><span class="st">- foldr :: (a -&gt; b -&gt; b) -&gt; b -&gt; [a] -&gt; b</span></a>
  <a class="sourceLine" id="cb1-64" title="64"><span class="va">+ foldr :: Foldable t =&gt; (a -&gt; b -&gt; b) -&gt; b -&gt; t a -&gt; b</span></a>
  <a class="sourceLine" id="cb1-65" title="65"><span class="st">- foldr1 :: (a -&gt; a -&gt; a) -&gt; [a] -&gt; a</span></a>
  <a class="sourceLine" id="cb1-66" title="66"><span class="va">+ foldr1 :: Foldable t =&gt; (a -&gt; a -&gt; a) -&gt; t a -&gt; a</span></a>
  <a class="sourceLine" id="cb1-67" title="67"><span class="va">+ isSubsequenceOf :: (Eq a) =&gt; [a] -&gt; [a] -&gt; Bool</span></a>
  <a class="sourceLine" id="cb1-68" title="68"><span class="st">- length :: [a] -&gt; Int</span></a>
  <a class="sourceLine" id="cb1-69" title="69"><span class="va">+ length :: Foldable t =&gt; t a -&gt; Int</span></a>
  <a class="sourceLine" id="cb1-70" title="70"><span class="st">- mapAccumL :: (acc -&gt; x -&gt; (acc, y)) -&gt; acc -&gt; [x] -&gt; (acc, [y])</span></a>
  <a class="sourceLine" id="cb1-71" title="71"><span class="va">+ mapAccumL :: Traversable t =&gt; (a -&gt; b -&gt; (a, c)) -&gt; a -&gt; t b -&gt; (a, t c)</span></a>
  <a class="sourceLine" id="cb1-72" title="72"><span class="st">- mapAccumR :: (acc -&gt; x -&gt; (acc, y)) -&gt; acc -&gt; [x] -&gt; (acc, [y])</span></a>
  <a class="sourceLine" id="cb1-73" title="73"><span class="va">+ mapAccumR :: Traversable t =&gt; (a -&gt; b -&gt; (a, c)) -&gt; a -&gt; t b -&gt; (a, t c)</span></a>
  <a class="sourceLine" id="cb1-74" title="74"><span class="st">- maximum :: Ord a =&gt; [a] -&gt; a</span></a>
  <a class="sourceLine" id="cb1-75" title="75"><span class="va">+ maximum :: (Foldable t, Ord a) =&gt; t a -&gt; a</span></a>
  <a class="sourceLine" id="cb1-76" title="76"><span class="st">- maximumBy :: (a -&gt; a -&gt; Ordering) -&gt; [a] -&gt; a</span></a>
  <a class="sourceLine" id="cb1-77" title="77"><span class="va">+ maximumBy :: Foldable t =&gt; (a -&gt; a -&gt; Ordering) -&gt; t a -&gt; a</span></a>
  <a class="sourceLine" id="cb1-78" title="78"><span class="st">- minimum :: Ord a =&gt; [a] -&gt; a</span></a>
  <a class="sourceLine" id="cb1-79" title="79"><span class="va">+ minimum :: (Foldable t, Ord a) =&gt; t a -&gt; a</span></a>
  <a class="sourceLine" id="cb1-80" title="80"><span class="st">- minimumBy :: (a -&gt; a -&gt; Ordering) -&gt; [a] -&gt; a</span></a>
  <a class="sourceLine" id="cb1-81" title="81"><span class="va">+ minimumBy :: Foldable t =&gt; (a -&gt; a -&gt; Ordering) -&gt; t a -&gt; a</span></a>
  <a class="sourceLine" id="cb1-82" title="82"><span class="st">- notElem :: Eq a =&gt; a -&gt; [a] -&gt; Bool</span></a>
  <a class="sourceLine" id="cb1-83" title="83"><span class="va">+ notElem :: (Foldable t, Eq a) =&gt; a -&gt; t a -&gt; Bool</span></a>
  <a class="sourceLine" id="cb1-84" title="84"><span class="st">- null :: [a] -&gt; Bool</span></a>
  <a class="sourceLine" id="cb1-85" title="85"><span class="va">+ null :: Foldable t =&gt; t a -&gt; Bool</span></a>
  <a class="sourceLine" id="cb1-86" title="86"><span class="st">- or :: [Bool] -&gt; Bool</span></a>
  <a class="sourceLine" id="cb1-87" title="87"><span class="va">+ or :: Foldable t =&gt; t Bool -&gt; Bool</span></a>
  <a class="sourceLine" id="cb1-88" title="88"><span class="st">- product :: Num a =&gt; [a] -&gt; a</span></a>
  <a class="sourceLine" id="cb1-89" title="89"><span class="va">+ product :: (Foldable t, Num a) =&gt; t a -&gt; a</span></a>
  <a class="sourceLine" id="cb1-90" title="90"><span class="va">+ scanl&#39; :: (b -&gt; a -&gt; b) -&gt; b -&gt; [a] -&gt; [b]</span></a>
  <a class="sourceLine" id="cb1-91" title="91"><span class="va">+ sortOn :: Ord b =&gt; (a -&gt; b) -&gt; [a] -&gt; [a]</span></a>
  <a class="sourceLine" id="cb1-92" title="92"><span class="st">- sum :: Num a =&gt; [a] -&gt; a</span></a>
  <a class="sourceLine" id="cb1-93" title="93"><span class="va">+ sum :: (Foldable t, Num a) =&gt; t a -&gt; a</span></a>
  <a class="sourceLine" id="cb1-94" title="94"><span class="va">+ uncons :: [a] -&gt; Maybe (a, [a])</span></a>
  <a class="sourceLine" id="cb1-95" title="95"></a>
  <a class="sourceLine" id="cb1-96" title="96">Foreign.Marshal.Alloc</a>
  <a class="sourceLine" id="cb1-97" title="97"><span class="va">+ calloc :: Storable a =&gt; IO (Ptr a)</span></a>
  <a class="sourceLine" id="cb1-98" title="98"><span class="va">+ callocBytes :: Int -&gt; IO (Ptr a)</span></a>
  <a class="sourceLine" id="cb1-99" title="99"></a>
  <a class="sourceLine" id="cb1-100" title="100">Foreign.Marshal.Utils</a>
  <a class="sourceLine" id="cb1-101" title="101"><span class="va">+ fillBytes :: Ptr a -&gt; Word8 -&gt; Int -&gt; IO ()</span></a>
  <a class="sourceLine" id="cb1-102" title="102"></a>
  <a class="sourceLine" id="cb1-103" title="103">Foreign.Marshal.Array</a>
  <a class="sourceLine" id="cb1-104" title="104"><span class="va">+ callocArray :: Storable a =&gt; Int -&gt; IO (Ptr a)</span></a>
  <a class="sourceLine" id="cb1-105" title="105"><span class="va">+ callocArray0 :: Storable a =&gt; Int -&gt; IO (Ptr a)</span></a>
  <a class="sourceLine" id="cb1-106" title="106"></a>
  <a class="sourceLine" id="cb1-107" title="107">Control.Exception.Base</a>
  <a class="sourceLine" id="cb1-108" title="108"><span class="va">+ AllocationLimitExceeded :: AllocationLimitExceeded</span></a>
  <a class="sourceLine" id="cb1-109" title="109"><span class="va">+ data AllocationLimitExceeded</span></a>
  <a class="sourceLine" id="cb1-110" title="110"><span class="va">+ displayException :: Exception e =&gt; e -&gt; String</span></a>
  <a class="sourceLine" id="cb1-111" title="111"></a>
  <a class="sourceLine" id="cb1-112" title="112">Control.Exception</a>
  <a class="sourceLine" id="cb1-113" title="113"><span class="va">+ AllocationLimitExceeded :: AllocationLimitExceeded</span></a>
  <a class="sourceLine" id="cb1-114" title="114"><span class="va">+ data AllocationLimitExceeded</span></a>
  <a class="sourceLine" id="cb1-115" title="115"><span class="va">+ displayException :: Exception e =&gt; e -&gt; String</span></a>
  <a class="sourceLine" id="cb1-116" title="116"></a>
  <a class="sourceLine" id="cb1-117" title="117">Prelude</a>
  <a class="sourceLine" id="cb1-118" title="118"><span class="va">+ (*&gt;) :: Applicative f =&gt; f a -&gt; f b -&gt; f b</span></a>
  <a class="sourceLine" id="cb1-119" title="119"><span class="va">+ (&lt;*) :: Applicative f =&gt; f a -&gt; f b -&gt; f a</span></a>
  <a class="sourceLine" id="cb1-120" title="120"><span class="va">+ (&lt;*&gt;) :: Applicative f =&gt; f (a -&gt; b) -&gt; f a -&gt; f b</span></a>
  <a class="sourceLine" id="cb1-121" title="121"><span class="st">- all :: (a -&gt; Bool) -&gt; [a] -&gt; Bool</span></a>
  <a class="sourceLine" id="cb1-122" title="122"><span class="va">+ all :: Foldable t =&gt; (a -&gt; Bool) -&gt; t a -&gt; Bool</span></a>
  <a class="sourceLine" id="cb1-123" title="123"><span class="st">- and :: [Bool] -&gt; Bool</span></a>
  <a class="sourceLine" id="cb1-124" title="124"><span class="va">+ and :: Foldable t =&gt; t Bool -&gt; Bool</span></a>
  <a class="sourceLine" id="cb1-125" title="125"><span class="st">- any :: (a -&gt; Bool) -&gt; [a] -&gt; Bool</span></a>
  <a class="sourceLine" id="cb1-126" title="126"><span class="va">+ any :: Foldable t =&gt; (a -&gt; Bool) -&gt; t a -&gt; Bool</span></a>
  <a class="sourceLine" id="cb1-127" title="127"><span class="st">- class Monad m</span></a>
  <a class="sourceLine" id="cb1-128" title="128"><span class="va">+ class Applicative m =&gt; Monad m</span></a>
  <a class="sourceLine" id="cb1-129" title="129"><span class="va">+ class Monoid a</span></a>
  <a class="sourceLine" id="cb1-130" title="130"><span class="va">+ class Functor f =&gt; Applicative f</span></a>
  <a class="sourceLine" id="cb1-131" title="131"><span class="va">+ class Foldable t</span></a>
  <a class="sourceLine" id="cb1-132" title="132"><span class="va">+ class (Functor t, Foldable t) =&gt; Traversable t</span></a>
  <a class="sourceLine" id="cb1-133" title="133"><span class="st">- concat :: [[a]] -&gt; [a]</span></a>
  <a class="sourceLine" id="cb1-134" title="134"><span class="va">+ concat :: Foldable t =&gt; t [a] -&gt; [a]</span></a>
  <a class="sourceLine" id="cb1-135" title="135"><span class="st">- concatMap :: (a -&gt; [b]) -&gt; [a] -&gt; [b]</span></a>
  <a class="sourceLine" id="cb1-136" title="136"><span class="va">+ concatMap :: Foldable t =&gt; (a -&gt; [b]) -&gt; t a -&gt; [b]</span></a>
  <a class="sourceLine" id="cb1-137" title="137"><span class="va">+ data Word :: *</span></a>
  <a class="sourceLine" id="cb1-138" title="138"><span class="st">- elem :: Eq a =&gt; a -&gt; [a] -&gt; Bool</span></a>
  <a class="sourceLine" id="cb1-139" title="139"><span class="va">+ elem :: (Foldable t, Eq a) =&gt; a -&gt; t a -&gt; Bool</span></a>
  <a class="sourceLine" id="cb1-140" title="140"><span class="va">+ foldMap :: (Foldable t, Monoid m) =&gt; (a -&gt; m) -&gt; t a -&gt; m</span></a>
  <a class="sourceLine" id="cb1-141" title="141"><span class="st">- foldl :: (b -&gt; a -&gt; b) -&gt; b -&gt; [a] -&gt; b</span></a>
  <a class="sourceLine" id="cb1-142" title="142"><span class="va">+ foldl :: Foldable t =&gt; (b -&gt; a -&gt; b) -&gt; b -&gt; t a -&gt; b</span></a>
  <a class="sourceLine" id="cb1-143" title="143"><span class="st">- foldl1 :: (a -&gt; a -&gt; a) -&gt; [a] -&gt; a</span></a>
  <a class="sourceLine" id="cb1-144" title="144"><span class="va">+ foldl1 :: Foldable t =&gt; (a -&gt; a -&gt; a) -&gt; t a -&gt; a</span></a>
  <a class="sourceLine" id="cb1-145" title="145"><span class="st">- foldr :: (a -&gt; b -&gt; b) -&gt; b -&gt; [a] -&gt; b</span></a>
  <a class="sourceLine" id="cb1-146" title="146"><span class="va">+ foldr :: Foldable t =&gt; (a -&gt; b -&gt; b) -&gt; b -&gt; t a -&gt; b</span></a>
  <a class="sourceLine" id="cb1-147" title="147"><span class="st">- foldr1 :: (a -&gt; a -&gt; a) -&gt; [a] -&gt; a</span></a>
  <a class="sourceLine" id="cb1-148" title="148"><span class="va">+ foldr1 :: Foldable t =&gt; (a -&gt; a -&gt; a) -&gt; t a -&gt; a</span></a>
  <a class="sourceLine" id="cb1-149" title="149"><span class="st">- length :: [a] -&gt; Int</span></a>
  <a class="sourceLine" id="cb1-150" title="150"><span class="va">+ length :: Foldable t =&gt; t a -&gt; Int</span></a>
  <a class="sourceLine" id="cb1-151" title="151"><span class="st">- mapM :: Monad m =&gt; (a -&gt; m b) -&gt; [a] -&gt; m [b]</span></a>
  <a class="sourceLine" id="cb1-152" title="152"><span class="va">+ mapM :: (Traversable t, Monad m) =&gt; (a -&gt; m b) -&gt; t a -&gt; m (t b)</span></a>
  <a class="sourceLine" id="cb1-153" title="153"><span class="st">- mapM_ :: Monad m =&gt; (a -&gt; m b) -&gt; [a] -&gt; m ()</span></a>
  <a class="sourceLine" id="cb1-154" title="154"><span class="va">+ mapM_ :: (Foldable t, Monad m) =&gt; (a -&gt; m b) -&gt; t a -&gt; m ()</span></a>
  <a class="sourceLine" id="cb1-155" title="155"><span class="va">+ mappend :: Monoid a =&gt; a -&gt; a -&gt; a</span></a>
  <a class="sourceLine" id="cb1-156" title="156"><span class="st">- maximum :: Ord a =&gt; [a] -&gt; a</span></a>
  <a class="sourceLine" id="cb1-157" title="157"><span class="va">+ maximum :: (Foldable t, Ord a) =&gt; t a -&gt; a</span></a>
  <a class="sourceLine" id="cb1-158" title="158"><span class="va">+ mconcat :: Monoid a =&gt; [a] -&gt; a</span></a>
  <a class="sourceLine" id="cb1-159" title="159"><span class="va">+ mempty :: Monoid a =&gt; a</span></a>
  <a class="sourceLine" id="cb1-160" title="160"><span class="st">- minimum :: Ord a =&gt; [a] -&gt; a</span></a>
  <a class="sourceLine" id="cb1-161" title="161"><span class="va">+ minimum :: (Foldable t, Ord a) =&gt; t a -&gt; a</span></a>
  <a class="sourceLine" id="cb1-162" title="162"><span class="st">- notElem :: Eq a =&gt; a -&gt; [a] -&gt; Bool</span></a>
  <a class="sourceLine" id="cb1-163" title="163"><span class="va">+ notElem :: (Foldable t, Eq a) =&gt; a -&gt; t a -&gt; Bool</span></a>
  <a class="sourceLine" id="cb1-164" title="164"><span class="st">- null :: [a] -&gt; Bool</span></a>
  <a class="sourceLine" id="cb1-165" title="165"><span class="va">+ null :: Foldable t =&gt; t a -&gt; Bool</span></a>
  <a class="sourceLine" id="cb1-166" title="166"><span class="st">- or :: [Bool] -&gt; Bool</span></a>
  <a class="sourceLine" id="cb1-167" title="167"><span class="va">+ or :: Foldable t =&gt; t Bool -&gt; Bool</span></a>
  <a class="sourceLine" id="cb1-168" title="168"><span class="st">- product :: Num a =&gt; [a] -&gt; a</span></a>
  <a class="sourceLine" id="cb1-169" title="169"><span class="va">+ product :: (Foldable t, Num a) =&gt; t a -&gt; a</span></a>
  <a class="sourceLine" id="cb1-170" title="170"><span class="va">+ pure :: Applicative f =&gt; a -&gt; f a</span></a>
  <a class="sourceLine" id="cb1-171" title="171"><span class="st">- sequence :: Monad m =&gt; [m a] -&gt; m [a]</span></a>
  <a class="sourceLine" id="cb1-172" title="172"><span class="va">+ sequence :: (Traversable t, Monad m) =&gt; t (m a) -&gt; m (t a)</span></a>
  <a class="sourceLine" id="cb1-173" title="173"><span class="va">+ sequenceA :: (Traversable t, Applicative f) =&gt; t (f a) -&gt; f (t a)</span></a>
  <a class="sourceLine" id="cb1-174" title="174"><span class="st">- sequence_ :: Monad m =&gt; [m a] -&gt; m ()</span></a>
  <a class="sourceLine" id="cb1-175" title="175"><span class="va">+ sequence_ :: (Foldable t, Monad m) =&gt; t (m a) -&gt; m ()</span></a>
  <a class="sourceLine" id="cb1-176" title="176"><span class="st">- sum :: Num a =&gt; [a] -&gt; a</span></a>
  <a class="sourceLine" id="cb1-177" title="177"><span class="va">+ sum :: (Foldable t, Num a) =&gt; t a -&gt; a</span></a>
  <a class="sourceLine" id="cb1-178" title="178"><span class="va">+ traverse :: (Traversable t, Applicative f) =&gt; (a -&gt; f b) -&gt; t a -&gt; f (t b)</span></a>
  <a class="sourceLine" id="cb1-179" title="179"></a>
  <a class="sourceLine" id="cb1-180" title="180">Data.Function</a>
  <a class="sourceLine" id="cb1-181" title="181"><span class="va">+ (&amp;) :: a -&gt; (a -&gt; b) -&gt; b</span></a>
  <a class="sourceLine" id="cb1-182" title="182"></a>
  <a class="sourceLine" id="cb1-183" title="183">Control.Monad.Instances</a>
  <a class="sourceLine" id="cb1-184" title="184"><span class="st">- class Monad m</span></a>
  <a class="sourceLine" id="cb1-185" title="185"><span class="va">+ class Applicative m =&gt; Monad m</span></a>
  <a class="sourceLine" id="cb1-186" title="186"></a>
  <a class="sourceLine" id="cb1-187" title="187">Data.Coerce</a>
  <a class="sourceLine" id="cb1-188" title="188"><span class="st">- coerce :: Coercible k a b =&gt; a -&gt; b</span></a>
  <a class="sourceLine" id="cb1-189" title="189"><span class="va">+ coerce :: Coercible * a b =&gt; a -&gt; b</span></a>
  <a class="sourceLine" id="cb1-190" title="190"></a>
  <a class="sourceLine" id="cb1-191" title="191">Data.Version</a>
  <a class="sourceLine" id="cb1-192" title="192"><span class="va">+ makeVersion :: [Int] -&gt; Version</span></a>
  <a class="sourceLine" id="cb1-193" title="193"></a>
  <a class="sourceLine" id="cb1-194" title="194">Data.Complex</a>
  <a class="sourceLine" id="cb1-195" title="195"><span class="st">- cis :: RealFloat a =&gt; a -&gt; Complex a</span></a>
  <a class="sourceLine" id="cb1-196" title="196"><span class="va">+ cis :: Floating a =&gt; a -&gt; Complex a</span></a>
  <a class="sourceLine" id="cb1-197" title="197"><span class="st">- conjugate :: RealFloat a =&gt; Complex a -&gt; Complex a</span></a>
  <a class="sourceLine" id="cb1-198" title="198"><span class="va">+ conjugate :: Num a =&gt; Complex a -&gt; Complex a</span></a>
  <a class="sourceLine" id="cb1-199" title="199"><span class="st">- imagPart :: RealFloat a =&gt; Complex a -&gt; a</span></a>
  <a class="sourceLine" id="cb1-200" title="200"><span class="va">+ imagPart :: Complex a -&gt; a</span></a>
  <a class="sourceLine" id="cb1-201" title="201"><span class="st">- mkPolar :: RealFloat a =&gt; a -&gt; a -&gt; Complex a</span></a>
  <a class="sourceLine" id="cb1-202" title="202"><span class="va">+ mkPolar :: Floating a =&gt; a -&gt; a -&gt; Complex a</span></a>
  <a class="sourceLine" id="cb1-203" title="203"><span class="st">- realPart :: RealFloat a =&gt; Complex a -&gt; a</span></a>
  <a class="sourceLine" id="cb1-204" title="204"><span class="va">+ realPart :: Complex a -&gt; a</span></a>
  <a class="sourceLine" id="cb1-205" title="205"></a>
  <a class="sourceLine" id="cb1-206" title="206">Data.Foldable</a>
  <a class="sourceLine" id="cb1-207" title="207"><span class="va">+ length :: Foldable t =&gt; t a -&gt; Int</span></a>
  <a class="sourceLine" id="cb1-208" title="208"><span class="va">+ null :: Foldable t =&gt; t a -&gt; Bool</span></a>
  <a class="sourceLine" id="cb1-209" title="209"></a>
  <a class="sourceLine" id="cb1-210" title="210">System.Exit</a>
  <a class="sourceLine" id="cb1-211" title="211"><span class="va">+ die :: String -&gt; IO a</span></a>
  <a class="sourceLine" id="cb1-212" title="212"></a>
  <a class="sourceLine" id="cb1-213" title="213">Numeric.Natural</a>
  <a class="sourceLine" id="cb1-214" title="214"><span class="va">+ data Natural</span></a>
  <a class="sourceLine" id="cb1-215" title="215"></a>
  <a class="sourceLine" id="cb1-216" title="216">Data.Bifunctor</a>
  <a class="sourceLine" id="cb1-217" title="217"><span class="va">+ bimap :: Bifunctor p =&gt; (a -&gt; b) -&gt; (c -&gt; d) -&gt; p a c -&gt; p b d</span></a>
  <a class="sourceLine" id="cb1-218" title="218"><span class="va">+ class Bifunctor p</span></a>
  <a class="sourceLine" id="cb1-219" title="219"><span class="va">+ first :: Bifunctor p =&gt; (a -&gt; b) -&gt; p a c -&gt; p b c</span></a>
  <a class="sourceLine" id="cb1-220" title="220"><span class="va">+ second :: Bifunctor p =&gt; (b -&gt; c) -&gt; p a b -&gt; p a c</span></a>
  <a class="sourceLine" id="cb1-221" title="221"></a>
  <a class="sourceLine" id="cb1-222" title="222">Data.Functor.Identity</a>
  <a class="sourceLine" id="cb1-223" title="223"><span class="va">+ Identity :: a -&gt; Identity a</span></a>
  <a class="sourceLine" id="cb1-224" title="224"><span class="va">+ newtype Identity a</span></a>
  <a class="sourceLine" id="cb1-225" title="225"><span class="va">+ runIdentity :: Identity a -&gt; a</span></a>
  <a class="sourceLine" id="cb1-226" title="226"></a>
  <a class="sourceLine" id="cb1-227" title="227">Data.Void</a>
  <a class="sourceLine" id="cb1-228" title="228"><span class="va">+ absurd :: Void -&gt; a</span></a>
  <a class="sourceLine" id="cb1-229" title="229"><span class="va">+ data Void</span></a>
  <a class="sourceLine" id="cb1-230" title="230"><span class="va">+ vacuous :: Functor f =&gt; f Void -&gt; f a</span></a></code></pre></div>]]></summary>
</entry>

</feed>
