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

What is wrong with the method definition

Below are my method signatures and definitions in scala def accumulate[T[_]: Traversable, O: Monoid, A]: (A => O) => T[A] => O = fao => ta => (implicitly[Traversable[T]].traverse[({type f[X] = Acc[O, X]})#f, A, O](ta)(a =>…
Abdul Rahman
  • 1,294
  • 22
  • 41
1
vote
1 answer

Avoiding nested applicative i.e. Applicative f => f (f a)

I have a function: someFun :: Applicative f => f a -> b -> f c someFun x y = … The argument for y that I need to give someFun is an “f b” Lets say I have values someX :: Applicative f => f a someY :: Applicative f => f b I tried to do LiftA…
error_null_pointer
  • 457
  • 1
  • 6
  • 21
1
vote
1 answer

Inverse lifting on applicative functors

I'm pretty sure this has a simple solution but it eludes me and I cannot seem to find a straight answer. Normally, when applying liftA2 assuming the binary function has already been lifted once, the signature looks like this: liftA2' ::…
Iedu
  • 117
  • 8
1
vote
2 answers

99 Haskell Questions # 14

I had a question about one of the implementations of the duplicate function as described in 99 Haskell Questions (https://wiki.haskell.org/99_questions/Solutions/14). One of the solutions uses the list instance of Applicative. That particular…
user348307
  • 21
  • 3
1
vote
1 answer

de sugaring do notation

I want to de sugar the following do notation. But im unsure whether I have got it right: Is: quote = do time <- qtime qcomma ask <- double qcomma bid <- double qcomma askVolume <- double …
Devitect
  • 31
  • 3
1
vote
1 answer

How do I use my Reader: newtype R r a = R { run :: r -> a }?

I'm trying to wrap my head around the applicative instance for Reader by matching the type definition with some examples. One problem is I do not know how to use my Reader newtype. My definition of Reader is newtype R r a = R { run :: r -> a…
The Unfun Cat
  • 29,987
  • 31
  • 114
  • 156
1
vote
1 answer

What is needed to get my custom ZipList applicative instance to compile?

Here is my custom List class used to create a custom ZipList class. I want to create a ZipList' applicative instance. import Control.Applicative data List a = Nil | Cons a (List a) deriving (Eq, Show) instance Functor List where fmap f…
The Unfun Cat
  • 29,987
  • 31
  • 114
  • 156
1
vote
1 answer

How can I implement Applicative on my list, modelling non-determinism?

I was practicing Functor / Applicative Functors + Monads in Haskell when I came across this assignment: 4) You have seen how a Monad instance can be used to handle things that can fail. Another use case for a Monad is dealing with…
S. Salman
  • 590
  • 1
  • 6
  • 22
1
vote
1 answer

Applicative instance for Reader monad

I thought that every monad in scalaz had also an Applicative instance. For example, I can use ApplicativeBuilder for Option like this: scala> (1.some |@| 2.some) {_ + _} res1: Option[Int] = Some(3) Now I'd like to do the same with Reader: scala>…
Michael
  • 41,026
  • 70
  • 193
  • 341
1
vote
2 answers

How to accumulate Throwable in scalaz` \/ notation

Let's assume that I have two lists of calculation results val a: List[ Throwable \/ A] = ... val b: List[ Throwable \/ B] = ... and I have function which is calculating final result such as def calc(a: A, b: B): Throwable \/ C = ... I need to…
ponkin
  • 2,363
  • 18
  • 25
1
vote
2 answers

Lifting function `a → b → c` to `[a] → [b] → [[c]]`

I would like to have a function foo :: (a → b → c) → [a] → [b] → [[c]] that takes a function f :: a → b → c and two lists xs and ys and returns a grid (i.e. a list of lists) containing the values of f applied to every combination of values from xs…
Manuel Eberl
  • 7,858
  • 15
  • 24
1
vote
3 answers

Call multiple IO functions on the same input

Suppose I have a list of tuples (e.g. [(a,b)]) each a result of some previous computation. And I want several functions to be applied on each of these elements (e.g one function might print it another send it over the network etc.) What I've…
George Ni
  • 13
  • 4
1
vote
2 answers

How can I turn this parser into an applicative, polyvariadic one?

I'm trying to parse dates such as 09/10/2015 17:20:52: {-# LANGUAGE FlexibleContexts #-} import Text.Parsec import Text.Parsec.String import Text.Read import Control.Applicative hiding (many, (<|>)) data Day = Day { mo :: Int , dy :: Int ,…
1
vote
1 answer

How to compose functions that return Applicative in point-free style?

This is a follow-up to my previous question: Suppose I have a few functions that return scalaz.Validaton: type Status[A] = ValidationNel[String, A] val isPositive: Int => Status[Int] = x => if (x > 0) x.success else s"$x not…
Michael
  • 41,026
  • 70
  • 193
  • 341
1
vote
1 answer

Applicative instance for ZipList in Scala

This is a follow-up to one of my recent previous questions: I would like to define a zip Applicative instance for List (and probably Set and Map). For example: val xs: List[Int] = List(1, 2, 3) val fs: List[Int => Int] = List(f1, f2, f3) val ys:…
Michael
  • 41,026
  • 70
  • 193
  • 341