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

What Does Monad Mean?

26 replies
Tony Morris 2
Joined: 2009-03-20,
User offline. Last seen 42 years 45 weeks ago.

Hey guys,
I gave a talk late last year titled "What Does Monad Mean?"

Here are the slides
http://projects.tmorris.net/public/what-does-monad-mean/artifacts/1.0/ch...

Here is the video
http://vimeo.com/8729673

Meredith Gregory
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: What Does Monad Mean?
Dear Tony,

As usual, nice job!

i notice that you don't mention much about monad composition. It seems to me that the 64K Qn in all this is a good notion of composition. After all, if monads are really a good way to structure programs, then we really need a good notion of composition. So, currently we have the monad transformer proposal on the table and we have the notion of distributive law (which -- btw -- has extremely nice algebraic properties and also works to allow to combine monads with comonads as well as comonads with comonads). i think it would be a good idea for someone to tackle an explication of best practices for monad composition.

Best wishes,

--greg

On Wed, Jan 13, 2010 at 7:27 PM, Tony Morris <tonymorris@gmail.com> wrote:
Hey guys,
I gave a talk late last year titled "What Does Monad Mean?"

Here are the slides
http://projects.tmorris.net/public/what-does-monad-mean/artifacts/1.0/chunk-html/index.html

Here is the video
http://vimeo.com/8729673

--
Tony Morris
http://tmorris.net/




--
L.G. Meredith
Managing Partner
Biosimilarity LLC
1219 NW 83rd St
Seattle, WA 98117

+1 206.650.3740

http://biosimilarity.blogspot.com
Tony Morris 2
Joined: 2009-03-20,
User offline. Last seen 42 years 45 weeks ago.
Re: What Does Monad Mean?

Yeah I think it would have been a bit tough on the audience to get into
that sticky issue. I'm hoping to build on this knowledge for the same
audience. Really I wanted to demystify the concept and give it a
concrete meaning.

Meredith Gregory wrote:
> Dear Tony,
>
> As usual, nice job!
>
> i notice that you don't mention much about monad composition. It seems to me
> that the 64K Qn in all this is a good notion of composition. After all, if
> monads are really a good way to structure programs, then we really need a
> good notion of composition. So, currently we have the monad transformer
> proposal on the table and we have the notion of distributive law (which --
> btw -- has extremely nice algebraic properties and also works to allow to
> combine monads with comonads as well as comonads with comonads). i think it
> would be a good idea for someone to tackle an explication of best practices
> for monad composition.
>
> Best wishes,
>
> --greg
>
> On Wed, Jan 13, 2010 at 7:27 PM, Tony Morris wrote:
>
>
>> Hey guys,
>> I gave a talk late last year titled "What Does Monad Mean?"
>>
>> Here are the slides
>>
>> http://projects.tmorris.net/public/what-does-monad-mean/artifacts/1.0/ch...
>>
>> Here is the video
>> http://vimeo.com/8729673
>>
>> --
>> Tony Morris
>> http://tmorris.net/
>>
>>
>>
>
>
>

ounos
Joined: 2008-12-29,
User offline. Last seen 3 years 44 weeks ago.
Re: What Does Monad Mean?

Sliding through the slides, I reached this one:

http://projects.tmorris.net/public/what-does-monad-mean/artifacts/1.0/ch...

This code in it seems peculiar:

public Value bind(Transformer> t, Value a) {
return new Value() { public B value() { return
t.transform(a).value(); }};
}

(First, I take it you meant t.transform(a.value()), instead of
t.transform(a)). Then, you seem to construct a new Value while
t.transform(a.value()) is already an equivalent Value - but it's
too late in the night here so I could be wrong.

Dimitris

2010/1/14 Tony Morris :
> Hey guys,
> I gave a talk late last year titled "What Does Monad Mean?"
>
> Here are the slides
> http://projects.tmorris.net/public/what-does-monad-mean/artifacts/1.0/ch...
>
> Here is the video
> http://vimeo.com/8729673
>
> --
> Tony Morris
> http://tmorris.net/
>
>

ounos
Joined: 2008-12-29,
User offline. Last seen 3 years 44 weeks ago.
Re: What Does Monad Mean?
Tony Morris 2
Joined: 2009-03-20,
User offline. Last seen 42 years 45 weeks ago.
Re: What Does Monad Mean?
bmaso
Joined: 2009-10-04,
User offline. Last seen 2 years 40 weeks ago.
Re: What Does Monad Mean?
Awesome talk. I think I might be able to fake it at a CS party now! I liked it so much I tweeted it, and I'm adding the vimeo to my blog.

