Newsletter 8: Free Monads and Free Courses

Hi,

There’s just time to squeeze in April’s newsletter before the month is over. In this edition we discuss the free monad and let you know about some free events running at Scala Days Amsterdam.

Our mini-series on error handling last newsletter had a great reception. We decided to repeat the idea, this time focusing on a more advanced topic: the free monad.

The free monad is one of those functional programming tools that from the outside seems both incredibly powerful (see Facebook’s Haxl and Twitter’s Stich for compelling applications) and incredibly unapproachable (if “left adjoint to a forgetful functor” means anything to you, I suggest you don’t need a tutorial on the free monad).

What we’ve tried to do is break down the free monad to its essential elements, and show that when you remove the jargon there are just a few simple concepts involved.

Continue reading…

Monadic IO: Laziness Makes You Free

Understanding monads is a puzzle with many parts. Understanding the monad interface was easy enough for me, as I’d been programming in functional languages for a while when I first started exploring them, but for a long time I didn’t understand how they made IO operations pure. The answer is to add an extra wrinkle, usually glossed over in Haskell oriented sources, by making all IO actions lazy. It this article we’re going to explore how this works and undercover some surprising relationships to the free monad, which we have been covering in recent posts.

Continue reading…

Deriving the Free Monad

The free monad is defined by this structure1:

sealed trait Free[F[_], A]
final case class Return[F[_], A](a: A) extends Free[F, A]
final case class Suspend[F[_], A](s: F[Free[F, A]]) extends Free[F, A]

We can use the free monad without understanding its implementation, but to really understand it we need to know why it is defined this way.

It certainly wasn’t obvious to me why this is the correct definition, and reading the literature quickly devolved into “doughnoids in the category of pretzelmorphisms” land. Here I want to present an explanation aimed at programmers that doesn’t involve abstract alphabet-soup.

  1. There are other ways of defining the free monad, but this is the most common in my reading. 

Continue reading…