Question

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.
Était-ce utile?

La solution

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.

Autres conseils

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.

Ertes answer as well as John L's are great. I just want to add something about functors and monoids: I believe that much of the Haskell terminology, while virtuous in its precision, can be a bit off-putting to new Haskell programmers. I always tell newcomers that monoids can be thought of as "appendables" and functors as "mappables". Obviously, there is some loss to this simiplification but it helps get over the initial lexical hurdles of the languages. The monoid interface (typeclass) has the "append" and "identity" functions, whereas the functor just specifies a map function. There is some slippage between the perennial idea of appending and mapping (for instance summation is a kind of appending) but the basic idea holds.

Taken as just simple interfaces for appending and mapping, monoids and functors quickly reveal themselves to have many uses: any time your data structure needs to support appending or mapping, you have a time wherein making your data strcuture an instance of monoid or functor could simplify the process.

Hope that was helpful.

As an afterward, here is a list of libraries you were asking about.

Functors: Look at a parsing library like attparsec. http://hackage.haskell.org/package/attoparsec-0.10.0.2 Functors allow you to easily compose parsers so that you can write easy-to-compose, easy-to-read parsers for even complicated data. Contrast an attoparsec parser to a comparable regex!

Monoid: Look at any array, vector library (http://hackage.haskell.org/packages/archive/vector/0.9/doc/html/Data-Vector.html) to see the uses of Monoid to implement the appendability of monoids. Also, this is a great article for putting monoids to work for you http://blog.sigfpe.com/2009/01/haskell-monoids-and-their-uses.html

Monads: look at Data.Binary - a simple and fundamental Haskell library - for a perfect use case of Monads. http://hackage.haskell.org/packages/archive/binary/0.4.1/doc/html/Data-Binary.html By using monads, you can write complicated series of instructions for parsing binary files in an almost-imperative fashion.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top