applicative

Arrows are exactly equivalent to applicative functors?

陌路散爱 提交于 2019-12-20 08:38:43
问题 According to the famous paper Idioms are oblivious, arrows are meticulous, monads are promiscuous, the expressive power of arrows (without any additional typeclasses) should be somewhere strictly between applicative functors and monads: monads are equivalent to ArrowApply , and Applicative should be equivalent to something the paper calls "static arrows". However, it is not clear to me what restriction this "static"-ness means. Playing around with the three typeclasses in question, I was able

Applicative parser example in Scala

天涯浪子 提交于 2019-12-20 03:24:05
问题 This is a new version of my previous question We can define a parser as type Parser[A] = String => List[(A, String)] . The parser takes an input string and yields a sequence of pairs. Each pair consists of the parsing result and unconsumed part of the input. (See more in this article) Now we can define a parser pa , which succeeds if the 1st input character is a and fails otherwise. def symbol(c: Char): Parser[Char] = {s: String => s.toList match { case x :: xs if x == c => List((x, xs

Can I write a higher order type for a -> b -> *?

放肆的年华 提交于 2019-12-19 05:24:08
问题 I understand that (->) a is a higher order type of kind * -> * , that when applied to a type argument b gives the type a -> b Can I write a type of kind * -> * that when applied to c would give a -> b -> c ? If not, why not? Maybe using some language extensions and forall ? This would let me write instances of Functor and Applicative (and other classes) where the functorial structure is " a -> b -> " as in: (<*>) :: Applicative t => t (c -> d) -> t c -> t d (<*>) :: (a -> b -> c -> d) -> (a -

Monad more powerful than Applicative?

谁都会走 提交于 2019-12-19 05:00:50
问题 I looked at past discussion but could not see why any of the answers are actually correct. Applicative <*> :: f (a -> b) -> f a -> f b Monad (>>=) :: m a -> (a -> m b) -> m b So if I get it right, the claim is that >>= cannot be written by only assuming the existence of <*> Well, let's assume I have <*> . And I want to create >>= . So I have f a . I have f (a -> b) . Now when you look at it, f (a -> b) can be written as (a -> b) (if something is a function of x, y , z - then it's also a

Haskell - What is Control.Applicative.Alternative good for?

ぐ巨炮叔叔 提交于 2019-12-18 01:58:48
问题 I was looking at the Applicative class within Haskell libraries and stumbled across Alternative . What is this class good for? A google search did not reveal anything particularly insightful. And it seems to be completely out of place, bundled as it is with the Applicative package. Could someone please post a possible scenario where you would use this class? 回答1: It's commonly used with parser combinators. For example, if space is a parser combinator that matches a single whitespace character

Why should Applicative be a superclass of Monad?

被刻印的时光 ゝ 提交于 2019-12-17 15:24:32
问题 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

Applicative functor evaluation is not clear to me

心不动则不痛 提交于 2019-12-17 09:58:12
问题 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

non-monadic error handling in Haskell?

杀马特。学长 韩版系。学妹 提交于 2019-12-14 03:51:34
问题 I was wondering if there is an elegant way to do non-monadic error handling in Haskell that is syntactically simpler than using plain Maybe or Either . What I wanted to deal with is non-IO exceptions such as in parsing, where you generate the exception yourself to let yourself know at a later point, e.g., something was wrong in the input string. The reason I ask is that monads seem to be viral to me. If I wanted to use exception or exception-like mechanism to report non-critical error in pure

Is the implementation of `<*>` based on `fmap` special to Maybe applicative or can it be generalized to other applicatives?

眉间皱痕 提交于 2019-12-13 09:46:50
问题 In Maybe applicative, <*> can be implemented based on fmap . Is it incidental, or can it be generalized to other applicative(s)? (<*>) :: Maybe (a -> b) -> Maybe a -> Maybe b Nothing <*> _ = Nothing (Just g) <*> mx = fmap g mx Thanks. See also In applicative, how can `<*>` be represented in terms of `fmap_i, i=0,1,2,...`? 回答1: It cannot be generalized. A Functor instance is unique: instance Functor [] where fmap = map but there can be multiple valid Applicative instances for the same type

The composition law of Applicative in the Typeclassopedia

前提是你 提交于 2019-12-12 19:11:14
问题 I am reading Typeclassopedia and I was having trouble in the section on Applicatives. I think I (sort of) have it figured out but I want to see if my understanding is correct. The laws for applicative made sense right up until the Composition law. I just couldn't parse the right-hand side of this: u <*> (v <*> w) = pure (.) <*> u <*> v <*> w So, I fired up GHCI and ran some experiments. Prelude> pure (.) <*> Just (+1) <*> Just (+2) <*> Just 34 Just 37 So this verifies the law, but I still