TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Algebraic patterns – Semigroup

82 pointsby lebekalmost 9 years ago

5 comments

chubotalmost 9 years ago
I think of these programming constructs as Monoids rather than semigroups. A monoid is just a semigroup with an identity element:<p><a href="http:&#x2F;&#x2F;mathworld.wolfram.com&#x2F;Monoid.html" rel="nofollow">http:&#x2F;&#x2F;mathworld.wolfram.com&#x2F;Monoid.html</a><p>Most of his examples have an identity, giving a monoid.<p><pre><code> * (string, concat): identity &#x27;&#x27; * numbers with addition&#x2F;multiplication: identity 0&#x2F;1 * numbers with min&#x2F;max: Numbers in computers are finite, so this is quite useful. stdint.h defines INT32_MAX, INT32_MIN, etc. * booleans with &amp;&amp; &#x2F; || : true &#x2F; false * function composition: identity function f(x) = x * frequency map: {} or {A: 0, B:0, ...} depending on how you define it </code></pre> Comparators: Is this a bug in the post? It doesn&#x27;t seem to follow the pattern because the arity of the functions is different. In a semigroup or monoid all the elements are supposed to be drawn from the same set.<p>Other examples I can think of:<p><pre><code> * Python&#x27;s dict with respect to member .update() (if you think of it as an operator rather than mutating member). Identity is {} * Unix Pipes: identity &#x27;cat&#x27; (note that the shell supports point-free programming)</code></pre>
评论 #12110936 未加载
评论 #12142950 未加载
评论 #12110666 未加载
skybrianalmost 9 years ago
I&#x27;m not sure how useful this is in practice. For example, addition in actual computer languages isn&#x27;t necessarily associative and compilers often can&#x27;t treat them as associative.<p>Floating point numbers added in the wrong order can result in catastrophic cancellation. So for example, as far as JavaScript is concerned, numbers aren&#x27;t associative.<p>Integers may overflow. Twos-complement addition is associative, but only because if you get the same bogus answer in the case of overflow. If you actually detect overflow, you may get overflow or not depending on the order in which you add the numbers.<p>Oftentimes we ignore these issues, but it seems like if you&#x27;re talking about taking advantage of associative rules to reorder calculations, you have to think about it. Thinking in terms of semigroups instead of concrete datatypes seems like a likely source of bugs when your model makes assumptions that aren&#x27;t actually true.
评论 #12111487 未加载
评论 #12112252 未加载
vikiomega9almost 9 years ago
I really wish the applications section had more detail or perhaps an &quot;editorial&quot;. I don&#x27;t really follow the consequence of a frequency map being associative.
评论 #12109555 未加载
评论 #12109784 未加载
kccqzyalmost 9 years ago
Might be slightly off topic but in the recently released Glasgow Haskell Compiler, semigroups are now built into the base library. No more dependencies when using it.
评论 #12109893 未加载
dkarapetyanalmost 9 years ago
Incidentally the typical mathematical curriculum does not even bother with monoids and semigroups when it comes to the initial introduction. The introductory material jumps right into the definition of a group and then mentions on the side that removing a few key pieces leaves you with a monoid and a semigroup. Same with fields, rings, rigs, etc. The most constrained piece is introduced first and then dropping requirements leaves you with something less constrained for which you can prove fewer theorems.<p>I learned groups and fields first and then learned about the more relaxed versions. I&#x27;m not sure which approach is better though. Groups are certainly more fun to play with but monoids probably show up in more contexts.