Lambda the Ultimate

inactiveTopic OCaml, an Introduction
started 2/10/2004; 11:12:25 AM - last post 2/12/2004; 10:11:57 PM
Patrick Logan - OCaml, an Introduction  blueArrow
2/10/2004; 11:12:25 AM (reads: 10873, responses: 9)
OCaml, an Introduction
An introduction to OCaml in the latest issue of Linux Gazette.

Object Caml is an ML type of language. For the non-gurus: it's a functional language that can also be programmed in a non-functional and object-oriented way.

This language is really easy to learn. It's powerful and keeps impressing me with its speed. Programs written in this language are almost always stable by default. No segmentation faults, only occasional unending loops for the programmers that still hang on to program their own loops. It is really not needed to write most loops, since the libraries contain standard functions that are good enough in 99% of the cases. So try to use those functions: It really pays off in terms of stability of your programs, and, unless you have intimate knowledge of the inner works of this language, they tend to be better optimised.
Posted to functional by Patrick Logan on 2/10/04; 11:14:48 AM

Sergey Goldgaber - Re: OCaml, an Introduction  blueArrow
2/10/2004; 6:15:37 PM (reads: 902, responses: 0)
While I am very pleased to see OCaml get more coverage in the mainstream technical press, a much stronger case can be made for using OCaml and getting programmers interested in it (see links below). And this is what is needed in a mainstream press article on OCaml, as the average imperative programmer will see the task set forth in this article (a common system administration task) and ask why couldn't he just use Perl, a language familiar to most everyone engaged in performing such administrative tasks. Ok, so OCaml will do this faster than Perl. But will the gain in speed alone make learning this language with its unfamiliar syntax worth it?

At the very least, each bit of code introduced and each unfamiliar syntactical convention should be contrasted with the way things are done in popular imperative languages and an explanation offered as to why this design choice was made and what advantage programming in this manner will provide. Otherwise the syntax will just seem to be different out of spite and the functional programming style will just provoke bafflement.

For example, one of the first things a novice will wonder at is why there are seperate arithmetic operators for ints vs floats. This tutorial only mentions this in passing, saying just that "functions are defined for only one specific type". This will seem like a perversion to programmers coming from the more popular languages where arithmetic operators are overloaded. And that's the impression they'll leave with unless good reason is presented for why you might want to have seperate operators.

The author seems to want to skip such fundamental details to get to some real world coding examples. This is laudible in that many readers will want to know what can be practically done with this fancy new language. What's so great about it? How can it be useful? So perhaps the author is hoping they'll swallow the bitter pill of unfamiliar syntax without lengthy explanations and see that powerful things are possible with it.

But the approach is not compelling, as not much of a case is made as to why you couldn't just use Perl or Java to do the same thing. Crawling through a directory? Parsing command line arguments? Why not just use Perl or Java? As for language features, both have automatic garbage collection, Java can handle exceptions. A stronger case needs to be made for using OCaml, the links here are an attempt to do just that.

Chris - Re: OCaml, an Introduction  blueArrow
2/10/2004; 8:47:21 PM (reads: 851, responses: 0)
I think O'Caml's toe into the toolbox of "working programmers" is its safety and speed. Java, Perl, and Python have reputations for being slow. O'Caml is faster AND safer. I think you make a good point that it needs to be introduced using real-world tasks. It also needs something like Perl's CPAN to provide helpful libraries for real-world tasks.

Sergey Goldgaber - Re: OCaml, an Introduction  blueArrow
2/10/2004; 11:19:41 PM (reads: 815, responses: 1)
OCaml has much more to offer than just safety and speed (though those are certainly important). It also offers very powerful mechanisms for dealing with abstractions: the functional programming paradigm and its module system. With OO recently being much maligned and as disillusioned Java programmers and programmers of other overhyped languages start looking for alternatives I see the functional paradigm and modules becoming real contenders in the near future. Besides, OCaml has OO (and imperative) features also, if you want to use them.

Combining all of these features with OCaml's highly expressive type system, static typing, and type inference allows the development of complex and powerful (and safe and fast) applications with very little time and effort, as Graydon Hoare demonstrated in his amazing One Day Compilers (which has been discussed here before). It's articles and presentations like Graydon's that we need more of in the mainstream technical press. Articles that you read and your jaw drops and you think, "Wow. So that is what OCaml is capable of."

Unfortunately, I think Graydon's presentation is far too advanced for the "working programmer". What is needed is not so much a Functional Programming for Dummies, but maybe a Functional Programming for the Rest of Us that makes clear what the functional paradigm in general, and perhaps OCaml in particular can offer us, without requiring courses in lambda calculus, types, or compiler design to understand. The articles I linked to in my earlier message are some attempts at that, but I think we could do even better.

