Published: 2012-01-28
Tagged: functional programming, haskell progrmming language, mathematics, monoids, patterns

Let me introduce the the Monoid typeclass in the Haskell programming language.

An Introduction to Monoids in Haskell

Monoids are a simple algebraic structure in which operations can be composed. The Haskell programming language features a typeclass with the same name that is very closely modeled after the mathematical notion. The class provides a standard mechanism to compose operations over a range of types.

Why Care about Monoids in Programming?

It should be obvious but let me reference one particular impact. The famous Ruby on Rails web-framework features composition of SQL statements in an algebraic way1. I highly recommend the reading of Magic Scaling Sprinkles - Why Arel? where the author describes the motivation behind creating the framework. While the effort was ignited in a different context (Scheme, through the seminal Wizard Book), the mentioned "closure under composition" is nothing but a monoidal structure2.

Prerequisites

It is recommended to have basic knowledge about types and classes in Haskell. It would also be beneficial to know how functions, including their type signature, fit into the system.

Here we go

The clip doesn't contain any sound. This is quite deliberate for several reasons. You should adjust the playback speed to your needs and pause when you need to contemplate. It would be annoying and disruptive if the video included sound.

Where to go from Here

The official GHC Data.Monoid documentation is a concise reference. The accompanying source code is quite enlightening. A very concise yet thorough introduction to Haskell is given in Hutton's book Programming in Haskell; however, monoids are not covered. Monoids are treated in an own chapter in Lipovača's book Learn You a Haskell for Great Good!. They also appear briefly in Real World Haskell.

About this Series

This is the first contribution of a series.

  1. An Introduction to Monoids in Haskell
  2. An Introduction to Functors in Haskell
  3. An Introduction to Applicative Functors in Haskell
  4. An Introduction to Monads in Haskell
  5. An Introduction to Performing IO with the IO-Monad in Haskell

  1. Arel is probably the most popular but surely not the first of its kind. It is predated by HaskellDB, for example.

  2. Relation algebras as expanded FL-algebras is a starting point for further exploration of the relation between monoids and relational algebras.