chapter six

6 Domain modeling with Free monads

 

This chapter covers

  • Using Free monads to model business domains
  • How to apply the Functor abstraction
  • Getting familiar with some advanced type-level concepts such as GADTs

If you ask me, lifetimes are what distinguishes interface-like abstractions from genericity-like ones, such as functors, monoids, categories. There are many ways to generalize a data structure, but none of them bother about lifetimes. Generalizations don’t have a dimension of a lifetime because they are just timeless expressions over data structures. Generalizations focus on some properties and ignore others, they exhibit a math nature of a structure, but they don’t “work” in a common way. They rather “declare”. They just exist on the design time and on the compile time and mostly disappear after the program is compiled.

I would also state that only interface-like abstractions help us to handle complexity. Unlike generalizations, interfaces carry some meaning from a business domain, and their purpose is to give us the minimum of information needed for the tasks. No implementation details, nothing unrelated, nothing too abstract. Interfaces communicate the essence of that domain, and often they serve as documentation. Knowing your interface means knowing a part of your business domain, at least its public representation.

Figure 6.1 summarized the said:

Figure 6.1 Two sorts of abstractions

6.1 Free monads overview

6.1.1 The Free monad pattern

6.1.2 Free monads versus IO

6.2 How Free monads work

6.2.1 Wrapping languages into the Free monad

6.2.2 The Functor instance for the Free monad

6.2.3 Learning the recursive Free type

6.2.4 Interpretation of Free monadic scripts

6.3 Free monadic eDSLs

6.3.1 Redesigning eDSLs with Free monads

6.3.2 Advantages and disadvantages of a free language

6.4 Summary