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
1 answer

Cannot create phantom Applicative functor class

I am trying to implement phantom Applicative functor from Monoid, described in section 4 in paper Applicative programming with effects: newtype Accy o a = Acc {acc :: o} instance Monoid o => Applicative (Accy o) where pure _ = Acc…
0
votes
1 answer

haskell Either and Validation Applicative

I have a question arising from the section "Either and Validation Applicative" of Chapter 17 "Applicative" of the book "Haskell Programming from first principles". I did the following code: data Validation err a = Failure err | Success a …
maxloo
  • 453
  • 2
  • 12
0
votes
1 answer

What is the correct way to "wrap" an object in the appropriate monad/applicative with Range-v3?

Let's say that given a range like this std::vector v{1, 4, 7, 2}; I want to generate another range where all even numbers are repeated a number of times equal to their value, whereas all odd numbers are left unchanged. A possible solution is…
Enlico
  • 23,259
  • 6
  • 48
  • 102
0
votes
0 answers

How to generalize from Lens to Traversal and from Getter to Fold optic?

I achieved to implement Getter, Setter, Lens, LensAt (aka At in monocle) and LensOpt (aka Optional in monocle), However, I failed to generalize from Lens to Traversal and from Getter to Fold by adding an Applicative constraint: const union = type…
user5536315
0
votes
0 answers

How can I write Applicative instances for a datatype with three arguments?

I tried to write some instances for two datatypes. (Three and Three') But I'm having a hard time implementing an Applicative (and Monad) instance. With a little help, I think I can solve the problem. Datatypes are as follows. data Three a b c = T a…
Larynx
  • 398
  • 1
  • 12
0
votes
1 answer

When do Applicatives run their effects?

Under "Why do Static Arrows generalise Arrows?" post, we often spoke of Applicative running side-effects before the application of the function-in-the-functor to the value-in-the-functor. Also, danidiaz used this fact to show that Arrow is not…
Zhiltsoff Igor
  • 1,812
  • 8
  • 24
0
votes
0 answers

Does this contrieved example reflect the purpose of the Reader monad?

The Reader monad covers computations that share a common environment. The only meaningful way to do this with functions is to implicitly thread arguments through a composition. So I attempted to create a most minimal example that still reflects the…
user5536315
0
votes
2 answers

What does this law of applicative mean?

Applicative is declared as class Functor f => Applicative f where pure :: a -> f a (<*>) :: f (a -> b) -> f a -> f b One of applicative's laws is: x <*> y <*> z = ( pure (.) <*> x <*> y) <*> z where (.) is…
Tim
  • 1
  • 141
  • 372
  • 590
0
votes
1 answer

Cats ValidatedNec mapN with Case Class Error Types

I'd like to use the Cats ValidatedNec data type similarly to the example in the Cats documentation for Validated in the section Meeting applicative-- in my case, I'm parsing Strings from a file, validating against an appropriate regex for the field,…
Timothy Perrigo
  • 723
  • 4
  • 18
0
votes
1 answer

Why does applicative work default only for Maybe

I am trying to understand why do applicative functors work by default (no implementation needed) for some functors like Maybe but for others don't: Example: Just (+3) <*> (Just 3) works fine "out of the box"- > 6 Left (+3) <*> Left 3 does not work…
Bercovici Adrian
  • 8,794
  • 17
  • 73
  • 152
0
votes
0 answers

Is there a better way to add values of two custom data types where both are instances of MonoFunctor?

I have some data defined as {-# LANGUAGE RankNTypes #-} {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TypeFamilies #-} module Test where import Control.Lens import Data.MonoTraversable data Custom = Custom { _a ::…
atis
  • 881
  • 5
  • 22
0
votes
1 answer

How do I add an Applicative context to a type expected by Lens' MyDataType?

I have a function generalized over a function func to be applied on MyDataType1 and another MyDataType1 like this setThis :: (Functor f0, Applicative f0) => Lens' MyDataType1 (f0 Float) -> Float -> MyDataType1 -> MyDataType1 -> MyDataType1 …
atis
  • 881
  • 5
  • 22
0
votes
1 answer

Haskell: Is it possible to define a datatype that can encapsulate Maybe, String & Double with ability to apply value applicatively?

I'm a Haskell beginner and while building a simple project I came across this odd problem. I have a list of functions e.g. [f1, f2, f3 ... fn] Which doesn't work because they don't have same type. Some have Ct -> String, Ct -> Double and Ct ->…
atis
  • 881
  • 5
  • 22
0
votes
1 answer

Haskell: Parsing an object that could be multiple types into one single type

I'm a haskell beginner going through aeson, learning more about both by parsing some data files. Usually when there's a data file, may it be .json, a lua table, .csv format or others, and you want to parse them, there's always a chance of error. For…
atis
  • 881
  • 5
  • 22
0
votes
1 answer

Applicative functor in F*: Type checking error

As an experiment while trying to get familiar with F*, I tried implementing an applicative functor. I'm stuck at a weird-looking type checking error. I'm not sure yet if this is due to some feature / logic in the type checking that I'm not aware of,…
madidier
  • 196
  • 9