Monads compared to InputRanges?

qznc qznc at web.de
Wed Dec 4 00:24:02 PST 2013


On Wednesday, 4 December 2013 at 01:53:39 UTC, Shammah Chancellor 
wrote:
> Or is D syntax not generic enough to define monads?

I started to port monads to D [0]. You can do it, but it looks 
ugly. The trick is to implement (Haskell) type classes via 
template specialization. I came to the conclusion that it is not 
worth it.

What D kind of lacks is a way to define a general type class aka 
the interface. Of course, you could use the "interface" keyword, 
but then you cannot apply it to structs. Haskell has no structs 
(value type records), so they do not have this problem. Look at 
how isInputRange is implemented [1]. The traits in Rust [2] 
provide this interface mechanisms as a language feature. D uses 
static-if instead.

Not Haskell, not D, not Rust can check, if your monad actually 
follows the monad laws [3]. This would probably require a full 
theorem prover in your language. So Coq, Isabelle, and maybe ATS 
could do that. A similar challenge would be to check if a 
user-defined plus operator is commutative (a+b == b+a) like 
arithmetic plus operations.

[0] 
https://bitbucket.org/qznc/d-monad/src/5b9d41c611093db74485b017a72473447f8d5595/generic.d?at=master
[1] 
https://github.com/D-Programming-Language/phobos/blob/master/std/range.d#L519
[2] http://static.rust-lang.org/doc/0.8/tutorial.html#generics
[3] http://www.haskell.org/haskellwiki/Monad_laws


More information about the Digitalmars-d-learn mailing list