views:

599

answers:

4

I am learning functional programming style. From this link http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-Dont-fear-the-Monads/, Brian Beckman gave a brilliant introduction about Monad. He mentioned that Monad is about composition of functions so as to address complexity.

A Monad includes a unit function that transfers type T to an amplified type M(T); and a Bind function that, given function from T to M(U), transforms type M(T) to another type M(U). (U can be T, but is not necessarily).

In my understanding, the language implementing monad should be type-checked statically. Otherwise, type errors cannot be found during compilation and "Complexity" is not controlled. Is my understanding correct?

A: 

It's certainly not the case that a language implementing monads must be statically typed, as your question title asks. It may be a good idea, for the reasons you outline, but errors failing to be detected at compile time has never stopped anyone. Just look at how many people write PHP.

chaos
I'm not sure, but any pattern in PHP is monad?
Morgan Cheng
+14  A: 
Jörg W Mittag
There's also Clojure's very nice clojure.contrib.monads library, complete with a number of great tutorials (links to some of them are included in the library's source).
Michał Marczyk
I think the last paragraph-and-a-bit is misleading. It's the interface `Monad<T>` which isn't implementable in Java/C# (and tautologically therefore not type-checked!). It is, on the other hand, most certainly possible for a *specific monad* to be statically type-checked.
Porges
@Porges: Thanks for catching that. I tried to work that in.
Jörg W Mittag
A: 
Edgar Klerks
I doubt that. First off, PHP *does* have closures. And secondly, I don't see why you would need closures to implement monads. See my answer for an implementation of the Identity Monad in PHP, which doesn't use closures. After all, PHP is Turing-complete language and everything you can do in one Turing-complete language can be done in every other Turing-complete language as well. So, if you can implement monads in Haskell, then you can do it in PHP.
Jörg W Mittag
A: 

You need closures for the State monad. I looked it up, PHP has closures since 5.3. So that wouldn't be a problem anymore.

Edgar Klerks
PHP 5.3 has anonymous function which is not exactly closure.
Morgan Cheng
The full power of the closure is not really necessary; all we need to do is chain and transform functions; anonymous functions will do fine.
Jonathan Sterling