As for CPAN-like libraries for real world tasks there are GODI, the OCaml Link Database, the Humps and even PLEAC. Unfortunately, none of these contain, to my knowledge, brief introductory tutorials featuring real-world tasks (like the Linux Gazette tutorial) which make the advantages of FP or OCaml sufficiently clear. Graydon's presentation is possibly the closest thing, but, as I mentioned above, it's too advanced. Cousinau and Mauny's Functional Approach to Programming implement a regular expression library, which is very practical, but is also too advanced (implementing the library comes after they have taught you the entire language).

Perhaps practical applications where the full potential of the language can be appreciated are simply incompatible with introductory tutorials. Maybe the best approach is to stick to general, unsupportable (within the scope of an introductory tutorial) claims about the language's potential for large-scale safe, efficient and flexible programming while teaching the very basics in as clear a manner as possible. If this is truly the best approach then the article in the Linux Gazette did not follow it, as it ommited or hurried past most of the fundamentals in a rush to present something practical (though, to an imperative programmer, equally achievable in Perl or Java), but also without making clear why OCaml would be a better choice for this task.

Which is not to say that exposure of OCaml in the mainstream technical media is a bad thing. I'm just concerned that if it's not done carefully enough, with enough attention to the needs and preconceptions of average imperative programmers it could wind up scaring more people than it attracts, as without adequate explanation it's easy to get blinded by the "weird syntax" or think, "What's the big deal? Perl/Java could do that!"

andrew cooke - Re: OCaml, an Introduction  blueArrow
2/11/2004; 1:28:16 AM (reads: 745, responses: 0)
i'm not sure the regexp library in c&m is a good example - it's not exactly industrial strength (the implementation is "naive"; there's no optimization / compilation of the state machine (which is only implicit in their approach)).

incidentally, does ocaml have corba support? i've been looking at it for the reasons listed earlier (it's a good language *and* a politically easy sell for numerical computation, having a reputation for speed), but corba is pretty much a requirement (and i can only find MS COM-related IDL packages). i posted a message to the ocaml mailing list yesterday, but it seems to have got lost somewhere.

Sergey Goldgaber - Re: OCaml, an Introduction  blueArrow
2/11/2004; 2:55:01 AM (reads: 715, responses: 1)
Ok, the regex library might not have been the best example, but I only meant that it was practical in aim (compared to, say, a factorial function, the greatest common divisor, the eight queens problem, or other such "toy problems" commonly seen in introductory to intermediate level texts). A "working programmer" should readily agree that this aim is a useful, practical one; and thus be interested in seeing how a functional language, perhaps even OCaml can be used to solve such a problem. Whether the actual implementation was naive is almost beside the point; as, naive or not, the understanding of a regular expression library implemented in OCaml is far beyond the capacity of the target audience of a mainstream novice-level OCaml tutorial... thus my lament that you can't really present practical examples in introductory articles and expect them to make sense or be convincing to a novice.

As for CORBA, the only things I've been able to find are CamlORB, which is in pre-alpha.

And a statement by Olivier Andrieu:

"I am writing bindings for ORBit2, the CORBA ORB of the GNOME project. There is a IDL compiler that maps CORBA types to OCaml types, and a runtime library that does the necessary conversions. It requires a fairly recent version of ORBit2 (>= 2.7.2)."

Xavier Leroy mentioned CORBA in this interesting message a few years ago.

There is also some brief mention of it here.

Members of the mailing list should be able to provide you with a more up-to-date answer, however.

andrew cooke - Re: OCaml, an Introduction  blueArrow
2/11/2004; 3:18:23 AM (reads: 722, responses: 0)
thanks for the pointers, i'll resend the mail message.

(maybe i was a bit too hard on the regexp library - i remember that it impressed me when i read it, enough to write a version of my own).

William D. Neumann - Re: OCaml, an Introduction  blueArrow
2/11/2004; 8:51:18 AM (reads: 550, responses: 0)
I have to agree with Sergey, while this certainly can't hurt OCaml in the eyes of your average Linux user, it probably won't do too much to get others to actually start using the language. What is really needed are article more along the lines of Eric Raymond's "Why Python?" article. That is, articles that say, "Look, here is a language that can save you time and headaches" rather than "Look, here is yet another language that does stuff..." Of course having said article written by someone with keyboard cred like Raymond wouldn't hurt ether.

Jason Gibson - Re: OCaml, an Introduction  blueArrow
2/12/2004; 10:11:57 PM (reads: 315, responses: 0)
Richard Jones has "Learning OCaml, for C, C++, Perl and Java programmers" at http://merjis.com/richj/computers/ocaml/tutorial/ and some other goodies (like perl4caml which lets you run perl code within ocaml, and mod_caml for apache) here: http://merjis.com/developers/