I don't believe it, though. That can't be your accent. I was picturing something more astute-sounding. Less Crocodile Dundee; maybe more Felix Unger. ;)

Best regards,
Brian Maso
(949) 395-8551
brian@blumenfeld-maso.com
twitter: @bmaso
skype: brian.maso
LinkedIn: http://www.linkedin.com/in/brianmaso

On Wed, Jan 13, 2010 at 7:27 PM, Tony Morris <tonymorris@gmail.com> wrote:
Hey guys,
I gave a talk late last year titled "What Does Monad Mean?"

Here are the slides
http://projects.tmorris.net/public/what-does-monad-mean/artifacts/1.0/chunk-html/index.html

Here is the video
http://vimeo.com/8729673

--
Tony Morris
http://tmorris.net/


channingwalton
Joined: 2008-09-27,
User offline. Last seen 2 weeks 1 day ago.
Re: What Does Monad Mean?

I actually get it now having previously been confused by all the analogies
and metaphors usually used to explain monads.

Thanks Tony.

Raoul Duke
Joined: 2009-01-05,
User offline. Last seen 42 years 45 weeks ago.
Re: What Does Monad Mean?

On Wed, Jan 13, 2010 at 7:27 PM, Tony Morris wrote:
> I gave a talk late last year titled "What Does Monad Mean?"

very neat! thanks for taking the time to help us grok it.

> http://projects.tmorris.net/public/what-does-monad-mean/artifacts/1.0/ch...

> It can be said that any instance (list) must satisfy list.get(list.length() - 1) == list.reverse().get(0)

i think this fails for an empty list.

sincerely.

Tony Morris 2
Joined: 2009-03-20,
User offline. Last seen 42 years 45 weeks ago.
Re: What Does Monad Mean?

No worries mate. Hope it helped. A good exercise would be to play
around in scala since the type system allows the general expression of
the interface.

Tony Morris
http://tmorris.net/

On Jan 16, 2010, at 5:54, Channing Walton
wrote:

>
> I actually get it now having previously been confused by all the
> analogies
> and metaphors usually used to explain monads.
>
> Thanks Tony.

Sebastien Bocq
Joined: 2008-12-18,
User offline. Last seen 42 years 45 weeks ago.
Re: What Does Monad Mean?
trait Monad[M[_]] {
def pure[A](a: A): M[A]
def bind[A, B](a: M[A], f: A => M[B]): M[B]
}
What is the most direct way to declare monadic types *the Scala way* that work out of the box in for-comprehensions? I played with the approach below but I'm stuck at how to define 'sequence' in a generic manner in the companion object because I don't know how to access the 'pure' method corresponding to the monad instance. Is there any new feature in 2.8 that could?

trait ScalaMonad[+A] {
    type Instance[A] <: ScalaMonad[A]
    def map[B](f: A => B):Instance[B]
    def flatMap[B](f: A => Instance[B]):Instance[B]
}

class StateMonad[S, +A](val run:S => (S,A)) extends ScalaMonad[A] {
    type Instance[A] = StateMonad[S,A]
 
    def map[B](f: A => B):StateMonad[S, B] = flatMap(a => new StateMonad[S,B](s => (s,f(a))))
    def flatMap[B](f: A => StateMonad[S, B]):StateMonad[S, B] =
      new StateMonad[S, B](s0 => {
        val (s1, a1) = run(s0)
        f(a1).run(s1)
      })
 
  }

object StateMonad {
    implicit def apply[S, A](a:A):StateMonad[S, A] = new StateMonad[S,A](s => (s,a))
}

I'm stuck here:

  object ScalaMonad {

    def sequence[A, M[X] <: ScalaMonad[X]](ms:List[M[A]]):M[List[A]] = {
      ms.foldRight(M(List[A]())) ((m, ms) => // How to access companion object apply method here?
        for (a  <- m;
             as <- ms) yield a :: as
      )                              
    }

  }

Thanks,
Sebastien

2010/1/14 Tony Morris <tonymorris@gmail.com>
Hey guys,
I gave a talk late last year titled "What Does Monad Mean?"

Here are the slides
http://projects.tmorris.net/public/what-does-monad-mean/artifacts/1.0/chunk-html/index.html

Here is the video
http://vimeo.com/8729673

--
Tony Morris
http://tmorris.net/


Luc Duponcheel
Joined: 2008-12-19,
User offline. Last seen 34 weeks 3 days ago.
Re: What Does Monad Mean?
Sebastien,

