www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Monads in D

reply Atila Neves <atila.neves gmail.com> writes:
I give you a facsimile of a Haskell do block:

     with(Maybe!int) {
         return_(5).bind!(a => return_(a + 
1)).shouldEqual(just(6));
         nothing.bind!(a => return_(a + 1)).shouldEqual(nothing);
         return_(8).bind!(a => nothing).bind!(a => return_(a + 
1)).shouldEqual(nothing);
     }

     with(Maybe!string) {
         return_("foo").bind!(a => return_(a ~ 
"bar")).shouldEqual(just("foobar"));
         nothing.bind!(a => return_(a ~ 
"bar")).shouldEqual(nothing);
         return_("foo").bind!(a => return_(a ~ "bar")).bind!(a => 
nothing).shouldEqual(nothing);
     }


Why? Because I could, I don't plan on using this for anything 
serious. I think "with" is my favourite D feature right now. I 
also wrote the Writer and State monads (not that D needs them):

https://github.com/atilaneves/felix

I tried overloading `>>>` for bind (closest overloadable operator 
to `>>=`) but it went horribly wrong. I always get problems when 
I try to pass lambdas as runtime values instead of template 
parameters.


Atila
Jun 11 2016
next sibling parent reply ArturG <var.spool.mail700 gmail.com> writes:
On Saturday, 11 June 2016 at 14:26:20 UTC, Atila Neves wrote:

 Why? Because I could, I don't plan on using this for anything 
 serious. I think "with" is my favourite D feature right now. I 
 also wrote the Writer and State monads (not that D needs them):

 https://github.com/atilaneves/felix


 Atila
im not an expert but is it something similar to this idea[1] as it seems to be used in a similar way. [1] http://forum.dlang.org/thread/ltalqpmpscdoziserqqx forum.dlang.org
Jun 11 2016
parent Atila Neves <atila.neves gmail.com> writes:
On Saturday, 11 June 2016 at 14:57:11 UTC, ArturG wrote:
 On Saturday, 11 June 2016 at 14:26:20 UTC, Atila Neves wrote:

 Why? Because I could, I don't plan on using this for anything 
 serious. I think "with" is my favourite D feature right now. I 
 also wrote the Writer and State monads (not that D needs them):

 https://github.com/atilaneves/felix


 Atila
im not an expert but is it something similar to this idea[1] as it seems to be used in a similar way. [1] http://forum.dlang.org/thread/ltalqpmpscdoziserqqx forum.dlang.org
Right, but that's only the Maybe monad. I was trying out monads in general. Atila
Jun 11 2016
prev sibling next sibling parent reply qznc <qznc web.de> writes:
On Saturday, 11 June 2016 at 14:26:20 UTC, Atila Neves wrote:
 Why? Because I could, I don't plan on using this for anything 
 serious. I think "with" is my favourite D feature right now. I 
 also wrote the Writer and State monads (not that D needs them):
I also tried this. Instead of Write and State, I tried to model the Functor > Applicative > Monad > MonadFail type hierarchy. I found no way for a good "template inheritance" construct. Overall, I quickly forgot about it, because it looks ugly and seems to have no advantage. Thinking more conceptually, Monads should be somewhat related to input ranges, as both model a linear sequence.
Jun 11 2016
parent Atila Neves <atila.neves gmail.com> writes:
On Saturday, 11 June 2016 at 16:50:15 UTC, qznc wrote:
 On Saturday, 11 June 2016 at 14:26:20 UTC, Atila Neves wrote:
 Why? Because I could, I don't plan on using this for anything 
 serious. I think "with" is my favourite D feature right now. I 
 also wrote the Writer and State monads (not that D needs them):
I also tried this. Instead of Write and State, I tried to model the Functor > Applicative > Monad > MonadFail type hierarchy. I found no way for a good "template inheritance" construct. Overall, I quickly forgot about it, because it looks ugly and seems to have no advantage.
I was thinking of doing that as well and was going to model the hierarchy like so: alias isFunctor = isInputRange; enum isApplicative(alias T, U...) = isFunctor!T && is(typeof() { ... })); enum isMonad(alias T, U...) = isApplicative!(T, U) && is(typeof(() { ... }));
 Thinking more conceptually, Monads should be somewhat related 
 to input ranges, as both model a linear sequence.
