Practical use of monads, monoids, functors, and arrows

I recently came across this post about useful resources for various aspects of functional programming, such as monads and monoids, etc.

But the question is, what kind of use can an average programmer make of such concepts. I often come across "academic" research on these issues. However, I have never met in practice (in a real project) who used them.

So the question is, are there any open source projects in Haskell that really use such things, projects that demonstrate the urgent need for these concepts in “production” software, and not in “academic” software, written "just for fun." It would be great to make a list like this:

  • Monads - used in projects like A and B, because otherwise, such a piece of code would have looked a lot more complicated.
  • The same goes for monoids.
  • The same goes for functors.
  • The same for arrows.
+6
source share
3 answers

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

All of them are widely used because they are abstractions that very often appear in code. Consider functors: mapping by container is a fairly common need, so it makes sense to have a single interface for any data structure similar to a container, and this is what Functor provides. It happens that even the concept of “container” is more specific than an abstraction of a functor, but, hopefully, this demonstrates meaning.

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

Monoids: Sequence structure in containers package is implemented with monoids . In addition, monoids are widely used for modeling sets, lists, etc., since two monoid operations provide empty list and concatenation (or empty set and concatenation).

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

Functors appear everywhere. For monadic code, quite often there are many <$> s, which means that a Functor instance is used. Most Haskell parsers use functors.

+9
source

I use arrows and monads (and therefore also functors) productively for real-world applications. My Netwire Functional Reactive Programming (FRP) library combines all four of the concepts you talked about, and more, and FRP is also a design pattern that you generally know. 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 converted to a stack of monad transformers wrapped with the Kleisli arrow.
  • Wire inhibition (e.g. exceptions or events that have not occurred) uses a monoid.

Version 3 will be released (hopefully today), which will also bring (unrelated) family types to the game.

+6
source

The answers of Ertes as well as John L are great. I just want to add something about functors and monoids: I believe that most of Haskell's terminology, while at the same time virtuous in its accuracy, can be a little distracted by new Haskell programmers. I always tell beginners that monoids can be considered as "appendicitis" and functors as "comparable." Obviously, there is some loss for this simulation, but it helps to overcome the initial lexical obstacles for languages. The monoid interface (typeclass) has the functions “add” and “identity”, while the functor simply defines the display function. There is some slippage between the long-standing idea of ​​adding and juxtaposing (for example, summation is a kind of addition), but the main idea does occur.

As simple interfaces for adding and matching, monoids and functors quickly find that they have many uses: any time your data structure needs to support adding or matching, you have time, so your strcuture data is an instance of a monoid or functor can simplify the process.

Hope this was helpful.

As later, here is a list of libraries that you asked about.

Functors: look at a syntax library like attparsec. http://hackage.haskell.org/package/attoparsec-0.10.0.2 Functors make it easy to compose parsers so you can write simple, easy to read parsers for even complex data, Attoparsec parser contrast for comparable regular expression!

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

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

+6
source

Source: https://habr.com/ru/post/902546/


All Articles