r/functionalprogramming • • Dec 31 '22
Question Why is Semigroup called Semigroup when it’s very different than the mathematical term?
It’s a type and a combination function where as in math it’s a set and a combination function. Why choose an abstract name for something when it’s not even the same as the functionality from you borrowed the name from? Why not just pick a simple name like Combinable instead. Then it would make the most sense.
Unless I missed something here it’s a borrowed name for something that is just similar but not the same. It’s unfortunate because it makes concepts like this harder to learn because of the confusion between the mathematical meaning and the meaning in computing. It is also harder because the name is very abstract. I’m math it makes sense through because of where it inherits from etc
Please help me understand the motivation why Semigroup is a good name for such a construct
9
u/ganderso Dec 31 '22
A type can be thought of as a set of values, recovering the exact mathematical definition of a semigroup. For example, there is no difference between the set of integers under the + operation and the type
Integerunder the function+.11
u/[deleted] Dec 31 '22
Technically a semigroup in programming is a semigroup object in the category of types (cf https://ncatlab.org/nlab/show/semigroup). Likewise what we call a monoid is a monoid object in the category of types, and so on.
But I'm unclear on your objection that types aren't sets because their values aren't unique? What do you even mean by this?
4
u/Purlox Dec 31 '22
A lot of things are written in terms of sets in math because that's considered the default and it's easy to interact with sets (and make proofs and show that all math can be based on sets, etc.). But the concept of Semigroups can easily be transferred to work on types, while working pretty much the same way it works on sets. So I would say those two concepts of Semigroups are practically identical and can be called the same way even though one operates on sets and the other on types.
2
u/Accurate_Koala_4698 Dec 31 '22
The short answer is that you're ultimately working with sets of data, and types are providing additional computational context to this set of data in your program. Stated in reverse: if you strip the type level information you reduce to a set of values.
So if I take some Haskell function
I can happily pass it a set of numbers
Or a set of strings
The longer answer is type theory. What you're doing when you write an instance in a typed program is providing a proof of your implementation for that set. In the above example I didn't have to use the primitive numbers and I can write my own implementation which I assert is a valid representation of the natural numbers (or whatever other set I'm trying to express). For any particular input there's some set-theoretic model that exists to express it (ℕ, finite length strings, rational numbers, etc).
Otherwise it's just what it says on the tin. A semigroup is an algebraic structure with associativity and a binary operation, and that's what a Semigroup implementation requires. For anyone coming at it from a mathematical angle it's descriptive, and for anyone else it's simply vocabulary.
Instances of Semigroup aren't the only combinable things generally so I don't know how that creates less confusion. The exercise of creating a program that uses Semigroup is that you're constraining the behavior of your program so that it behaves like the mathematical construct and so I'm not seeing the difference in meaning you're referring to.
12
u/kinow mod Dec 31 '22
Thread locked. Multiple users tried to answer this question (thanks!), but it didn't appear to be creating a very positive/constructive discussion with OP.
19
u/joelangeway Dec 31 '22
It seems to me that you’re misunderstanding what it is about a type that can be modeled as a set. When a variable is of a type, then it’s value must be of that type. When we model a type as a set, we’re talking about the set of all the unique values that a variable of the type may convey. Two variables of the same type being assigned the same value, just means we assigned two different names to the same value from the one set of all the values that the type can convey.
Now, the set model of a type is not perfect. Even IEEE754 floating point numbers - basically any
float,double, orrealon a modern cpu - have two distinct bit representations for the value0.0and many different bit representations forNaN. The hardware makes that mostly easy for the underlying software platform, language runtime, to smooth over, but when modeling arbitrary states of a thing even as basic as a number, the set analogy is not perfect unless steps are taken to make it so. It mostly works out though that the set analogy fits easily enough and so we can borrow bigger abstract algebraic concepts too.For instance, an immutable textual string type, with a concatenation operation, are great example of a semi-group. An instance of the string type can represent an element - have a value - from the set of all possible textual strings, and concatenation of two strings is guaranteed to produce a string, and the operation is associative. This breaks down when strings get too long for the representation scheme or the hardware, but if we can prove that we don’t hit that bound, then we can model strings as a semi-group.
9
u/SV-97 Dec 31 '22
Well floats don't behave like real numbers at all and what we usually call
intis different from ZFC integers - but we still use them like that all the time and it's useful to consider one of them as a model for the other.If you're not willing to accept that a type is a good model for a set (like the set of all values of the type): look into type and category theory and the curry-howard isomorphism. I'm sure you can actually justify the naming on a formal level.
It really shouldn't cause any problems - what problems have you encountered? If anything it makes both sides easier to understand imo. Ultimately it's the same concept, even if the specific encoding in the respective theory are "merely isomorphic".
7
u/Luchtverfrisser Dec 31 '22
I am very confused by your post (and others as well). I assume we are misunderstanding you, or you may be misunderstanding something?
As someone with a math background, for me I'd disagree. For me it's easier as it is pretty much exactly the same thing in computing as in maths? I can only agree that for those outside math it may be a weird name though, but your post does not seem to be about that?
7
u/sintrastes Dec 31 '22
Most mathematicians think in terms of set theory. However, many of the same concepts can also be formalized in type theory. That doesn't make them any less mathematical. And as others have pointed out, types can in fact be thought of as sets.
This is like saying "Why is the notion of a group still called a "group" in IZF when the mathematical term is (typically considered to be) defined in ZFC?".