---
title: Data Modeling Resources in Elm
teaser: Links to some of the best Elm data modeling resources around the web.
tags: elm,good code
author: Joël Quenneville
published_on: 2022-11-15
---

I get a lot of requests about data modeling resources in [Elm]. I've written and
spoken extensively on the topic as well as collected material from around the
community. Over time, I've accumulated a list of links to various articles and
talks that I share with people. Here is that list, made public, along with some
brief commentary.

![Elm logo with the words "+ data modeling"](https://images.thoughtbot.com/jq-elm-modeling/MmL2sqKHQjCjVPHgCwzs_elm-plus-data-modeling.png)

[Elm]: https://elm-lang.org

## All about types

Most data modeling in Elm is done using its excellent type system. These
articles explain various aspects of the type system and the tools that it gives
you for modeling.

* ✍[Elm Type Glossary](https://gist.github.com/JoelQ/6b303d9ad450537163b6f8f6cf8a4ed8) (by Joël Quenneville)
* ✍[Advanced Types in Elm](https://medium.com/@ckoster22/advanced-types-in-elm-opaque-types-ec5ec3b84ed2) (4-part series by Charlie Koster)
* ✍[Types Without Values](https://thoughtbot.com/blog/types-without-values) (by Joël Quenneville)

## Eliminating impossible states

🎥[Making Impossible States Impossible] is one of the defining talks of the Elm
community. Richard Feldman showcases some simple type design tricks that make it
impossible to even represent invalid states. This approach is at the heart of a
lot of domain modeling in Elm. The official Elm guide has an appendix on ✍[Types
as Sets] that digs into the more theoretical ideas of why this works.

Other resources on this topic include:

* ✍[Shaping Values with Types](https://thoughtbot.com/blog/shaping-values-with-types) (by Josh Clayton)
* 🎥[The Life of a File](https://www.youtube.com/watch?v=XpDsk374LDE) (by Evan Czaplicki)
* 🎥[Immutable Relational Data](https://www.youtube.com/watch?v=28OdemxhfbU) (by Richard Feldman)

[Making Impossible States Impossible]: https://www.youtube.com/watch?v=IcgmSRJHu_8
[Types as Sets]: https://guide.elm-lang.org/appendix/types_as_sets.html

## Primitives

Elm comes with many primitive types such as `Bool`, `Maybe`, and `Int`. While
these have their place, it's easy to overuse them - a code smell known as
**primitive obsession**. In many cases, it's better to model domain concepts with
a richer value using a custom type.

* ✍[Elm Slays a UI Antipattern](http://blog.jenkster.com/2016/06/how-elm-slays-a-ui-antipattern.html) (by Kris Jenkins)
* ✍[Booleans and Enums](https://thoughtbot.com/blog/booleans-and-enums) (by Joël Quenneville)
* 🎥[Solving the Boolean Identity Crisis](https://www.youtube.com/watch?v=6TDKHGtAxeg) (by Jeremy Fairbank) 
* ✍[Modeling with Union Types](https://thoughtbot.com/blog/modeling-with-union-types) (by Joël Quenneville)
* 🎥[A Number by Any Other Name](https://www.youtube.com/watch?v=WnTw0z7rD3E) (by Joël Quenneville)

## Using the compiler to check for invariants

Business systems are full of **invariants**. Many of these can be encoded into a
program's type system such that the compiler prevents you from writing code that
breaks certain rules. Nifty!

* ✍[Using Elm Types to Prevent Logging SSNs](https://incrementalelm.com/articles/exit-gatekeepers) (by Dillon Kearns)
* ✍[Page State in Elm](https://korban.net/posts/elm/2019-09-16-page-state-in-elm/) (by Alex Korban)
* ✍[Modeling Currency in Elm Using Phantom Types](https://thoughtbot.com/blog/modeling-currency-in-elm-using-phantom-types) (by Joël Quenneville) 
* ✍[Compile-Time Checks on Values in a List Using Phantom Types](https://korban.net/posts/elm/2020-05-18-compile-time-checks-values-lists-phantom-types) (by Alex Korban) 
* ✍[Fewer Operations on Custom Types is Valuable](https://thoughtbot.com/blog/fewer-operations-on-custom-types-is-valuable) (by Joël Quenneville)

## Type Narrowing

Often in a system, entities will start as permissive, loosely formed data and
progress to a stricter more well-defined entity. Modeling this journey in the
type system is often called **type narrowing**. The canonical resource on the
topic is Alexis King's ✍[Parse, Don't Validate].

This can take an incremental approach as explored in ✍[A Broader Take on Parsing]
where an application is modeled as a series of layers and each time data moves
up one of the layers, its type gets narrowed.

[A Broader Take on Parsing]: https://thoughtbot.com/blog/a-broader-take-on-parsing
[Parse, Don't Validate]: https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/

## General domain modeling

Still looking for more domain modeling ideas? These resources explore the
general concept of domain modeling. You might come away with some new
perspectives!

* 🎥[Domain Modeling Made Functional](https://www.youtube.com/watch?v=1pSH8kElmM4) (by Scott Wlaschin, talk is in F# but concepts carry over to Elm very well)
* ✍[Models that Match Reality](https://thoughtbot.com/blog/models-that-match-reality) (by Joël Quenneville)
* 🎥[Make Data Structures](https://www.youtube.com/watch?v=x1FU3e0sT1I) (by Richard Feldman)
