This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Example of a Monad

11 replies
Andrew Milkowski
Joined: 2010-07-26,
User offline. Last seen 42 years 45 weeks ago.

Upon arriving on the concept of a Monad (Programming in Huskell by G. Hutton) and the definition:

class Monad m where
return :: a -> m a
(>>=) :: m a -> (a -> m b) -> m b

Where (>>=) is a sequencing operator

Any instance of a class defining these two functions are known to be of "monadic type"

And text goes into nicely showing 2 examples one in Parsers and another in Input / Output in an abstract sense they both "relate" and find common denomination via Monad (I sort of see this as a template albeit of much higher order)

Monad makes a "touch down" with category theory, and is really a nice thing to see it "materializing" in programming construct.

I would love to see if scala has implementation of a Monadic class
And examples of usage of such.

If you have examples in and around this concept (in scala) I really would appreciate it (this includes equivalent scala type: (>>=) None I could find in introductory texts.

Just "philosophically" Monad (in my view) looks like an interface except it is an interface for functions!

Thanks much!
Sent from my Verizon Wireless BlackBerry

David Flemström
Joined: 2009-08-10,
User offline. Last seen 42 years 45 weeks ago.
Re: Example of a Monad
Pretty much every collection type in Scala is a monadic type. The monadic functions are just named differently in Scala:
return → apply
>>=    → flatMap
e.g. the Scala code:
Option.apply(5).flatMap(x => Option(x + 1))
corresponds to the Haskell code:
Just 5 >>= \x -> Just (x + 1)
So some monadic types in Scala include, but are not limited to, Traversable, Iterable, Seq, IndexedSeq, Set, Map, Option, List, Vector, etc, etc.
On Sat, Aug 14, 2010 at 1:19 AM, Andrew Milkowski <andrewmilkowski [at] gmail [dot] com> wrote:
Upon arriving on the concept of a Monad (Programming in Huskell by G. Hutton) and the definition:

class Monad m where
return :: a -> m a
(>>=) :: m a -> (a -> m b) -> m b

Where (>>=) is a sequencing operator

Any instance of a class defining these two functions are known to be of "monadic type"

And text goes into nicely showing 2 examples one in Parsers  and another in Input / Output in an abstract sense they both "relate" and find common denomination via Monad (I sort of see this as a template albeit of much higher order)

Monad makes a "touch down" with category theory, and is really a nice thing to see it "materializing" in programming construct.

I would love to see if scala has implementation of a Monadic class
And examples of usage of such.

If you have examples in and around this concept (in scala) I really would appreciate it (this includes equivalent scala type: (>>=) None I could find in introductory texts.

Just "philosophically" Monad (in my view) looks like an interface except it is an interface for functions!

Thanks much!
Sent from my Verizon Wireless BlackBerry

Kevin Wright 2
Joined: 2010-05-30,
User offline. Last seen 26 weeks 4 days ago.
Re: Example of a Monad
Start  here:http://james-iry.blogspot.com/2007/09/monads-are-elephants-part-1.htmlhttp://james-iry.blogspot.com/2007/10/monads-are-elephants-part-2.html http://james-iry.blogspot.com/2007/10/monads-are-elephants-part-3.html http://james-iry.blogspot.com/2007/11/monads-are-elephants-part-4.html

Then take another look at Option and the entire collections library
Then move onto here:http://code.google.com/p/scalaz/
If you know Haskell and your category theory is reasonably strong then you should be able to jump into Scalaz quite quickly.Hey, we even have type-classes in there, and much more besides, definitely not just Monads :)


On 14 August 2010 00:19, Andrew Milkowski <andrewmilkowski [at] gmail [dot] com> wrote:
Upon arriving on the concept of a Monad (Programming in Huskell by G. Hutton) and the definition:

class Monad m where
return :: a -> m a
(>>=) :: m a -> (a -> m b) -> m b

Where (>>=) is a sequencing operator

Any instance of a class defining these two functions are known to be of "monadic type"

And text goes into nicely showing 2 examples one in Parsers  and another in Input / Output in an abstract sense they both "relate" and find common denomination via Monad (I sort of see this as a template albeit of much higher order)

Monad makes a "touch down" with category theory, and is really a nice thing to see it "materializing" in programming construct.

I would love to see if scala has implementation of a Monadic class
And examples of usage of such.

If you have examples in and around this concept (in scala) I really would appreciate it (this includes equivalent scala type: (>>=) None I could find in introductory texts.

Just "philosophically" Monad (in my view) looks like an interface except it is an interface for functions!

Thanks much!
Sent from my Verizon Wireless BlackBerry



--
Kevin Wright

mail/google talk: kev [dot] lee [dot] wright [at] gmail [dot] com
wave: kev [dot] lee [dot] wright [at] googlewave [dot] com
skype: kev.lee.wright
twitter: @thecoda

Kevin Wright 2
Joined: 2010-05-30,
User offline. Last seen 26 weeks 4 days ago.
Re: Example of a Monad
Though we don't typically use the Option constructor, or explicitly specify applyUse this translation table:
Option → Maybe Some → Just
We can drop the dots too, and use operator notation...
You'd then want to write:
Some(5) flatMap {x => Some(x + 1)}
or even:
Some(5) flatMap {Some(_ + 1)}
or better still:
Some(5) map {_ + 1}

On 14 August 2010 00:32, David Flemström <david [dot] flemstrom [at] gmail [dot] com> wrote:
Pretty much every collection type in Scala is a monadic type. The monadic functions are just named differently in Scala:
return → apply
>>=    → flatMap
e.g. the Scala code:
Option.apply(5).flatMap(x => Option(x + 1))
corresponds to the Haskell code:
Just 5 >>= \x -> Just (x + 1)
So some monadic types in Scala include, but are not limited to, Traversable, Iterable, Seq, IndexedSeq, Set, Map, Option, List, Vector, etc, etc.
On Sat, Aug 14, 2010 at 1:19 AM, Andrew Milkowski <andrewmilkowski [at] gmail [dot] com> wrote:
Upon arriving on the concept of a Monad (Programming in Huskell by G. Hutton) and the definition:

class Monad m where
return :: a -> m a
(>>=) :: m a -> (a -> m b) -> m b

Where (>>=) is a sequencing operator

Any instance of a class defining these two functions are known to be of "monadic type"

And text goes into nicely showing 2 examples one in Parsers  and another in Input / Output in an abstract sense they both "relate" and find common denomination via Monad (I sort of see this as a template albeit of much higher order)

Monad makes a "touch down" with category theory, and is really a nice thing to see it "materializing" in programming construct.

I would love to see if scala has implementation of a Monadic class
And examples of usage of such.

If you have examples in and around this concept (in scala) I really would appreciate it (this includes equivalent scala type: (>>=) None I could find in introductory texts.

Just "philosophically" Monad (in my view) looks like an interface except it is an interface for functions!

Thanks much!
Sent from my Verizon Wireless BlackBerry




--
Kevin Wright

mail/google talk: kev [dot] lee [dot] wright [at] gmail [dot] com
wave: kev [dot] lee [dot] wright [at] googlewave [dot] com
skype: kev.lee.wright
twitter: @thecoda

Andrew Milkowski
Joined: 2010-07-26,
User offline. Last seen 42 years 45 weeks ago.
Re: Example of a Monad
Thanks David, intro books to scala should really start with a definition of a Monad first:)

Sent from my Verizon Wireless BlackBerry

From: David Flemström <david [dot] flemstrom [at] gmail [dot] com> Date: Sat, 14 Aug 2010 01:32:16 +0200To: <andrewmilkowski [at] gmail [dot] com>Cc: scala-user<scala-user [at] listes [dot] epfl [dot] ch>Subject: Re: [scala-user] Example of a Monad
Pretty much every collection type in Scala is a monadic type. The monadic functions are just named differently in Scala:
return → apply
>>=    → flatMap
e.g. the Scala code:
Option.apply(5).flatMap(x => Option(x + 1))
corresponds to the Haskell code:
Just 5 >>= \x -> Just (x + 1)
So some monadic types in Scala include, but are not limited to, Traversable, Iterable, Seq, IndexedSeq, Set, Map, Option, List, Vector, etc, etc.
On Sat, Aug 14, 2010 at 1:19 AM, Andrew Milkowski <andrewmilkowski [at] gmail [dot] com> wrote:
Upon arriving on the concept of a Monad (Programming in Huskell by G. Hutton) and the definition:

class Monad m where
return :: a -> m a
(>>=) :: m a -> (a -> m b) -> m b

Where (>>=) is a sequencing operator

Any instance of a class defining these two functions are known to be of "monadic type"

And text goes into nicely showing 2 examples one in Parsers  and another in Input / Output in an abstract sense they both "relate" and find common denomination via Monad (I sort of see this as a template albeit of much higher order)

Monad makes a "touch down" with category theory, and is really a nice thing to see it "materializing" in programming construct.

I would love to see if scala has implementation of a Monadic class
And examples of usage of such.

If you have examples in and around this concept (in scala) I really would appreciate it (this includes equivalent scala type: (>>=) None I could find in introductory texts.

Just "philosophically" Monad (in my view) looks like an interface except it is an interface for functions!

Thanks much!
Sent from my Verizon Wireless BlackBerry

Andrew Milkowski
Joined: 2010-07-26,
User offline. Last seen 42 years 45 weeks ago.
Re: Example of a Monad
Thanks Kevin! Trying to delay entry to scalaz but it is inevitable:) my analogy between Monad and an Interface is not correct I think -Is a Monad one special case in much larger set of Class types?

Sent from my Verizon Wireless BlackBerry

From: Kevin Wright <kev [dot] lee [dot] wright [at] gmail [dot] com> Date: Sat, 14 Aug 2010 00:32:18 +0100To: <andrewmilkowski [at] gmail [dot] com>Cc: scala-user<scala-user [at] listes [dot] epfl [dot] ch>Subject: Re: [scala-user] Example of a Monad
Start  here:http://james-iry.blogspot.com/2007/09/monads-are-elephants-part-1.htmlhttp://james-iry.blogspot.com/2007/10/monads-are-elephants-part-2.html http://james-iry.blogspot.com/2007/10/monads-are-elephants-part-3.html http://james-iry.blogspot.com/2007/11/monads-are-elephants-part-4.html

Then take another look at Option and the entire collections library
Then move onto here:http://code.google.com/p/scalaz/
If you know Haskell and your category theory is reasonably strong then you should be able to jump into Scalaz quite quickly.Hey, we even have type-classes in there, and much more besides, definitely not just Monads :)


On 14 August 2010 00:19, Andrew Milkowski <andrewmilkowski [at] gmail [dot] com> wrote:
Upon arriving on the concept of a Monad (Programming in Huskell by G. Hutton) and the definition:

class Monad m where
return :: a -> m a
(>>=) :: m a -> (a -> m b) -> m b

Where (>>=) is a sequencing operator

Any instance of a class defining these two functions are known to be of "monadic type"

And text goes into nicely showing 2 examples one in Parsers  and another in Input / Output in an abstract sense they both "relate" and find common denomination via Monad (I sort of see this as a template albeit of much higher order)

Monad makes a "touch down" with category theory, and is really a nice thing to see it "materializing" in programming construct.

I would love to see if scala has implementation of a Monadic class
And examples of usage of such.

If you have examples in and around this concept (in scala) I really would appreciate it (this includes equivalent scala type: (>>=) None I could find in introductory texts.

Just "philosophically" Monad (in my view) looks like an interface except it is an interface for functions!

Thanks much!
Sent from my Verizon Wireless BlackBerry



--
Kevin Wright

mail/google talk: kev [dot] lee [dot] wright [at] gmail [dot] com
wave: kev [dot] lee [dot] wright [at] googlewave [dot] com
skype: kev.lee.wright
twitter: @thecoda

David Flemström
Joined: 2009-08-10,
User offline. Last seen 42 years 45 weeks ago.
Re: Example of a Monad
(Andrew, see below for a response to you)
On Sat, Aug 14, 2010 at 1:46 AM, Kevin Wright <kev [dot] lee [dot] wright [at] gmail [dot] com> wrote:
or better still:
Some(5) map {_ + 1}
Or, even better:
Some(6)
or better still (in some circumstances):
6
Interestingly enough, this kind of simplification pulls us further and further away from the point I was trying to make: the similarities between Haskell monads and Scala monads. Thanks for taking the time to demonstrate the brevity of Scala, however, for those who were unaware :-)
On Sat, Aug 14, 2010 at 1:47 AM, Andrew Milkowski <andrewmilkowski [at] gmail [dot] com> wrote:
Thanks Kevin! Trying to delay entry to scalaz but it is inevitable:) my analogy between Monad and an Interface is not correct I think -Is a Monad one special case in much larger set of Class types?
  I tend to think of a Monad as "a container that can contain anything". You can perform operations on the contents of the monad, without knowing what is in it, or "how much" is in it. See the response by Kevin for a more formal definition.
