How Free Monads Yield Extensible Effects

submited by
Style Pass
2021-08-17 01:00:09

The Free monad give you a Monad for any Functor. The Free monad also can be used to construct extensible effect systems. I never understood why Free can be used to construct effect systems. It turns out it is deeply connected to their ability to yield monads for functors.

Free allows you to build up an AST which describes monadic actions for any given Functor and defers the interpretation of that AST to a later date. In other words, Free breaks apart the syntax and semantics of your monadic effects and lets you describe effects syntacally without assigning those effects any sort of semantics.

Squint your eyes and ignore the Free type constructors and you can see there is a symmetry here. The chief difference is that Free's data constructors lack Applicative and Monad constraints. Free does not need those constraints because it doesn't actually perform any effects, it is merely a syntax tree describing effects yet to be interpreted.

These instances are utterly boring and essentially just serve to thread f's fmap throughout the syntactic structure of the Free data constructors.

Leave a Comment