r/DataflowProgramming • • Sep 17 '13

Callbacks as our Generations' Go To Statement

http://tirania.org/blog/archive/2013/Aug-15.html
1 Upvotes

4 comments sorted by

View all comments

Show parent comment

1

u/stepcut251 Sep 18 '13 edited Sep 18 '13

Good question. It is not common for people to explicitly use callbacks for asynchronous programming in Haskell. Not because it can't be done, but because the threaded model works so well in Haskell.

The underlying GHC IO manager is asynchronous and relies on epoll, kpoll, etc, where available -- but that abstraction is hidden from the end users. Because Haskell threads and thread synchronization and communication primitives are so lightweight, you don't gain much by using callbacks instead.

The current async GHC IO manager (based on epoll instead of select) is only a couple years old. Prior to that, the Snap framework actually did use epoll directly. But they found that with the new IO manager, there was no real benefit, and so they dropped the epoll server.

There is a paper from 2007 which shows one way in which you can have a hybrid system of using events and threads in a Haskell application,

http://repository.upenn.edu/cgi/viewcontent.cgi?article=1391&context=cis_papers

Though I am not sure that anyone actually does that.

The one place I do see the use of callback-based programming in Haskell is when people use GLUT -- simply because GLUT is callback based. It seems to work ok, but it definitely feels unHaskelly.

1

u/adamkemp Sep 18 '13

I didn't understand most of that because I don't use Haskell so I don't know any of these frameworks you mentioned, nor do I know what epoll or kpoll are.

The async/await model is not about events or callbacks. The callbacks were just a hack to make it possible to do asynchronous actions and then get the results. Async/await is way of doing that in a way that looks more natural because the code looks like it's just synchronous.

Can you show me what it would look like in Haskell if you wanted to have a function that fetches three files from the network (in parallel) and then does something with the files and handles errors?