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

How can I generalize the arity of rxjava2 Zip function (from Single/Observable) to n Nullable arguments without lose its types?

Two Main Problems to solve: 1) Type check is lost Using the array argument Single.zip() version I lose the strongly typed arguments. 2) Source argument Cannot be Nullable I cannot send nullable source values as argument of Single.zip() function 3) I…
developer_hatch
  • 15,898
  • 3
  • 42
  • 75
6
votes
1 answer

An alternative Alternative for lists

A few times now, I've found myself defining: () :: [a] -> [a] -> [a] [] ys = ys xs _ = xs This is an associative operation, of course, and the empty list [] is both left- and right-identity. It functions like Python's or. It seems to me…
6
votes
0 answers

A categorical view on applicative containers

This answer from Conor McBride (pigworker) discusses Applicative functors that are also containers (data types given by a set of shapes and a family of positions). Among other things, he mentions that: A polymorphic function between two containers…
6
votes
3 answers

How does <*> derived from pure and (>>=)?

class Applicative f => Monad f where return :: a -> f a (>>=) :: f a -> (a -> f b) -> f b (<*>) can be derived from pure and (>>=): fs <*> as = fs >>= (\f -> as >>= (\a -> pure (f a))) For the line fs >>= (\f -> as >>= (\a ->…
user8314628
  • 1,952
  • 2
  • 22
  • 46
6
votes
1 answer

Why would validation break the monad laws?

On SO an explanation is given why a Validation like in scalaz, cats (Scala), or Arrow (Kotlin) can't be a monad. As far as I understand it's because they've modelled monads in terms of applicative functors and the desired behaviour of a Validation…
Jan Bols
  • 457
  • 4
  • 11
6
votes
2 answers

Combining validators in applicative style in Haskell

I have a good grasp on imperative programming, but now I learn myself a Haskell for great good. I think, I have a good theoretical understanding of Monads, Functors and Applicatives, but I need some practice. And for practice I sometimes bring some…
dmzkrsk
  • 2,011
  • 2
  • 20
  • 30
6
votes
2 answers

Haskell: How do I create a function that allows none, one or two Applicatives?

Control.Applicative.optional allows to handle zero or one Applicatives. many & some allow for 0 or more, or 1 or more, respectively. I'd like to create a function that handles zero, one or two, specifically. The signature could be as for many/some,…
user3416536
  • 1,429
  • 9
  • 20
6
votes
2 answers

Difference between 'Just' and 'pure'

Just (+) <*> Just 3 <*> Just 5 Just 8 pure (+) <*> Just 3 <*> Just 5 Just 8 pure (*3) <*> [0..10] [0,3,6,9,12,15,18,21,24,27,30] Just (*3) <*> [0..10] Couldn't match type ‘[]’ with ‘Maybe’ Expected type: Maybe b Actual type: [b] In the…
Rahn
  • 4,787
  • 4
  • 31
  • 57
6
votes
5 answers

(r ->) applicative functor

I am having some trouble understanding how the function instance (->) r of Applicative works in Haskell. For example if I have (+) <$> (+3) <*> (*100) $ 5 I know you get the result 508, I sort of understand that you take the result of (5 + 3) and…
Yusuf
  • 491
  • 2
  • 8
6
votes
2 answers

How does GHCi print partially-applied values created from "pure"?

I've been playing around with Applicative instances in order to figure out how they work. However, I honestly don't understand this behavior. If I define my own datatype, then apply pure to it with no other arguments, nothing prints out, but it…
Alexis King
  • 43,109
  • 15
  • 131
  • 205
6
votes
1 answer

How to use Applicative for concurrency?

This is a follow-up to my previous question. I copied the example below from Haxl Suppose I am fetching data from a blog server to render a blog page, which contains the recent posts, popular posts and posts topics. I have the following Data…
Michael
  • 41,026
  • 70
  • 193
  • 341
6
votes
3 answers

Struggling with Applicative parsing

So I'm having a go at writing a complex parser, using only Applicative (the parser in question doesn't even implement Monad at all). For trivial parsers, this is quite easy. For non-trivial ones... not so much. The applicative interface seems to…
MathematicalOrchid
  • 61,854
  • 19
  • 123
  • 220
6
votes
1 answer

Which is the most idiomatic way to "lift up" by some transformation both arguments of a binary function in Haskell?

Which is the most idiomatic way to "lift up" by some transformation both arguments of a binary function in Haskell? Let this operator be named "lift", so I expect it's type will be lift :: (a -> b) -> (b -> b -> c) -> (a -> a -> c) and a naive…
6
votes
4 answers

Proving Composition Law for Maybe Applicative

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…
Sibi
  • 47,472
  • 16
  • 95
  • 163
6
votes
1 answer

Ignoring arguments in Control.Applicative

I am writing an xml-conduit parser, and I prefer applicative syntax to monadic. With lots of arguments to combine, I get somewhat lost in applicative though. My current problem is given 8 arguments, I only want to use the 4th and the 6th to…