Questions tagged [semigroup]

Semigroup is an algebraic structure consisting of a set together with an associative binary operation

In mathematics, a semigroup is an algebraic structure consisting of a set together with an associative binary operation. A semigroup generalizes a monoid in that a semigroup need not have an identity element. It also (originally) generalized a group (a monoid with all inverses) to a type where every element did not have to have an inverse, thus the name semigroup.

The binary operation of a semigroup is most often denoted multiplicatively: x•y, or simply xy, denotes the result of applying the semigroup operation to the ordered pair (x,y). The operation is required to be associative so that (x•y)•z = x•(y•z) for all x, y and z, but need not be commutative so that x•y does not have to equal y•x (contrast to the standard multiplication operator on real numbers, where xy = yx).

By definition, a semigroup is an associative magma. A semigroup with an identity element is called a monoid. A group is then a monoid in which every element has an inverse element. Semigroups must not be confused with quasigroups which are sets with a not necessarily associative binary operation such that division is always possible.

Wikipedia page: http://en.wikipedia.org/wiki/Semigroup

47 questions
17
votes
2 answers

Why is there no alternative instance for Either but a semigroup that behaves similarily to alternative?

I am a Haskell newbie and I wonder why there is no alternative instance for Either but a semigroup, which behaves as I would expect it from alternative: instance Semigroup (Either a b) where Left _ <> b = b a <> _ = a This instance discards or…
user6445533
13
votes
2 answers

Does liftA2 preserve associativity?

Given an operation (??) such that (a ?? b) ?? c = a ?? (b ?? c) (that is to say (??) is associative) must it be the case that liftA2 (??) (liftA2 (??) a b) c = liftA2 (??) a (liftA2 (??) b c) (that is to say that liftA2 (??) is associative) If we…
Wheat Wizard
  • 3,982
  • 14
  • 34
11
votes
2 answers

Use named instances for other instances

I'm trying to make a Semigroup and VerifiedSemigroup instance on my custom Bool datatype both on operator && and operator ||: %case data Lógico = Cierto | Falso (&&) : Lógico -> Lógico -> Lógico (&&) Cierto Cierto = Cierto (&&) _ _ = Falso (||) :…
chamini2
  • 2,820
  • 2
  • 24
  • 37
11
votes
1 answer

How to get all algebraic associative operations on a finite set by efficient algorithm?

Number of binary operation on a set of 2 elements is 2^(2*2)=16. Number of associative binary operation on that set is only 8. Number of binary operation on a set of 3 elements is 3^(3*3)=19683. Number of associative binary operation on that…
8
votes
2 answers

Can you formulate a monoid or semigroup for the radix sort?

This is the pseudocode for the radix sort: Pseudocode for Radix Sort: Radix-Sort(A, d) // Each key in A[1..n] is a d-digit integer. (Digits are // numbered 1 to d from right to left.) 1. for i = 1 to d do Use a stable sorting algorithm to sort A on…
hawkeye
  • 34,745
  • 30
  • 150
  • 304
7
votes
3 answers

What is the purpose of the ArgMin and ArgMax type synonyms in Data.Semigroup?

The base library in Haskell has the following type synonyms in Data.Semigroup: type ArgMin a b = Min (Arg a b) type ArgMax a b = Max (Arg a b) Here are links to the haddocks: ArgMin and ArgMax What is the purpose of these two type synonyms? …
illabout
  • 3,517
  • 1
  • 18
  • 39
7
votes
2 answers

Why is Maybe's Semigroup instance biased towards Just and the Monoid uses Nothing as its empty element?

Maybe expresses computations that might not produce a result due to an error. Therefore such computations must be short circuited. Now Maybe's Semigroup/Monoid instances seem to break with this semantics, because the former is biased towards Just…
user6445533
6
votes
1 answer

Haskell newtype that flips semigroup operation?

Is there any newtype in base that would basically achieve the following? newtype SemigroupFlip a = SemigroupFlip a instance Semigroup a => Semigroup (SemigroupFlip a) where (SemigroupFlip a) <> (SemigroupFlip b) = SemigroupFlip (b <> a) instance…
Clinton
  • 22,361
  • 15
  • 67
  • 163
5
votes
1 answer

Semigroup typeclass (Either) with slightly altered combine

Using cats.Semigroup one can write this: import cats.Semigroup import cats.implicits._ val l1: String Either Int = Left("error") val r1: String Either Int = Right(1) val r2: String Either Int = Right(2) l1 |+| r1 // Left("error") r1 |+| r2 //…
Florian Baierl
  • 2,378
  • 3
  • 25
  • 50
5
votes
2 answers

Keeping IO lazy under append

I may have been under the false impression that Haskell is lazier than it is, but I wonder if there's a way to get the best of both worlds... Data.Monoid and Data.Semigroup define two variations of First. The monoidal version models the leftmost,…
Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
5
votes
1 answer

Can you formulate the Bubble sort as a monoid or semigroup?

Given the following pseudocode for the bubble-sort procedure bubbleSort( A : list of sortable items ) repeat swapped = false for i = 1 to length(A) - 1 inclusive do: /* if this pair is out of order */ if A[i-1] > A[i]…
hawkeye
  • 34,745
  • 30
  • 150
  • 304
4
votes
1 answer

Is there a class that models "patches"?

I'm looking for a class in like the following in a Haskell library (or at least to know the mathematical name for such a thing): class Monoid patch => MyThing patch t where applyPatch :: t -> patch -> t I can have instances like this: instance…
Clinton
  • 22,361
  • 15
  • 67
  • 163
4
votes
1 answer

In TypeScript, how to reference the implementing class from an interface?

I'm exploring the Typescript type system by implementing the Fantasy Land Spec and I ran into an issue while trying to implement the spec for Semigroup. The spec stipulates that a Semigroup should adhere to the following type definition: concat ::…
snowfrogdev
  • 5,963
  • 3
  • 31
  • 58
4
votes
1 answer

What does Data.Semigroup ((<>)) do in this Haskell sort code?

I know that the program can sort the list by Mfg or by Year(descending). That is defined on line 9 and 10. But what exactly is the code doing? What is (<>)? import Data.Semigroup ((<>)) import Data.List (sort, sortOn) import Data.Ord…
profound_swami
  • 103
  • 2
  • 7
4
votes
1 answer

Hiding typeclass instance declarations while importing in Haskell

I am trying to make a tic-tac-toe game, and I decided to construct types for cells (elements of the board) and the board as follows: data Cell = X | O deriving (Show, Eq) type Board = [[Maybe Cell]] Here, Nothing represents an empty cell, (Just X)…
Jad Issa
  • 65
  • 1
  • 6
1
2 3 4