this is how I would do it


trait Monad_ {
  type M[+A] <: Monad[A]

  def success[B]: B => M[B]

  def sequence[B]: List[M[B]] => M[List[B]] =
    _.foldRight(success(List[B]()))((m, ms) => for{b <- m; bs <- ms} yield b :: bs)

  trait Monad[+A] {
    self: M[A] =>
    def flatMap[B](f: A => M[B]): M[B]

    def map[B](f: A => B): M[B] =
      flatMap(f andThen success)
  }
}

one advantage of this approach is covariance

see my latest blog(s) for similar code

Luc

On Sat, Jan 16, 2010 at 3:21 PM, Sébastien Bocq <sebastien.bocq@gmail.com> wrote:
trait Monad[M[_]] {
def pure[A](a: A): M[A]
def bind[A, B](a: M[A], f: A => M[B]): M[B]
}
What is the most direct way to declare monadic types *the Scala way* that work out of the box in for-comprehensions? I played with the approach below but I'm stuck at how to define 'sequence' in a generic manner in the companion object because I don't know how to access the 'pure' method corresponding to the monad instance. Is there any new feature in 2.8 that could?

trait ScalaMonad[+A] {
    type Instance[A] <: ScalaMonad[A]
    def map[B](f: A => B):Instance[B]
    def flatMap[B](f: A => Instance[B]):Instance[B]
}

class StateMonad[S, +A](val run:S => (S,A)) extends ScalaMonad[A] {
    type Instance[A] = StateMonad[S,A]
 
    def map[B](f: A => B):StateMonad[S, B] = flatMap(a => new StateMonad[S,B](s => (s,f(a))))
    def flatMap[B](f: A => StateMonad[S, B]):StateMonad[S, B] =
      new StateMonad[S, B](s0 => {
        val (s1, a1) = run(s0)
        f(a1).run(s1)
      })
 
  }

object StateMonad {
    implicit def apply[S, A](a:A):StateMonad[S, A] = new StateMonad[S,A](s => (s,a))
}

I'm stuck here:

  object ScalaMonad {

    def sequence[A, M[X] <: ScalaMonad[X]](ms:List[M[A]]):M[List[A]] = {
      ms.foldRight(M(List[A]())) ((m, ms) => // How to access companion object apply method here?
        for (a  <- m;
             as <- ms) yield a :: as
      )                              
    }

  }

Thanks,
Sebastien

2010/1/14 Tony Morris <tonymorris@gmail.com>
Hey guys,
I gave a talk late last year titled "What Does Monad Mean?"

Here are the slides
http://projects.tmorris.net/public/what-does-monad-mean/artifacts/1.0/chunk-html/index.html

Here is the video
http://vimeo.com/8729673

--
Tony Morris
http://tmorris.net/





--
  __~O
 -\ <,
(*)/ (*)

reality goes far beyond imagination

Sebastien Bocq
Joined: 2008-12-18,
User offline. Last seen 42 years 45 weeks ago.
Re: What Does Monad Mean?
I changed 'success' to 'apply' because for me this is analog to the Scala factory pattern. Now the import clause required to access 'sequence' and implicits still feels a bit strange but it works!

I'm wondering if it could be simplified even more...

object Test {

  trait Monad_ {
    type M[+A] <: Monad[A]

    implicit def apply[B](b:B):M[B]

    def sequence[B](ms:List[M[B]]):M[List[B]] =
      ms.foldRight(apply(List[B]()))((m, ms) => for{b <- m; bs <- ms} yield b :: bs)

    trait Monad[+A] {
      self: M[A] =>
     
      def flatMap[B](f: A => M[B]): M[B]

      def map[B](f: A => B): M[B] =
        flatMap(a => apply(f(a)))
    }
  }

  class StateMonad_[S] extends Monad_ {
   
    type M[+A] = StateMonad[A]

    implicit def apply[B](b:B) = new StateMonad[B](s => (s,b))

    class StateMonad[+A](val run:S => (S,A)) extends Monad[A] {
 
      def flatMap[B](f: A => StateMonad[B]):StateMonad[B] =
        new StateMonad[B](s0 => {
          val (s1, a1) = run(s0)
          f(a1).run(s1)
      }) 
    }
  }


