ANN: Monad Comprehensions in Groovy

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

ANN: Monad Comprehensions in Groovy

Dinko Srkoc
Recently, a topic of monads[1] occupied my attention. Since I'm not
very good at writing blogs, I decided to write a library instead.
Monadologie is the result of such effort:

https://github.com/dsrkoc/monadologie

The library is in its early stage of development and if there is
someone who shared my interest in such a subject, I would very much
appreciate a feedback.

I'm posting this on the Groovy mailing list because, although tons of
material about monads lay around the internet, there doesn't seem to
be much related to Groovy in particular (and, as I said, I'm a lousy
blog writer).

Monad comprehension[2] is generalization of list comprehensions[3]
known from other languages. My implementation was heavily influenced
by Scala's for comprehension[4].

As an example, let's calculate all Pythagorean triples[5] (sets of
three integers satisfying Pythagorean theorem) up to a given number:

8<------------------------------
import static hr.helix.monadologie.MonadComprehension.foreach

def pythags(n) {
    foreach {
        z = takeFrom { 1..n }
        x = takeFrom { 1..z }
        y = takeFrom { x..z }

        guard { x**2 + y**2 == z**2 }

        yield { [x, y, z] }
    }
}

assert pythags(12) == [[3, 4, 5], [6, 8, 10]]
------------------------------>8

where 'foreach' block denotes the comprehension. 'y' is taken from the
(x..z) range, for each 'x', and 'x' is taken for each 'z'. 'guard'
acts as a filter that lets only values that satisfy given expression
to pass to 'yield'. 'yield' gives result for each step. The final
result is a list of all yields.

[1] http://en.wikipedia.org/wiki/Monad_(functional_programming)
[2] http://homepages.inf.ed.ac.uk/wadler/papers/monads/monads.ps (postscript)
[3] http://en.wikipedia.org/wiki/List_comprehension
[4] http://www.artima.com/pins1ed/for-expressions-revisited.html#23.1
[5] http://en.wikipedia.org/wiki/Pythagorean_triple

Thank you for your attention,

Dinko

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ANN: Monad Comprehensions in Groovy

Omar Abou Mrad
Dinko,

That's a blog post right there :-)

On Fri, May 13, 2011 at 10:59 AM, Dinko Srkoc <[hidden email]> wrote:
Recently, a topic of monads[1] occupied my attention. Since I'm not
very good at writing blogs, I decided to write a library instead.
Monadologie is the result of such effort:

https://github.com/dsrkoc/monadologie

The library is in its early stage of development and if there is
someone who shared my interest in such a subject, I would very much
appreciate a feedback.

I'm posting this on the Groovy mailing list because, although tons of
material about monads lay around the internet, there doesn't seem to
be much related to Groovy in particular (and, as I said, I'm a lousy
blog writer).

Monad comprehension[2] is generalization of list comprehensions[3]
known from other languages. My implementation was heavily influenced
by Scala's for comprehension[4].

As an example, let's calculate all Pythagorean triples[5] (sets of
three integers satisfying Pythagorean theorem) up to a given number:

8<------------------------------
import static hr.helix.monadologie.MonadComprehension.foreach

def pythags(n) {
   foreach {
       z = takeFrom { 1..n }
       x = takeFrom { 1..z }
       y = takeFrom { x..z }

       guard { x**2 + y**2 == z**2 }

       yield { [x, y, z] }
   }
}

assert pythags(12) == [[3, 4, 5], [6, 8, 10]]
------------------------------>8

where 'foreach' block denotes the comprehension. 'y' is taken from the
(x..z) range, for each 'x', and 'x' is taken for each 'z'. 'guard'
acts as a filter that lets only values that satisfy given expression
to pass to 'yield'. 'yield' gives result for each step. The final
result is a list of all yields.

