News for hackers. Stuff that matters.
Re:What Monoids teach us about software by wyc on Wednesday September 13, 2017 @02:11PM (#15240962)
Here's the ELI5 of monoids used in programming that might help get the idea across:

Conceptually, a monoid is anything that:

- Has a "zero-value" (mempty, e.g., 0)

- Can be "appended" together (mappend, e.g., +)

- Has an identity with the zero-value: x + 0 == x, 0 + x == x

- Is associative: x + y + z == (x + y) + z == x + (y + z)

This isn't limited to just arithmetic with operators like +. You can define whole new types as monoids if they have these characteristics. For example, we can merge together configuration files by using monoid operators (in pseudo-code):

    -- Zero Value
    mempty == {}

    -- Append
    {"color": "Blue"} `mappend` {"fontSize": 12} == {"color": "Blue", "fontSize": 12}

    -- Identity
    {"color": "Blue"} `mappend`               {} == {"color": "Blue"}
                   {} `mappend` {"fontSize": 12} == {"fontSize": 12}

    -- Associativity
    {} `mappend` {"a": "b"} `mappend` {"c": "d"}
        == ({} `mappend` {"a": "b"}) `mappend` {"c": "d"}
        ==  {} `mappend` ({"a": "b"} `mappend` {"c": "d"})
There's no need to break out the really formal definitions if you're just trying to get some nice working code.

Borrowing from mathematics is beneficial because we can work with structures that have simple but useful guarantees. In Haskell, you can expect all the above to be true for correct type instances of Monoid, without the implementation details at hand. It's a core tenet of functional programming to disarm complexity with simplicity.

Re:What Monoids teach us about software by pluma on Wednesday September 13, 2017 @03:02PM (#15241498)
Thanks. This is a lot more helpful for non-math geeks than the two initial definitions in the article:

> A Monoid can be defined a tuple (M, op, e)

This definition lost me completely because I'm not even sure what "defining it as a tuple" is meant to imply. Based on your explanation it sounds like that is fancy talk for saying it is defined by having three properties.

> In a statically typed language, we can translate the notion of set into the notion of type.

The second definition again completely lost me because I'm not even sure what "set" and "type" mean in this context and being able to "translate the notion of set" is unhelpful when the definition using a set is the one I didn't understand in the first place.

I'm a programmer with over a decade of professional experience. I don't understand the article because I have no formal education in higher math nor in anything beyond basic computer science. Your explanation proves it's possible to explain these concepts with practical examples rather than academic jargon.

Re:What Monoids teach us about software by lou1306 on Wednesday September 13, 2017 @03:47PM (#15241921)
A tuple is just an ordered bag of elements. E.G. a graph is a tuple (V, E) where V (vertices) is a set and E (edges) is a binary relation between vertices.

Defining stuff as tuples makes sense, because then you can define the set of all such tuples (e.g. the set of all monoids for a given set M or the set of all graphs for the given vertices V) and formally reason about it :)

Re:What Monoids teach us about software by equalunique on Wednesday September 13, 2017 @03:13PM (#15241593)
Leading up to monads, monoids are covered in this excellent presentation: https://www.youtube.com/watch?v=ZhuHCtR3xq8
Re:What Monoids teach us about software by ebola1717 on Wednesday September 13, 2017 @04:57PM (#15242590)
> I'm not even sure what "defining it as a tuple" is meant to imply

Yeah, in a computer science situation it's needlessly obfuscatory. It's literally just an interface with a "plus" method and an identity value.

Re:What Monoids teach us about software by feanaro on Wednesday September 13, 2017 @05:32PM (#15242910)
"interface", "method"... One man's obfuscatory is another man's revelatory. It heavily depends on the "axiom set" you first learned about computer science concepts on. I would contend that "interface" and "method" are objectively more obvious concepts.
Re:What Monoids teach us about software by Retra on Wednesday September 13, 2017 @07:27PM (#15243695)
If you keep talking like that, you'll start to lose sight of the mathematics upon which your field was founded. Like when someone comes out with a fantastic new theorem about posets or monoids, you're probably going to go around thinking it doesn't bear any meaningful impact on your job.

You learn both languages. You do it because the vast majority of human invention and ingenuity consists if acts of translation. Anyone serious about working with computers should understand that.

Re:What Monoids teach us about software by ebola1717 on Wednesday September 13, 2017 @07:45PM (#15243797)
Monoids and posets have very little structure, so you really aren't gonna get deep useful theorems out of them. Maybe you can make some clever reductions, but those are hardly deep theorems.

I'm not arguing the underlying math isn't beautiful. I took grad level algebra and logic classes, so believe me when I say I know the math is awesome. But it just doesn't have any practical utility in programming, and more often than not just makes things more confusing.

edit: Also, since I majored in math, with a focus on logic, I don't think I'll have any trouble with losing sight of the foundations :)

Re:What Monoids teach us about software by Retra on Wednesday September 13, 2017 @08:12PM (#15243924)
Well, maybe not you personally then. :) But most programmers don't have a strong maths background, and yet it is a very relevant subject.
Re:What Monoids teach us about software by pluma on Thursday September 14, 2017 @05:27AM (#15246236)
Sure. Convince me that the concepts are useful and I may be willing to invest the energy to learn the maths around it to extract more value out of it.

But I'm a professional, not a CS student. I'm already spending my free time honing my craft and looking into new techniques and developments in software engineering. I can't afford spending yet more time on mostly orthogonal higher mathematics based on the hearsay of some guy on the Internet arguing it will allow me to understand a point somewhere down the line that might make me a better developer.

This is exactly the same thing people say who come to programming from playing jazz music or artistic painting or figure skating. They all claim what they did before makes them a better programmer and they're probably even right. But that doesn't mean I should pick up figure skating as a hobby at this point.

There are many different fields related to software development that can directly improve your performance as a developer. Not all of them are equally to all tasks a software developer might have to perform. I think it helps to have a mix of them, especially at the level of teams. There are times where the UX zealot can save the day and there are times where it's the one with the Master's degree in Metamathemagics. I wouldn't want to miss either of them, even they're mostly oblivious to each other's specialisation.

Re:What Monoids teach us about software by Retra on Thursday September 14, 2017 @08:42PM (#15253687)
>But I'm a professional, not a CS student.

"I get paid, I don't learn."

You want to get paid, get paid. I won't care if you fail.

I suppose Alan Turing and Charles Babbage are just shit programmers next to you.

Re:What Monoids teach us about software by bboreham on Thursday September 14, 2017 @03:07AM (#15245563)
I found "tuple" to be an odd word, when I first came across it.

Eventually I figured out it is a generalisation of "couple", "triple", "quadruple", etc. It's one of those things, but we don't want to say the number.

(I just tried googling it, and the definitions I get are very maths-y and hard to understand)

Re:What Monoids teach us about software by yanowitz on Wednesday September 13, 2017 @02:48PM (#15241356)
This is a great summary. I think it's valuable to say that learning how to map your problem domain onto instances of categories like Monoid is useful because it immediately makes your problem embarassingly parallel, because you can parallelize the addition process (just making sure to preserve overall ordering of operations). I.e., map/reduce. And if it's a commutative monoid, you don't even have to sweat the ordering.
Re:What Monoids teach us about software by dllthomas on Wednesday September 13, 2017 @06:36PM (#15243380)
It's maybe worth noting that we have a choice of how values are merged on collision. I think it's a monoid only when that choice forms a semigroup.

In the case of configurations, "right bias" (aka Last) is probably a good choice. I don't like that Haskell chose it for the general Map instance.

Re:What Monoids teach us about software by whateveracct on Wednesday September 13, 2017 @07:35PM (#15243745)
Yeah the Map monoid instance is very unfortunate :( When I came from Scala I really missed the one in scalaz (which is as you describe)

reducers has a UnionWith newtype which gives you the better Map monoid. But I think it uses a lazy unionWith, which will cause your Map to leak space when used in a foldMap over many elements :(

Re:What Monoids teach us about software by a-saleh on Thursday September 14, 2017 @01:49AM (#15245274)
Yep, when I was trying to understand the fuss about monoids (I blame Gabriel Gonzales excelent talk [1]), I made myself a little toy EDSL for configuration in Python, that was made mostly by implementing mappend for dictionaries [3] :)

[1] https://www.youtube.com/watch?v=WsA7GtUQeB8 [2] http://notes.asaleh.net/posts/monoid-pattern-in-python/ [3] http://notes.asaleh.net/posts/monoid-for-specifying-configur...

Re:What Monoids teach us about software by rmetzler on Thursday September 14, 2017 @12:21PM (#15249499)
Can I use a composite object or dict for the type?

So all you need to do is to create the null object and the add and sub function on the type? And I can use an implementation of that type as the state for something?

How can I guarantee the order? Would I need to use something like a blockchain or is a distributed database enough, as long as only I need the data and trust myself? In a cluster would it make sense to use etcd

I think, even better would be a commutative monoid, that doesn't depend on the order. It should be possible to scale the global state in a distributed system very concurrent, because of the commutativity.

I would have to store a log (easiest part) and the current state. To sync state between the nodes in a distributed system, they would have to send update messages to each other. They're able to batch process multiple messages to improve throughput.

Re:What Monoids teach us about software by gbacon on Wednesday September 13, 2017 @02:29PM (#15241162)
Re:What Monoids teach us about software by wyc on Wednesday September 13, 2017 @02:31PM (#15241185)
oops thanks
Re:What Monoids teach us about software by reese_john on Wednesday September 13, 2017 @03:59PM (#15242021)
That reminds me of Vector Spaces. Is there any relation? Are Vector Spaces monoids?
Re:What Monoids teach us about software by Jtsummers on Wednesday September 13, 2017 @04:39PM (#15242389)
tome speaks the truth. More generally, in mathematics there are hierarchies of structures [0]. In this case monoids are both groupoids/magmas (a set with a binary operation returning elements of that set), and semigroups (a set with an associative binary operation returning elements of that set). Monoids add in an identity element.

Vector spaces have addition over vectors and multiplication of vectors by scalars. Considering vector spaces and addition only, that is a monoid. It also happens to be further along this hierarchy as you have commutativity, inverses, and other properties.

A bit like the statement: all squares are rectangles, not all rectangles are squares. We can say that all vector spaces are monoids (under addition), but not all monoids are vector spaces.

[0] https://en.wikipedia.org/wiki/Algebraic_structure

Re:What Monoids teach us about software by tome on Wednesday September 13, 2017 @04:05PM (#15242080)
Yes, a vector space is a monoid under its addition.