  def main(args : Array[String]) : Unit = {
   
    val sm_ = new StateMonad_[Int]
    import sm_._
   
    class Robot[A](run:Int => (Int,A)) extends StateMonad[A](run)
   
    def move = new Robot(pos => (pos + 1, ()))
    def get = new Robot(pos => (pos, pos))
   
    def move2 = sequence(List(move, move))
   
    val test = for (_ <- move2;
                    x <- get;
                    y <- x + 1
    ) yield y
   
    println(test.run(0)._2)
}


2010/1/16 Luc Duponcheel <luc.duponcheel@gmail.com>
Sebastien,

this is how I would do it


trait Monad_ {
  type M[+A] <: Monad[A]

  def success[B]: B => M[B]

  def sequence[B]: List[M[B]] => M[List[B]] =
    _.foldRight(success(List[B]()))((m, ms) => for{b <- m; bs <- ms} yield b :: bs)

  trait Monad[+A] {
    self: M[A] =>
    def flatMap[B](f: A => M[B]): M[B]

    def map[B](f: A => B): M[B] =
      flatMap(f andThen success)
  }
}

one advantage of this approach is covariance

see my latest blog(s) for similar code

Luc

On Sat, Jan 16, 2010 at 3:21 PM, Sébastien Bocq <sebastien.bocq@gmail.com> wrote:
trait Monad[M[_]] {
def pure[A](a: A): M[A]
def bind[A, B](a: M[A], f: A => M[B]): M[B]
}
What is the most direct way to declare monadic types *the Scala way* that work out of the box in for-comprehensions? I played with the approach below but I'm stuck at how to define 'sequence' in a generic manner in the companion object because I don't know how to access the 'pure' method corresponding to the monad instance. Is there any new feature in 2.8 that could?

trait ScalaMonad[+A] {
    type Instance[A] <: ScalaMonad[A]
    def map[B](f: A => B):Instance[B]
    def flatMap[B](f: A => Instance[B]):Instance[B]
}

class StateMonad[S, +A](val run:S => (S,A)) extends ScalaMonad[A] {
    type Instance[A] = StateMonad[S,A]
 
    def map[B](f: A => B):StateMonad[S, B] = flatMap(a => new StateMonad[S,B](s => (s,f(a))))
    def flatMap[B](f: A => StateMonad[S, B]):StateMonad[S, B] =
      new StateMonad[S, B](s0 => {
        val (s1, a1) = run(s0)
        f(a1).run(s1)
      })
 
  }

object StateMonad {
    implicit def apply[S, A](a:A):StateMonad[S, A] = new StateMonad[S,A](s => (s,a))
}

I'm stuck here:

  object ScalaMonad {

