Hacker News new | ask | show | jobs
by jrockway 5971 days ago
But that's not what monads are, what you have without a type system is just "calling a function with the result of another function". You could use the function composition operator to the same effect.

Having also implemented a monad library in a dynamically-typed language, I am well aware of what happens when one operand of bind returns the wrong type. The rest of the computation silently proceeds with that type, until it happens to change again. This results in a subtle loss of information, rather than outright failure.

Like I said, programming with combinators is a great way to reuse code -- but not all combinators that are of type "f a -> (a -> g b) -> g b" are monadic. It's only a monad when f = g.

3 comments

I totally agree that you loose static safety guarantees when you program in a dynamically-typed language. But the lack of compiler support does not prevent you from writing code that is monadic (both in its types, and in the other monad laws - which another commenter mentioned are not provable by most statically-typed languages either). You seem to agree with this, since you say you've implemented monadic functions in a dynamic language. Really you are just saying that it requires other sources of discipline, which is true of lots of things besides monads.

For example, I see you are a Perl hacker. In Perl, even my simple (42 + "foo") example results in "subtle loss of information." This doesn't mean that + doesn't work in Perl, just that someone other than the compiler has to make sure the values on either side have compatible types.

Look at the clojure.contrib.monad documentation, and you'll see that monads do have benefits beyond "normal" combinators, even if those benefits don't include all the static safety of Haskell or ML. You get the "domonad" macro which provides the same benefits as Haskell's "do" syntactic sugar. You get generic lifting/mapping/chaining functions that you can use with any monad (like the Control.Monad module in Haskell), and you can write similar generic functions of your own:

http://richhickey.github.com/clojure-contrib/monads-api.html

Generic/parametric monad actions do look different in a language with a weak type system. Rather than letting type inference take care of polymorphism for you, you have to use other language features. For example, in JavaScript you can use objects and duck-typing, letting the function access the enclosing monad's operations through an implicit object parameter. I don't know enough Clojure yet to know the equivalent idiom.

For example, I see you are a Perl hacker. In Perl, even my simple (42 + "foo") example results in "subtle loss of information."

Sure, but I don't call "+" addition, it's just Perl's "plus operator" which happens to be defined over numbers and strings.

Personally, I don't see why every language is trying to copy Haskell's monads. A monad is not some deeply important concept, it's just an abstraction that happened to be very useful in the context of Haskell's type system. Without Haskell's type system, it is not quite as useful, and it's possible that other abstractions would be better.

"Without Haskell's type system, it is not quite as useful, and it's possible that other abstractions would be better."

Sure! I'd be happy to agree with this statement.

I prefer to disagree. Monads are an important concept [1], that crop up in a lot of different places. (Or express differently: That help to better organize your code in a lot of situations.)

Functors and pointed functors and arrows are also quite important.

[1] Though not the most important one.

"...without a type system..."

Sorry to be a pedant, but dynamically typed is not the same as without a type system, it is merely without a type checker. They probably look the same to somebody well versed in Haskell but the distinction matters.

As somebody well-versed in Haskell, I can attest that a dynamic type system does look different to me than no type system.

With a dynamic type system you get your errors at least at runtime. With no type system your program just behaves wrong.

There is nothing to stop one from using reflection to ensure the types are correct at runtime...