Hacker News new | past | comments | ask | show | jobs | submit login
"Welcome to the Machine": State monads in Clojure (brehaut.net)
27 points by stevenashley on Feb 9, 2010 | hide | past | favorite | 17 comments

I don't think monads really work in dynamically typed languages, because there is no way for the bind combinator to ensure that both operands return the same type. Consider:

    f :: Int -> Either String Int
    f 0 = Left "Zero is not allowed!"
    f x = x + 42

    g :: Int -> [Int]
    g x | x > 1 = [1..x]
(These are functions of one argument that return an Int in a monad; Either and [] are the monads. The Error monad will bind the "Right" answer to the next function, or just return the Left answer as soon as it is produced. The List monad will bind every input list element to the function, and concatenate the resulting lists.)

Then, in a dynamically typed language, nothing stops you from writing something like:

    f =<< g 42
even though the expression does not make any sense. Since you don't know the type of f before you bind the output of g to it, you can't ensure that you are still in the [] monad. So while it's possible to bind [1..42] (the output of g) to f, it's not possible to make sense of the result (which would be something like Right 43 ++ Right 44 ++, except there is no ++ on Either).

Anyway, this means you don't have a monad -- you just have a function that calls other functions. It's a fine way to reuse code, but it's not a monad.

That's like saying arithmetic doesn't work in dynamic languages because nothing stops you from writing (+ 42 "foo"). Sure, you won't get a compiler error - that's why it's a dynamic language! - but you will get an error at runtime. What "stops" you from writing broken code are automated tests, lint checkers, discipline, and common sense.

I say this as someone who has implemented monadic code in both JavaScript and Haskell, including a small homegrown parser combinator module: http://limpet.net/mbrubeck/2009/10/30/compleat.html

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.

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:


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...

So as a summary: In dynamic languages the compiler does not enforce the monad laws.

I add: They aren't enforced completely in Haskell, either.

(And you can't even express every monad in Haskell. E.g. you can't even make Set a functor in Haskell.)

you can't even make Set a functor in Haskell

Well sure, because map on a set is only defined over functions that return values that can be compared for equality, whereas the generic functor map is defined over all functions.

Exactly. (However Data.Set is only defined for comparable values, i.e. the Ord typeclass, not Eq.)

That is an efficiency hack to make it O(n log n) instead of O(n^2) rather than an intrinsic property of sets, functors, or Haskell :)

Indeed. But Hindley-Milner [1] type systems have trouble expressing commutative stuff in general.

[1] I hope I got the names correct.

If I remember correctly, there was a more tricky problem in implementing monads without the type system - sometimes which monad is chosen for an expression, depends not just on the arguments, but the expected type of the result. Ex : "return 3" might be [3] or an IO action with result 3. The compiler decides this based on the surrounding context. In dynamical languages one would have to manually specify which kind of 'return' you mean.

This surrounding context inference feature, which is useful for type classes other than monads, has the unusual consequence that you need to specify more typeinfo in a dynamic language.

Clojure (being a lisp) uses macros and some dynamic variables to manage this sort of polymorphism. The clojure library provides a defmonad macro to let you define the functions required (m-bind and m-result are a must, and m-plus and m-zero are optional).

Unlike haskell which can choose an implementation based on the return type of return and bind, you do have to spell it out. The macro with-monad will wrap a set of expressions up in the correct monad, and the domonad monad comprehension form is also able to accept an optional monad name (useful if you aren't inside a with-monad form, or if you need a different monad)

Applications are open for YC Winter 2024

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact