applicative

Haskell: some and many [duplicate]

筅森魡賤 提交于 2019-12-04 17:43:08
问题 This question already has answers here : What are Alternative's “some” and “many” useful for? (4 answers) Closed 12 months ago . What are some and many in Control.Applicative.Alternative good for? If I write something like some $ Just 42 , it seems to cause infinite recursion, which seems not very useful... 回答1: They make sense, when used as a parser combinator. some means, that the parser is applied as often as possible, but at least once. many is similar, but allows no parse. In case of

How and why is ap defined as liftM2 id in Haskell

故事扮演 提交于 2019-12-04 17:18:48
问题 Whilst trying to better understand Applicative, I looked at the definition of <*>, which tends to be defined as ap, which in turn is defined as: ap :: (Monad m) => m (a -> b) -> m a -> m b ap = liftM2 id Looking at the type signatures for liftM2 and id, namely: liftM2 :: (Monad m) => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r id :: a -> a I fail to understand how just by passing in id, the relevant part of the type signature seems to transform from (a1 -> a2 -> r) -> m a1 to m (a -> b) . What am

the equivalence between applicative functor and monad

爷,独闯天下 提交于 2019-12-04 14:07:10
问题 People say monads are an extension of applicative functors, but I don't see that. Let's take an example of applicative functor: (<*>) :: f(a->b) -> f a -> f b [(+3)] <*> [2,3,4] Now, I also expect I can do the same thing as monad, it means I can apply 2 parameters: a context contains a function, and another context to get a context. But for monad, I can't. All I need is to write an ugly function like this: [2,3,4] >>= (\x->[x+3]) Yes, of course, you can say that [(+3)] is equivalent to [\x->

How `sequenceA` works

∥☆過路亽.° 提交于 2019-12-04 11:09:59
I'm new to Haskell and trying to understand how does this work? sequenceA [(+3),(+2),(+1)] 3 I have started from the definition sequenceA :: (Applicative f) => [f a] -> f [a] sequenceA [] = pure [] sequenceA (x:xs) = (:) <$> x <*> sequenceA xs And then unfolded recursion into this (:) <$> (+3) <*> $ (:) <$> (+2) <*> $ (:) <$> (+1) <*> pure [] (:) <$> (+3) <*> $ (:) <$> (+2) <*> $ (:) <$> (+1) <*> [] But here i don't understand for which applicative functor operator <*> will be called, for ((->) r) or for [] (:) <$> (+1) <*> [] Can somebody go step by step and parse sequenceA [(+3),(+2),(+1)] 3

Translate from monad to applicative

喜欢而已 提交于 2019-12-04 10:03:22
问题 OK, so I know what the Applicative type class contains, and why that's useful. But I can't quite wrap my brain around how you'd use it in a non-trivial example. Consider, for example, the following fairly simple Parsec parser: integer :: Parser Integer integer = do many1 space ds <- many1 digit return $ read ds Now how the heck would you write that without using the Monad instance for Parser ? Lots of people claim that this can be done and is a good idea, but I can't figure out how exactly.

Applicative instance for MaybeT m assumes Monad m

余生颓废 提交于 2019-12-04 01:56:52
I've been using the Haxl monad (described here: http://www.reddit.com/r/haskell/comments/1le4y5/the_haxl_project_at_facebook_slides_from_my_talk ), which has the interesting feature that <*> for its Applicative instance isn't the same as ap from Control.Monad. This is a key feature that allows it to do concurrent computations without blocking. For example, if hf and ha are long computations, then let hf :: Haxl (a -> b) = ... ha :: Haxl a = ... in do f <- hf a <- ha return (f a) will do them sequentially, while hf <*> ha will do them in parallel and then combine the results. I would like to be

Applicative is to monad what X is to comonad

杀马特。学长 韩版系。学妹 提交于 2019-12-04 00:42:05
Can we solve this equation for X ? Applicative is to monad what X is to comonad After giving it some thought, I think this is actually a backward question. One might think that ComonadApply is to Comonad what Applicative is to Monad , but that is not the case. But to see this, let us use PureScript's typeclass hierarchy: class Functor f where fmap :: (a -> b) -> f a -> f b class Functor f => Apply f where apply :: f (a -> b) -> f a -> f b -- (<*>) class Apply f => Applicative f where pure :: a -> f a class Applicative m => Monad m where bind :: m a -> (a -> m b) -> m b -- (>>=) -- join :: m (m

How to show that a monad is a functor and an applicative functor?

别等时光非礼了梦想. 提交于 2019-12-03 23:30:35
Monads are known to be theoretically a subset of functors and specifically applicative functors, even though it's not indicated in Haskell's type system. Knowing that, given a monad and basing on return and bind , how to: derive fmap , derive <*> ? ehird Well, fmap is just (a -> b) -> f a -> f b , i.e. we want to transform the monadic action's result with a pure function. That's easy to write with do notation: fmap f m = do a <- m return (f a) or, written "raw": fmap f m = m >>= \a -> return (f a) This is available as Control.Monad.liftM . pure :: a -> f a is of course return . (<*>) :: f (a -

Examples of a monad whose Applicative part can be better optimized than the Monad part

本秂侑毒 提交于 2019-12-03 14:33:34
问题 In one discussion I heard that Applicative interface of some parsers is implemented differently, more efficiently than their Monad interface. The reason is that with Applicative we know all "effects" in advance, before the whole effectful computation is run. With monads, effects can depend on values during the computation so this optimization is not possible. I'd like to see some good examples of this. It can be some very simple parser or some different monad, that's not important. The

Does * in (<*>) have a special meaning?

家住魔仙堡 提交于 2019-12-03 12:13:39
问题 Trying to expand my understanding about symbols in Haskell : ($) : Function Application operator (Allow you to apply arguments over a function) (&) : flipped version of Function Application Operator? (&) = flip ($) (<>) : associative operator (You'll find it in Semigroups and Monoids) (<$>) : function application ($) lifted over a Functor structure (<&>) : flipped functor map Can we make a link between (*) and (<*>) ? I don't understand the meaning of * actually... 回答1: This is deliberate. <*