applicative

How arbitrary is the “ap” implementation for monads?

三世轮回 提交于 2019-11-30 17:23:50
I am currently studying the bonds between monad and applicative functors. I see two implementation for ap: ap m1 m2 = do { f <- m1 ; x <- m2 ; return (f x) } and ap m1 m2 = do { x <- m2 ; f <- m1 ; return (f x) } The second one is different, yet, would it be a good implementation for <*> ? I got lost in the proof of pure (.) <*> u <*> v <*> w = u <*> (v <*> w) I try to get an intuition of "what part of the monad is the applicative functor"... There are at least three relevant aspects to this question. Given a Monad m instance, what is the specification of its necessary Applicative m superclass

Haskell - Is effect order deterministic in case of Applicative?

倾然丶 夕夏残阳落幕 提交于 2019-11-30 11:31:02
When executing the IO action defined by someFun <$> (a :: IO ()) <$> (b :: IO ()) , is the execution of the a and b actions ordered? That is, can I count on that a is executed before b is? For GHC, I can see the IO is implemented using State, and also see here that it is an Applicative instance, but can't find the source of the actual instance declaration. Being implemented through State suggests that different IO effects need to be sequential, but doesn't necessary defines their ordering. Playing around in GHCi seems that Appliative retains effect order, but is that some universal guarantee,

Lax monoidal functors with a different monoidal structure

感情迁移 提交于 2019-11-30 11:20:01
问题 Applicative functors are well-known and well-loved among Haskellers, for their ability to apply functions in an effectful context. In category-theoretic terms, it can be shown that the methods of Applicative : pure :: a -> f a (<*>) :: f (a -> b) -> f a -> f b are equivalent to having a Functor f with the operations: unit :: f () (**) :: (f a, f b) -> f (a,b) the idea being that to write pure you just replace the () in unit with the given value, and to write (<*>) you squish the function and

applicative functor: <*> and partial application, how it works

主宰稳场 提交于 2019-11-30 02:59:51
问题 I am reading the book Programming in Haskell by Graham Hutton and I have some problem to understand how <*> and partial application can be used to parse a string. I know that pure (+1) <*> Just 2 produces Just 3 because pure (+1) produces Just (+1) and then Just (+1) <*> Just 2 produces Just (2+1) and then Just 3 But in more complex case like this: -- Define a new type containing a parser function newtype Parser a = P (String -> [(a,String)]) -- This function apply the parser p on inp parse :

How arbitrary is the “ap” implementation for monads?

笑着哭i 提交于 2019-11-30 01:00:09
问题 I am currently studying the bonds between monad and applicative functors. I see two implementation for ap: ap m1 m2 = do { f <- m1 ; x <- m2 ; return (f x) } and ap m1 m2 = do { x <- m2 ; f <- m1 ; return (f x) } The second one is different, yet, would it be a good implementation for <*> ? I got lost in the proof of pure (.) <*> u <*> v <*> w = u <*> (v <*> w) I try to get an intuition of "what part of the monad is the applicative functor"... 回答1: There are at least three relevant aspects to

Why can AccValidation not have a Monad instance?

你离开我真会死。 提交于 2019-11-29 19:48:24
问题 From the documentation of the validation package: The AccValidation data type is isomorphic to Either , but has an instance of Applicative that accumulates on the error side. That is to say, if two (or more) errors are encountered, they are appended using a Semigroup operation. As a consequence of this Applicative instance, there is no corresponding Bind or Monad instance. AccValidation is an example of, "An applicative functor that is not a monad." It isn't evident to me why this is a

What are practical uses of applicative style?

那年仲夏 提交于 2019-11-29 19:31:18
I am a Scala programmer, learning Haskell now. It's easy to find practical use cases and real world examples for OO concepts, such as decorators, strategy pattern etc. Books and interwebs are filled with it. I came to the realization that this somehow is not the case for functional concepts. Case in point: applicatives . I am struggling to find practical use cases for applicatives. Almost all of the tutorials and books I have come across so far provide the examples of [] and Maybe . I expected applicatives to be more applicable than that, seeing all the attention they get in the FP community.

Haskell - Is effect order deterministic in case of Applicative?

我们两清 提交于 2019-11-29 17:00:33
问题 When executing the IO action defined by someFun <$> (a :: IO ()) <$> (b :: IO ()) , is the execution of the a and b actions ordered? That is, can I count on that a is executed before b is? For GHC, I can see the IO is implemented using State, and also see here that it is an Applicative instance, but can't find the source of the actual instance declaration. Being implemented through State suggests that different IO effects need to be sequential, but doesn't necessary defines their ordering.

More fun with applicative functors

别等时光非礼了梦想. 提交于 2019-11-29 02:13:29
问题 Earlier I asked about translating monadic code to use only the applicative functor instance of Parsec. Unfortunately I got several replies which answered the question I literally asked, but didn't really give me much insight. So let me try this again... Summarising my knowledge so far, an applicative functor is something which is somewhat more restricted than a monad. In the tradition of "less is more", restricting what the code can do increases the possibilities for crazy code manipulation.

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

一笑奈何 提交于 2019-11-28 23:21:46
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...? 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 to chain multiple computations. class Functor f where fmap :: (a -> b) -> f a -> f b The only thing a