Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using monads, monoids, functors and arrows in practice

I recently ran into this post about useful resources for different aspects of functional programming, such as monads and monoids, etc.

But the question is - what use can an average programmer make out of such concepts. I often run into "academic" researches about those issues. However, I've never met in practice (in a real project) anyone using them.

So the question is - are there any widely-used open-source projects in Haskell that really make use of such things, such projects that demonstrate the actual necessity of this concepts in "production" software, not in the "academic" software written "just for fun". It would be cool to make a list like this:

  • Monads - used in projects like A and B because otherwise such piece of code would look much more complicated.
  • The same for monoids.
  • The same for functors.
  • The same for arrows.
like image 295
SPIRiT_1984 Avatar asked Nov 28 '11 11:11

SPIRiT_1984


People also ask

What are functors and monads?

A functor is an interface with one method i.e a mapping of the category to category. Monads basically is a mechanism for sequencing computations. A monad is a way to wrap stuff, then operate on the wrapped stuff without unwrapping it.

What does -> mean in Haskell?

(->) is often called the "function arrow" or "function type constructor", and while it does have some special syntax, there's not that much special about it. It's essentially an infix type operator. Give it two types, and it gives you the type of functions between those types.

Why are functors useful?

Functors are also important because they are a building block for applicatives and monads, which are coming in future posts.

What are monads in functional programming?

In functional programming, a monad is a software design pattern with a structure that combines program fragments (functions) and wraps their return values in a type with additional computation.


2 Answers

Many of these concepts are so implicit in Haskell code it's easier to list examples that don't use them (assuming you can find one). Every Haskell program uses monads, at least for IO.

All of these are widely used because they're abstractions that appear very frequently in code. Consider functors: mapping over a containers is a fairly common need, so it makes sense to have a single interface for any container-like data structure, which is exactly what Functor provides. It happens that even the concept of a "container" is more concrete than the functor abstraction, but hopefully this demonstrates the point.

Monads: The XMonad window manager is a widely-used program which makes extensive use of monad transformers and the zipper structure. STM is a library that provides a new monad with useful properties.

Monoids: the Sequence structure in the containers package is implemented with monoids. Also, monoids are widely used to model sets, lists, and similar because the two monoid operations provide an empty list and concatenation (or an empty set and union).

Arrows: Yampa and HXT (Haskell XML Toolbox) immediately come to mind.

Functors show up everywhere. It's fairly common for monadic code to have a lot of <$>s, which means that the Functor instance is in use. Most Haskell parsers make heavy use of functors.

like image 198
John L Avatar answered Sep 21 '22 16:09

John L


I'm using arrows and monads (and thus also functors) productively for real world applications. My functional reactive programming (FRP) library Netwire combines all four concepts you mentioned and more, and FRP itself is also a design pattern, which you usually know from academics. These are the concepts used:

  • Arrows and arrow transformers: Netwire provides the Wire type, which is an arrow transformer.
  • Monads and monad transformers: Wire usually transforms to a stack of monad transformers wrapped by a Kleisli arrow.
  • Wire inhibition (like exceptions or non-happened events) uses a monoid.

Version 3 is about to be released (I hope today), which will also bring (non-associated) type families into the game.

like image 32
ertes Avatar answered Sep 18 '22 16:09

ertes