applicative

Nested applicative functors of different types in Haskell

纵然是瞬间 提交于 2019-12-24 03:41:11
问题 I'd like to make the nested applicative functors of different types. For example, nested simple functors of different types (in ghci) work fine: Prelude> ((+2) <$>) <$> (Just [1..4]) Just [3,4,5,6] But for applicative functors of different types: Prelude> ((*) <$>) <$> (Just [1,2,3]) <*> (Just [4,5,6,7]) <interactive>:56:1: error: * Couldn't match type `[Integer -> Integer]' with `[Integer] -> b' isn't working! I want to obtain something like this: Just [4,5,6,7,8,10,12,14,12,15,18,21] I know

Applicative style parser for constructor with two arguments

拟墨画扇 提交于 2019-12-24 00:15:04
问题 I want to write a parser for a comma separated pair of values in angle brackets. I got it to work with the following approach: pair p1 p2 = do x1 <- p1 comma x2 <- p2 return (x1, x2) data Foo = Foo (Bar, Bar) foo :: Parser Foo foo = Foo <$> (angles $ pair bar bar) However I would prefer the Foo constructor to take two parameter rather than a tuple: data Foo = Foo Bar Bar What is the best way to write such a parser? Ideally I would like to reuse standard Parsec parsers such a angles and use

Applicative parser stuck in infinite loop

拜拜、爱过 提交于 2019-12-23 23:24:05
问题 I'm trying to implement my own Applicative parser, here's the code I use: {-# LANGUAGE ApplicativeDo, LambdaCase #-} module Parser where -- Implementation of an Applicative Parser import Data.Char import Control.Applicative (some, many, empty, (<*>), (<$>), (<|>), Alternative) data Parser a = Parser { runParser :: String -> [(a, String)] } instance Functor Parser where -- fmap :: (a -> b) -> (Parser a -> Parser b) fmap f (Parser p) = Parser (\s -> [(f a, s') | (a,s') <- p s]) instance

How do I use Name as an applicative?

ぐ巨炮叔叔 提交于 2019-12-23 13:14:05
问题 scala> val a = Need(20) a: scalaz.Name[Int] = scalaz.Name$$anon$2@173f990 scala> val b = Need(3) b: scalaz.Name[Int] = scalaz.Name$$anon$2@35201f scala> for(a0 <- a; b0 <- b) yield a0 + b0 res90: scalaz.Name[Int] = scalaz.Name$$anon$2@16f7209 scala> (a |@| b) res91: scalaz.ApplicativeBuilder[scalaz.Name,Int,Int] = scalaz.ApplicativeBuilde r@11219ec scala> (a |@| b) { _ + _ } <console>:19: error: ambiguous implicit values: both method FunctorBindApply in class ApplyLow of type [Z[_]](implicit

x <*> y <$> z in Haskell

僤鯓⒐⒋嵵緔 提交于 2019-12-23 11:54:39
问题 I'm trying to understand some Haskell source code, and I encountered this structure some times: x <*> y <$> z e.g. (+) <*> (+1) <$> a Can somebody explain this structure to me? I get that it translates to fmap a (+ a + 1) , but I can't make the connection 回答1: Let's start with: x <*> y <$> z Adding parentheses, it becomes: (x <*> y) <$> z Given that (<$>) :: Functor f => (a -> b) -> f a -> f b , we have: x <*> y :: a -> b z :: Functor f => f a Given that (<*>) :: Applicative g => g (c -> d) -

Why is f <$> g <$> x equivalent to (f . g) <$> x although <$> is not right-associative?

吃可爱长大的小学妹 提交于 2019-12-23 07:03:12
问题 Why is f <$> g <$> x equivalent to (f . g) <$> x although <$> is not right-associative? (This kind of equivalence is valid in a popular idiom with plain $ , but currently $ is right-associative!) <*> has the same associativity and precedence as <$> , but behaves differently! Example: Prelude Control.Applicative> (show . show) <$> Just 3 Just "\"3\"" Prelude Control.Applicative> show <$> show <$> Just 3 Just "\"3\"" Prelude Control.Applicative> pure show <*> pure show <*> Just 3 <interactive>

How to transform Either[Future[A], Future[B]] to Future[Either[A, B]]

孤街醉人 提交于 2019-12-22 04:01:23
问题 I have an instance of Either[Future[A], Future[B]] and I would like to transform it to Future[Either[A, B]] . Since my previous question, cats 0.8.1 has been released, changing the structure of the library and dropping Xor in favor of Either , which is right-biased in 2.12. Thus the method described in the previous accepted answer does not work anymore. I have tried to find the appropriate imports but failed. cats.instances.either._ cats.implicits._ cats.syntax.bitraverse._ looked plausible

Resolving the type of `f = f (<*>) pure`

半城伤御伤魂 提交于 2019-12-22 03:59:11
问题 Recently I noticed that humourously liftA can be written as liftA (<*>) pure I thought this was neat and so as a bit of a joke I thought I would make a new "definition" of liftA based on this property: f = f (<*>) pure Now I had expected that this would be something of the same type as liftA that just never halted. However it fails to compile. • Occurs check: cannot construct the infinite type: t ~ (f (a -> b) -> f a -> f b) -> (a1 -> f1 a1) -> t • In the expression: f (<*>) pure In an

Is it better to define Functor in terms of Applicative in terms of Monad, or vice versa?

≡放荡痞女 提交于 2019-12-20 10:43:23
问题 This is a general question, not tied to any one piece of code. Say you have a type T a that can be given an instance of Monad . Since every monad is an Applicative by assigning pure = return and (<*>) = ap , and then every applicative is a Functor via fmap f x = pure f <*> x , is it better to define your instance of Monad first, and then trivially give T instances of Applicative and Functor ? It feels a bit backward to me. If I were doing math instead of programming, I would think that I

Arrows are exactly equivalent to applicative functors?

北战南征 提交于 2019-12-20 08:38:57
问题 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