monoids

Why is there not 'Alternative' instance for 'Control.Applicative.Const'

假如想象 提交于 2019-12-01 03:08:51
There is an instance Monoid a => Monoid (Const a b) for the Const functor from Control.Applicative . There is also an instance Monoid m => Applicative (Const m) . I would therefore expect that there is also an instance Monoid m => Alternative (Const m) that coincides with the one for Monoid . Is this just an omission that should be fixed, or is there a deeper reason? Petr Pudlák I believe there is a deeper reason. While it seems there is no canonical set of rules for Alternative , in order for Alternative to make sense, there definitely ought to be a relationship between Alternative and its

Monoid vs MonadPlus [duplicate]

↘锁芯ラ 提交于 2019-11-29 22:22:13
This question already has an answer here: Why MonadPlus and not Monad + Monoid? 3 answers I am very new to both Monads and Monoids and recently also learned about MonadPlus . From what I see, Monoid and MonadPlus both provide a type with a associative binary operation and an identity. (I'd call this a semigroup in mathematical parlance.) So what is the difference between Monoid and MonadPlus ? A semigroup is a structure equipped with an associative binary operation. A monoid is a semigroup with an identity element for the binary operation. Monads and semigroups Every monad has to adhere to the

What's the practical value of all those newtype wrappers in `Data.Monoid`?

别来无恙 提交于 2019-11-29 03:27:11
When looking at Data.Monoid , I see there are various newtype wrappers, such as All , Sum , or Product , which encode various kinds of monoids. However, when trying to use those wrappers, I can't help but wonder what's the benefit over using their non- Data.Monoid counterparts. For instance, compare the rather cumbersome summation print $ getSum $ mconcat [ Sum 33, Sum 2, Sum 55 ] vs. the more succinct idiomatic variant print $ sum [ 33, 2, 55 ] But what's the point? Is there any practical value having all those newtype wrappers? Are there more convincing examples of Monoid newtype wrapper

Haskell: How to write a `Monoid` instance for something that depends on parameters

橙三吉。 提交于 2019-11-29 03:03:15
问题 I am working on a small library for the university that does integer calculations in a cyclic group; Things like: (3 (% 11)) + (10 (% 11)) --> (2 (% 11)) 'Integers (% n)' clearly form a monoid under addition with '0 (% n)' as identity element. However, addition only makes sense when the modulo of the two operands being added is the same: a (% n) + b (% n) makes sense, while a (% n) + b (% m) does not. Is there any way to enforce this with Haskell's type system? The same of course holds true

Monoid vs MonadPlus [duplicate]

江枫思渺然 提交于 2019-11-28 19:57:45
问题 This question already has an answer here: Why MonadPlus and not Monad + Monoid? 3 answers I am very new to both Monads and Monoids and recently also learned about MonadPlus . From what I see, Monoid and MonadPlus both provide a type with a associative binary operation and an identity. (I'd call this a semigroup in mathematical parlance.) So what is the difference between Monoid and MonadPlus ? 回答1: A semigroup is a structure equipped with an associative binary operation. A monoid is a

Why MonadPlus and not Monad + Monoid?

不问归期 提交于 2019-11-28 19:08:58
I'm trying to understand the motivation behind the MonadPlus . Why is it necessary if there are already the typeclasses Monad and Monoid ? Granted, instances of Monoid are concrete types, whereas instances of Monad require a single type parameter. (See Monoid vs MonadPlus for a helpful explanation.) But couldn't you rewrite any type constraint of (MonadPlus m) => ... as a combination of Monad and Monoid ? (Monad m, Monoid (m a)) => ... Take the guard function from Control.Monad , for example. Its implementation is: guard :: (MonadPlus m) => Bool -> m () guard True = return () guard False =

Distinction between typeclasses MonadPlus, Alternative, and Monoid?

无人久伴 提交于 2019-11-27 02:44:58
The standard-library Haskell typeclasses MonadPlus , Alternative , and Monoid each provide two methods with essentially the same semantics: An empty value: mzero , empty , or mempty . An operator a -> a -> a that joins values in the typeclass together: mplus , <|> , or mappend . All three specify these laws to which instances should adhere: mempty `mappend` x = x x `mappend` mempty = x Thus, it seems the three typeclasses are all providing the same methods. ( Alternative also provides some and many , but their default definitions are usually sufficient, and so they're not too important in

Why “and []” is True and “or []” is False

心已入冬 提交于 2019-11-26 23:21:10
问题 Why "and" on an empty list returns true, does it imply that an empty list holds True? Sorry but I cannot read and comprehend this correctly, so please correct me. Thanks. Prelude> and [] True Prelude> or [] False 回答1: In mathematics, it's often useful to talk about a binary operation, such as && , || , + , * , etc as having an identity . The identity is a value e such that the following property holds for some generic binary operation <> e <> x = x x <> e = x For the operators I listed above,

A monad is just a monoid in the category of endofunctors, what's the problem?

谁都会走 提交于 2019-11-26 16:50:14
Who first said the following? A monad is just a monoid in the category of endofunctors, what's the problem? And on a less important note, is this true and if so could you give an explanation (hopefully one that can be understood by someone who doesn't have much Haskell experience)? Tom Crockett That particular phrasing is by James Iry, from his highly entertaining Brief, Incomplete and Mostly Wrong History of Programming Languages , in which he fictionally attributes it to Philip Wadler. The original quote is from Saunders Mac Lane in Categories for the Working Mathematician , one of the

Distinction between typeclasses MonadPlus, Alternative, and Monoid?

。_饼干妹妹 提交于 2019-11-26 10:09:04
问题 The standard-library Haskell typeclasses MonadPlus , Alternative , and Monoid each provide two methods with essentially the same semantics: An empty value: mzero , empty , or mempty . An operator a -> a -> a that joins values in the typeclass together: mplus , <|> , or mappend . All three specify these laws to which instances should adhere: mempty `mappend` x = x x `mappend` mempty = x Thus, it seems the three typeclasses are all providing the same methods. ( Alternative also provides some