Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell operator or function for applying function from monad to a value?

Tags:

haskell

monads

I'm looking for a function or operator in Haskell that is effectively a Monad equivalent of the Applicative operator <*> that applies a monadic action rather than a bare function, that is instead of this:

(<*>) :: Applicative f => f (a -> b) -> f a -> f b

I'm looking for something that looks like this:

... :: Monad m => m (a -> m b) -> m a -> m b

I can't believe that this isn't a standard function, but I can't see anything that matches. Am I missing something?

like image 718
Jules Avatar asked Jun 18 '16 23:06

Jules


People also ask

What does the bind operator do Haskell?

The bind operations, >> and >>=, combine two monadic values while the return operation injects a value into the monad (container).

What is Haskell monad function?

What is a Monad? A monad is an algebraic structure in category theory, and in Haskell it is used to describe computations as sequences of steps, and to handle side effects such as state and IO. Monads are abstract, and they have many useful concrete instances. Monads provide a way to structure a program.

What is a monadic function?

A monadic function is a function with a single argument, written to its right. It is one of three possible function valences; the other two are dyadic and niladic. The term prefix function is used outside of APL to describe APL's monadic function syntax.

Is maybe a monad in Haskell?

The Maybe type is also a monad. It is a simple kind of error monad, where all errors are represented by Nothing . A richer error monad can be built using the Either type.


1 Answers

This will be a simple composition of two other basic monad functions, namely join and ap; since:

ap :: Monad m => m (a -> b) -> m a -> m b
join :: Monad m => m (m a) -> m a

we get:

(join .) . ap :: Monad m => m (a -> m b) -> m a -> m b

as well as:

(join .) . (<*>) :: Monad m => m (a -> m b) -> m a -> m b

or, using only bind operator, another construct would be:

(. (>>=)) . (>>=) :: Monad m => m (a -> m b) -> m a -> m b
like image 134
behzad.nouri Avatar answered Oct 05 '22 23:10

behzad.nouri