Kevin Wright 2
Joined: 2010-05-30,
User offline. Last seen 26 weeks 4 days ago.
Re: Example of a Monad
Wikipedia has it down pretty well, if you want to get into theory :)http://en.wikipedia.org/wiki/Monad_(category_theory)http://en.wikipedia.org/wiki/Monad_(functional_programming)
Even if that second article has an especially strong bias towards Haskell...

On 14 August 2010 00:47, Andrew Milkowski <andrewmilkowski [at] gmail [dot] com> wrote:
Thanks Kevin! Trying to delay entry to scalaz but it is inevitable:) my analogy between Monad and an Interface is not correct I think -Is a Monad one special case in much larger set of Class types?

Sent from my Verizon Wireless BlackBerry

From: Kevin Wright <kev [dot] lee [dot] wright [at] gmail [dot] com> Date: Sat, 14 Aug 2010 00:32:18 +0100To: <andrewmilkowski [at] gmail [dot] com>Cc: scala-user<scala-user [at] listes [dot] epfl [dot] ch> Subject: Re: [scala-user] Example of a Monad
Start  here:http://james-iry.blogspot.com/2007/09/monads-are-elephants-part-1.html http://james-iry.blogspot.com/2007/10/monads-are-elephants-part-2.html http://james-iry.blogspot.com/2007/10/monads-are-elephants-part-3.html http://james-iry.blogspot.com/2007/11/monads-are-elephants-part-4.html

