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
0
votes
4 answers

Looking for a function similar sequenceA

Tying to learn more about applicatives. I have the following definitions: class Applicative' f where pack :: a -> f a unpack :: f a -> a instance Applicative' Box where pack x = Box x unpack (Box x) = x instance Applicative' Bag…
coder_bro
  • 10,503
  • 13
  • 56
  • 88
0
votes
1 answer

Lifting a function from real numbers to complex numbers in Haskell

I wrote a function that takes a list and a number and returns a sum of shifted inverses: sumInvZ::[Float]->Float->Float sumInvZ zList z = let invList = [if z==x then 0 else 1/(z-x)|x<-zList] in foldr (+) 0 invList I want to generalize this function…
Whelp
  • 103
  • 3
0
votes
1 answer

What is a "applicative transformation" in naturality of traversability?

The traverse and sequenceA function in Traversable class must satisfy the following 'naturality' laws: t . traverse f == traverse (t . f) t . sequenceA == sequenceA . fmap t for every 'applicative transformation' t. But what is it? It doesn't seem…
Dannyu NDos
  • 2,458
  • 13
  • 32
0
votes
1 answer

Applicative style using cats library

This is a follow-up to my previous question: Suppose I want to use Applicative to apply function A => B => C to List[A] and List[B]. I believe it looks like that in Haskell: pure f <*> as <*> bs // apply f to List[A] and List[B] or f <$> as <*> bs…
Michael
  • 41,026
  • 70
  • 193
  • 341
0
votes
1 answer

What are get() and unit() in this definition of applicative functor?

I am trying to code up some functors, monads, and applicatives in java. i found a few and picked the one below. In terms category theory, what is get() returning? The unit() seems like some kind of identity, but from what to what? Or perhaps this is…
Ray Tayek
  • 9,841
  • 8
  • 50
  • 90
0
votes
1 answer

How to make a phantom applicative functor in PureScript?

I'm reading through this paper and it says that Monoids are phantom applicative functors. I tried setting up a phantom type in purescript but I get a type error in the Functor instance. My guess is the compiler doesn't know what the a is in Accy o…
Albtzrly
  • 924
  • 1
  • 6
  • 15
0
votes
1 answer

Understanding different monadic and applicative binds/combinators in the context of a data type constructor?

I've been given the following piece of code, which I am trying to get my head around: data MyExample e i = MyExample (CustomMonad e i) | forall b. MyExample e b :>>= (b -> CustomMonad e b) | forall b. (MyExample e (b -> a)) :<*> (MyExample e…
Babra Cunningham
  • 2,949
  • 1
  • 23
  • 50
0
votes
1 answer

Haskell: How do you figure out the definition of <*> from a Monad definition?

I have a parser object defined: newtype Parser a = Parser (String -> [(String, a)]) And a function to produce them: produce :: a -> Parser a produce x = Parser (\ts -> [(ts, x)]) And an instance of Monad for this parser object to allow me to bind…
James
  • 104
  • 1
  • 8
0
votes
0 answers

Meaning of void angle brackets in Haskell

The example from optparse-applicative package uses void angle brackets (<>): sample :: Parser Sample sample = Sample <$> strOption ( long "hello" <> metavar "TARGET" <> help "Target for the greeting" ) <*> switch ( long…
E. Nerush
  • 69
  • 7
0
votes
1 answer

Why can't the compiler deduce the Functor type in the Free Applicative examples?

module Main where data Toy b next = Output b next | Bell next | Done data FixE f e = Fix (f (FixE f e)) | Throw e -- The working monadic function catch :: (Functor f) => FixE f e1 -> (e1 -> FixE f e2) -> FixE f e2 catch (Fix x) f = Fix…
Marko Grdinić
  • 3,798
  • 3
  • 18
  • 21
0
votes
1 answer

Applicative functors pure matching not wrapped

I'm learning about applicative functors. In the source for an applicative Maybe, the pure function looks like this: instance Applicative Maybe where pure = Just ...etc With the arguments expanded, I think it looks like: pure x = Just…
Ben
  • 54,723
  • 49
  • 178
  • 224
0
votes
2 answers

Is it possible to constrain generic classes for specific methods?

Let's say I create a container in TypeScript. It could be any container but I'll use the following simple example: class Container { val: T; constructor(t: T) { this.val = t; } } Say I'd like to offer a function so that if I have two…
paldepind
  • 4,680
  • 3
  • 31
  • 36
0
votes
0 answers

Are Monad instances uniquely determined by their Applicative instances?

In To what extent are Applicative/Monad instances uniquely determined? I asked if the for a given data type the instances are uniquely determined (which is not the case). However, a related but different question is: If we have a data type with an…
Petr
  • 62,528
  • 13
  • 153
  • 317
0
votes
0 answers

Wrapping a function's arguments

I am trying to figure out the mechanism to transform a generic function with n arguments into a function where each of the n argument is wrapped inside a context. A simple example in pseudo-Haskell, where the wrapped type is tuple: wrap :: (a1 -> a2…
Iedu
  • 117
  • 8
0
votes
1 answer

How Scotty make an Applicative constructor

{-# LANGUAGE OverloadedStrings #-} import Control.Applicative import Data.Monoid import Data.String import Network.Wai.Middleware.RequestLogger import Web.Scotty data FullName = FullName { firstName :: String, lastName :: String } lastFirst ::…
nobody
  • 414
  • 2
  • 8
  • 18