monoids

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

醉酒当歌 提交于 2019-12-30 08:06:27
问题 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? 回答1: I believe there is a deeper reason. While it seems there is no canonical set of rules for Alternative , in order for

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

风流意气都作罢 提交于 2019-12-30 08:06:07
问题 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? 回答1: I believe there is a deeper reason. While it seems there is no canonical set of rules for Alternative , in order for

How to check XML with monoid in Scala?

≡放荡痞女 提交于 2019-12-25 04:43:27
问题 Suppose I need to validate an input XML, e.g. <a> <a1>a1a1a1</a1> <a2>a2a2a2</a2> <a3/> </a> I need to make sure that its root element has label "a" and children with labels "a1" , "a2" , "a3" and texts "a1a1a1" , "a2a2a2" and "" respectively. I can define the basic validation functions as follows: type Status = ... // either Ok or list of error messages type Validate[A] = A => Status type ValidateNode = Validate[scala.xml.Node] val label(l: String): ValidateNode = ... // trivial val text(t:

How do I use a monoid instance of a function?

倖福魔咒の 提交于 2019-12-22 06:58:52
问题 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? 回答1: 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 -

How to use the maybe monoid and combine values with a custom operation, easily?

☆樱花仙子☆ 提交于 2019-12-20 20:40:09
问题 What I'm trying to do is trivial to define by hand, basically maybeCombine :: (a->a->a) -> Maybe a -> Maybe a -> Maybe a maybeCombine _ Nothing Nothing = Nothing maybeCombine _ (Just a) Nothing = Just a maybeCombine _ Nothing (Just a) = Just a maybeCombine f (Just a) (Just a') = Just $ f a a' It's not a big deal to define this locally when needed, but still cumbersone and being so basic and general it seems there should be a standard implementation, yet I can't seem to find one. Perhaps I'm

How to use the maybe monoid and combine values with a custom operation, easily?

谁都会走 提交于 2019-12-20 20:38:03
问题 What I'm trying to do is trivial to define by hand, basically maybeCombine :: (a->a->a) -> Maybe a -> Maybe a -> Maybe a maybeCombine _ Nothing Nothing = Nothing maybeCombine _ (Just a) Nothing = Just a maybeCombine _ Nothing (Just a) = Just a maybeCombine f (Just a) (Just a') = Just $ f a a' It's not a big deal to define this locally when needed, but still cumbersone and being so basic and general it seems there should be a standard implementation, yet I can't seem to find one. Perhaps I'm

Why MonadPlus and not Monad + Monoid?

落爺英雄遲暮 提交于 2019-12-17 22:38:12
问题 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.

Arrow to add one element at a time

点点圈 提交于 2019-12-11 02:54:40
问题 This question is about HXT, but I guess it's applicable to conception of ArrowPlus in general. Consider the following program: module Main (main) where import Text.XML.HXT.Core import Control.Monad (void) main :: IO () main = void $ runX $ root [] [foo] >>> writeDocument [withIndent yes] "test.xml" foo :: ArrowXml a => a XmlTree XmlTree foo = selem "foo" [bar >>> bar >>> bar] bar :: ArrowXml a => a XmlTree XmlTree bar = this <+> eelem "bar" Can you tell what will be saved in test.xml ? My

Keeping IO lazy under append

穿精又带淫゛_ 提交于 2019-12-10 15:27:39
问题 I may have been under the false impression that Haskell is lazier than it is, but I wonder if there's a way to get the best of both worlds... Data.Monoid and Data.Semigroup define two variations of First . The monoidal version models the leftmost, non-empty value, whereas the semigroup version simply models the leftmost value. This works fine for pure value values, but consider impure values: x = putStrLn "x" >> return 42 y = putStrLn "y" >> return 1337 Both of these values have the type Num

Why can't GHC derive instances for Monoid?

我与影子孤独终老i 提交于 2019-12-10 01:10:15
问题 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