What is the Haskell response to Node.js?

I believe the Erlang community is not envious of Node.js as it does non-blocking I/O natively and has ways to scale deployments easily to more than one processor (something not even built-in in Node.js). More details at http://journal.dedasys.com/2010/04/29/erlang-vs-node-js and Node.js or Erlang

What about Haskell? Can Haskell provide some of the benefits of Node.js, namely a clean solution to avoid blocking I/O without having recourse to multi-thread programming?


There are many things that are attractive with Node.js

  • Events: No thread manipulation, the programmer only provides callbacks (as in Snap framework)
  • Callbacks are guaranteed to be run in a single thread: no race condition possible.
  • Nice and simple UNIX-friendly API. Bonus: Excellent HTTP support. DNS also available.
  • Every I/O is by default asynchronous. This makes it easier to avoid locks. However, too much CPU processing in a callback will impact other connections (in this case, the task should split into smaller sub-tasks and re-scheduled).
  • Same language for client-side and server-side. (I don't see too much value in this one, however. jQuery and Node.js share the event programming model but the rest is very different. I just can't see how sharing code between server-side and client-side could be useful in practice.)
  • All this packaged in a single product.

  • Ok, so having watched a little of the node.js presentation that @gawi pointed me at, I can say a bit more about how Haskell compares to node.js. In the presentation, Ryan describes some of the benefits of Green Threads, but then goes on to say that he doesn't find the lack of a thread abstraction to be a disadvantage. I'd disagree with his position, particularly in the context of Haskell: I think the abstractions that threads provide are essential for making server code easier to get right, and more robust. In particular:

  • using one thread per connection lets you write code that expresses the communication with a single client, rather that writing code that deals with all the clients at the same time. Think of it like this: a server that handles multiple clients with threads looks almost the same as one that handles a single client; the main difference is there's a fork somewhere in the former. If the protocol you're implementing is at all complex, managing the state machine for multiple clients simultaneously gets quite tricky, whereas threads let you just script the communication with a single client. The code is easier to get right, and easier to understand and maintain.

  • callbacks on a single OS thread is cooperative multitasking, as opposed to preemptive multitasking, which is what you get with threads. The main disadvantage with cooperative multitasking is that the programmer is responsible for making sure that there's no starvation. It loses modularity: make a mistake in one place, and it can screw up the whole system. This is really something you don't want to have to worry about, and preemption is the simple solution. Moreover, communication between callbacks isn't possible (it would deadlock).

  • concurrency isn't hard in Haskell, because most code is pure and so is thread-safe by construction. There are simple communication primitives. It's much harder to shoot yourself in the foot with concurrency in Haskell than in a language with unrestricted side effects.


  • Can Haskell provide some of the benefits of Node.js, namely a clean solution to avoid blocking I/O without having recourse to multi-thread programming?

    Yes, in fact events and threads are unified in Haskell.

  • You can program in explicit lightweight threads (eg millions of threads on a single laptop).
  • Or; you can program in an async event-driven style, based on scalable event notification.
  • Threads are actually implemented in terms of events, and run across multiple cores, with seamless thread migration, with documented performance, and applications.

    Eg for

  • massively concurrent job orchestration
  • concurrent collections scaling on 32 or 48 cores
  • tool support for profiling and debugging multi-threaded/multi-event programs.
  • high performance event-driven web servers.
  • interesting users: such as high-frequency trading.
  • Concurrent collections nbody on 32 cores

    替代文字

    In Haskell you have both events and threads, and as it is all events under the hood.

    Read the paper describing the implementation.


    First up, I don't hold your view that node.js is doing the right thing exposing all of those callbacks. You end up writing your program in CPS (continuation passing style) and I think it should be the compiler's job to do that transformation.

    Events: No thread manipulation, the programmer only provides callbacks (as in Snap framework)

    So with this in mind, you can write using a asynchronous style if you so wish, but by doing so you'd miss out on writing in an efficient synchronous style, with one thread per request. Haskell is ludicrously efficient at synchronous code, especially when compared to other languages. It's all events underneath.

    Callbacks are guaranteed to be run in a single thread: no race condition possible.

    You could still have a race condition in node.js, but it's more difficult.

    Every request is in it's own thread. When you write code that has to communicate with other threads, it's very simple to make it threadsafe thanks to haskell's concurrency primitives.

    Nice and simple UNIX-friendly API. Bonus: Excellent HTTP support. DNS also available.

    Take a look in hackage and see for yourself.

    Every I/O is by default asynchronous (this can be annoying sometimes, though). This makes it easier to avoid locks. However, too much CPU processing in a callback will impact other connections (in this case, the task should split into smaller sub-tasks and re-scheduled).

    You have no such problems, ghc will distribute your work amongst real OS threads.

    Same language for client-side and server-side. (I don't see too much value in this one, however. JQuery and Node.js share the event programming model but the rest is very different. I just can't see how sharing code between server-side and client-side could be useful in practice.)

    Haskell can't possibly win here... right? Think again, http://www.haskell.org/haskellwiki/Haskell_in_web_browser .

    All this packaged in a single product.

    Download ghc, fire up cabal. There's a package for every need.

    链接地址: http://www.djcxy.com/p/42940.html

    上一篇: 世界应用zygohistomorphic prepromorphisms

    下一篇: 什么是Haskell对Node.js的响应?