Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inject a value into a Monoid?

Tags:

haskell

It just appeared to me that there is no way to inject a single value into a Monoid in Data.Monoid. I hesitate to use Data.Monad.return for this but was hoping to find something like singleton for several types.

like image 893
fho Avatar asked Jul 01 '13 09:07

fho


2 Answers

The Monoid class does not allow for any kind of injection since the monoid is not any kind of container. Some containers are monoids, and then they will have their own means for injection. For a relatively general injection you can use pure from Applicative, or return from Monad (the former is more general).

like image 188
augustss Avatar answered Nov 13 '22 10:11

augustss


Adding to what others have said: Int forms a monoid (in several different ways). How would you "inject" a value into Int? Well, you don't; an Int is just an Int. You could maybe use zero or something...?

Now, if something is a container, it forms a monoid. But the monoid bit doesn't help you treat it as a container; you need to try something else for that. Lots of things that aren't containers form monoids.

like image 6
MathematicalOrchid Avatar answered Nov 13 '22 09:11

MathematicalOrchid