applicative

Applicative functors as monoidal functors

久未见 提交于 2019-12-01 21:59:07
问题 As mentioned in Hackage for Applicative Functors, they are strong lax monoidal functors. So why doesn't their definition in Haskell show it like so : class Functor f => MonoidalApplicative f where mult :: f a -> f b -> f (a,b) unit :: a -> f a starAp :: f (a -> b) -> f a -> f b starAp h x = fmap (uncurry ($)) (mult h x) <*> (starAp) is easily reconstructed in terms of the multiplication and this definition looks simpler to me. For exemple, here is the Maybe instance : instance

optparse-applicative Backtracking

ⅰ亾dé卋堺 提交于 2019-12-01 20:26:44
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 <|> parseRectPerimeter parseQuadPerimeter = GeneralQuadranglePerimeter <$> parseLength "SIDE1" <*>

Applicative functors as monoidal functors

雨燕双飞 提交于 2019-12-01 19:22:02
As mentioned in Hackage for Applicative Functors , they are strong lax monoidal functors. So why doesn't their definition in Haskell show it like so : class Functor f => MonoidalApplicative f where mult :: f a -> f b -> f (a,b) unit :: a -> f a starAp :: f (a -> b) -> f a -> f b starAp h x = fmap (uncurry ($)) (mult h x) <*> (starAp) is easily reconstructed in terms of the multiplication and this definition looks simpler to me. For exemple, here is the Maybe instance : instance MonoidalApplicative Maybe where mult (Just x) (Just y) = Just (x,y) mult _ _ = Nothing unit x = Just x As it was

How exactly does the `(<*>) = ap` Applicative/Monad law relate the two classes?

岁酱吖の 提交于 2019-12-01 19:20:32
ap doesn't have a documented spec, and reads with a comment pointing out it could be <*> , but isn't for practical reasons: ap :: (Monad m) => m (a -> b) -> m a -> m b ap m1 m2 = do { x1 <- m1; x2 <- m2; return (x1 x2) } -- Since many Applicative instances define (<*>) = ap, we -- cannot define ap = (<*>) So I assume the ap in the (<*>) = ap law is shorthand for "right-hand side of ap" and the law actually expresses a relationship between >>= , return and <*> right? Otherwise the law is meaningless. The context is me thinking about Validation and how unsatisfying it is that it can't seem to

How exactly does the `(<*>) = ap` Applicative/Monad law relate the two classes?

妖精的绣舞 提交于 2019-12-01 17:58:57
问题 ap doesn't have a documented spec, and reads with a comment pointing out it could be <*> , but isn't for practical reasons: ap :: (Monad m) => m (a -> b) -> m a -> m b ap m1 m2 = do { x1 <- m1; x2 <- m2; return (x1 x2) } -- Since many Applicative instances define (<*>) = ap, we -- cannot define ap = (<*>) So I assume the ap in the (<*>) = ap law is shorthand for "right-hand side of ap" and the law actually expresses a relationship between >>= , return and <*> right? Otherwise the law is

Proving Composition Law for Maybe Applicative

不羁的心 提交于 2019-12-01 05:07:10
So, I wanted to manually prove the Composition law for Maybe applicative which is: u <*> (v <*> w) = pure (.) <*> u <*> v <*> w I used these steps to prove it: u <*> (v <*> w) [Left hand side of the law] = (Just f) <*> (v <*> w) [Assume u ~ Just f] = fmap f (v <*> w) = fmap f (Just g <*> w) [Assume v ~ Just g] = fmap f (fmap g w) = fmap (f . g) w pure (.) <*> u <*> v <*> w [Right hand side of the law] = Just (.) <*> u <*> v <*> w = fmap (.) u <*> v <*> w = fmap (.) (Just f) <*> v <*> w [Replacing u with Just f] = Just (f .) <*> v <*> w = Just (f .) <*> Just g <*> w [Replacing v with Just g] =

How to implement Future as Applicative in Scala?

百般思念 提交于 2019-12-01 04:49:45
Suppose I need to run two concurrent computations, wait for both of them, and then combine their results. More specifically, I need to run f1: X1 => Y1 and f2: X2 => Y2 concurrently and then call f: (Y1, Y2) => Y to finally get a value of Y . I can create future computations fut1: X1 => Future[Y1] and fut2: X2 => Future[Y2] and then compose them to get fut: (X1, X2) => Future[Y] using monadic composition. The problem is that monadic composition implies sequential wait . In our case it implies that we wait for one future first and then we will wait for another. For instance. if it takes 2 sec.

Why is there not 'Alternative' instance for 'Control.Applicative.Const'

假如想象 提交于 2019-12-01 03:08:51
There is an instance Monoid a => Monoid (Const a b) for the Const functor from Control.Applicative . There is also an instance Monoid m => Applicative (Const m) . I would therefore expect that there is also an instance Monoid m => Alternative (Const m) that coincides with the one for Monoid . Is this just an omission that should be fixed, or is there a deeper reason? Petr Pudlák I believe there is a deeper reason. While it seems there is no canonical set of rules for Alternative , in order for Alternative to make sense, there definitely ought to be a relationship between Alternative and its

Proving Composition Law for Maybe Applicative

纵然是瞬间 提交于 2019-12-01 02:46:57
问题 So, I wanted to manually prove the Composition law for Maybe applicative which is: u <*> (v <*> w) = pure (.) <*> u <*> v <*> w I used these steps to prove it: u <*> (v <*> w) [Left hand side of the law] = (Just f) <*> (v <*> w) [Assume u ~ Just f] = fmap f (v <*> w) = fmap f (Just g <*> w) [Assume v ~ Just g] = fmap f (fmap g w) = fmap (f . g) w pure (.) <*> u <*> v <*> w [Right hand side of the law] = Just (.) <*> u <*> v <*> w = fmap (.) u <*> v <*> w = fmap (.) (Just f) <*> v <*> w

Monad more powerful than Applicative?

独自空忆成欢 提交于 2019-12-01 02:02:18
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 function of x, y). So from the existence of <*> we get (a -> b) -> f a -> f b which again can be written as