Questions tagged [applicative]

In Haskell, Applicative functors are functors such that two functorial values can be combined into one, whilst the two values inside are combined via a functional application. An applicative functor has more structure than a functor but less than a monad.

In Haskell, applicative functors are functors such that two functorial values (of type Applicative f => f a) can be combined into one, and the two values inside will be combined via a functional application (so the types "on the inside" must be compatible).

Definition

 class (Functor f) => Applicative f where
   pure  :: a -> f a                          -- injection
   (<*>) :: f (a -> b) -> f a -> f b          -- combination

The pure function lifts any value into the functor. (<*>) changes a functorial function into a function over functorial values. Applicative functor should satisfy some laws:

 pure id <*> v = v                            -- Identity
 pure (.) <*> u <*> v <*> w = u <*> (v <*> w) -- Composition
 pure f <*> pure x = pure (f x)               -- Homomorphism
 u <*> pure y = pure ($ y) <*> u              -- Interchange

And the Functor instance should satisfy the following law:

 fmap f x = pure f <*> x                      -- Fmap
572 questions
1
vote
1 answer

Optparse-applicative: consecutive parsing (ReadM)

I have a basic command add that takes 2 kind of arguments: a word or a tag. A tag is just a word starting by +. A word is just a String. It can contain at least one argument (I use some for this). data Arg = Add AddOpts data AddOpts = AddOpts {…
soywod
  • 4,377
  • 3
  • 26
  • 47
1
vote
2 answers

understanding functors and monoids

I have been looking over this code for so long now and I still do not understand what it is trying to do. what does Functor ((,) x) mean? Similarly, what does Applicative ((,) x) mean why does pure a = (mempty ,a), what is mempty? what are the…
henry
  • 61
  • 5
1
vote
1 answer

How could sequence be written down for Arrows?

sequenceA is a well-known function: sequenceA :: (Traversable t, Applicative f) => t (f a) -> f (t a) I wonder whether we can write down something similar for Arrows. Unfortunately, I did not manage to implement the following: sequenceArr ::…
Zhiltsoff Igor
  • 1,812
  • 8
  • 24
1
vote
1 answer

How to turn applicative validation to return MonadThrow?

It seems to me that an idiomatic way to validate input data in Haskell is via an applicative chain: mkMyData :: a -> b -> c -> Maybe MyData mkMyData x y z = MyData <$> validateA x <*> validateB y <*> validateC z where…
Ulrich Schuster
  • 1,670
  • 15
  • 24
1
vote
4 answers

How to combine two composed applicative functors?

I have two composed applicative functors Maybe [Integer] and want to combine them with <$>/<*> but I am stuck with applying the applicative operation. The following does not typecheck: (<*>) (<*>) ((<$>) ((+) <$>) $ Just [1,2,3]) $ Just…
user5536315
1
vote
1 answer

Can Haskell Typeclasses be Partially Implemented?

I want to make a pair type to represent modular arithmetic. I made its constructor {- LANGUAGE GADTs -} data Zn e where Zn :: Integer -> Integer -> Zn Integer because I would like to be able to fmap over it and all the things. So if I try to…
hacatu
  • 638
  • 6
  • 15
1
vote
1 answer

The <* syntax in Haskell

I'm given the following code newtype Parser a = Parser { parse :: String -> Maybe (a,String) } instance Applicative Parser where pure a = Parser $ \s -> Just (a,s) f <*> a = Parser $ \s -> case parse f s of Just (g,s') ->…
Tomer
  • 1,159
  • 7
  • 15
1
vote
1 answer

Combining parsers in Haskell

I'm given the following parsers newtype Parser a = Parser { parse :: String -> Maybe (a,String) } instance Functor Parser where fmap f p = Parser $ \s -> (\(a,c) -> (f a, c)) <$> parse p s instance Applicative Parser where pure a = Parser $…
Tomer
  • 1,159
  • 7
  • 15
1
vote
1 answer

How do you define the monad instance for the following data types?

Here's the code I have to work with: infixl 9 :@: -- This is newly defined symbol used in the application of expressions data Expr = Lit Integer -- a literal | Var String -- a variable | Bin Expr Op Expr -- binary operator | …
Iamfour
  • 11
  • 1
1
vote
1 answer

How to define (*>), (<*) in terms of (<*>) and pure?

I can define them using monads. (<*) :: Monad m => m a -> m b -> m a (<*) fa fb = fa >>= \a -> (fb >>= \_ -> return a) (<*) fa fb = ??? -- In terms of pure & (<*>) (*>) :: Monad m => m a -> m b -> m b (*>) fa fb = fa >>= \_ -> (fb >>= \b -> return…
Pawan Kumar
  • 1,443
  • 2
  • 16
  • 30
1
vote
3 answers

Understanding Functions as Applicatives in Haskell

I've recently been trying to learn Haskell with the "Learn You a Haskell" and have been really struggling with understanding functions as Applicatives. I should point out that using other types of Applicatives like Lists and Maybe I seem to…
1
vote
1 answer

Cartesian product not working for applicative

I was trying to understand applicative and how i can use it as a cartesian product between K functions and N parameters ,and i can't comprehend why i can't do the following: [Just (+1),Just (+2)] <*> [Just 1 ,Just 2] renders Error * Couldn't match…
Bercovici Adrian
  • 8,794
  • 17
  • 73
  • 152
1
vote
1 answer

Scala, cats - how to do not use Applicative[F] explicity?

I would like to use Applicative[F] in some other way then explicity. Currently I have a simple code: class BettingServiceMock[F[_] : Async] extends BettingService[F] { override def put(bet: Bet): F[Bet] = { for { created…
Developus
  • 1,400
  • 2
  • 14
  • 50
1
vote
2 answers

How can I use functors or applicatives to rewrite this Haskell function over lists of tuples

Is there a nicer way to write the following function fs' with functors or applicatives? fncnB = (* 2) fncnA = (* 3) fs' fs = zip (map (fncnA . fst) fs) $ map (fncnB . snd) fs I see from this question that I could rely on the functor instance of…
cronburg
  • 892
  • 1
  • 8
  • 24
1
vote
2 answers

How to compose functions with applicative effects for Validation in the Cats in Scala

Here is an example from the Scala with Cats book: object Ex { import cats.data.Validated type FormData = Map[String, String] type FailFast[A] = Either[List[String], A] def getValue(name: String)(data: FormData): FailFast[String] = …
Alexandr
  • 9,213
  • 12
  • 62
  • 102