News for hackers. Stuff that matters.
Re:What Monoids teach us about software by rbehrends on Wednesday September 13, 2017 @02:16PM (#15241020)
The problem with this argument is that a monoid is one of the most basic constructs you can find in abstract algebra. Using monoids as a shorthand for a couple of simple axioms doesn't buy you much; heck, the shape example didn't even have a use case for the neutral element, so all it basically said was: "intersection is associative". Just using a whole lot more words.

Had the shapes example be modified to describe (say) a topological space via open sets, you may have a point.

Re:What Monoids teach us about software by tikhonj on Wednesday September 13, 2017 @07:15PM (#15243630)
The real advantage is that we can express the abstraction in the language, which lets us write libraries around it. A single function like fold is much nicer than either having one per type or having to pass in the operation and identity manually:

    fold :: (Monoid m, Foldable t) => t m -> m
The fact that monoids are so simple is actually what makes this powerful: fold works for a large set of the types I use day-to-day, whether they're from the base library or specific to my own codebase. There is only a handful of other generic operations that rely on the Monoid class, but that's enough to make the class quite useful. It's a simple abstraction that applies to a lot of different types.

I actually did a count recently; something like 30% of our modules import Data.Monoid. Many of them were just using the (<>) operator, but that's useful in and of itself: no need to define a distinct operator for each of our types that wants one.

It's a simple, incredibly general abstraction that pulls its weight—partly because it doesn't have much to pull.

Re:What Monoids teach us about software by mbrock on Wednesday September 13, 2017 @02:26PM (#15241129)
The cool thing about algebra is that it lets you reuse concepts and computations between elementary arithmetic and a wide range of abstract or complex structures. It's probably true that most programs use only very simple monoids, but look at e.g. this article to see how composition of monoids can be a very powerful design pattern in functional programming.

http://repository.upenn.edu/cgi/viewcontent.cgi?article=1773...

Re:What Monoids teach us about software by rbehrends on Wednesday September 13, 2017 @02:30PM (#15241174)
And the article introduces a whole lot more concepts, such as homomorphisms in order to be sufficiently interesting. I don't say that you can't build interesting stuff on top of monoids (I have several colleagues that are doing just that), just that the concept of a monoid by itself isn't very interesting or deep.
Re:What Monoids teach us about software by runeks on Wednesday September 13, 2017 @03:04PM (#15241513)
The goal of programming is to reduce all parts to such simplicity that they become boringly simple, and thus impossible to get wrong. Concepts are only interesting while we don’t fully understand them yet; when we’re done fully understanding them, they become boring and we move on to the next thing. Thus, the solution to programming is to compose programs out of boringly simple parts, using a boringly simple method of composition.
Re:What Monoids teach us about software by kbenson on Wednesday September 13, 2017 @03:37PM (#15241827)
> The goal of programming is to reduce all parts to such simplicity that they become boringly simple, and thus impossible to get wrong.

No, that's a strategy to achieve a goal of programming. Programming is about using machines that accept well defined instructions to automate processes for people. A complex and poorly understood program that still ends up reducing the overall amount of work required for individuals is still worthwhile, even if not reduced to being boringly simple.

Re:What Monoids teach us about software by lou1306 on Thursday September 14, 2017 @04:54AM (#15246084)
The main selling point is that reduction and "boring simplicity" can lead to a formally correct program, or at least one where most errors are caught by static analysis/type checking/etc.
Re:What Monoids teach us about software by kbenson on Thursday September 14, 2017 @12:35PM (#15249644)
I'm not saying it's not worth while, I'm just saying that it's not the goal (in general. Some academic exercises might have goals such as that because the point is to learn).

It's sort of like saying the goal of owning a store is to match inventory to sales as closely as possible. That's a strategy or sub-goal towards reaching the real goal at best, which is to make enough money that the store pays for itself and hopefully supports the owner. Some stores don't even have inventory, so that strategy doesn't even apply to them. Similarly, that programming strategy may not apply well to some problems (and some problems cannot be proven formally correct, at least in whole).

My original reply was really just meant to point out that what they viewed as the goal of programming, is really just one of many competing strategies for producing software, and many others don't care about that at all.

Re:What Monoids teach us about software by rbehrends on Wednesday September 13, 2017 @03:37PM (#15241825)
And how do monoids play into this? This is just a vague generality; unless you can show exactly what more complex programming concepts you can reduce to monoids and how that helps with understanding programming, that doesn't mean much.
Re:What Monoids teach us about software by mbrock on Wednesday September 13, 2017 @02:39PM (#15241263)
Sure. It's funny with these algebraic things. If they're simple, people complain that they're not interesting... but when you add some depth, people complain that it's too difficult!
Re:What Monoids teach us about software by tome on Wednesday September 13, 2017 @04:08PM (#15242099)
"I don't want to have to understand abstract algebra to use Haskell!"

"Abstract algebra is too simple to bother writing programming blogs about!"

Re:What Monoids teach us about software by dllthomas on Thursday September 14, 2017 @11:04AM (#15248721)
To be fair, it may not be the same people.
Re:What Monoids teach us about software by tome on Thursday September 14, 2017 @01:51PM (#15250570)
I'm sure it's not! But even so it's interesting to be caught between these two antipodes. As your probably know there's another group of people, Andrej Bauer among them, who provide a different antithesis to "I don't want to have to understand category theory to use Haskell!" by exclaiming "Haskell doesn't use real category theory!".

It's just generally quite amusing to be caught in the middle of all this.

Re:What Monoids teach us about software by coldtea on Wednesday September 13, 2017 @02:43PM (#15241310)
>The problem with this argument is that a monoid is one of the most basic constructs you can find in abstract algebra.

That's irrelevant, because we are not naming them to use them in abstract algebra, but as abstract constructs in a different domain with its own complexity (programming).

Re:What Monoids teach us about software by rbehrends on Wednesday September 13, 2017 @02:55PM (#15241430)
The lack of complexity comes from the fact that monoids are just defined by two simple axioms. Using them in a different domain does not change that. Associativity or having a neutral element do not become more complicated concepts in computer science.
Re:What Monoids teach us about software by coldtea on Wednesday September 13, 2017 @03:30PM (#15241735)
>The lack of complexity comes from the fact that monoids are just defined by two simple axioms. Using them in a different domain does not change that.

You'd be surprised. A pointer (as in C) is an even simpler notion, but the complexity it represents to new programmers is big.

Complexity in a programming element from the interplay it has with everything else -- not from it isolated.

Re:What Monoids teach us about software by rbehrends on Wednesday September 13, 2017 @03:40PM (#15241849)
The original argument was that naming them "monoids" reduces cognitive load. Can you explain how exactly you see this reduction in cognitive load coming to pass in programming? Because from your previous two responses, I don't see it. What aspects of associative operations with a neutral element are so central to teaching programming that naming such a construct "monoid" gives us a stepping stone towards better understanding programming?
Re:What Monoids teach us about software by coldtea on Wednesday September 13, 2017 @05:06PM (#15242686)
>The original argument was that naming them "monoids" reduces cognitive load. Can you explain how exactly you see this reduction in cognitive load coming to pass in programming?

My original argument (in this thread above) is that identifying patterns like monoids (and monads) gives more power through better abstractions.

Not that it's simpler than not knowing those abstractions (obviously assembler which ditches all abstractions is as simple as it gets conceptually).

>What aspects of associative operations with a neutral element are so central to teaching programming that naming such a construct "monoid" gives us a stepping stone towards better understanding programming?

It's about unifying similar usage patterns and having a mechanism to treat things like optionals (Maybe), errors, IO etc in a uniform way.

As a simplified no-monady example, consider languages with a string distinction between primitives (ints, etc) and objects (like Java, at least before generics/auto-boxing). A language that comes and treats both of these things (primitives and objects) as the same -- all objects, is an eye opener, and allows for more kinds of expressions than a language that treats them as distinct kinds of variables does.

Re:What Monoids teach us about software by argv_empty on Wednesday September 13, 2017 @04:14PM (#15242160)
The original argument was that naming them "monoids" reduces cognitive load.

The argument as I see it written is only that naming them reduces cognitive load. I don't see anyone suggesting that the specific name they happen to have carries any special power.

Re:What Monoids teach us about software by rbehrends on Wednesday September 13, 2017 @04:38PM (#15242374)
In what way is the specific name relevant? What alternative name instead of monoid for a monoid do you have in mind that would accomplish this goal?
Re:What Monoids teach us about software by argv_empty on Thursday September 14, 2017 @08:18AM (#15247169)
Has someone claimed to have a better name?
Re:What Monoids teach us about software by tome on Wednesday September 13, 2017 @04:12PM (#15242142)
Which is easier to comprehend at a glance?

* Under my Foo API you are able to combine Bars monoidally

* Under my Foo API you are able to combine Bars using baz such that `baz (baz bar1 bar2) bar3 == baz bar1 (baz bar2 bar3)` for all `bar1, bar2, bar3 :: Bar`, and also `baz == Bar quux baz == Bar baz quux` for all `baz :: Bar`.

Re:What Monoids teach us about software by rbehrends on Wednesday September 13, 2017 @04:36PM (#15242359)
How often do you document an API in such a way? Does the frequency with which this occurs justify introducing new terminology? Plus, you artificially inflated the wording to make "x is associative and has a neutral element y" look more complicated than it actually is, while not even naming in your "short" case what the operation and neutral element are.
Re:What Monoids teach us about software by marcosdumay on Wednesday September 13, 2017 @06:42PM (#15243418)
If you are writing Haskell, you document your code this way first¹, and what is missing from this kind of definition you write down with words.

It's incredibly useful, since you can just go into Hoogle or some similar tool and query a function that:

    Monoid m => (a -> m) -> [a] -> m
And it will tell you it's called `foldMap`, and is available by default. Not long ago I have made this exact query because I forgot the name.

1 - Well, the autodoc write those comments for you.

Re:What Monoids teach us about software by romwell on Wednesday September 13, 2017 @07:22PM (#15243667)
I think at this point, I'll settle on "code people like taking fancy words from math people and running with them".

Associativity is a boring word, because high-schoolers have seen it.

Eh. Your patience here is admirable.

Re:What Monoids teach us about software by tome on Wednesday September 13, 2017 @04:54PM (#15242559)
> How often do you document an API in such a way?

Every time `instance Monoid Foo` occurs in the Haddock documentation of a Haskell datatype which is pretty often!

> Does the frequency with which this occurs justify introducing new terminology?

"Justify" according to what set of criteria? Personally I prefer it.

> you artificially inflated the wording to make "x is associative and has a neutral element y" look more complicated than it actually is

So your suggestion is

* baz is associative and quux is a neutral element for it

Really, if you're going to go that far you may as well go all the way and just say

* Foo is a Monoid under baz and quux

> while not even naming in your "short" case what the operation and neutral element are.

Well, in the Haskell world they're implied by the typeclass instance, but I take your point.

Re:What Monoids teach us about software by Veedrac on Wednesday September 13, 2017 @04:36PM (#15242360)
"`baz` is associative and `quux` is an identity element."

(Though normally the latter would be more like "`quux` is a noop", "`quux` is an empty set", or whatever domain-specific term you need, and the associativity claim would be a brief parenthetical inside meaningful documentation.)

Re:What Monoids teach us about software by dmitriid on Wednesday September 13, 2017 @04:43PM (#15242435)
It depends on what knowledge you have. To me "monoidally" could just as well be "bryllyg"[1] for all it's worth.

I think jQuery satisfies monoids most of the time, and it worked and could be explained just fine without saying the word "monoidally" even once.

[1] From Jabberwocky, of course

Re:What Monoids teach us about software by romwell on Wednesday September 13, 2017 @07:15PM (#15243629)
How about this:

"Under my Foo API, Bars are combined associatively".

Associativity is a word. It's also a word that is taught to people in high schools. So use that.

Re:What Monoids teach us about software by tome on Thursday September 14, 2017 @03:16AM (#15245597)
That's absolutely fine. Then you also have to mention that there's an identity element. Once you've used the words "associatively" and "identity" it's no longer scary to use the word "monoid" so you may as well use it.
Re:What Monoids teach us about software by romwell on Thursday September 14, 2017 @02:09PM (#15250775)
Well, you still have to use the words "associativity" and "identity" when you describe the operation and say what the identity element is. You can't just say that you can "combine" elements "monoidally" for it to mean anything.

Also, the elements are still combined associatively. With closure and and identity properties, the set forms a monoid under this operation. "Monoidness" is not a property of operation (in a way that the language is usually used, at least in math).

For that matter, "combine monoidally" is a phrase that has 2 Google hits at the moment, which indicates to me that in real life, you still have to resort to less-shiny words to describe things.

Re:What Monoids teach us about software by tome on Friday September 15, 2017 @02:47AM (#15255135)
> You can't just say that you can "combine" elements "monoidally" for it to mean anything. ... For that matter, "combine monoidally" is a phrase that has 2 Google hits at the moment, which indicates to me that in real life, you still have to resort to less-shiny words to describe things.

There doesn't seems anything hugely objectionable to me in saying "Bars combine monoidally". But if you prefer, one can say "Bar forms a monoid under baz and quux".

It may not be obvious to you that it increases simplicity merely to combine two words into one but across an ecosystem of perhaps a thousand Monoid instances it indeed is.

Re:What Monoids teach us about software by Terr_ on Wednesday September 13, 2017 @04:35PM (#15242336)
* Under my Foo API you are able to combine Bars just like adding numbers.

But seriously, the use of Bar/Baz etc. in this example makes it more convoluted, rather than less, precisely because the names offer no hint for how to understand the nature of each thingy.

Re:What Monoids teach us about software by tome on Wednesday September 13, 2017 @04:55PM (#15242573)
> Under my Foo API you are able to combine Bars just like adding numbers.

No. Firstly, Bars may not be commutative. Secondly, would you really say that taking the unions of sets is "just like adding numbers"?