3 ms·
Because monoids, abstract algebra, etc. More simply, "addition" is generally defined as a commutative operation, which this isn't. When people want a non-commu
by dataflow 5y ago
Because monoids, abstract algebra, etc.
More simply, "addition" is generally defined as a commutative operation, which this isn't. When people want a non-commutative operation, they call it "multiplication".
For a more superficial but tangible reason, if you just consider the notation, AB is the concatenation of A and B, which is the notation for a product in math. (Recall putting "A" "B" next to each other in Python literally means concatenation, and it's a product in math.)
- housecarpenter 5y agoI think when mathematicians call an operation denoted by juxtaposition a "product", that's usually because it's denoted by juxtaposition for some independent reason and they're drawing the syntactic analogy with the numerical product, and there isn't any deeper connection with multiplication than that. Independent reasons for using juxtaposition might include: 1. juxtaposition is shorter than any alternative and hence a natural choice for the most common / only operation on the set in question 2. the operation is roughly analogous to function composition, and when you apply two composed functions it looks like f(g(x)) with f and g juxtaposed (if the functions are among those that have the longest history of being written as prefix operators, e.g. sin, cos, log, you don't even need the paren between the f and the g) 3. juxtaposition is a natural way to express an operation which is just "putting stuff together", such as string concatenation So while it makes sense to denote string concatenation by juxtaposition in mathematics, I think it's a bit of a leap to go from there to say that A * B is the right notation for a programming language. (Actually using juxtaposition would be fine, but a general-purpose programming language is likely to want to reserve juxtaposition for something else.) Also if you run with the idea that string concatenation = putting two strings together, well, addition is sort of like putting two numbers together. Rephrasing that in mathematical language: natural numbers can be encoded as elements of the free monoid on one generator, in which case the monoid operation turns out to be addition. I think that might be why the A + B notation is preferred over A * B when we have to choose one or the other.
- rocqua 5y agoI think multiplication for string concatenation makes more sense than just "its juxtaposition so we call it multiplication". If you look at type theory, then a tuple of type (A, B) is called the product type of A and B. This is because there are #A times #B elements in the new type. A Sum type, in type theory is a tagged union. It is an element of Either A or B. (Like a C union with an extra tag saying which type it contains). This is a sum because it contains #A plus #B elements. Concatenation of strings is much closer to the tuple approach. And the argument about the number of elements in the type still holds if you keep the string-length fixed.
- lisper 5y ago> "addition" is generally defined as a commutative operation, which this isn't. Multiplication of numbers is commutative, as is the dot product of vectors. So multiplication is sometimes commutative, sometimes not depending on what is being multiplied and how. So why cannot addition be likewise sometimes commutative and sometimes not?
- dataflow 5y agoI'm not saying it can't (you'll find occasional counterexamples if you Google), I'm just saying that's the general convention. I don't know why the math community chose this convention, but I know it makes sense to me in English: remember, multiplication is repeated addition. The multiplier doesn't serve the same purpose as the multiplicand; their meanings are fundamentally unrelated. Think of an example. If you have "two rectangles", it's pretty easy to understand what that means, but "rectangle twos" doesn't make any sense (or at least doesn't mean the same thing...).
- lisper 5y ago> The multiplier doesn't serve the same purpose as the multiplicand Yes, that's true. But this seems to me like another argument against classifying concatenation as multiplication because if "A concat B" makes sense, then "B concat A" necessarily makes sense as well (notwithstanding that they produce different results).
- dataflow 5y ago> because if "A concat B" makes sense, then "B concat A" necessarily makes sense as well Except it makes perfect sense. B concat A gives you B followed by A, as opposed to A followed by B. They produce different results, but they make perfect sense. Nobody claimed multiplicative operations cannot make sense if you swap the operands. The claim was they don't have to. Like I said, in addition, not only do they make sense, but they give you the same result. Here they clearly don't. Therefore, it makes no sense to suggest concatenation is more similar to addition than multiplication.
- joppy 5y agoAnother argument is in regular expressions, where you can see that concatenation distributes over alternation: A(B|C) is the same regex as AB|AC.
- rocqua 5y agoHuh, neat!. Especially since alternation is commutative. So this is a ring-like structure! We have 'identity' if you allow a pattern that matches nothing. No inverse elements though. Not sure what the name of this ring-like thing is. But pretty certain it is named.