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

How to make "liftM2 (+) (T 1) (T 2)" working to produce "T 3" for "newtype T = T Int"

I have a type T defined as below and I want to wrap an integer in it. newtype T = T Int -- liftM2 (+) (T 1) (T 2) -- error: `T' is not an instance of Monad Now if I want to wrap an operation the values in the wrappers and return a similar the…
shouya
  • 2,863
  • 1
  • 24
  • 45
1
vote
2 answers

Partially apply mplus to rewrite function in point-free style

I'm going through some Haskell tutorials and trying to get familiar with the language. I've seen this example in a Monad/MonadPlus tutorial: data Sheep = Sheep {name :: String, mother :: Maybe Sheep, father :: Maybe Sheep} parent :: Sheep -> Maybe…
BruceBerry
  • 1,166
  • 1
  • 9
  • 21
1
vote
1 answer

Parse fixed length text with attoparsec

I need to parse fixed length fields with attoparsec but im now struggling with the compiler. Im still a newbie, the code below is the closest solution I have: > {-# LANGUAGE OverloadedStrings #-} > import Control.Applicative > import Data.Text as…
georgina
  • 13
  • 2
0
votes
0 answers

I wonder if Applicative is derived naturally through MonadTransformer's Monad

For the type constraint's point of view, Monad be derived from Applicative, which is the reverse of what I wrote in the title. However, it's not a mistake. While studying Monad, I have checked all the relationships below to see if they are…
kwonryul
  • 481
  • 3
  • 10
0
votes
0 answers

Monad instance for EvalOption[A] in Scala

Imagine an EvalOption type defined as follows: type EvalOption = [A] =>> Eval[Option[A]] Is it possible to create an instance of Monad in such a way that the stack safety of Eval is maintained? I was able to create an Applicative instance for such…
0
votes
1 answer

Quickcheck Applicative homomorphism law for Binary Tree

I am aware that the following question exists: haskell - How to quickcheck applicative homomorphism property? - Stack Overflow However, the introduction of the following PRAGMA {-# LANGUAGE ScopedTypeVariables #-} didn't solve my issue. These are…
F. Zer
  • 1,081
  • 7
  • 9
0
votes
1 answer

Traverse/sequence an Array> into Either, Array> in fp-ts

I have a list of entities where even one failed validation would yield an error. However, I'd still like to iterate the whole list and collect all the errors for further logging. Traverse/sequence with a default Either's Applicative would yield…
Igor Loskutov
  • 2,157
  • 2
  • 20
  • 33
0
votes
4 answers

Why type of pure is a -> f a, not (a -> b) -> f (a -> b) on Applicative?

Pure is used to transform normal function into function in Applicative container. With this, any multi-parameter operations become can be used on Applicative. In this context, pure is not desired to be a -> f a type, it is just desired to be (a ->…
lunuy lunuy
  • 429
  • 3
  • 7
0
votes
1 answer

Why don't define all the functions over Applicative?

internalAnd :: Bool -> Bool -> Bool internalAnd True True = True internalAnd _ _ = False (&&) :: Applicative m => m Bool -> m Bool -> m Bool (&&) = liftA2 internalAnd -- Usage greaterThan x = (x <) lessThan x = (x >) validateAge = greaterThan 0…
lunuy lunuy
  • 429
  • 3
  • 7
0
votes
1 answer

Boilerplate code in the definition of a monad

Since the Functor-Applicative-Monad Proposal, Monads are a subclass of Applicative which in turn is a subclass of Functor. Mathematically speaking, this seems to be a sensible choice and I do not have any problems with that. However, what is…
exchange
  • 363
  • 4
  • 9
0
votes
2 answers

How to create the instance for my own type?

I'm new to Haskell, learning Functor, Applicative, and Monad. I checked the Functor instance for Either type from hackage is: data Either a b = Left a | Right b instance Functor (Either a) where fmap _ (Left x) = Left x fmap f (Right…
0
votes
1 answer

Haskell parsing, how '<*' '*>' works to parse something between delimiters

I start discovering parsing in Haskell without any libraries. I created my own data type to implement a couple of type classes. data Parser a = Parser { runParser :: String -> Maybe (a, String) } instance Functor Parser where fmap fct (Parser…
0
votes
1 answer

Perform checks that include multiple options

I have type Month = Int parseMonths :: OP.Parser (Month, Month) parseMonths = liftA2 (,) (OP.option (OP.eitherReader $ parseNumber "month" (\n -> 1<=n && n<=12) "month") (OP.metavar "MONTH" <> …
Lemming
  • 577
  • 6
  • 16
0
votes
1 answer

How do I prevent a recursively defined optparse-applicative Parser from hanging?

Supposing I have the following recursively defined ADT data GraphType = Character | Colour | Nested GraphType deriving (Show) I can define an Parser for this structure (using optparse-applicative, imported as OA), recursively, as…
0
votes
0 answers

Applicative asking for unexpected type

I'm having difficulty implementing Applicative for my type. Here's Functor data Connection : repr -> out -> Type where MkConnection : (repr -> ty) -> (ty -> out) -> Connection repr out Functor (Connection repr) where map f (MkConnection get g)…
joel
  • 6,359
  • 2
  • 30
  • 55