[1] http://en.wikipedia.org/wiki/Monad_(functional_programming)
[2] http://homepages.inf.ed.ac.uk/wadler/papers/monads/monads.ps (postscript)
[3] http://en.wikipedia.org/wiki/List_comprehension
[4] http://www.artima.com/pins1ed/for-expressions-revisited.html#23.1
[5] http://en.wikipedia.org/wiki/Pythagorean_triple

Thank you for your attention,

Dinko

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ANN: Monad Comprehensions in Groovy

Dinko Srkoc
On 13 May 2011 10:26, Omar Abou Mrad <[hidden email]> wrote:
> Dinko,
> That's a blog post right there :-)

Indeed, that didn't occur to me. I should start blogging then. :-)

>
> On Fri, May 13, 2011 at 10:59 AM, Dinko Srkoc <[hidden email]> wrote:
>>
>> Recently, a topic of monads[1] occupied my attention. Since I'm not
>> very good at writing blogs, I decided to write a library instead.
>> Monadologie is the result of such effort:
>>
>> https://github.com/dsrkoc/monadologie
>>
>> The library is in its early stage of development and if there is
>> someone who shared my interest in such a subject, I would very much
>> appreciate a feedback.
>>
>> I'm posting this on the Groovy mailing list because, although tons of
>> material about monads lay around the internet, there doesn't seem to
>> be much related to Groovy in particular (and, as I said, I'm a lousy
>> blog writer).
>>
>> Monad comprehension[2] is generalization of list comprehensions[3]
>> known from other languages. My implementation was heavily influenced
>> by Scala's for comprehension[4].
>>
>> As an example, let's calculate all Pythagorean triples[5] (sets of
>> three integers satisfying Pythagorean theorem) up to a given number:
>>
>> 8<------------------------------
>> import static hr.helix.monadologie.MonadComprehension.foreach
>>
>> def pythags(n) {
>>    foreach {
>>        z = takeFrom { 1..n }
>>        x = takeFrom { 1..z }
>>        y = takeFrom { x..z }
>>
>>        guard { x**2 + y**2 == z**2 }
>>
>>        yield { [x, y, z] }
>>    }
>> }
>>
>> assert pythags(12) == [[3, 4, 5], [6, 8, 10]]
>> ------------------------------>8
>>
>> where 'foreach' block denotes the comprehension. 'y' is taken from the
>> (x..z) range, for each 'x', and 'x' is taken for each 'z'. 'guard'
>> acts as a filter that lets only values that satisfy given expression
>> to pass to 'yield'. 'yield' gives result for each step. The final
>> result is a list of all yields.
>>
>> [1] http://en.wikipedia.org/wiki/Monad_(functional_programming)
>> [2] http://homepages.inf.ed.ac.uk/wadler/papers/monads/monads.ps
>> (postscript)
>> [3] http://en.wikipedia.org/wiki/List_comprehension
>> [4] http://www.artima.com/pins1ed/for-expressions-revisited.html#23.1
>> [5] http://en.wikipedia.org/wiki/Pythagorean_triple
>>
>> Thank you for your attention,
>>
>> Dinko
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ANN: Monad Comprehensions in Groovy

Guillaume Laforge
In reply to this post by Dinko Srkoc
Pretty interesting!

On Fri, May 13, 2011 at 09:59, Dinko Srkoc <[hidden email]> wrote:
Recently, a topic of monads[1] occupied my attention. Since I'm not
very good at writing blogs, I decided to write a library instead.
Monadologie is the result of such effort:

https://github.com/dsrkoc/monadologie

The library is in its early stage of development and if there is
someone who shared my interest in such a subject, I would very much
appreciate a feedback.

I'm posting this on the Groovy mailing list because, although tons of
material about monads lay around the internet, there doesn't seem to
be much related to Groovy in particular (and, as I said, I'm a lousy
blog writer).

Monad comprehension[2] is generalization of list comprehensions[3]
known from other languages. My implementation was heavily influenced
by Scala's for comprehension[4].

