Questions tagged [monoids]

A monoid is a set that is closed under an associative binary operation and has an identity element I ∈ Such that for all a ∈ S, Ia = aI = a. Note that unlike a group, its elements need not have inverses. It can also be thought of as a semigroup with an identity element.

A monoid is a set that is closed under an associative binary operation and has an identity element I ∈ Such that for all a ∈ S, Ia = aI = a. Note that unlike a group, its elements need not have inverses. It can also be thought of as a semigroup with an identity element. Put simply, a monoid is an algebraic structure with an associative binary operation that has an identity element. Examples include:

  • lists under concatenation
  • numbers under addition or multiplication
  • booleans under conjunction or disjunction
  • sets under union or intersection
  • functions from a type to itself, under composition

Note that in most of these cases the operation is also commutative, but it need not be; concatenation and function composition are not commutative.

Useful links:

Wikipedia - Monoid

Wikipedia - Monoid (category theory)

239 questions
1
vote
1 answer

How does Foldable knows the implementation of mappend

I am learning Haskell with the help of "http://learnyouahaskell.com". I am following the example of a BST (Binary Search Tree) that is an instance of Foldable: data Tree a = Nil | Node a (Tree a) (Tree a) deriving (Show, Read, Eq) instance…
OneEyeQuestion
  • 742
  • 7
  • 22
1
vote
1 answer

scala: type bounds on a binary operation

The problem is simple, I have an object which does a binary operation with two parameters. I want to only add fuels that have the same type, like this: object Fuels { case class Fuel[F <: FuelType](amount: Double, fuelType: F = Petrol) { def…
fracca
  • 2,417
  • 1
  • 23
  • 22
0
votes
1 answer

Application of monoid to sum of list for special case when list is empty

interface Monoid{ operator fun plus(other:T):T fun zero():T } class MyList>(val l:List){ fun sum():T{ var result:T = ... // what should be here? Ideally it could be smth like T.Zero or T.zero() with static …
0
votes
1 answer

A monoid can see its elements all in the same shape

I'm not looking for the mathematical definition of a monoid, I'm looking for why monoid are important in haskell. (I'm not talking about Monoid class, just I'm talking about monoid structure) Is it correct to describe the following as one of the…
0
votes
1 answer

How can I write the fsum foldable function in haskell?

fsum :: (Foldable f, Num a) ⇒ f a → a It computes the sum of all numbers in a container-like data structure. My problem is that I cannot define f a as a list or Maybe or any other foldable types to have access. So I am assuming I should write fsum…
0
votes
0 answers

Instantiating a Writer Monad as a Monoid typeclass results in deduction error

When I try to implement the LYAH instantiation of a Writer Monad with a Monoid as the constraining type class by the book as written here I get the Output stated afterwards in my file tryouts.hs: newtype Writer w a = Writer { runWriter :: (a, w) }…
Piskator
  • 605
  • 1
  • 9
0
votes
2 answers

Writing a Haskell Options Datatype for Safe Query Strings for Fixed API

I'm practicing "real-world" Haskell by writing an application that makes web requests to a music catalog. I can call an endpoint like https://example.com/search with any combination of optional parameters like title, artist, year. For example, any…
Benjamin Bray
  • 416
  • 4
  • 14
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

Further problems with the monoid type class

I am trying to toy around with monoids using the Semigroup typeclass, and I am trying to define a monoid on the natural numbers. I put the following class and instance declarations into GHCI Prelude:{ Prelude| class Semigroup a where Prelude| …
user65526
  • 685
  • 6
  • 19
0
votes
1 answer

The Monoid type class

I am trying to play around with monoids in Haskell, using this page: https://en.wikibooks.org/wiki/Haskell/Monoids. I entered the following information in the terminal (after importing Data.Monoid): class Monoid a where mempty :: a mappend…
user65526
  • 685
  • 6
  • 19
0
votes
1 answer

how to deal with duplicate record field with lenses?

given this piece of code: {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE DuplicateRecordFields #-} {-# LANGUAGE FunctionalDependencies #-} module Foo where import Control.Lens ((^.), makeFieldsNoPrefix) import…
itsu
  • 222
  • 1
  • 10
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 define a `mempty` on this `Or` type?

In the book Haskell Programming from first Principles, there is an exercise which asks me to instantiate Monoid on an Or type: data Or a b = Fst a | Snd b deriving (Eq, Show) previously, we defined the rules when instantiate Semigroup:…
cmal
  • 2,062
  • 1
  • 18
  • 35
0
votes
1 answer

How to compute the maximum of a floating point property of a Foldable via foldMap?

Say I have a map with measurable objects, and I'd like the maximum of their widths, defaulting to 0. The machinery of Foldable (foldMap) and Semigroup (Max) seems perfect, except that I can't seem to introduce the arbitrary lower bound. data…
robx
  • 2,221
  • 1
  • 14
  • 31