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
2 answers

Haskell do (syntactically) inline monad unpacking with left arrow

I can't find where I saw this but I found somewhere on a reddit discussion the following syntax in a do block: do case (<- fooM) of Foo x -> .. ... instead of the usual do foo <- fooM case foo of Foo x -> .. ... Which would…
fakedrake
  • 6,528
  • 8
  • 41
  • 64
1
vote
1 answer

Validation of multiple ADTs with Scala Cats Validated

I am trying to validate a config in scala. First I convert the config json to the respective case class and then I validate it. As I want to fail slow I collect all the validations that fail rather than returning after the first validation that…
advocateofnone
  • 2,527
  • 3
  • 17
  • 39
1
vote
1 answer

Implementing sequenceAL for lists

Typeclassopedia (Haskell Wiki) has this exercise in Applicative's section: Implement a function sequenceAL :: Applicative f => [f a] -> f [a] I could only do it, if used the do notation (requiring Monad f to be added to the context: sequenceAL ::…
coder_bro
  • 10,503
  • 13
  • 56
  • 88
1
vote
1 answer

ApplicativeDo pragma and Applicative Functor in Haskell

An example from Functor, Applicative, and Monad slightly changed: {-# LANGUAGE ApplicativeDo #-} import Safe (readMay) -- import Control.Applicative ((<$>), (<*>)) displayAge maybeAge = case maybeAge of Nothing -> putStrLn "You…
Vladimir
  • 541
  • 2
  • 8
1
vote
0 answers

Haskell: Will Kurt exercise - getting more info from dictionary

Exercise 28.3 "get programming with haskell" from Will Kurt. Idea is to use the cheapest robot part using applicative functors. Now I wrote the code below, which does exactly that, but I don't want to know the cost, but actually the entire part…
Madderote
  • 1,107
  • 10
  • 19
1
vote
2 answers

Instance applicative on datatype `List`

The Haskell book Haskell Programming from First Principles has an exercise which asks me to instance Applicative on the datatype List: data List a = Nil | Cons a (List a) deriving (Eq, Show) instance Functor List where fmap _ Nil = Nil …
cmal
  • 2,062
  • 1
  • 18
  • 35
1
vote
0 answers

Why does the Functor class in Haskell not include a function on objects? Is `pure` that function?

In Category theory, it is very conspicuous that a definition of a functor should include two functions: on objects and on arrows. However, the usual Haskell Prelude.Functor does not make any mention of the former. Why is that? Would pure be that…
Ignat Insarov
  • 4,660
  • 18
  • 37
1
vote
3 answers

What does <*> do in addRecip x y = fmap (+) (recipMay x) <*> recipMay y?

addRecip :: Double -> Double -> Maybe Double addRecip x y = fmap (+) (recipMay x) <*> recipMay y where recipMay a | a == 0 = Nothing | otherwise = Just (1 / a) I look up some explanation for <*>. <*> takes a functor that…
user8314628
  • 1,952
  • 2
  • 22
  • 46
1
vote
2 answers

what does (<*>) :: f (a -> b) -> f a -> f b exactly do in the Functor class

class Functor f => Applicative f where pure :: a -> f a (<*>) :: f (a -> b) -> f a -> f b From my understanding, it takes a function f, where another function (a -> b) as its argument, returns a function f. Applying f to a then…
user8314628
  • 1,952
  • 2
  • 22
  • 46
1
vote
4 answers

Applicative instance trying to use monoidal functors

I'm learning Haskell and trying to do exercises from book Haskell Programming from first principles and I'm stack trying to write applicative for Pair type data Pair a = Pair a a deriving Show I have seen some other examples on web but I'm trying…
simkestuff
  • 55
  • 1
  • 3
1
vote
1 answer

Defining arity-generic lift

I'm trying to define liftN for Haskell. The value-level implementation in dynamically typed languages like JS is fairly straightforward, I'm just having trouble expressing it in Haskell. After some trial and error, I arrived at the following, which…
Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
1
vote
1 answer

Haskell ZipList Applicative

I'm trying to write an instance of Applicative for my ZipList and I'm getting some confusing results. data List a = Nil | Cons a (List a) deriving (Eq, Show) newtype ZipList' a = ZipList' (List a) deriving (Eq, Show) instance…
Aaron
  • 15
  • 7
1
vote
2 answers

Adding a Monoid constraint to a hidden variable

I'm in the Haskell Book chapter on Applicative. I am writing the Applicative instance for ZipList and I know I overthought it and am changing my approach. My old approach was: data List a = Nil | Cons a (List a) deriving (Eq, Show) newtype ZipList'…
tesserakt
  • 3,231
  • 4
  • 27
  • 40
1
vote
2 answers

Multiple validations on single object?

scalaz Validations have +++ which accumlates both errors and successes. However my success type isn't a F[T] with Semigroup[F], it's just T (unless I use the Id semigroup...). Basically I want to accumulate errors only. Is there such a method? i.e.…
allidoiswin
  • 2,543
  • 1
  • 20
  • 23
1
vote
1 answer

Haskell Applicative usage of <* and *>

So I'm creating a basic parser in Haskell, and I recently learned that instead of something like this: sumParser = fmap (\_ a _ b _ -> a + b) ws <*> val <*> plus <*> val <*> eof I could make it cleaner using something like sumParser = fmap (+) ws…