applicative

How and why is ap defined as liftM2 id in Haskell

梦想的初衷 提交于 2019-12-03 11:06:41
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 I missing here? The type variable a from id can be instantiated at any type, and in this case that type

the equivalence between applicative functor and monad

这一生的挚爱 提交于 2019-12-03 08:47:13
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->(x+3)] . But at least, this function is in context. Finally, I don't see the equivalence or extension

Examples of Haskell Applicative Transformers

南楼画角 提交于 2019-12-03 06:50:45
The wiki on www.haskell.org tells us the following about Applicative Transformers: So where are applicative transformers? The answer is, that we do not need special transformers for applicative functors since they can be combined in a generic way. http://www.haskell.org/haskellwiki/Applicative_functor#Applicative_transfomers I tried the following in order to try to combine a bunch of applicative functors. But all I got was bunch of errors. Here is the code: import Control.Applicative import System.IO ex x y = (:) <$> x <*> y test1 = ex "abc" ["pqr", "xyz"] -- only this works correctly as

How to parse an optional flag as a Maybe value?

ε祈祈猫儿з 提交于 2019-12-03 05:36:01
I'm trying to use optparse-applicative to parse a Maybe String but I can't find anywhere how to deal with Maybe . The only thing I found is to add a default value but I really need a Nothing if user didn't supply an option instead of "" . Is there any way to achieve this ? Here is an example of working code: import Options.Applicative data Config = Config { cIn :: String , cOut :: String } deriving Show configParser :: Parser Config configParser = Config <$> strOption (long "in" <> short 'i') <*> strOption (long "out" <> short 'o') main :: IO () main = do conf <- execParser (info configParser

Translate from monad to applicative

≡放荡痞女 提交于 2019-12-03 04:52:27
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. integer :: Parser Integer integer = read <$> (many1 space *> many1 digit) Or integer = const read <$>

Is it possible to use a bracketing syntactic sugar for an applicative functor?

自作多情 提交于 2019-12-03 04:49:56
In McBride and Paterson's 'Applicative programming with effects' they introduce some lovely syntactic sugar for lifting a pure function: [| f x y z |] for f <$> x <*> y <*> z and I recall someone somewhere else using li f w x y z il or il f v w x y z li , and I thought/hoped that might be because it could be defined using some existing language feature and cunning definition of li and il . I can't find any reference to this beyond the paper, and assuming that [| and |] aren't likely to turn up in ghc any time soon, is it possible to implement li and il somehow? I can't think of a sensible type

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

天大地大妈咪最大 提交于 2019-12-03 04:20:47
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 important thing is that the Applicative interface of such a monad complies with its return and ap , but using

How to combine Futures of different types into a single Future without using zip()

喜欢而已 提交于 2019-12-03 03:37:54
问题 I want to create a Future of type Future[(Class1,Class2,Class3)] from below code. However the only way I have found to do this is by using zip(). I find the solution ugly and properly not optimal. Can anybody enlightened me. val v = for ( a <- { val f0:Future[Class1] = process1 val f1:Future[Class2] = process2 val f2:Future[Class3] = process3 f0.zip(f1).zip(f2).map(x => (x._1._1,x._1._2,x._2)) } yield a // Future[(Class1,Class2,Class3)] I have also tried to use Future.sequence(List(f0, f1, f2

How to combine Futures of different types into a single Future without using zip()

牧云@^-^@ 提交于 2019-12-02 17:05:31
I want to create a Future of type Future[(Class1,Class2,Class3)] from below code. However the only way I have found to do this is by using zip(). I find the solution ugly and properly not optimal. Can anybody enlightened me. val v = for ( a <- { val f0:Future[Class1] = process1 val f1:Future[Class2] = process2 val f2:Future[Class3] = process3 f0.zip(f1).zip(f2).map(x => (x._1._1,x._1._2,x._2)) } yield a // Future[(Class1,Class2,Class3)] I have also tried to use Future.sequence(List(f0, f1, f2)) but this will not work as the new Future will have type of Future[List[U]] where U is the lub of

optparse-applicative Backtracking

假如想象 提交于 2019-12-01 23:28:02
问题 I'm trying to use the optparse-applicative library in an program which should perform a different action depending on the number of arguments. For example, the argument parsing for a program which calculates perimeters: module TestOpts where import Options.Applicative type Length = Double data PerimeterCommand = GeneralQuadranglePerimeter Length Length Length Length | RectanglePerimeter Length Length parsePerimeterCommand :: Parser PerimeterCommand parsePerimeterCommand = parseQuadPerimeter <