Plurrrr

Tue 01 Feb 2022

Introduction to Free Monads

If you’ve been around Haskell circles for a bit, you’ve probably seen the term “free monads”. This article aims to introduce free monads and explain why they are useful.

To whet your appetite a little, free monads are basically a way to easily get a generic pure Monad instance for any Functor. This can be rather useful in many cases when you’re dealing with tree-like structures, but to name a few:

  • To build an AST for an eDSL using do-notation.
  • To have different semantics for the same monad in different contexts, e.g., define an interpreter and a pretty-printer for an eDSL, or have a mock interpreter in addition to a real one.
  • To build a decision-tree type structure harnessing the do-notation for non-determinism (like with lists, but for trees).

Source: Introduction to Free Monads, an article by Nikolay Yakimov.