applicative

Applicative functor evaluation is not clear to me

故事扮演 提交于 2019-11-28 01:34:39
I am currently reading Learn You a Haskell for Great Good! and am stumbling on the explanation for the evaluation of a certain code block. I've read the explanations several times and am starting to doubt if even the author understands what this piece of code is doing. ghci> (+) <$> (+3) <*> (*100) $ 5 508 An applicative functor applies a function in some context to a value in some context to get some result in some context. I have spent a few hours studying this code block and have come up with a few explanations for how this expression is evaluated, and none of them are satisfactory. I

What are Alternative's “some” and “many” useful for?

假如想象 提交于 2019-11-27 19:38:37
Alternative , an extension of Applicative , declares empty , <|> and these two functions: One or more: some :: f a -> f [a] Zero or more: many :: f a -> f [a] If defined, some and many should be the least solutions of the equations: some v = (:) <$> v <*> many v many v = some v <|> pure [] I couldn't find an instance for which some and many are defined. What is their meaning and practical use? Are they used at all? I've been unable to grasp their purpose just from this definition. Update: I'm not asking what is Alternative , just what are some and many I tend to see them in Applicative parser

To what extent are Applicative/Monad instances uniquely determined?

旧巷老猫 提交于 2019-11-27 19:17:12
问题 As described this question/answers, Functor instances are uniquely determined, if they exists. For lists, there are two well know Applicative instances: [] and ZipList. So Applicative isn't unique (see also Can GHC derive Functor and Applicative instances for a monad transformer? and Why is there no -XDeriveApplicative extension?). However, ZipList needs infinite lists, as its pure repeats a given element indefinitely. Are there other, perhaps better examples of data structures that have at

What exactly does “effectful” mean

人盡茶涼 提交于 2019-11-27 18:22:43
Time and again I read the term effectful , but I am still unable to give a clear definition of what it means. I assume the correct context is effectful computations , but I've also seen the term effectful values ) I used to think that effectful means having side effects . But in Haskell there are no side-effects (except to some extent IO). Still there are effectful computations all over the place. Then I read that monads are used to create effectful computations. I can somewhat understand this in the context of the State Monad. But I fail to see any side-effect in the Maybe monad. In general

Why should Applicative be a superclass of Monad?

岁酱吖の 提交于 2019-11-27 18:07:30
Given: Applicative m, Monad m => mf :: m (a -> b), ma :: m a it seems to be considered a law that: mf <*> ma === do { f <- mf; a <- ma; return (f a) } or more concisely: (<*>) === ap The documentation for Control.Applicative says that <*> is "sequential application," and that suggests that (<*>) = ap . This means that <*> must evaluate effects sequentially from left to right, for consistency with >>= ... But that feels wrong. McBride and Paterson's original paper seems to imply that the left-to-right sequencing is arbitrary: The IO monad, and indeed any Monad, can be made Applicative by taking

How to handle side effect with Applicative?

送分小仙女□ 提交于 2019-11-27 15:39:08
I see everywhere that Applicative can handle side effects, but all the simple examples I've seen are just combining stuff together like: > (,,) <$> [1,2] <*> ["a", "b", "c"] <*> ["foo", "bar"] [(1,"a","foo"),(1,"a","bar"),(1,"b","foo"),(1,"b","bar"), (1,"c","foo"),(1,"c","bar"),(2,"a","foo"),(2,"a","bar"), (2,"b","foo"),(2,"b","bar"),(2,"c","foo"),(2,"c","bar")] Which is cool but I can't see how that links to side effects. My understanding is that Applicative is a weak monad and so you can handle side effects (as you would do with a State monad) but you can't reuse the result of the previous

Why can applicative functors have side effects, but functors can't?

匆匆过客 提交于 2019-11-27 14:54:13
问题 I'm feeling rather silly asking this question, but it's been on my mind for a while and I can't find any answers. So the question is: why can applicative functors have side effects, but functors can't? Maybe they can and I've just never noticed...? 回答1: This answer is a bit of an over-simplification, but if we define side effects as computations being affected by previous computations, it's easy to see that the Functor typeclass is insufficient for side effects simply because there is no way

Is it possible to encode a generic “lift” function in Haskell?

梦想与她 提交于 2019-11-27 13:10:31
问题 I'm not the biggest fan of varargs, but I always thought both the applicative ( f <$> x <*> y ) and idiom ( [i| f x y |] ) styles have too many symbols. I usually prefer going the liftA2 f x y way, but I, too, think that A2 is a little ugly. From this question, I've learned it is possible to implement vararg functions in Haskell. This way, is it possible to use the same principle in order implement a lift function, such that: lift f a b == pure f <*> a <*> b I've tried replacing the + by <*>

What is Applicative Functor definition from the category theory POV?

不想你离开。 提交于 2019-11-27 10:09:17
问题 I was able to map Functor's definition from category theory to Haskell's definition in the following way: since objects of Hask are types, the functor F maps every type a of Hask to the new type F a by, roughly saying, prepending "F " to it. maps every morphism a -> b of Hask to the new morphism F a -> F b using fmap :: (a -> b) -> (f a -> f b) . So far, so good. Now I get to the Applicative , and can't find any mention of such a concept in textbooks. By looking at what it adds to Functor ,

What are the benefits of applicative parsing over monadic parsing?

自闭症网瘾萝莉.ら 提交于 2019-11-27 09:15:33
问题 There seems to be a consensus that you should use Parsec as an applicative rather than a monad. What are the benefits of applicative parsing over monadic parsing? style performance abstraction Is monadic parsing out? 回答1: The main difference between monadic and applicative parsing is in how sequential composition is handled. In the case of an applicative parser, we use (<*>) , whereas with a monad we use (>>=) . (<*>) :: Parser (a -> b) -> Parser a -> Parser b (>>=) :: Parser a -> (a ->