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

Operator section for applicative with <$> and <*>

Consider functions of type a -> b -> c, and the applicative values a1, a2 :: (Applicative f) => f a. I wish to construct a function which may be applied to functions of type a -> b -> c to obtain values of type Applicative f :: f c. I can do this…
frasertweedale
  • 5,424
  • 3
  • 26
  • 38
5
votes
1 answer

Concurrent data access as in Haxl and Stitch

This is a follow-up to my previous question. As I understand from Haxl and Stitch they use a monad for data access. The monad is actually a tree of data access commands. The children are the commands the node depends on. The siblings are executed…
Michael
  • 41,026
  • 70
  • 193
  • 341
5
votes
1 answer

optparse-applicative Backtracking

I'm trying to use the optparse-applicative library in an program which should perform a different action depending on the number of arguments. For example, the argument parsing for a program which calculates perimeters: module TestOpts where import…
5
votes
2 answers

Haskell Applicative [] why can I not replace pure[] with [] in function?

ghci> :t pure [] pure [] :: Applicative f => f [a] ghci> pure [] [] ghci> :t [] [] :: [a] ghci> fmap ((:) 2) (pure []) [2] ghci> fmap ((:) 2) ([]) [] I would have thought replacing pure[] with [] in fmap ((:) 2) (pure []) would result in…
Roely de Vries
  • 213
  • 1
  • 4
5
votes
1 answer

Applicative Instance for (Monad m, Monoid o) => m o?

Sorry for the terrible title. I'm trying to make an instance of Applicative for a Monad wrapping a type that is a Monoid. instance (Monad m, Monoid o) => Applicative (m o) where pure x = return mempty xm <*> ym = do x <- xm y…
Callum Rogers
  • 15,630
  • 17
  • 67
  • 90
5
votes
2 answers

Function in haskell that like catMaybes, but having type [Maybe a] -> Maybe [a]

I would like to have a function with the type: f :: [Maybe a] -> Maybe [a] e.g. f [Just 3, Just 5] == Just [3, 5] f [Just 3, Nothing] == Nothing f [] == Just [] It is similar to catMaybes :: [Maybe a] -> [a] in Data.Maybe, except that catMaybes…
Causality
  • 1,123
  • 1
  • 16
  • 28
5
votes
4 answers

Does Scalaz have something to accumulate in both error and success?

I started to use Scalaz 7 Validation and/or disjunction to process a list of possibly failing operation and managing their result. There is two well documented case for that kind of use cases: 1/ You want to check a list of conditions on something,…
fanf42
  • 1,828
  • 15
  • 22
5
votes
2 answers

Applicative constructor for records

I want to generically create applicative constructors for haskell records in order to create a parser for the record. Consider the record: data Record = Record {i :: Int, f :: Float} the constructor I want: Record <$> pInt <*> pFloat Parsers for…
Maarten
  • 1,037
  • 1
  • 11
  • 32
4
votes
2 answers

Combining the elements of 2 lists

Assume we have two lists : val l1=List("a","b","c") val l2 = List("1","2","3") What I want is : List("a1", "b2", "c3") that is, adding the nth element of l1 with the nth element of l2 A way to achieve it is : (l1 zip l2).map (c => {c._1+c._2}) I…
bhericher
  • 667
  • 4
  • 13
4
votes
1 answer

Monadic equivalent of applicative <*

After having read Anthony's response on a style-related parser question, I was trying to convince myself that writing monadic parsers can still be rather compact. So instead of reference :: Parser Transc reference = try $ do string "#{" …
gawi
  • 13,940
  • 7
  • 42
  • 78
4
votes
5 answers

Put two monadic values into a pair and return it

I am playing with Parsec and I want to combine two parsers into one with the result put in a pair, and then feed it another function to operate on the parse result to write something like this: try (pFactor <&> (char '*' *> pTerm) `using` (*)) So I…
Lin Jen-Shin
  • 172
  • 8
4
votes
1 answer

How do I use Name as an applicative?

scala> val a = Need(20) a: scalaz.Name[Int] = scalaz.Name$$anon$2@173f990 scala> val b = Need(3) b: scalaz.Name[Int] = scalaz.Name$$anon$2@35201f scala> for(a0 <- a; b0 <- b) yield a0 + b0 res90: scalaz.Name[Int] =…
missingfaktor
  • 90,905
  • 62
  • 285
  • 365
4
votes
2 answers

What is a cocartesian comonoid, and what is a cocartesian comonoidal functor?

I've been experimenting with monoids and Distributives lately, and I think I've found something of interest (described in my answer) - are these already known structures? (I've been unable to find any reference to them online, and I don't think I've…
janet
  • 221
  • 1
  • 9
4
votes
2 answers

In Haskell how to "apply" functions in nested context to a value in context?

nestedApply :: (Applicative f, Applicative g) => g (f (a -> b)) -> f a -> g (f b) As the type indicates, how to get that (a->b) applied to that a in the context f? Thanks for help.
Chaot1c
  • 67
  • 5
4
votes
0 answers

Could an Applicative Language use Postfix Notation?

I've always found postfix languages like Factor to be far more readable than prefix (Lispy languages) and infix/postfix languages (all C-style languages, if we include both operators and functions). Unlike prefix languages, you don't need for…
Louis
  • 2,442
  • 1
  • 18
  • 15