Monads are related to input ranges because every monad is a functor, and input ranges are functors. Atila
Jun 11 2016
prev sibling parent reply deadalnix <deadalnix gmail.com> writes:
On Saturday, 11 June 2016 at 14:26:20 UTC, Atila Neves wrote:
 I give you a facsimile of a Haskell do block:

     with(Maybe!int) {
         return_(5).bind!(a => return_(a + 
 1)).shouldEqual(just(6));
         nothing.bind!(a => return_(a + 1)).shouldEqual(nothing);
         return_(8).bind!(a => nothing).bind!(a => return_(a + 
 1)).shouldEqual(nothing);
     }

     with(Maybe!string) {
         return_("foo").bind!(a => return_(a ~ 
 "bar")).shouldEqual(just("foobar"));
         nothing.bind!(a => return_(a ~ 
 "bar")).shouldEqual(nothing);
         return_("foo").bind!(a => return_(a ~ "bar")).bind!(a 
 => nothing).shouldEqual(nothing);
     }


 Why? Because I could, I don't plan on using this for anything 
 serious. I think "with" is my favourite D feature right now. I 
 also wrote the Writer and State monads (not that D needs them):

 https://github.com/atilaneves/felix

 I tried overloading `>>>` for bind (closest overloadable 
 operator to `>>=`) but it went horribly wrong. I always get 
 problems when I try to pass lambdas as runtime values instead 
 of template parameters.


 Atila
Honestly, the whole bind/return is just a giant NIH. >>> and >>= are its symptoms. There is a reason why nobody understands jack shit about monad even after 10s of tutorial when they aren't even hard in any way: because haskell and alike have made all that is in their power to obfuscate what is a monad. I could go on, but this guy already did it way better that I can: https://www.infoq.com/presentations/functional-pros-cons The part about monad starts 25mins in.
Jun 11 2016
next sibling parent Max Samukha <maxsamukha gmail.com> writes:
On Saturday, 11 June 2016 at 18:27:19 UTC, deadalnix wrote:

 I could go on, but this guy already did it way better that I 
 can:
 https://www.infoq.com/presentations/functional-pros-cons

 The part about monad starts 25mins in.
The guy is good.
Jun 11 2016
prev sibling next sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 11.06.2016 20:27, deadalnix wrote:

 Honestly, the whole bind/return is just a giant NIH. >>> and >>= are its
 symptoms. There is a reason why nobody understands jack shit about monad
 even after 10s of tutorial when they aren't even hard in any way:
 because haskell and alike have made all that is in their power to
 obfuscate what is a monad.

 I could go on, but this guy already did it way better that I can:
 https://www.infoq.com/presentations/functional-pros-cons

 The part about monad starts 25mins in.
I don't see the point of this criticism. Different language makes sense to different people.
Jun 12 2016
parent reply Ola Fosheim =?UTF-8?B?R3LDuHN0YWQ=?= writes:
On Sunday, 12 June 2016 at 15:33:31 UTC, Timon Gehr wrote:
 I don't see the point of this criticism. Different language 
 makes sense to different people.
I think he had a point, but his explanation was harder to follow than this one: http://research.microsoft.com/en-us/um/people/simonpj/papers/marktoberdorf/mark.pdf Input world, output modified world... Geez, why doesn't tutorials on Monads say that before they try to explain it with code?
Jun 12 2016
parent reply Max Samukha <maxsamukha gmail.com> writes:
On Sunday, 12 June 2016 at 16:46:13 UTC, Ola Fosheim Grøstad 
wrote:

 Input world, output modified world... Geez, why doesn't 
 tutorials on Monads say that before they try to explain it with 
 code?
Because IO is just one of many applications of the monad concept?
Jun 12 2016
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 12.06.2016 20:28, Max Samukha wrote:
 On Sunday, 12 June 2016 at 16:46:13 UTC, Ola Fosheim Grøstad wrote:

 Input world, output modified world... Geez, why doesn't tutorials on
 Monads say that before they try to explain it with code?
Because IO is just one of many applications of the monad concept?
Most of the tutorials are not actually very good.
Jun 12 2016
prev sibling parent Atila Neves <atila.neves gmail.com> writes:
On Saturday, 11 June 2016 at 18:27:19 UTC, deadalnix wrote:
 On Saturday, 11 June 2016 at 14:26:20 UTC, Atila Neves wrote:
 [...]
Honestly, the whole bind/return is just a giant NIH. >>> and
[...]
jack shit about monad even after 10s of tutorial when they aren't even hard in any way: because haskell and alike have made all that is in their power to obfuscate what is a monad. I could go on, but this guy already did it way better that I can: https://www.infoq.com/presentations/functional-pros-cons The part about monad starts 25mins in.
That was awesome. I didn't mean to imply I think monads are great and that `>>=` and `return` are great names. I just wanted to see how far I could take in D. For the lulz. Atila
Jun 13 2016