As an example, let's calculate all Pythagorean triples[5] (sets of
three integers satisfying Pythagorean theorem) up to a given number:

8<------------------------------
import static hr.helix.monadologie.MonadComprehension.foreach

def pythags(n) {
   foreach {
       z = takeFrom { 1..n }
       x = takeFrom { 1..z }
       y = takeFrom { x..z }

       guard { x**2 + y**2 == z**2 }

       yield { [x, y, z] }
   }
}

assert pythags(12) == [[3, 4, 5], [6, 8, 10]]
------------------------------>8

where 'foreach' block denotes the comprehension. 'y' is taken from the
(x..z) range, for each 'x', and 'x' is taken for each 'z'. 'guard'
acts as a filter that lets only values that satisfy given expression
to pass to 'yield'. 'yield' gives result for each step. The final
result is a list of all yields.

[1] http://en.wikipedia.org/wiki/Monad_(functional_programming)
[2] http://homepages.inf.ed.ac.uk/wadler/papers/monads/monads.ps (postscript)
[3] http://en.wikipedia.org/wiki/List_comprehension
[4] http://www.artima.com/pins1ed/for-expressions-revisited.html#23.1
[5] http://en.wikipedia.org/wiki/Pythagorean_triple

Thank you for your attention,

Dinko

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email





--
Guillaume Laforge
Groovy Project Manager
Head of Groovy Development at SpringSource
http://www.springsource.com/g2one
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ANN: Monad Comprehensions in Groovy

Dinko Srkoc
On 13 May 2011 10:54, Guillaume Laforge <[hidden email]> wrote:
> Pretty interesting!

Thanks Guillaume. A few months ago I found this whole other world I
hadn't known about and it's fascinating.

>
> On Fri, May 13, 2011 at 09:59, Dinko Srkoc <[hidden email]> wrote:
>>
>> Recently, a topic of monads[1] occupied my attention. Since I'm not
>> very good at writing blogs, I decided to write a library instead.
>> Monadologie is the result of such effort:
>>
>> https://github.com/dsrkoc/monadologie
>>
>> The library is in its early stage of development and if there is
>> someone who shared my interest in such a subject, I would very much
>> appreciate a feedback.
>>
>> I'm posting this on the Groovy mailing list because, although tons of
>> material about monads lay around the internet, there doesn't seem to
>> be much related to Groovy in particular (and, as I said, I'm a lousy
>> blog writer).
>>
>> Monad comprehension[2] is generalization of list comprehensions[3]
>> known from other languages. My implementation was heavily influenced
>> by Scala's for comprehension[4].
>>
>> As an example, let's calculate all Pythagorean triples[5] (sets of
>> three integers satisfying Pythagorean theorem) up to a given number:
>>
>> 8<------------------------------
>> import static hr.helix.monadologie.MonadComprehension.foreach
>>
>> def pythags(n) {
>>    foreach {
>>        z = takeFrom { 1..n }
>>        x = takeFrom { 1..z }
>>        y = takeFrom { x..z }
>>
>>        guard { x**2 + y**2 == z**2 }
>>
>>        yield { [x, y, z] }
>>    }
>> }
>>
>> assert pythags(12) == [[3, 4, 5], [6, 8, 10]]
>> ------------------------------>8
>>
>> where 'foreach' block denotes the comprehension. 'y' is taken from the
>> (x..z) range, for each 'x', and 'x' is taken for each 'z'. 'guard'
>> acts as a filter that lets only values that satisfy given expression
>> to pass to 'yield'. 'yield' gives result for each step. The final
>> result is a list of all yields.
>>
>> [1] http://en.wikipedia.org/wiki/Monad_(functional_programming)
>> [2] http://homepages.inf.ed.ac.uk/wadler/papers/monads/monads.ps
>> (postscript)
>> [3] http://en.wikipedia.org/wiki/List_comprehension
>> [4] http://www.artima.com/pins1ed/for-expressions-revisited.html#23.1
>> [5] http://en.wikipedia.org/wiki/Pythagorean_triple
>>
>> Thank you for your attention,
>>
>> Dinko
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>
>
>
> --
> Guillaume Laforge
> Groovy Project Manager
> Head of Groovy Development at SpringSource
> http://www.springsource.com/g2one
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ANN: Monad Comprehensions in Groovy