Then take another look at Option and the entire collections library
Then move onto here:http://code.google.com/p/scalaz/
If you know Haskell and your category theory is reasonably strong then you should be able to jump into Scalaz quite quickly.Hey, we even have type-classes in there, and much more besides, definitely not just Monads :)


On 14 August 2010 00:19, Andrew Milkowski <andrewmilkowski [at] gmail [dot] com> wrote:
Upon arriving on the concept of a Monad (Programming in Huskell by G. Hutton) and the definition:

class Monad m where
return :: a -> m a
(>>=) :: m a -> (a -> m b) -> m b

Where (>>=) is a sequencing operator

Any instance of a class defining these two functions are known to be of "monadic type"

And text goes into nicely showing 2 examples one in Parsers  and another in Input / Output in an abstract sense they both "relate" and find common denomination via Monad (I sort of see this as a template albeit of much higher order)

Monad makes a "touch down" with category theory, and is really a nice thing to see it "materializing" in programming construct.

I would love to see if scala has implementation of a Monadic class
And examples of usage of such.

If you have examples in and around this concept (in scala) I really would appreciate it (this includes equivalent scala type: (>>=) None I could find in introductory texts.

Just "philosophically" Monad (in my view) looks like an interface except it is an interface for functions!

Thanks much!
Sent from my Verizon Wireless BlackBerry



--
Kevin Wright

mail/google talk: kev [dot] lee [dot] wright [at] gmail [dot] com
wave: kev [dot] lee [dot] wright [at] googlewave [dot] com
skype: kev.lee.wright
twitter: @thecoda




--
Kevin Wright

mail/google talk: kev [dot] lee [dot] wright [at] gmail [dot] com
wave: kev [dot] lee [dot] wright [at] googlewave [dot] com
skype: kev.lee.wright
twitter: @thecoda

Andrew Milkowski
Joined: 2010-07-26,
User offline. Last seen 42 years 45 weeks ago.
Re: Example of a Monad
Thanks, also trying to avoid fate of "curiosity ate the cat" (mouse ?:) by getting to "abstract nonsense" but that also is inevitable :)