    def sequence[A, M[X] <: ScalaMonad[X]](ms:List[M[A]]):M[List[A]] = {
      ms.foldRight(M(List[A]())) ((m, ms) => // How to access companion object apply method here?
        for (a  <- m;
             as <- ms) yield a :: as
      )                              
    }

  }

Thanks,
Sebastien

2010/1/14 Tony Morris <tonymorris@gmail.com>
Hey guys,
I gave a talk late last year titled "What Does Monad Mean?"

Here are the slides
http://projects.tmorris.net/public/what-does-monad-mean/artifacts/1.0/chunk-html/index.html

Here is the video
http://vimeo.com/8729673

--
Tony Morris
http://tmorris.net/





--
  __~O
 -\ <,
(*)/ (*)

reality goes far beyond imagination


channingwalton
Joined: 2008-09-27,
User offline. Last seen 2 weeks 1 day ago.
Re: What Does Monad Mean?

I'm getting my head around monads now. But obviously not well enough as I'm
struggling to see their real power. Are there some (simple?) examples that
show how monads simplify the solution to problems?

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: What Does Monad Mean?
There's lot of people here to disagree with me on this one, and I'm sure they'll make themselves heard. But, imho, monads are not about simplifying problems.
Monads are about composability and reusability of code. And one just can't show "simple" examples of such things.

On Sat, Jan 16, 2010 at 8:27 PM, Channing Walton <channingwalton@mac.com> wrote:

I'm getting my head around monads now. But obviously not well enough as I'm
struggling to see their real power. Are there some (simple?) examples that
show how monads simplify the solution to problems?
--
View this message in context: http://old.nabble.com/What-Does-Monad-Mean--tp27155935p27194297.html
Sent from the Scala - User mailing list archive at Nabble.com.




--
Daniel C. Sobral

I travel to the future all the time.
channingwalton
Joined: 2008-09-27,
User offline. Last seen 2 weeks 1 day ago.
Re: What Does Monad Mean?

Fair enough. I think I'm looking for how to recognise when monads might be
applicable - hence the need for some examples. A lot of the examples I have
seen are Lists and Options where its quite clear how monads are used, it
would be nice to see other examples.

Daniel Sobral wrote:
>
> There's lot of people here to disagree with me on this one, and I'm sure
> they'll make themselves heard. But, imho, monads are not about simplifying
> problems.
>
> Monads are about composability and reusability of code. And one just can't
> show "simple" examples of such things.
>

Tony Morris 2
Joined: 2009-03-20,
User offline. Last seen 42 years 45 weeks ago.
Re: What Does Monad Mean?

Daniel is mostly correct. Consider functions written to run across
*all* monads. For example what is the type of this expression

a => b => f => for(a <- x; b <- y) yield f(a, b)

I should give a better explanation but yyping code on this mobile
device is a PITA.

Tony Morris
http://tmorris.net/

On Jan 17, 2010, at 9:22, Channing Walton
wrote:

>
> Fair enough. I think I'm looking for how to recognise when monads
> might be
> applicable - hence the need for some examples. A lot of the examples
> I have
> seen are Lists and Options where its quite clear how monads are
> used, it
> would be nice to see other examples.
>
>
> Daniel Sobral wrote:
>>
>> There's lot of people here to disagree with me on this one, and I'm
>> sure
>> they'll make themselves heard. But, imho, monads are not about
>> simplifying
>> problems.
>>
>> Monads are about composability and reusability of code. And one
>> just can't
>> show "simple" examples of such things.
>>
>

Stefan Langer
Joined: 2009-10-23,
User offline. Last seen 42 years 45 weeks ago.
Re: What Does Monad Mean?
This site gives some inside on how Monad can be very good for abstracting away code duplication
http://en.wikibooks.org/wiki/Haskell/Understanding_monads
It is for Haskell so you should know haskell syntax. If I find some time I will try to get the example running using scalaz (mostly as a learning experience for myself) but this can take some time.

Stefan

2010/1/17 Tony Morris <tonymorris@gmail.com>
Daniel is mostly correct. Consider functions written to run across *all* monads. For example what is the type of this expression

a => b => f => for(a <- x; b <- y) yield f(a, b)

I should give a better explanation but yyping code on this mobile device is a PITA.

Tony Morris
http://tmorris.net/


On Jan 17, 2010, at 9:22, Channing Walton <channingwalton@mac.com> wrote:


Fair enough. I think I'm looking for how to recognise when monads might be
applicable - hence the need for some examples. A lot of the examples I have
seen are Lists and Options where its quite clear how monads are used, it
would be nice to see other examples.


Daniel Sobral wrote:

There's lot of people here to disagree with me on this one, and I'm sure
they'll make themselves heard. But, imho, monads are not about simplifying
problems.

Monads are about composability and reusability of code. And one just can't
show "simple" examples of such things.


dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: What Does Monad Mean?
I'm starting to think that Haskell/Monads are an egg/chicken problem.

On Mon, Jan 18, 2010 at 7:13 AM, Stefan Langer <mailtolanger@googlemail.com> wrote:
This site gives some inside on how Monad can be very good for abstracting away code duplication
http://en.wikibooks.org/wiki/Haskell/Understanding_monads
It is for Haskell so you should know haskell syntax. If I find some time I will try to get the example running using scalaz (mostly as a learning experience for myself) but this can take some time.

Stefan

2010/1/17 Tony Morris <tonymorris@gmail.com>
Daniel is mostly correct. Consider functions written to run across *all* monads. For example what is the type of this expression

a => b => f => for(a <- x; b <- y) yield f(a, b)

I should give a better explanation but yyping code on this mobile device is a PITA.

Tony Morris
http://tmorris.net/


On Jan 17, 2010, at 9:22, Channing Walton <channingwalton@mac.com> wrote:


Fair enough. I think I'm looking for how to recognise when monads might be
applicable - hence the need for some examples. A lot of the examples I have
seen are Lists and Options where its quite clear how monads are used, it
would be nice to see other examples.


Daniel Sobral wrote:

There's lot of people here to disagree with me on this one, and I'm sure
they'll make themselves heard. But, imho, monads are not about simplifying
problems.

Monads are about composability and reusability of code. And one just can't
show "simple" examples of such things.


Tony Morris 2
Joined: 2009-03-20,
User offline. Last seen 42 years 45 weeks ago.
Re: What Does Monad Mean?

What a bizarre thing to say.

Daniel Sobral wrote:
> I'm starting to think that Haskell/Monads are an egg/chicken problem.
>
> On Mon, Jan 18, 2010 at 7:13 AM, Stefan Langer
> > wrote:
>
> This site gives some inside on how Monad can be very good for
> abstracting away code duplication
> http://en.wikibooks.org/wiki/Haskell/Understanding_monads
> It is for Haskell so you should know haskell syntax. If I find
> some time I will try to get the example running using scalaz
> (mostly as a learning experience for myself) but this can take
> some time.
>
> Stefan
>
> 2010/1/17 Tony Morris >
>
> Daniel is mostly correct. Consider functions written to run
> across *all* monads. For example what is the type of this
> expression
>
> a => b => f => for(a <- x; b <- y) yield f(a, b)
>
> I should give a better explanation but yyping code on this
> mobile device is a PITA.
>
>
> Tony Morris
> http://tmorris.net/
>
>
> On Jan 17, 2010, at 9:22, Channing Walton
> > wrote:
>
>
> Fair enough. I think I'm looking for how to recognise when
> monads might be
> applicable - hence the need for some examples. A lot of
> the examples I have
> seen are Lists and Options where its quite clear how
> monads are used, it
> would be nice to see other examples.
>
>
> Daniel Sobral wrote:
>
>
> There's lot of people here to disagree with me on this
> one, and I'm sure
> they'll make themselves heard. But, imho, monads are
> not about simplifying
> problems.
>
> Monads are about composability and reusability of
> code. And one just can't
> show "simple" examples of such things.
>
>
> --
> View this message in context:
> http://old.nabble.com/What-Does-Monad-Mean--tp27155935p27194734.html
> Sent from the Scala - User mailing list archive at Nabble.com.
>
>
>
>
>

Stefan Langer
Joined: 2009-10-23,
User offline. Last seen 42 years 45 weeks ago.
Re: What Does Monad Mean?
2010/1/18 Daniel Sobral <dcsobral@gmail.com>
I'm starting to think that Haskell/Monads are an egg/chicken problem.
Can you elaborate on this? What is so egg/chicken about it?
dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: What Does Monad Mean?
People don't learn Haskell because of fear of monads. People don't learn monads because most of the learning material and examples are in Haskell.

On Mon, Jan 18, 2010 at 8:56 AM, Stefan Langer <mailtolanger@googlemail.com> wrote:
2010/1/18 Daniel Sobral <dcsobral@gmail.com>
I'm starting to think that Haskell/Monads are an egg/chicken problem.
Can you elaborate on this? What is so egg/chicken about it?



--
Daniel C. Sobral

I travel to the future all the time.
Sebastien Bocq
Joined: 2008-12-18,
User offline. Last seen 42 years 45 weeks ago.
Re: What Does Monad Mean?
I agree. I'd add that people don't learn monads in Scala because they are less convenient to use and require a certain amount of plumbing that is not very didactic. Sorry to hammer on this one but the following is all you need in Haskell to get a state monad running. If you had to teach monads, which language would you use?

data SM a = SM (S -> (a,S))  -- The monadic type

instance Monad SM where
  -- defines state propagation
  SM c1 >>= fc2         =  SM (\s0 -> let (r,s1) = c1 s0 
                                          SM c2 = fc2 r in
                                         c2 s1)
  return k              =  SM (\s -> (k,s))


2010/1/18 Daniel Sobral <dcsobral@gmail.com>
People don't learn Haskell because of fear of monads. People don't learn monads because most of the learning material and examples are in Haskell.

On Mon, Jan 18, 2010 at 8:56 AM, Stefan Langer <mailtolanger@googlemail.com> wrote:
2010/1/18 Daniel Sobral <dcsobral@gmail.com>
I'm starting to think that Haskell/Monads are an egg/chicken problem.
Can you elaborate on this? What is so egg/chicken about it?



--
Daniel C. Sobral

I travel to the future all the time.

ichoran
Joined: 2009-08-14,
User offline. Last seen 2 years 3 weeks ago.
Re: What Does Monad Mean?
I'd use whichever language the audience was familiar with.

Your example is certainly very *short*, but it that does not make it any more clear to someone who does not understand the syntax.

(If the audience did not know any language, then perhaps Haskell would be a sensible choice.)

