Harness the Power of the Union Type (part 1)
Tags: elm, functional
Published: 2017-11-14 14:08:31

---
title: Harness the Power of the Union Type (part 1)
published: true
description: Using the Elm Union Type to simplify functional programs
tags: elm, fp
canonical_url: http://marshallformula.codes/harness-the-power-of-the-union/
cover_image: https://images.unsplash.com/photo-1448745799564-e2c1ed534c94?ixlib&#x3D;rb-0.3.5&amp;q&#x3D;80&amp;fm&#x3D;jpg&amp;crop&#x3D;entropy&amp;cs&#x3D;tinysrgb&amp;w&#x3D;1080&amp;fit&#x3D;max&amp;s&#x3D;9d1d8f1d618549db654bd16227baeade
---

Originally posted at [marshallformula.codes](https://marshallformula.codes/harness-the-power-of-the-union/)

---



I've been using elm at [work](https://volumeintegration.com) for about a year and a half at this point. Elm was my first experience in a purely functional language and I'm still deeply smitten.  I'm still only beginning to scratch the surface of what functional programming has to offer. I keep discovering these new gems of insight and making new cognitive connections. One such recent discovery was the inherent power of the [Union Type](https://guide.elm-lang.org/types/union_types.html).

## A point of Clarification
Technicaly speaking - the construct that Elm uses is a [Tagged Union Type](https://en.wikipedia.org/wiki/Tagged_union):
> a data structure used to hold a value that could take on several different, but fixed, types. Only one of the types can be in use at any one time, and a tag field explicitly indicates which one is in use

Additionally - other [languages](https://wiki.haskell.org/Algebraic_data_type) call it an [Algebraic Data Type](https://en.wikipedia.org/wiki/Algebraic_data_type) (or ADT).

## How to use it

Before we get to some concrete examples - I'd like to clear up some misconceptions that I started with when first learning about Union Types in elm.  Here's what a Union Type looks like:

```haskell
type Shape
    = Cirle
    | Rectangle
    | Triangle
```

Based on the definition above and this example - you might be assuming (as I was) that this is basically just an enum.  This is not a bad place to start. You can definitely use a Union Type in the same manner as an enum, but you would be missing the real power that it provides.

It may help first to discuss some of the terminology when talking about Union Types. In our example above, the type is a `Shape` and each of the specified values of `Shape` are the **type constructors**.

```haskell
type Shape     -- Type definition
    = Circle   -- Circle Shape constructor
    | Rectangle   -- Rectangle Shape constructor
    | Triangle -- Triangle Shape constructor
```

Coming from an Object-Oriented background - this delineation of terms is what really made things start to click for me. The constructors are used to create a `Shape` value - just like a class (which can have multiple constructors) would use constructors to instantiate a class object. Now that's just about where the similarities end so don't take that concept too far, but perhaps that association might trigger some further understanding as it did for me.

So lets start using our new `Shape` type:
```haskell
makeCircle : Shape
makeCircle =
    Circle
    
makeRectangle : Shape
makeRectangle =
    Rectangle
    
-- you get it
```

Ok.  Admittedly that's pretty underwhelming. But it does demonstrate how to use the `Shape`'s constructors to create new `Shape` types.  Let's make it a little more interesting.

### Tag Payload

So far - our `Shape` is not much more than an enum. If you actually continued reading the [wikipedia entry referenced above](https://en.wikipedia.org/wiki/Tagged_union) you might have seen this snippet:
> An enumerated type can be seen as a degenerate case: a tagged union of unit types. It corresponds to a set of nullary constructors and may be implemented as a simple tag variable, since it holds no additional data besides the value of the tag.

Did that last sentence imply that unlike an enum type - the tag of a union type can hold data?  Why yes it did.  Nice catch.  

This is where the real power of the union type comes in to play.  A union type's constructors can specify additonal data (think arguments to a constructor function in OO) that are required to construct that version of the union type. Now we can make our `Shape` a little more useful. 

```haskell
type Shape
    = Circle Int
    | Rectangle Int Int
    | Triangle Int
```

Neat! Now the `Circle` can store it's radius, the `Rectangle` can store it's height and width, and the `Triangle` can store the length of it's sides (we'll keep things simple for now and only allow equilateral triangles).

Now - you might be arguing that it's not completely obvious that the `Rectangle`'s two constructor arguments are meant to be the width and height - since all we see is that it takes two `Int` values. Here's a tip... `alias` is your friend:

```haskell
type alias Radius = Int
type alias Height = Int
type alias Width = Int
type alias Side = Int

type Shape
    = Circle Radius
    | Rectangle Height Width
    | Triangle Side
```

That might be a little overkill for our simple example - but it illustrates how constructor definitions can be very explicit. I use the this technique anytime that my constructor arguments might be ambigious or confusing. I've found it incredibly valuable.

We'd better fix our creator functions to reflect our new type constructors:

```haskell
makeCircle : Radius -> Shape
makeCircle radius =
    Circle radius
    
makeRectangle : Height -> Width -> Shape
makeRectangle height width =
    Rectangle height width
    
makeTriangle : Side -> Shape
makeTriangle side =
    Triangle side
``` 

---

Ok - on to the really good stuff. Now that we have different type constructors - we need a way to retrieve data we're storing in the type. In order to do that we need to use [pattern matching](https://gist.github.com/yang-wei/4f563fbf81ff843e8b1e).

So lets create function that will find the perimiter of a given shape:

```haskell
pi = 3 -- rounding for simplicity

findPerimeter : Shape -> Int
findPerimeter shape =
    case shape of 
        Circle radius -> 
            2 * pi * radius
            
        Rectangle height width ->
            (height * 2) + (width * 2)
            
        Triangle side ->
            (side * 3) -- again equilateral triangle for simplicity
            
            
-- let's try it out

findPerimeter (makeCircle 5)        -- 30

findPerimiter (makeRectangle 2 10)  -- 24

findPerimeter (makeTriangle 6)      -- 18
```

Pretty sweet right? But what happens when we get a new requirement to support a new shape... say a `Hexagon`.  Well, let's just add a new constructor then:

```haskell
type Shape
    -- other types already defined here
    | Hexagon Side -- equal sides for simplicity
```

Easy Peasy. Now, perhaps you thought we had already exposed all of the awesomeness that union types had to offer? Good news... 

![ron](https://marshallformula.codes/content/images/2017/11/ron.gif)

Yep!  More awesome headed your way.  You see we've just added a new type constructor, however we forgot to account for it in the `findPerimeter` function.  You see - unlike in other languages, or by using an enum - our friendly elm compiler simply won't let this stand. 
            
```
-- MISSING PATTERNS ---------------------------------------------

This `case` does not have branches for all possibilities.

|>    case shape of
|>        Circle radius ->
|>            2 * pi * radius
|>
|>        Rectangle height width ->
|>            (height * 2) + (width * 2)
|>
|>        Triangle side ->
|>            (side * 3)

You need to account for the following values:

    Hexagon _

Add a branch to cover this pattern!
```

What an incredibly helpful and informative compiler! It just saved us from our own folly. Whenever you use a union type, the Elm compiler checks to make sure you've covered all of the possibilities leading to fewer [footguns](https://en.wiktionary.org/wiki/footgun), and therefore - fewer bugs.

---

In part 2, I'll be extracting even more magic from the union type. I'll be exposing some of the unique benefits of using union types when paired with concepts like recursion, encapsulation, and polymorphism.

All of the code discussed here is available in [this ellie](https://ellie-app.com/sF8YfTW2ca1/0).