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
4
votes
1 answer

Why is there no >=> semigroup for A => M[A] in Scalaz?

This is a followup to my previous question Kleisli defines two operators <=< (compose) and >=> (andThen). The >=> looks very natural for me and I don't understand how <=< can be useful. Moreover, it looks like there is no >=> semigroup for A => M[A]…
Michael
  • 41,026
  • 70
  • 193
  • 341
3
votes
2 answers

What is the relationship between a monoid and a functor?

I'm trying to understand the relationship between a functor and a monoid. They are often mentioned together but I haven't been able to quite connect the dots. I understand that, simply speaking, in programming a monoid can be thought of as a…
automasean
  • 391
  • 2
  • 8
3
votes
0 answers

Compatible code for Semigroup Monoid Proposal

Semigroup is becoming a superclass of Monoid. I did read the recommendations on that page for writing compatible code, but I neither like to conditionally depend on the semigroups package nor to put my mappend code in a top-level declaration. My…
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
3
votes
1 answer

Relation between the Semigroupoid and Semigroup classes

In the last week I've been trying to grasp some of Haskell's "core" types and type classes (but have been studying Haskell for at most two weeks total), and I've found something that bugs me: a "Semigroupoid" is a generalization of a "Category",…
2
votes
2 answers

Haskell, implementing Monoids. What is Semigroup and why does it act so weird?

I wanted to implement a Custom Datatype named ComplexNumber like this: data ComplexNumber a = C (a,a) Now I wanted to implement the Monoid Variable and define the binary mempty Element and the mappend like this: instance Num a => Monoid…
2
votes
2 answers

How to solve a Haskell code error "<>' is not a (visible) method of class `Monoid'"?

Lets see declaration of a new data type used to deal with reverse list in Haskell: import Data.Monoid data RevList a = Nil | RCons (RevList a) a deriving (Eq, Show) instance Monoid a => Monoid (RevList a) where mempty = Nil instance Semigroup…
Haskell bit
  • 151
  • 8
2
votes
2 answers

Join semilattice examples amongst semigroup instances in Haskell

I am collecting examples of join semilattices amongst Semigroup instances. As you may know join semilattice is similar to semigroup but requires additionally commutativity and idempotence. From the quick scan of the libraries on hackage I found the…
2
votes
1 answer

List instance of semigroup mappeding respective pairs of elements

I am looking for most common Haskell library that introduces [a] wrapper that instantiates Semigroup by delegating mappending to its elements in the similar way to: newtype SList a = SList { slist :: [a] } instance Semigroup a => Semigroup (SList…
1
vote
1 answer

Creating a Semigroup data type instance in Haskell

May goal concerned with creating a new Semigroup type class instance for newly defined datatype in Haskell (For those who knows the "Get programming with Haskell" book by Will Kurt, I may refer you to the page 428,i.e. the end of the capstone…
A. G
  • 187
  • 7
1
vote
1 answer

Make a parametrized data type instance of Semigroup

I want to make the data type Moneda a instance of the Semigroup, and implement the associative operation as +. I'm having difficulties understanding it properly. The working solution I found, is the following: data Moneda a = RON Double | EUR…
Mihai
  • 631
  • 1
  • 14
  • 30
1
vote
1 answer

Cats semigroup to merge List of HashMaps into One Scala

I have a list of hashMaps as follows : val listHashMaps = List(Map(1 -> List("one", "A") , Map(2 -> List("two", "B"), Map(3 -> List("three", "C"), Map(4 -> List("four", "D") I want to merge these HashMaps into a single HashMap as : Map(1 ->…
Her sincerly
  • 373
  • 1
  • 13
1
vote
1 answer

Using Validated from the Cats library in Scala

Cannot understand the actual difference between Semigroupal.product and Semigroupal.tuple2. Here is a short example: import cats.Semigroupal import cats.data.Validated import cats.data.Validated.Invalid import cats.instances.list._ // for Monoid …
Alexandr
  • 9,213
  • 12
  • 62
  • 102
1
vote
2 answers

"mayBeMempty" function for a Semigroup

The following function - mayBeMempty :: (Eq a, Semigroup a) => a -> a -> Bool mayBeMempty candidate ref = candidate <> ref == ref Is a (less efficient) generalization of Data.Set.isSubSetOf. It checks if the first argument is "contained" in the…
yairchu
  • 23,680
  • 7
  • 69
  • 109
1
vote
0 answers

Floating points wrapped in Sum and Product even if it breaks associativity?

Trying to learn Haskell and stumbled upon this: Prelude> import Data.Semigroup Prelude Data.Semigroup> let x = Sum 0.1 <> Sum 0.2 <> Sum 0.3 Prelude Data.Semigroup> let y = (Sum 0.1 <> Sum 0.2) <> Sum 0.3 Prelude Data.Semigroup> x ==…
HBu
  • 559
  • 6
  • 18
1
vote
1 answer

Defining a Semigroup instance that depends on itself

... or mishaps of a Haskell programmer that has to code Scala, part 5. I have the following structure in Scala: case class ResourceTree( resources: Map[String, ResourceTree] ) And, using Cats, I would like to define a Semigroup instance of…