
Hi, I am learning about monads, and this question came to mind: is there a way to have a sequence of functions left to right like so: g = return 2 >>= \n -> return (n + 1) >>= \n -> return (n + 3) Either: some kind of generic monad that makes this legal, or some way to do this without monad (i.e., without the "returns"). -- http://justonemoremathproblem.com To protect my privacy, please use PGP encryption. It's free and easy to use! My public key ID is 0x340EA95A (pgp.mit.edu).

If you're looking to do this without returns, you should take a look at the
RebindableSyntax extension.
[1]:
https://ocharles.org.uk/blog/guest-posts/2014-12-06-rebindable-syntax.html
On Sun 12 Jun, 2016, 11:17 AM Christopher Howard,
Hi, I am learning about monads, and this question came to mind: is there a way to have a sequence of functions left to right like so:
g = return 2 >>= \n -> return (n + 1) >>= \n -> return (n + 3)
Either: some kind of generic monad that makes this legal, or some way to do this without monad (i.e., without the "returns").
-- http://justonemoremathproblem.com To protect my privacy, please use PGP encryption. It's free and easy to use! My public key ID is 0x340EA95A (pgp.mit.edu).
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
-- Regards, Sumit

Hi, (>>=) :: Monad m => m a -> (a -> m b) -> m b, and if I understand correctly, what you need is a ‘generic’ version, that works without the returns? 'return :: a -> m a' for some monad m. This m comes from the monad instance you use, but if we want a generic one, it’s only possible by fixing a specific m. The obvious choice is to use the Identity monad and have a specialised (>>=‘) :: Identity a -> (a -> Identity b) -> Identity b. For all `a`, `Identity a` is isomorphic to `a`, so with a bit of cheating, you could rewrite the above to (>>=‘’) :: a -> (a -> b) -> b, which starts to look really familiar, in fact, it’s just the function composition (.) with its arguments flipped, and is actually defined as (&) in Data.Function. g' = 2 & \n -> (n + 1) & \n -> (n + 3) This was obviously a really roundabout way of arriving at the result, but I think the analogy with function composition is really nice, as we can think of (a -> b) as a special case of (a -> m b), the so-called Kleisli arrow. What monads do is they compose these Kleisli arrows. -- Csongor On 12 June 2016 at 06:47:13, Christopher Howard (ch.howard@zoho.com) wrote: Hi, I am learning about monads, and this question came to mind: is there a way to have a sequence of functions left to right like so: g = return 2 >>= \n -> return (n + 1) >>= \n -> return (n + 3) Either: some kind of generic monad that makes this legal, or some way to do this without monad (i.e., without the "returns"). -- http://justonemoremathproblem.com To protect my privacy, please use PGP encryption. It's free and easy to use! My public key ID is 0x340EA95A (pgp.mit.edu). _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

Nitpick: (&) is flipped function application ($), not composition (.) --
though the signature you gave was correct.
On Jun 11, 2016 11:07 PM, "Csongor Kiss"
Hi,
(>>=) :: Monad m => m a -> (a -> m b) -> m b, and if I understand correctly, what you need is a ‘generic’ version, that works without the returns?
'return :: a -> m a' for some monad m. This m comes from the monad instance you use, but if we want a generic one, it’s only possible by fixing a specific m. The obvious choice is to use the Identity monad and have a specialised (>>=‘) :: Identity a -> (a -> Identity b) -> Identity b.
For all `a`, `Identity a` is isomorphic to `a`, so with a bit of cheating, you could rewrite the above to (>>=‘’) :: a -> (a -> b) -> b, which starts to look really familiar, in fact, it’s just the function composition (.) with its arguments flipped, and is actually defined as (&) in Data.Function.
g' = 2 & \n -> (n + 1) & \n -> (n + 3)
This was obviously a really roundabout way of arriving at the result, but I think the analogy with function composition is really nice, as we can think of (a -> b) as a special case of (a -> m b), the so-called Kleisli arrow. What monads do is they compose these Kleisli arrows.
-- Csongor
On 12 June 2016 at 06:47:13, Christopher Howard (ch.howard@zoho.com) wrote:
Hi, I am learning about monads, and this question came to mind: is there a way to have a sequence of functions left to right like so:
g = return 2 >>= \n -> return (n + 1) >>= \n -> return (n + 3)
Either: some kind of generic monad that makes this legal, or some way to do this without monad (i.e., without the "returns").
-- http://justonemoremathproblem.com To protect my privacy, please use PGP encryption. It's free and easy to use! My public key ID is 0x340EA95A (pgp.mit.edu).
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

Yes, my bad, thanks for pointing it out.
--
Csongor
On 12 June 2016 at 07:19:37, Theodore Lief Gannon (tanuki@gmail.com) wrote:
Nitpick: (&) is flipped function application ($), not composition (.) -- though the signature you gave was correct.
On Jun 11, 2016 11:07 PM, "Csongor Kiss"

I suspect you're looking for something simpler: >>> from Control.Category,
which just composes things in reverse. Another nice function is >=> from
Control.Monad.
On Jun 12, 2016 1:47 AM, "Christopher Howard"
Hi, I am learning about monads, and this question came to mind: is there a way to have a sequence of functions left to right like so:
g = return 2 >>= \n -> return (n + 1) >>= \n -> return (n + 3)
Either: some kind of generic monad that makes this legal, or some way to do this without monad (i.e., without the "returns").
-- http://justonemoremathproblem.com To protect my privacy, please use PGP encryption. It's free and easy to use! My public key ID is 0x340EA95A (pgp.mit.edu).
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
participants (5)
-
Christopher Howard
-
Csongor Kiss
-
David Feuer
-
Sumit Sahrawat
-
Theodore Lief Gannon