  --Rex

On Mon, Jan 18, 2010 at 10:22 AM, Sébastien Bocq <sebastien.bocq@gmail.com> wrote:
I agree. I'd add that people don't learn monads in Scala because they are less convenient to use and require a certain amount of plumbing that is not very didactic. Sorry to hammer on this one but the following is all you need in Haskell to get a state monad running. If you had to teach monads, which language would you use?

data SM a = SM (S -> (a,S))  -- The monadic type

instance Monad SM where
  -- defines state propagation
  SM c1 >>= fc2         =  SM (\s0 -> let (r,s1) = c1 s0 
                                          SM c2 = fc2 r in
                                         c2 s1)
  return k              =  SM (\s -> (k,s))


2010/1/18 Daniel Sobral <dcsobral@gmail.com>
People don't learn Haskell because of fear of monads. People don't learn monads because most of the learning material and examples are in Haskell.

On Mon, Jan 18, 2010 at 8:56 AM, Stefan Langer <mailtolanger@googlemail.com> wrote:
2010/1/18 Daniel Sobral <dcsobral@gmail.com>
I'm starting to think that Haskell/Monads are an egg/chicken problem.
Can you elaborate on this? What is so egg/chicken about it?



--
Daniel C. Sobral

I travel to the future all the time.


Burkhard Ludwig
Joined: 2009-04-18,
User offline. Last seen 42 years 45 weeks ago.
Re: What Does Monad Mean?
If I take "Programming in Scala" (1st Edition Version 5) p. 487 serious, all you have to do in Scala when you want a monad is to define map, flatMap and filter. Is more or less an everyday task. So where is the plumbing? 
Burkhard
PS. I got the impression that one of the points of T.Morris talk is that one need not wait for (need not be afraid of) a monad in language X – it is already there (you are already using it). It just needs somebody who tells you how in does look like in e.g. Scala (Odersky, Spoon and Venners untertook this task in my case). If that impression is correct, the amount of excitation on this topic is difficult to understand.
2010/1/18 Sébastien Bocq <sebastien.bocq@gmail.com>
I agree. I'd add that people don't learn monads in Scala because they are less convenient to use and require a certain amount of plumbing that is not very didactic. Sorry to hammer on this one but the following is all you need in Haskell to get a state monad running. If you had to teach monads, which language would you use?

data SM a = SM (S -> (a,S))  -- The monadic type

instance Monad SM where
  -- defines state propagation
  SM c1 >>= fc2         =  SM (\s0 -> let (r,s1) = c1 s0 
                                          SM c2 = fc2 r in
                                         c2 s1)
  return k              =  SM (\s -> (k,s))


2010/1/18 Daniel Sobral <dcsobral@gmail.com>
People don't learn Haskell because of fear of monads. People don't learn monads because most of the learning material and examples are in Haskell.

On Mon, Jan 18, 2010 at 8:56 AM, Stefan Langer <mailtolanger@googlemail.com> wrote:
2010/1/18 Daniel Sobral <dcsobral@gmail.com>
I'm starting to think that Haskell/Monads are an egg/chicken problem.
Can you elaborate on this? What is so egg/chicken about it?



--
Daniel C. Sobral

I travel to the future all the time.


dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: What Does Monad Mean?
Reading your last reply got me to think about this code. I think using inheritance is the wrong approach here. Rather, I think it would be best to define Monad like Numeric and Ordering, and use context bounds on the state monad.

On Sat, Jan 16, 2010 at 3:42 PM, Sébastien Bocq <sebastien.bocq@gmail.com> wrote:
I changed 'success' to 'apply' because for me this is analog to the Scala factory pattern. Now the import clause required to access 'sequence' and implicits still feels a bit strange but it works!

I'm wondering if it could be simplified even more...

object Test {

