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

Inferred conversion to applicative

I am using the hardware description tool Clash. Though this is a hardware description tool, my question is purely about Haskell. There is a datatype of the form data Signal dom a = ... This datatype has an Applicative instance as follows: instance…
1
vote
1 answer

Applicative functors for my own data type (Haskell)

I´m trying to understand haskell and I´m stuck with a "cannot construct the infinite type"-error I want to implement "<*>" for my own data type, imitating the behaviour of a list. My functioning code so far: data List a = Nil | Cons a (List a)…
1
vote
2 answers

How are <$> and <*> pronounced?

I am learning Haskell. One of the exercises I was asked to do was to compute all sums of a power set of a set of integers, e.g.: allSums [1, 2, 5] -- should be [8,3,6,1,7,2,5,0] I came up with this after reading about applicatives and functors, to…
daikonradish
  • 682
  • 5
  • 14
1
vote
1 answer

Consuming Hasql statement outputs using something like a parser

I have an application which models a data domain using some deeply nested record structures. A contrived but analogous example would be something like: Book - Genre - Author - Hometown - Country I've found that when writing queries…
GTF
  • 8,031
  • 5
  • 36
  • 59
1
vote
0 answers

Kotlin: Applicatives with Extension Functions

From what I understood, Applicatives are classes which implement the method apply, but I've seen a version with functions too. This is what it should look like: fun List.ap(fab: List<(T) -> R>): List = fab.flatMap { f -> this.map(f)…
RidiX
  • 819
  • 6
  • 9
1
vote
1 answer

How to pipe ap calls after using getApplicativeValidation on an Either in fp-ts?

Yesterday me and a few colleagues were trying to get a toy example for applicative validation in fp-ts to work. We finally got it working with manually storing each interim step in a variable and calling the next step. But it would be way more…
groma
  • 83
  • 1
  • 4
1
vote
2 answers

Create an Applicative implementation of `Prob` from Learn you a Haskell

The problem description It seems that something has changed in Haskell since the publication of Learn you a Haskell for Great Good. When creating Monads you not only have to define the Functor but also now the Applicative implementation. In Chapter…
Fogmeister
  • 76,236
  • 42
  • 207
  • 306
1
vote
1 answer

How do I write Functor and Applicative for Vector-Composition?

I´m trying to learn more about dependent types using IDRIS. The example I am trying to emulate uses composition of Vectors. I understand Functor and Applicative implementations for Vectors but I am struggling to implement them for the…
Illuia
  • 11
  • 1
1
vote
0 answers

In Python, why not a different `bind` method instead of an applicative functor for "no short circuit" input validation?

I've been exploring how to use a monad/applicative functor for data validation in Python from scratch. In particular, I have created a new Validation class allowing to represent the result of a validation: Success and Error. It's basically a nominal…
Jiehong
  • 786
  • 1
  • 7
  • 16
1
vote
2 answers

How to define instances of Applicative and Monad when working with a constructor defined using record syntax

I am trying to define instances for Applicative and Monad of RWS, but there must be something that I am missing. For pure, I am getting an occurs check error. It must have something to do with the fact that I give RWS f as the result of pure,…
andy_bruja
  • 21
  • 4
1
vote
1 answer

Haskell: How is this formula calculated? `(return (+1)) (Just 10) 10`

This is about combining two simple arithmetic operators (two partial functions) by Applicative and Monad. I roughly understood it so far. -- ┌──────────────────────────────────────┐ -- | instance Applicative ((->) r) where | …
Larynx
  • 398
  • 1
  • 12
1
vote
1 answer
1
vote
0 answers

Is a pair of homogeneous types inherently not a monad?

This is a wrapper to a couple of two objects of the same type data Pair a = Pair a a deriving (Show) --newtype Pair a = Pair (a,a) deriving (Show) -- not this It seems to be easily made a functor, by having the mapping function act on both…
Enlico
  • 23,259
  • 6
  • 48
  • 102
1
vote
2 answers

What is the difference between pure and mempty in Haskell?

In shool, I was task to write a function which appends Numbers to the left side of a list, if they are even. The type-signature was given as: appendIfEven :: (Applicative f, Monoid (f a), Integral a) => a -> f a -> f a My answer was the following…
Felix Hohnstein
  • 459
  • 5
  • 13
1
vote
3 answers

Haskell - Applicative upon Either's Left

I'm trying to understand Applicative and Either's Left. Here is the source: instance Applicative (Either e) where pure = Right Left e <*> _ = Left e Right f <*> r = fmap f r I'm unable to understand the Left e <*> _ = Left e…
xilpex
  • 3,097
  • 2
  • 14
  • 45