Sent from my Verizon Wireless BlackBerry

From: Kevin Wright <kev [dot] lee [dot] wright [at] gmail [dot] com> Date: Sat, 14 Aug 2010 00:53:23 +0100To: <andrewmilkowski [at] gmail [dot] com>Cc: scala-user<scala-user [at] listes [dot] epfl [dot] ch>Subject: Re: [scala-user] Example of a Monad
Wikipedia has it down pretty well, if you want to get into theory :)http://en.wikipedia.org/wiki/Monad_(category_theory)http://en.wikipedia.org/wiki/Monad_(functional_programming)
Even if that second article has an especially strong bias towards Haskell...

On 14 August 2010 00:47, Andrew Milkowski <andrewmilkowski [at] gmail [dot] com> wrote:
Thanks Kevin! Trying to delay entry to scalaz but it is inevitable:) my analogy between Monad and an Interface is not correct I think -Is a Monad one special case in much larger set of Class types?

Sent from my Verizon Wireless BlackBerry

From: Kevin Wright <kev [dot] lee [dot] wright [at] gmail [dot] com> Date: Sat, 14 Aug 2010 00:32:18 +0100To: <andrewmilkowski [at] gmail [dot] com>Cc: scala-user<scala-user [at] listes [dot] epfl [dot] ch> Subject: Re: [scala-user] Example of a Monad
Start  here:http://james-iry.blogspot.com/2007/09/monads-are-elephants-part-1.html http://james-iry.blogspot.com/2007/10/monads-are-elephants-part-2.html http://james-iry.blogspot.com/2007/10/monads-are-elephants-part-3.html http://james-iry.blogspot.com/2007/11/monads-are-elephants-part-4.html

