monoids

Monoid mempty in pattern matching

和自甴很熟 提交于 2019-12-07 08:08:51
问题 I tried to write a generalized maximum function similar to the one in Prelude . My first naiv approach looked like this: maximum' :: (F.Foldable a, Ord b) => a b -> Maybe b maximum' mempty = Nothing maximum' xs = Just $ F.foldl1 max xs However, when I test it it always returns Nothing regardless of the input: > maximum' [1,2,3] > Nothing Now I wonder whether it's possible to obtain the empty value of a Monoid type instance. A test function I wrote works correctly: getMempty :: (Monoid a) => a

How do you implement monoid interface for this tree in haskell?

て烟熏妆下的殇ゞ 提交于 2019-12-07 06:30:21
问题 Please excuse the terminology, my mind is still bending. The tree: data Ftree a = Empty | Leaf a | Branch ( Ftree a ) ( Ftree a ) deriving ( Show ) I have a few questions: If Ftree could not be Empty , would it no longer be a Monoid since there is no identity value. How would you implement mappend with this tree? Can you just arbitrarily graft two trees together willy nilly? For binary search trees, would you have to introspect some of the elements in both trees to make sure the result of

What can we do with Alternative but cannot do with Monoid?

爱⌒轻易说出口 提交于 2019-12-06 23:26:45
问题 I read Why MonadPlus and not Monad + Monoid? and I understand a theoretical difference, but I cannot figure out a practical difference, because for List it looks the same. mappend [1] [2] == [1] <|> [2] Yes. Maybe has different implementations mappend (Just "a") (Just "b") /= (Just "a") <|> (Just "b") But we can implement Maybe Monoid in the same way as Alternative instance Monoid (Maybe a) where Nothing `mappend` m = m m `mappend` _ = m So, can someone show the code example which explains a

Write a Maximum Monoid using Maybe in Haskell

别说谁变了你拦得住时间么 提交于 2019-12-06 19:02:32
问题 I've been going through Haskell monoids and their uses, which has given me a fairly good understanding of the basics of monoids. One of the things introduced in the blog post is the Any monoid, and it's usage like the following: foldMap (Any . (== 1)) tree foldMap (All . (> 1)) [1,2,3] In a similar vein, I've been trying to construct a Maximum monoid and have come up with the following: newtype Maximum a = Maximum { getMaximum :: Maybe a } deriving (Eq, Ord, Read, Show) instance Ord a =>

Use HSpec and QuickCheck to verify Data.Monoid properties

余生颓废 提交于 2019-12-06 02:37:01
问题 I'm trying to use HSpec and QuickCheck to verify properties of Monoids (associativity and identity element). I am going to verify particular instances, but would like to keep most of the code polymorphic. This is what I came up with after several hours: module Test where import Test.Hspec import Test.QuickCheck import Data.Monoid instance (Arbitrary a) => Arbitrary (Sum a) where arbitrary = fmap Sum arbitrary instance (Arbitrary a) => Arbitrary (Product a) where arbitrary = fmap Product

Monoid mempty in pattern matching

╄→尐↘猪︶ㄣ 提交于 2019-12-05 10:51:31
I tried to write a generalized maximum function similar to the one in Prelude . My first naiv approach looked like this: maximum' :: (F.Foldable a, Ord b) => a b -> Maybe b maximum' mempty = Nothing maximum' xs = Just $ F.foldl1 max xs However, when I test it it always returns Nothing regardless of the input: > maximum' [1,2,3] > Nothing Now I wonder whether it's possible to obtain the empty value of a Monoid type instance. A test function I wrote works correctly: getMempty :: (Monoid a) => a -> a getMempty _ = mempty > getMempty [1,2,3] > [] I had already a look at these two questions but I

How do I use a monoid instance of a function?

点点圈 提交于 2019-12-05 10:37:02
Today I tried to reduce a list of functions trough monoid typeclass but the resulting function expects its argument to be an instance of Monoid for some reason. GHCI tells me that the type of mconcat [id, id, id, id] is Monoid a => a -> a . Yet I would expect it to be a -> a . What is happening? You're using this instance: instance Monoid b => Monoid (a -> b) where mempty _ = mempty mappend f g x = f x `mappend` g x which is more general because it doesn't require endomorphisms (i.e. a -> a ). To get the instance you were expecting, you can wrap your functions in Endo : appEndo (mconcat [Endo

What can we do with Alternative but cannot do with Monoid?

99封情书 提交于 2019-12-05 02:12:11
I read Why MonadPlus and not Monad + Monoid? and I understand a theoretical difference, but I cannot figure out a practical difference, because for List it looks the same. mappend [1] [2] == [1] <|> [2] Yes. Maybe has different implementations mappend (Just "a") (Just "b") /= (Just "a") <|> (Just "b") But we can implement Maybe Monoid in the same way as Alternative instance Monoid (Maybe a) where Nothing `mappend` m = m m `mappend` _ = m So, can someone show the code example which explains a practical difference between Alternative and Monoid? The question is not a duplicate of Why MonadPlus

Write a Maximum Monoid using Maybe in Haskell

你说的曾经没有我的故事 提交于 2019-12-05 00:23:00
I've been going through Haskell monoids and their uses , which has given me a fairly good understanding of the basics of monoids. One of the things introduced in the blog post is the Any monoid, and it's usage like the following: foldMap (Any . (== 1)) tree foldMap (All . (> 1)) [1,2,3] In a similar vein, I've been trying to construct a Maximum monoid and have come up with the following: newtype Maximum a = Maximum { getMaximum :: Maybe a } deriving (Eq, Ord, Read, Show) instance Ord a => Monoid (Maximum a) where mempty = Maximum Nothing m@(Maximum (Just x)) `mappend` Maximum Nothing = m

Why can't GHC derive instances for Monoid?

妖精的绣舞 提交于 2019-12-05 00:00:41
GHC has a few language flags, such as DeriveFunctor , DeriveDataTypeable etc., which enable compiler generation of derived instances for type classes other than those allowed in Haskell 98. This especially makes sense for something like Functor , where the laws of that class dictate an obvious, "natural" derived instance. So why not for Monoid ? It seems like for any data type with a single data constructor: data T = MkT a b c ... one could mechanically produce a Monoid instance (excuse the pseudocode): instance (Monoid a, Monoid b, Monoid c, ...) => Monoid T where mempty = MkT mempty mempty