  trait Monad_ {
    type M[+A] <: Monad[A]

    implicit def apply[B](b:B):M[B]

    def sequence[B](ms:List[M[B]]):M[List[B]] =
      ms.foldRight(apply(List[B]()))((m, ms) => for{b <- m; bs <- ms} yield b :: bs)

    trait Monad[+A] {
      self: M[A] =>
     
      def flatMap[B](f: A => M[B]): M[B]

      def map[B](f: A => B): M[B] =
        flatMap(a => apply(f(a)))
    }
  }

  class StateMonad_[S] extends Monad_ {
   
    type M[+A] = StateMonad[A]

    implicit def apply[B](b:B) = new StateMonad[B](s => (s,b))

    class StateMonad[+A](val run:S => (S,A)) extends Monad[A] {
 
      def flatMap[B](f: A => StateMonad[B]):StateMonad[B] =
        new StateMonad[B](s0 => {
          val (s1, a1) = run(s0)
          f(a1).run(s1)
      }) 
    }
  }


  def main(args : Array[String]) : Unit = {
   
    val sm_ = new StateMonad_[Int]
    import sm_._
   
    class Robot[A](run:Int => (Int,A)) extends StateMonad[A](run)
   
    def move = new Robot(pos => (pos + 1, ()))
    def get = new Robot(pos => (pos, pos))
   
    def move2 = sequence(List(move, move))
   
    val test = for (_ <- move2;
                    x <- get;
                    y <- x + 1
    ) yield y
   
    println(test.run(0)._2)
}


2010/1/16 Luc Duponcheel <luc.duponcheel@gmail.com>
Sebastien,

this is how I would do it


trait Monad_ {
  type M[+A] <: Monad[A]