Then take another look at Option and the entire collections library
Then move onto here:http://code.google.com/p/scalaz/
If you know Haskell and your category theory is reasonably strong then you should be able to jump into Scalaz quite quickly.Hey, we even have type-classes in there, and much more besides, definitely not just Monads :)


On 14 August 2010 00:19, Andrew Milkowski <andrewmilkowski [at] gmail [dot] com> wrote:
Upon arriving on the concept of a Monad (Programming in Huskell by G. Hutton) and the definition:

class Monad m where
return :: a -> m a
(>>=) :: m a -> (a -> m b) -> m b

Where (>>=) is a sequencing operator

Any instance of a class defining these two functions are known to be of "monadic type"

And text goes into nicely showing 2 examples one in Parsers  and another in Input / Output in an abstract sense they both "relate" and find common denomination via Monad (I sort of see this as a template albeit of much higher order)

Monad makes a "touch down" with category theory, and is really a nice thing to see it "materializing" in programming construct.

I would love to see if scala has implementation of a Monadic class
And examples of usage of such.

If you have examples in and around this concept (in scala) I really would appreciate it (this includes equivalent scala type: (>>=) None I could find in introductory texts.

Just "philosophically" Monad (in my view) looks like an interface except it is an interface for functions!

Thanks much!
Sent from my Verizon Wireless BlackBerry



--
Kevin Wright

mail/google talk: kev [dot] lee [dot] wright [at] gmail [dot] com
wave: kev [dot] lee [dot] wright [at] googlewave [dot] com
skype: kev.lee.wright
twitter: @thecoda




--
Kevin Wright

mail/google talk: kev [dot] lee [dot] wright [at] gmail [dot] com
wave: kev [dot] lee [dot] wright [at] googlewave [dot] com
skype: kev.lee.wright
twitter: @thecoda

Andrew Milkowski
Joined: 2010-07-26,
User offline. Last seen 42 years 45 weeks ago.
Re: Example of a Monad
I particularly like "6" from which abs 0 (in a non-context) information can be derived:) I am sure in the "context" it is different, but thanks for exposing these "reductions" to me

As per suggestion I really need to "deconstruct" scala Predef even if for one Monad like the Option

Somewhat unrelated. I am still mighty confused as to a difference between "apply" and "unapply" for some reason my mind is making me think of "unapply" as "extracting" or "getting result from" from the function, and it is apply function and return from the function (result) this is all semantics... was searching for unapply in Huskell but with no avail...

Sent from my Verizon Wireless BlackBerry

From: David Flemström <david [dot] flemstrom [at] gmail [dot] com> Date: Sat, 14 Aug 2010 01:57:55 +0200To: Kevin Wright<kev [dot] lee [dot] wright [at] gmail [dot] com>Cc: <andrewmilkowski [at] gmail [dot] com>; scala-user<scala-user [at] listes [dot] epfl [dot] ch>Subject: Re: [scala-user] Example of a Monad
(Andrew, see below for a response to you)
On Sat, Aug 14, 2010 at 1:46 AM, Kevin Wright <kev [dot] lee [dot] wright [at] gmail [dot] com> wrote:
or better still:
Some(5) map {_ + 1}
Or, even better:
Some(6)
or better still (in some circumstances):
6
Interestingly enough, this kind of simplification pulls us further and further away from the point I was trying to make: the similarities between Haskell monads and Scala monads. Thanks for taking the time to demonstrate the brevity of Scala, however, for those who were unaware :-)
On Sat, Aug 14, 2010 at 1:47 AM, Andrew Milkowski <andrewmilkowski [at] gmail [dot] com> wrote:
Thanks Kevin! Trying to delay entry to scalaz but it is inevitable:) my analogy between Monad and an Interface is not correct I think -Is a Monad one special case in much larger set of Class types?
  I tend to think of a Monad as "a container that can contain anything". You can perform operations on the contents of the monad, without knowing what is in it, or "how much" is in it. See the response by Kevin for a more formal definition.
Tony Morris 2
Joined: 2009-03-20,
User offline. Last seen 42 years 45 weeks ago.
Re: Example of a Monad

Andrew Milkowski wrote:
> I would love to see if scala has implementation of a Monadic class
>
http://github.com/scalaz/scalaz/blob/master/core/src/main/scala/scalaz/M...
> And examples of usage of such.
>
http://github.com/scalaz/scalaz/blob/master/example/src/main/scala/scala...
> If you have examples in and around this concept (in scala) I really would appreciate it (this includes equivalent scala type: (>>=) None I could find in introductory texts.
>
> Just "philosophically" Monad (in my view) looks like an interface except it is an interface for functions!
>
http://projects.tmorris.net/public/what-does-monad-mean/artifacts/1.1/ch...
"A monad is any instance of that interface. That is all it is."

> Thanks much!
> Sent from my Verizon Wireless BlackBerry

Florian Hars 2
Joined: 2009-11-01,
User offline. Last seen 42 years 45 weeks ago.
Re: Example of a Monad

Andrew Milkowski schrieb:
> Is a Monad one special case in much larger set of Class types?
A monad is one special case in a much larger set of type classes.
See the Typclassopedia in
http://www.haskell.org/sitewiki/images/8/85/TMR-Issue13.pdf

And for the connection to scala
http://dcsobral.blogspot.com/2010/06/implicit-tricks-type-class-pattern....
http://lambda-the-ultimate.org/node/4039

- Florian.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland