If I want to generalize those monadic classes I have to apply an
implicit conversion which results in additional wrapper objects that
mostly have no additional use.
Any thoughts?
--
Best Regards
Ka Ter
Best Regards
Ka Ter
Let me try my hand real quick at a midnight example J. This same question haunted me for a long time: who’s the monad?
List is the data structure. It already has the flatMap but ignore that for a moment.
The monad implementation for it then is:
object ListMonad extends Monad[List[_]] {
def unit[A] (a:A) = List(a)
def flatMap[A,B] (f:A=>List[B], l:List[A]):List[B] = l.flatMap (f)
Who’s the monad? Well, it says right there, the object is…
The data structure List, is usually also called ‘a monad’ because of the agreement that there is a ‘natural’ monad for it, i.e. a monad with the natural List flattening… in scala, things are a bit muddied also because the flatMap is a method on the List itself rather than a typeclass. However, the unit and type constructor are itself… so they can’t be expressed in a separate interface…
Note that Greg used the mult() in the Monad trait – that is a better depiction of the fact that this is merely an operation – the flattening. I used flatMap directly. For List, this this natural operation is concatenation.
Also – one of the things that a monad needs is a ‘type constructor’. List is this type constructor, for instance List[Int] is a newly constructed type.
Because List itself has all three characteristics of a monad, in scala, one could argue that List itself is the monad. However, there’s not much you can do with it other than using it in a for comprehension. That’s why the typeclass stuff is also needed (and available in scalaz).
- type constructor is List[]
- unit is List(a)
- flatMap is a method
To complete the pattern, you need an implicit from List to ListMonad – this is the typeclass pattern that for instance scalaz uses.
To understand this better, I’m sorry, but a dash of Haskell is in order. Mind you, I only understand enough Haskell to have a vague idea what the following do…
There is a clear distinction between the data structure, list or [] and the monadic stuff…
In Haskell, the Monad typeclass is defined as :
class Monad m where
return :: a -> m a
(>>=) :: m a -> (a -> m b) -> m b
This is somewhat equivalent to scala’s:
trait Monad[m[_]] {
def return[a] (value:a):m[a]
def >>=[a,b] (x1 : m[a], x2 : a=>m[b]):m[b]
NOW it gets interesting. To create different implementation for this, in Haskell you do this:
instance Monad [] where
m >>= k = concat (map k m)
this is the list monad truly. This is equivalent to the object ListMonad I had above, together with an implicit – in Haskell, the implicit is not necessary, the language does the magic.
Remember that [] is the List in Haskell, so the above translates into Monad[List[_]]
It gets even screwier than that in Haskell J but in a good way…
Well, I probably managed to confuse you even more, but these are bits and pieces that it took me a long time to piece together. Figured it might help at least me to review their relationship J
Cheers,
Razie
But, is there a reason why there is no distinct interface for it, like a trait Monadic?
Best Regards Ka Ter
Good point, same with monoid, something becomes a monad only if we provide unit and multiplication (If I understood you correctly).
Thanks,
-Vlad
On Wed, Oct 5, 2011 at 8:30 PM, Razvan Cojocaru <p...@razie.com> wrote:
Let me try my hand real quick at a midnight example J. This same question haunted me for a long time: who�s the monad?
�
List is the data structure. It already has the flatMap but ignore that for a moment.
�
The monad implementation for it then is:
�
object ListMonad extends Monad[List[_]] {
� def unit[A] (a:A) = List(a)
� def flatMap[A,B] (f:A=>List[B], l:List[A]):List[B] = l.flatMap (f)
�
Who�s the monad? Well, it says right there, the object is�
�
The data structure List, is usually also called �a monad� because of the agreement that there is a �natural� monad for it, i.e. a monad with the natural List flattening� in scala, things are a bit muddied also because the flatMap is a method on the List itself rather than a typeclass. However, the unit and type constructor are itself� so they can�t be expressed in a separate interface�
�
Note that Greg used the mult() in the Monad trait � that is a better depiction of the fact that this is merely an operation � the flattening. I used flatMap directly. For List, this this natural operation is concatenation.
�
Also � one of the things that a monad needs is a �type constructor�. List is this type constructor, for instance List[Int] is a newly constructed type.
�
Because List itself has all three characteristics of a monad, in scala, one could argue that List itself is the monad. However, there�s not much you can do with it other than using it in a for comprehension. That�s why the typeclass stuff is also needed (and available in scalaz).
- type constructor is List[]
- unit is List(a)
- flatMap is a method
�
�
To complete the pattern, you need an implicit from List to ListMonad � this is the typeclass pattern that for instance scalaz uses.
�
To understand this better, I�m sorry, but a dash of Haskell is in order. Mind you, I only understand enough Haskell to have a vague idea what the following do�
�
There is a clear distinction between the data structure, list or [] and the monadic stuff�
�
In Haskell, the Monad typeclass is defined as :
�
class Monad m where�
����return :: a -> m a�
��
����(>>=) :: m a -> (a -> m b) -> m b�
��
This is somewhat equivalent to scala�s:
�
trait Monad[m[_]] {
� def return[a] (value:a):m[a]
� def >>=[a,b] (x1 : m[a], x2 : a=>m[b]):m[b]
�
NOW it gets interesting. To create different implementation for this, in Haskell you do this:
�
instance� Monad []� where
� m >>= k��������� = concat (map k m)
�
this is the list monad truly. This is equivalent to the object ListMonad I had above, together with an implicit � in Haskell, the implicit is not necessary, the language does the magic.
�
Remember that [] is the List in Haskell, so the above translates into Monad[List[_]]
�
It gets even screwier than that in Haskell J but in a good way�
�
�
Well, I probably managed to confuse you even more, but these are bits and pieces that it took me a long time to piece together. Figured it might help at least me to review their relationship J
�
Cheers,
Razie
�
From: scala...@googlegroups.com [mailto:scala...@googlegroups.com] On Behalf Of Ken McDonald
Sent: October-05-11 9:05 PM
To: scala...@googlegroups.com
Subject: Re: [scala-user] Re: Trait for monads
�
Hi L.G,
�
Very cool. Also slightly spacy given that I'm operating on 2 1/2 glasses of wine. I realize you may not have the time, but if you do, would you be able to give a concrete example of your pure monadic defs (ignoring the conversion needed and all aspects of Scala for-syntax)? I'm slowly comprehending monads, but the missing pieces for me are "this...is a monad because...." and "this...is a monad because..." etc. Real examples, basically.
�
Thanks,
Ken
ok, thanks.
Well, if Monads stay beside classes like collections or options, what is a 'monadic'? Can I say that a class that combines a data structure together with the methods of a monad methods can be called a monadic?
Best Regards Ka Ter
Am 06.10.2011 08:53, schrieb Vlad Patryshev:
Good point, same with monoid, something becomes a monad only if we provide unit and multiplication (If I understood you correctly).
Thanks,
-Vlad
On Wed, Oct 5, 2011 at 8:30 PM, Razvan Cojocaru <p...@razie.com> wrote:
Let me try my hand real quick at a midnight example J. This same question haunted me for a long time: who’s the monad?
List is the data structure. It already has the flatMap but ignore that for a moment.
The monad implementation for it then is:
object ListMonad extends Monad[List[_]] {
def unit[A] (a:A) = List(a)
def flatMap[A,B] (f:A=>List[B], l:List[A]):List[B] = l.flatMap (f)
Who’s the monad? Well, it says right there, the object is…
The data structure List, is usually also called ‘a monad’ because of the agreement that there is a ‘natural’ monad for it, i.e. a monad with the natural List flattening… in scala, things are a bit muddied also because the flatMap is a method on the List itself rather than a typeclass. However, the unit and type constructor are itself… so they can’t be expressed in a separate interface…
Note that Greg used the mult() in the Monad trait – that is a better depiction of the fact that this is merely an operation – the flattening. I used flatMap directly. For List, this this natural operation is concatenation.
Also – one of the things that a monad needs is a ‘type constructor’. List is this type constructor, for instance List[Int] is a newly constructed type.
Because List itself has all three characteristics of a monad, in scala, one could argue that List itself is the monad. However, there’s not much you can do with it other than using it in a for comprehension. That’s why the typeclass stuff is also needed (and available in scalaz).
- type constructor is List[]
- unit is List(a)
- flatMap is a method
To complete the pattern, you need an implicit from List to ListMonad – this is the typeclass pattern that for instance scalaz uses.
To understand this better, I’m sorry, but a dash of Haskell is in order. Mind you, I only understand enough Haskell to have a vague idea what the following do…
There is a clear distinction between the data structure, list or [] and the monadic stuff…
In Haskell, the Monad typeclass is defined as :
class Monad m where
return :: a -> m a
(>>=) :: m a -> (a -> m b) -> m b
This is somewhat equivalent to scala’s:
trait Monad[m[_]] {
def return[a] (value:a):m[a]
def >>=[a,b] (x1 : m[a], x2 : a=>m[b]):m[b]
NOW it gets interesting. To create different implementation for this, in Haskell you do this:
instance Monad [] where
m >>= k = concat (map k m)
this is the list monad truly. This is equivalent to the object ListMonad I had above, together with an implicit – in Haskell, the implicit is not necessary, the language does the magic.
Remember that [] is the List in Haskell, so the above translates into Monad[List[_]]
It gets even screwier than that in Haskell J but in a good way…
Well, I probably managed to confuse you even more, but these are bits and pieces that it took me a long time to piece together. Figured it might help at least me to review their relationship J
Cheers,
Razie
From: scala...@googlegroups.com [mailto:scala...@googlegroups.com] On Behalf Of Ken McDonald
Sent: October-05-11 9:05 PM
To: scala...@googlegroups.com
Subject: Re: [scala-user] Re: Trait for monads
Hi L.G,
Very cool. Also slightly spacy given that I'm operating on 2 1/2 glasses of wine. I realize you may not have the time, but if you do, would you be able to give a concrete example of your pure monadic defs (ignoring the conversion needed and all aspects of Scala for-syntax)? I'm slowly comprehending monads, but the missing pieces for me are "this...is a monad because...." and "this...is a monad because..." etc. Real examples, basically.
Thanks,
Ken
> anyway, and most importantly, please (pretty please), let's not start
> another monadic thread on scala-user (scala-debate is meant for that)
This is still my favourite explanation:
http://blog.plover.com/prog/burritos.html ;-)
--
Alan Burlison
--
Scalaz has asMA to assist implicit resolution when you don't feel like using type annotation to do same. This is unrelated to using the Monad trait.
There is no known way to represent the Monad trait in scala than how scalaz does it (since if there is, scalaz would steal it!)
I assume its absence on for-comprehensions in the language is for historical reasons, since that ability was before higher kinds.
> yes, because our collections don't adhere to the classical Monadic interface
Thanks for that Adriaan, I hadn't seen that spelled out so clearly
before, and it's interesting (and helpful) to see a real example of
'stepping outside' of a purely monadic model.
--
Alan Burlison
--
These both return List(2, 3, 4) but the first one steps outside the List
monad :)
for (x <- List(1,2,3); y <- Some(x)) yield y+1
for (x <- List(1,2,3); y <- List(x)) yield y+1
-----Original Message-----
From: scala...@googlegroups.com [mailto:scala...@googlegroups.com] On
Best Regards Ka Ter