Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What good is the monad instance of Cont?

I'm playing around with CPS and Control.Monad.Cont and wonder what we gain by noticing the monadic structure. For code like this:

sumOfSquares'cps :: Cont r Int -> Cont r Int -> Cont r Int
sumOfSquares'cps x y = x >>= \x' ->
                       y >>= \y' ->
                       return (x'*x' + y'*y')

Can easily be rewritten as

type Cont' r a = (a -> r) -> r

sos'cps :: Cont' r Int -> Cont' r Int -> Cont' r Int
sos'cps x y = \k -> x $ \x' -> 
                    y $ \y' -> 
                    k (x'*x' + y'*y') 

Don't get me wrong, but I can't see the sensation here apart from being able to use do notation and a newtype. I don't think that callCC is dependent on the monad instance either.

I'm lacking imagination to come up with an example. What do we actually get for declaring Cont r a monad?

like image 529
Sebastian Graf Avatar asked Jun 10 '14 18:06

Sebastian Graf


People also ask

Why is monad useful?

monads are used to address the more general problem of computations (involving state, input/output, backtracking, ...) returning values: they do not solve any input/output-problems directly but rather provide an elegant and flexible abstraction of many solutions to related problems.

What problem do monads solve?

Monad is a simple and powerful design pattern for function composition that helps us to solve very common IT problems such as input/output, exception handling, parsing, concurrency and other. Application becomes less error prone.

What is monad explain briefly?

monad, (from Greek monas “unit”), an elementary individual substance that reflects the order of the world and from which material properties are derived. The term was first used by the Pythagoreans as the name of the beginning number of a series, from which all following numbers derived.

What is 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.


2 Answers

You could ask the same question of any Monad. Off the top of my head, I can think of three advantages:

  1. You get access to the huge collection of functions that are designed to work with Monads.
  2. You can use do-notation.
  3. You can stack up monad transformers to create something more powerful.

This also allows you to reason better about your code, since you can rely on identity and associative properties and the like.

like image 113
Michael Snoyman Avatar answered Oct 01 '22 05:10

Michael Snoyman


One obvious advantage is that you can use the combinators defined for Monads (and Functors). For example, your function could be written using liftM2:

sumOfSquares'cps :: Cont r Int -> Cont r Int -> Cont r Int
sumOfSquares'cps = liftM2 sumSquares
  where sumSquares x y = x * x + y * y

this function does not rely on the monad being Cont and could be written with a more general type e.g.

sumOfSquaresM :: Monad m => m Int -> m Int -> m Int
like image 39
Lee Avatar answered Oct 01 '22 06:10

Lee