More Haskell is more finicky than ML, and also "lazy" 

Forum: U Chicago, Com Sci 221, autumn 2000
Re: Question Haskell (Oliver Rothschild)
Date: 2000, Nov 29
From: Mike O'Donnell <odonnell@cs.uchicago.edu>

Haskell is a more recent language design than ML. ML arose to satisfy the particular needs of a theorem-proving project at Edinburgh, much as LISP arose to satisfy the particular needs of the AI people at MIT. By contrast, Haskell was designed by a committee of computer scientists as a research vehicle to study lazy functional programming. From my hyperlogical point of view, it still contains some unfortunate compromises, but as well-known language projects go, it is one of the most consistent. I'm pretty sure that it has no procedural constructs at all, while ML has quite a few. It's also ``lazy,'' which means that components of structures are only calculated to the extent that they actually affect the output.

Theoretically, everybody interested in lazy functional programming was going to compete to make the best Haskell compiler, so that there could be real comparisons instead of the apple-and-orange comparisons between different languages. In practice, people implemented subsets of, and variations on Haskell, so this didn't quite work. But it still focused a lot of intelligent research on a uniform definition, which was mostly a good thing.

From a Schemeish point of view, laziness means that the cons/car/cdr system is treated the same way as the if/true/false system. This allows a program to define infinite lists, or very long lists with no predetermined bound on their size, and to automatically compute only the finite portions of these infinite lists that are viewed in the final result. A lazily computed list is often called a stream. It's a more consistently handled variant of the Unix pipe. While a Unix pipe computes somewhat lazily, it works with a fixed buffer size, and always fills that buffer. A real stream, as implemented in Haskell, computes one element at a time so that there is no extra computation at all.

One of the neat things that functional programmers do with streams is I/O. Before streams, functional languages had crappy I/O, with none of the nice programmer control that we're used to in procedural languages. Haskell can treat input and output as two streams, and do real interaction in a functional style. This doesn't work as well as you think at first, since good interaction requires more synchronization between the input and output streams than you get from simple-minded stream programming. But it works well enough to get people interested, and now there are more sophisticated lazy mechanisms for functional I/O, including one developed in my student Sam Rebelsky's dissertation.

With (sophisticated variants of) streams, functional I/O becomes even better than procedural I/O. The right logical connection between a calculation and its I/O is essentially a sort of communication between processes running in parallel. Normal procedural languages, including C++, provide I/O through procedures that can be called by a user's program. This forces the structure of the computation to follow the structure of the I/O, and the calculation often gets lost in the muddle. If one function producer(x) returns a stream, and another function consumer(s) takes a stream as input, then the composition consumer(producer(x)) actually works by running producer and consumer in parallel, communicating through the stream. This decouples the flow of computational control between input, calculation, and output, allowing programs to be structured much more nicely.

Of course, the evaluation of a particular language is never very objective. Haskell supporters brag that they entered a contest to implement some battle-decision-support prototype for the navy, and delivered a working product that was shorter and faster than the competition, and was completed first. They say that the judges didn't recognize their program as an implementation, and thought they had only written down a careful specification of the problem. But it was really completely executable. To the extent that a practical program can look like just a careful description of a solution, functional programming will have real value. Now, all we need to do is retrain all those C++ programmers, and port all of the libraries. Oh, well.



Mike O'D.


Messages

1. Question is "Lazy" a qualitative description? by Oliver Rothschild, 2000, Nov 29
1. Feedback "Lazy" is a technical term by Mike O'Donnell, 2000, Nov 29
2. Feedback I think Haskell could replace C++ by Mike O'Donnell, 2000, Nov 29

to: "Haskell is more finicky than ML, and also "lazy""