Skip to content

Instantly share code, notes, and snippets.

@CMCDragonkai
Last active January 7, 2026 14:35
Show Gist options
  • Select an option

  • Save CMCDragonkai/165d9a598b8fb333ea65 to your computer and use it in GitHub Desktop.

Select an option

Save CMCDragonkai/165d9a598b8fb333ea65 to your computer and use it in GitHub Desktop.
Haskell: Free Monad + Interpreter Pattern

Free Monad + Interpreter Pattern

It's like creating the front end and back end of a compiler inside Haskell without the need of Template Haskell!

Write your DSL AST as a Free Monad, and then interpret the monad any way you like.

The advantage is that you get to swap out your interpreter, and your main code remains pure.

A prerequisite to understanding this, is understanding how monads are monoids in the monoidal category of endofunctors:

More to come (ideas are still floating in my head).

Ideas:

  • Free is left adjoint to Forgetful
  • Free adds structure.
  • Forgetful forgets structure.
  • A free object is an object the minimum amount of laws/properties to make it a specific type of object.
  • A list is a free monoid.
  • Free objects are constructed from the Free functor.
  • Left adjoint is considered a weak inverse.
  • An adjunction is a pair of functors Free and Forgetful.
  • Monoid -> Set is Forgetful, while Set -> Monoid is Free. The functor acts like a generator. The Free functor adds just enough properties to a set so that it becomes a monoid!
  • A free monad, is a specific type of free object. It has been constructed from a Free functor. But from what? Well from some underlying object in some other category. For example an DSL AST!
  • There's a universal property (construction?) when there's an adjunction. It's just a law that must be true.
  • Algebraic structures are just some set(s) with some operations. Structure is then synonymous with operations.
@ulidtko
Copy link

ulidtko commented Jan 7, 2026

The fpcomplete link is not rotten; it's moved to https://www.schoolofhaskell.com/user/dolio/many-roads-to-free-monads

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment