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
1
vote
1 answer

Why there is no Semigroup instance for TrieMap in scalaz

While I can do Map("a" -> 1) |+| Map("a" -> 2) It seems there is no support for TrieMap("a" -> 1) |+| TrieMap("a" -> 2) Why ?
Yann Moisan
  • 8,161
  • 8
  • 47
  • 91
1
vote
1 answer

Why scalaz Semigroup is not covariant?

Is their a simple raison why Scalaz SemiGroup is not covariant …
ouertani
  • 347
  • 3
  • 8
0
votes
0 answers

Generic Named Multiple Counter in Scala

I have multiple case classes that represent some sort of dimensions, and some sort of field level statistics that can be aggregated agnostic from all the other fields. This forms a semigroup by defining combine case class Precipitation(date: String,…
Michael K
  • 2,196
  • 6
  • 34
  • 52
0
votes
1 answer

How to add custom Semigroup instance for Int

import cats._ import cats.implicits._ object SemigroupInstances { implicit val intSemigroup: Semigroup[Int] = new Semigroup[Int] { override def combine(a: Int, b: Int) = a * b } } import SemigroupInstances._ object Semigroup { def…
Lesha Pipiev
  • 3,251
  • 4
  • 31
  • 65
0
votes
1 answer

Agda type checker rejecting simple definition of a semigroup

I have defined a Semigroup as follows: record Semigroup : Set1 where field X : Set e : X _◇_ : X -> X -> X assoc : ∀ {x y z} -> (x ◇ y) ◇ z -> x ◇ (y ◇ z) But I get the following error: X should be a sort, but…
user1023733
  • 805
  • 5
  • 14
0
votes
1 answer

Semigroup instace for Data.Map

I'm not sure how to write the semigroup instance for this map newtype: import Data.Map (Map) import qualified Data.Map as Map newtype mymap = MyMap (Map Int String) instance Semigroup (MyMap k v) where MyMap a <> MyMap b = Map.compose a b…
k-shar
  • 79
  • 1
  • 8
0
votes
0 answers

Derive semigroup instance for F[A]

It seems trivial to implement it on our own: implicit def s[F[_]: Monad, A: Semigroup]: Semigroup[F[A]] = Semigroup.instance((fa, fb) => for { fa <- fa fb <- fb } yield fa |+| fb) But is it possible to derive it automatically? Kittens…
Some Name
  • 8,555
  • 5
  • 27
  • 77
0
votes
0 answers

Moving from List[IO(Abstraction[List[A]]]] to IO[Abstraction[List[A]]] using fold and empty instance of case class

First I have a case class defined as (this is part of a library): final case class Abstraction[A]( result: Either[AbError, A], status: Int, info: Map[String, String] ) I want to use it as a primary component for fold in order to go from…
Mar Es
  • 33
  • 4
0
votes
1 answer

Why does Semigroup has an Option type and behave like a neutral element in Monoid?

Why does Semigroup has an Option type and None behave like a neutral element in Monoid? val two: Option[Int] = Option(2) val n: Option[Int] = None n |+| two should be(Some(2))//why do we have the opportunity to do this? two |+| n should…
0
votes
1 answer

Require semigroup to be associative in scala

A semigroup is required to be associative, but I could define a Semigroup like: trait Semigroup[T] { def op(t1:T, t2:T) : T } def plus = new Semigroup[Int] { def op(t1:Int, t2:Int) = t1 - t2 } I am able to implement plus which is not…
Rob
  • 3,333
  • 5
  • 28
  • 71
0
votes
0 answers

Haskell Cookbook, Monoid instance

Repeating the books until I am sure to get it right. Got this in the Haskell Cookbook: data Option = Option { boolOption :: Bool, selections :: [String] } deriving Show instance Monoid Option where mempty = Option False [] …
Madderote
  • 1,107
  • 10
  • 19
0
votes
1 answer

How to write `Semigroup` instance and their `quickCheck`s on parameterized types?

In the exercises of Haskell Programming from First Principle book on Semigroup, I am asked to write quickCheck for user defined typeclasses. There are many typeclasses, but I do not understand how to write even the basic ones: Problems: The first is…
cmal
  • 2,062
  • 1
  • 18
  • 35
0
votes
2 answers

Semigroup with function in Scala

I'm trying to convert an Haskell Semigroup to Scala. The Haskell code works fine but I can't write it in Scala Haskell: import Data.Semigroup newtype Combine a b = Combine { unCombine :: (a -> b) } instance Semigroup b => Semigroup (Combine a b)…
gekomad
  • 525
  • 9
  • 17
0
votes
1 answer

Scalaz implementation of Semigroup using advanced Scala features

I was digging through the implementation of Monoids in Scalaz. I came across the |+| operator that is supposed to come out of the box if you define the append operation on Monoid. The definition of this operator is in SemigroupSyntax. That class…
zaxme
  • 1,065
  • 11
  • 29
0
votes
0 answers

Why scalaz |+| operator only doesn't work with Maps which value are Seq?

I have two maps as follows. import scalaz._, Scalaz._ val map1: Map[String, Seq[String]] = Some value val map2: Map[String, Seq[String]] = Somve Value This compiles fine and everything works as expected. // Compiles map1.mapValues{_.toList} |+|…
user1819676
  • 369
  • 1
  • 12