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

Omit defining an Applicative instance for a Monad

I'm defining a data an instance Monad just for utilizing the do notation. However, by the Applicative-Monad Proposal implemented in GHC 7.10+, I will have to also define the data as an instance of Applicative and Functor. I don't plan to define…
shouya
  • 2,863
  • 1
  • 24
  • 45
0
votes
1 answer

Haskell applicative functor - compilation failure

I'm trying to chain together functions via the applicative functor pattern, but I'm having a problem compiling my code: import Control.Applicative buildMyList :: Float -> Float -> [Float] buildMyList ul tick = [p | p <-…
Anthony W
  • 1,289
  • 2
  • 15
  • 28
0
votes
0 answers

Haskell Data.Sequence - No instance for (Applicative Seq)

Edit: Sorry, just stupidity on my part. I 'm using the 7.6.3 Platform, but reading online docs for 'latest', and I just did this: >:i Seq newtype Seq a = Data.Sequence.Seq (Data.Sequence.FingerTree (Data.Sequence.Elem a)) …
0
votes
2 answers

Haskell Applicative and ErrorT?

Why is it that I can do the following: import Data.Word import Data.Binary.Get import Control.Applicative import Control.Monad.Error getW1 :: ErrorT String Get Word8 getW1 = lift getWord8 f1 = (+1) <$> getW1 but I cannot do: f2 = (+) <$> getW1…
me2
  • 3,069
  • 2
  • 26
  • 33
0
votes
1 answer

scalaz using reduceLeft for applicative builder

I am little confused about |@| magic in scalaz. Here is my code: def isThree(x: Int): Validation[NonEmptyList[String], Int] = if (x!= 3){("failed: %d" format x).wrapNel.failure} else {x.success} println((isThree(6) |@| isThree(7) |@| isThree(13) )…
Xiaohe Dong
  • 4,953
  • 6
  • 24
  • 53
0
votes
2 answers

Folding values list with binary operators list

I'm trying to write a reduce function of type [a -> a -> a] -> [a] -> a which would fold a list of n values with a list of n - 1 binary operators, as in the following example: reduce [(+), (*), (-)] [2, 3, 5, 7] Running this example should return…
killy971
  • 650
  • 6
  • 16
0
votes
1 answer

What does "the composition of UNIX byte streams" mean?

In the opening page of the book of "Lisp In Small Pieces", there is a paragraph goes like this: Based on the idea of "function", an idea that has matured over several centuries of mathematical research, applicative languages are omnipresent in…
bruin
  • 979
  • 1
  • 10
  • 30
0
votes
2 answers

uu-parsinglib parsing with conditional fail

EDITED for more complete problem: I'd like to create a parser (I'm using uu-parsinglib) that takes the result of a previous parser, and conditionally fails if the result contains a certain constructor: I now realise this must be a monadic parser. I…
OllieB
  • 1,431
  • 9
  • 14
0
votes
2 answers

Composing Haskell filters

I am converting the zxcvbn password strength algorithm to Haskell. I have two functions that check for all characters being ASCII and that a brute force attack is possible: filterAscii :: [String] -- ^terms to filter -> [String] --…
Ralph
  • 31,584
  • 38
  • 145
  • 282
-1
votes
1 answer

Haskell: How can I implement an applicative functor on own map data type?

I am very new to Haskell and I wrote a Data Type in Haskell for representing an interval map. What does that mean? Briefly: A map data type that gives you a value back for every possible key (put simply in my case [0..]). Then you insert "sequences"…
jayb
  • 1
  • 2
-1
votes
2 answers

ignore :: Applicative f => f a -> f ()

I need any function with this signature : ignore :: Applicative f => f a -> f () Can someone point me to the right direction ? Thanks!
Tomer
  • 1,159
  • 7
  • 15
-1
votes
1 answer

Why managing data integrity at the applicative level? Since we can do it at the database level? Why avoid constraints at the database level?

One of my professor said: "If we consider that, if constraints are sometimes completely avoided and therefore not defined in some database of some project, it is only at the application level that we can manage data integrity." In which condition…
Miguel
  • 11
  • 1
-1
votes
1 answer

What is the relation and difference between `seqn` and `sequenceA`?

sequenceA :: Applicative f => [f a] -> f [a] sequenceA [] = pure [] sequenceA (x:xs) = pure (:) <*> x <*> sequenceA xs From Programming in Haskell by Hutton seqn :: Monad m => [m a] -> m [a] and is its implementation seqn [] = return [] seqn…
Tim
  • 1
  • 141
  • 372
  • 590
-2
votes
1 answer

Is the implementation of `<*>` based on `fmap` special to Maybe applicative or can it be generalized to other applicatives?

In Maybe applicative, <*> can be implemented based on fmap. Is it incidental, or can it be generalized to other applicative(s)? (<*>) :: Maybe (a -> b) -> Maybe a -> Maybe b Nothing <*> _ = Nothing (Just g) <*> mx = fmap …
Tim
  • 1
  • 141
  • 372
  • 590
-3
votes
2 answers

What does `sequenceA (replicate n getChar)` do?

sequenceA :: Applicative f => [f a] -> f [a] sequenceA [] = pure [] sequenceA (x:xs) = pure (:) <*> x <*> sequenceA xs which can be used for example: getChars :: Int -> IO String getChars n = sequenceA (replicate n getChar) replicate n getChar…
Tim
  • 1
  • 141
  • 372
  • 590
1 2 3
38
39