  def success[B]: B => M[B]

  def sequence[B]: List[M[B]] => M[List[B]] =
    _.foldRight(success(List[B]()))((m, ms) => for{b <- m; bs <- ms} yield b :: bs)

  trait Monad[+A] {
    self: M[A] =>
    def flatMap[B](f: A => M[B]): M[B]

    def map[B](f: A => B): M[B] =
      flatMap(f andThen success)
  }
}

one advantage of this approach is covariance

see my latest blog(s) for similar code

Luc

On Sat, Jan 16, 2010 at 3:21 PM, Sébastien Bocq <sebastien.bocq@gmail.com> wrote:
trait Monad[M[_]] {
def pure[A](a: A): M[A]
def bind[A, B](a: M[A], f: A => M[B]): M[B]
}
What is the most direct way to declare monadic types *the Scala way* that work out of the box in for-comprehensions? I played with the approach below but I'm stuck at how to define 'sequence' in a generic manner in the companion object because I don't know how to access the 'pure' method corresponding to the monad instance. Is there any new feature in 2.8 that could?

trait ScalaMonad[+A] {
    type Instance[A] <: ScalaMonad[A]
    def map[B](f: A => B):Instance[B]
    def flatMap[B](f: A => Instance[B]):Instance[B]
}

class StateMonad[S, +A](val run:S => (S,A)) extends ScalaMonad[A] {
    type Instance[A] = StateMonad[S,A]
 
    def map[B](f: A => B):StateMonad[S, B] = flatMap(a => new StateMonad[S,B](s => (s,f(a))))
    def flatMap[B](f: A => StateMonad[S, B]):StateMonad[S, B] =
      new StateMonad[S, B](s0 => {
        val (s1, a1) = run(s0)
        f(a1).run(s1)
      })
 
  }

object StateMonad {
    implicit def apply[S, A](a:A):StateMonad[S, A] = new StateMonad[S,A](s => (s,a))
}

I'm stuck here:

  object ScalaMonad {

    def sequence[A, M[X] <: ScalaMonad[X]](ms:List[M[A]]):M[List[A]] = {
      ms.foldRight(M(List[A]())) ((m, ms) => // How to access companion object apply method here?
        for (a  <- m;
             as <- ms) yield a :: as
      )                              
    }

  }

Thanks,
Sebastien

2010/1/14 Tony Morris <tonymorris@gmail.com>
Hey guys,
I gave a talk late last year titled "What Does Monad Mean?"

Here are the slides
http://projects.tmorris.net/public/what-does-monad-mean/artifacts/1.0/chunk-html/index.html

Here is the video
http://vimeo.com/8729673

--
Tony Morris
http://tmorris.net/





--
  __~O
 -\ <,
(*)/ (*)

reality goes far beyond imagination





--
Daniel C. Sobral

I travel to the future all the time.
Tony Morris 2
Joined: 2009-03-20,
User offline. Last seen 42 years 45 weeks ago.
Re: What Does Monad Mean?
And yet monads have nothing to do with Haskell and Haskell has nothing to do with monads. I think it's an education problem or perhaps a lack of sceptical inquiry.

Tony Morrishttp://tmorris.net/

On Jan 18, 2010, at 23:26, Daniel Sobral <dcsobral@gmail.com> wrote:

People don't learn Haskell because of fear of monads. People don't learn monads because most of the learning material and examples are in Haskell.

On Mon, Jan 18, 2010 at 8:56 AM, Stefan Langer < (mailtolanger [at] googlemail [dot] com> wrote:
2010/1/18 Daniel Sobral < (dcsobral [at] gmail [dot] com>
I'm starting to think that Haskell/Monads are an egg/chicken problem.
Can you elaborate on this? What is so egg/chicken about it?



--
Daniel C. Sobral

I travel to the future all the time.

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