François Rey
In reply to this post by Dinko Srkoc

Thanks Dinko for bringing this to our attention. I never heard of monad programming before. I quite enjoyed this funny tutorial though. Getting my hands dirty with Monadologie would probably be a good way to learn more.

While we're at it, couldn't monadologie be of use for our friend Charlie who just asked for advice in his post 'String creation based on a rule' ?


On Friday 13 May 2011 09:59:24 you wrote:

> Recently, a topic of monads[1] occupied my attention. Since I'm not

> very good at writing blogs, I decided to write a library instead.

> Monadologie is the result of such effort:

>

> https://github.com/dsrkoc/monadologie


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ANN: Monad Comprehensions in Groovy

Guillaume Laforge
In reply to this post by Dinko Srkoc

On Fri, May 13, 2011 at 18:19, Dinko Srkoc <[hidden email]> wrote:
On 13 May 2011 10:54, Guillaume Laforge <[hidden email]> wrote:
> Pretty interesting!

Thanks Guillaume. A few months ago I found this whole other world I
hadn't known about and it's fascinating.

What's interesting too is that your examples somehow demystify monads, which are usually explained in much more complex terms!

Guillaume


--
Guillaume Laforge
Groovy Project Manager
Head of Groovy Development at SpringSource
http://www.springsource.com/g2one
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ANN: Monad Comprehensions in Groovy

Dinko Srkoc
In reply to this post by François Rey
2011/5/13 François Rey <[hidden email]>:
> Thanks Dinko for bringing this to our attention. I never heard of monad
> programming before. I quite enjoyed this funny tutorial though. Getting my
> hands dirty with Monadologie would probably be a good way to learn more.

I certainly learned quite a lot more.
If it helped you in any way, it would put additional value to the time
I spent writing it.

>
> While we're at it, couldn't monadologie be of use for our friend Charlie who
> just asked for advice in his post 'String creation based on a rule' ?

Ah, the challenge! :-) I offered a solution, but I didn't use
monadologie. Monads are all about composing operations and I used but
one 'inject'. I did use three 'replace'/'replaceAll' methods, but they
compose naturally, no need for outside help.

Cheers,
- Dinko

>
> On Friday 13 May 2011 09:59:24 you wrote:
>
>> Recently, a topic of monads[1] occupied my attention. Since I'm not
>
>> very good at writing blogs, I decided to write a library instead.
>
>> Monadologie is the result of such effort:
>
>>
>
>> https://github.com/dsrkoc/monadologie
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ANN: Monad Comprehensions in Groovy

Dinko Srkoc
In reply to this post by Guillaume Laforge
On 13 May 2011 22:15, Guillaume Laforge <[hidden email]> wrote:
>
> What's interesting too is that your examples somehow demystify monads, which
> are usually explained in much more complex terms!
> Guillaume
>

I did try to avoid mentioning category theory. ;-)

Dinko

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ANN: Monad Comprehensions in Groovy

Ken Barclay
This post has NOT been accepted by the mailing list yet.
In reply to this post by Dinko Srkoc
Dinko

Very neat implementation of the for comprehension.

Had a quick look at your code and here are some observations:

1. The None and Some classes have redundant wrapper properties.

2. Should the Monad and Functor interfaces not be related by inheritance?
    In such as hierarchy should there not also be an ApplicativeFunctor interface?
    I am using such a hierarchy to develop types such as Option, Either,
    List, Map, etc as well as a monadic Groovy combinator parser library.

Ken
Loading...