Prolog: Just the beginning?

If you’ve been following my recent computer-related posts about learning 7 languages in 7 weeks from a similarly titled book, you may be wondering why several weeks have elapsed since I wrote about my experiences with Io and there has been nothing about the next language on the list, whose tutorial I should have completed at least a fortnight ago.  If you know me reasonably well, you may have guessed that this project has fallen by the wayside as I’ve moved on to other interests.  Ordinarily, that would not be a bad guess but, as it happens, I’ve just been taking a slightly more leisurely pace than the book’s title indicates (and, yes, it’s partly because I have other interests and obligations and can’t spend all my time just concentrating on learning one programming language for a whole week).

The third language covered in the book is Prolog.  This is the oldest language covered in the book, by quite some margin.  It dates back to around 1972, so it’s a bit older than me. It’s also the only language covered, apart from Ruby, that I’d ever really looked at before.  It’s been a few years since my last foray into Prolog and I’d forgotten most of what little I learned about it at the time.

Prolog is an example, perhaps the Ur-example, of logic programming languages, which are based on a programming paradigm called declarative programming.  Thus, the Prolog approach to programming is quite strange to someone used to the more common imperative programming paradigm.  Instead of issuing the computer with a set of commands to solve your problem, you give it a description of the problem and leave the solution to the computer.  It takes a bit of getting used to this idea, but it means that Prolog is very good at solving some problems that would be much more difficult with other languages and, conversely, much less well suited to other problems.

The tutorial in the book builds up to a couple of quite neat programs.  One is designed to find solutions to the classic Eight Queens puzzle in chess, while the other is a Sudoku solver.  The worked example of the latter in the book is only for a 4×4 sudoku, and it leaves the full solution of the standard 9×9 puzzles as an exercise (which is a fairly straightforward generalisation, involving quite a bit more typing but no new concepts that don’t appear in the smaller version).  Unfortunately, the program in the book is designed to work with GNU Prolog and uses some library functions that are not available in other implementations, so I couldn’t just type it in and get it to work on the SWI Prolog system I was using.  However, a bit of Googling turned up a couple of other solutions written for SWI Prolog (making use of some libraries for that version, and hence not directly useful to other Prolog versions – this lack of portability seems to be a particular feature of the language, although it’s by no means unique).  One of these managed to fit the whole program into 13 lines of code and seems very elegant, although a couple of the details are slightly beyond my current grasp of the language.  This is certainly a big improvement on sudoku solvers I’ve looked at in other languages, which are typically much longer and more convoluted.

Due to the very different approach to programming, Prolog represents a quite different tool from any of the other programming languages with which I am acquainted, and it seems especially good at some kinds of problem that especially interest me (including, but not limited to, natural language processing).  For that reason, I’m hoping to keep on developing my Prolog skills and hope to be able to do useful things with the language before too long.  In the meantime, I’m going to carry on with the remaining 4 language tutorials in the book (the next one up, IIRC, being Scala).

The film that Tate associates with Prolog is Rain Man.  This is, by all accounts, a classic film on the subject of autism and generally very well regarded by critics.  Although it dates back to 1988 I have not previously got round to watching it, but I managed to pick up a cheap copy the other day and I thought it was an excellent, thought-provoking and very moving film.  I can certainly see how Prolog resembles the semi-eponymous character, Raymond (an autistic savant), who seems very difficult to interact with in many respects but is outstandingly brilliant at certain things.

 

 

Io, by Jove!

I have been continuing the programming project I started the other week of working through the book Seven Languages in Seven Weeks, although it is already in danger of getting swamped by the many other things vying for my attention (especially, at the moment, my resurgent interest in human languages as fuelled by the Bangor Polyglots group I’ve just joined).

The second programming language in the book, and the one I’ve just finished working on, is Io.  Of all the languages covered, this was the only one I’d never even heard of, putting it at the opposite end of the spectrum from Ruby (the only one I’d used much already). The official Io website, which I’ve just linked to, describes it as “prototype-based programming language inspired by Smalltalk, Self, NewtonScript , Act1, LISP  and Lua” (check the original source to see the nature of the inspiration in each case).  While Self, NewtonScript and Act1 are also languages that I’m entirely unfamiliar with (though I’d heard of NewtonScript), I have played a bit with Smalltalk, LISP and (to a lesser extent) Lua, so I could see some resemblance.

Prototype-based languages, such as Io, Lua and Javascript, are object-oriented languages but, unlike most OO languages (such as Ruby, Java and C++) , they are classless.  Instead, each object inherits (via a process known as cloning) and extends or modifies behaviour from another object, which acts as its prototype.  This approach is supposed to encourage a very flexible style of programming.  I have previously done a fair amount of work with Javascript (though mostly via the excellent jQuery library, which hides most of the gory details out of sight) but have not fully grokked the idea of prototype-based programming.  My experiences of Io this week have deepened my understanding somewhat, although I think I’m still a very long way off full mastery of the concept.

The advanced features of Io which are the culmination of Tate’s tutorial are its facility for writing domain-specific languages (essentially by rewriting the syntax of Io itself) and its tools for handling concurrency.  Neither of these are areas of which I have much previous experience, so I’m not in a position to judge how much of an improvement Io offers over other languages for work in these areas.  I think several of the other language tutorials in the book will also be looking at concurrent programming (I’m not sure about DSLs) so hopefully by the time I’ve finished the book I’ll have a better idea which language to turn to if I should ever need to do anything like that.

The film character Tate associates with Io is Ferris Bueller, the protagonist of the 1986 movie Ferris Bueller’s Day Off.  I first saw this film (or at least a large chunk of it) very shortly after it came out, as it was shown to my primary school class on the last day of term.  In hindsight, I find it quite a surprising choice of film to show in this context since (a) it’s all about somebody skiving off school who is clearly depicted as the hero of the film and not seen to suffer any ill-consequences from his truancy (an odd message for a teacher to want to convey, surely!?) and (b) it’s 15 rated in the UK and none of us in the class were older than 11.  Still, I suppose it was the last day of term and the party mood was probably affecting our teachers too (as in, they just wanted something to keep the kids quiet while they went and made a start on their own end-of-term party).  My second viewing of the film took place only a few days ago and I enjoyed it a lot more than I thought I was going to; unlike many films of its era it doesn’t feel painfully dated by now.

Returning to the programming language, the name Io is shared with (and presumably comes from, though I’ve not been able to find anything to confirm or contradict the supposition) one of the moons of Jupiter.  Interestingly, one of the languages listed as an inspiration for Io is Lua, which is the Portuguese word for moon (to find out why, check out the Wikipedia article on Lua).  I don’t know whether that’s a coincidence or whether Lua inspired the name as well as some of the features of Io.

At first, I found Io quite fun to play with.  As I began to try to go deeper, it quickly became quite frustrating as it was so different to other languages I’ve used (although that is, really, the point of the exercise).  After a few days, some of the ideas began to click and I was able to make fairly good progress.  I was heartened to notice that Bruce Tate himself said that it took him several weeks of working with Io to really begin to get a grip on it.  I’m glad to have made its acquaintance but I don’t plan to do any more work with Io for the moment as I think my limited programming time can better be spent in other directions.  I may well return to it for another look in the future though and would certainly recommend it as an interesting language to have a look at.

Ruby, my dear

I recently acquired a new computer programming book entitled Seven Languages in Seven Weeks (by Bruce A. Tate, published 2010 by the Pragmatic Programmers; ISBN: 978-1-934356-59-3).  As the name suggests, this is an introduction to not one but seven different programming languages and is designed to be worked through (it’s very much a hands-on kind of book) in a fairly short space of time.  Rather than purporting to provide complete coverage of each language (which would be impossible, probably even for a single one of them, in just one book) it aims to give a fairly general introduction to each one and demonstrate some of it’s particular characteristics, leaving the reader to hit Google in search of further documentation etc.  As well as giving you a broad exposure to a range of different programming paradigms, this book is intended to equip you to pick up the elements of a new language quickly and efficiently.

The seven languages covered by the book are (in order of appearance): Ruby, Io, Prolog, Scala, Erlang, Clojure and Haskell.  I’ve just started working on the Io section of the book and, if all goes according to plan, I intend to write a blog post about each of the languages once I’ve finished the relevant section.  For now, I’ll focus on Ruby.

Of all the languages in the book, Ruby is the only one that I already have a reasonable amount of familiarity with (although I’ve played a bit with Prolog and I’m fairly sure I had a quick look at Haskell once too).  I have already written a handful of, admittedly fairly small, real world programs (as opposed to tutorial exercises) in Ruby, which could reasonably be described as a general purpose scripting language.  At the moment, I would probably describe it as my second choice go-to language for general purpose programming; Python would be my first choice, largely because I’ve been using it for somewhat longer, am a lot more familiar with it and have a bit more dead-tree-format reference material for it; they are both excellent languages (each with several definite pros and cons relative to the other) and I’d rather get reasonably proficient at both than concentrate exclusively on either one of them.

The particular (though by no means unique) feature of Ruby that Tate focuses on in his book is metaprogramming, which (he says) “means writing programs that write programs”.  I’ve come across the idea before, mainly in my studies of Lisp (although I didn’t quite get as far as doing any metaprogramming for myself).  I still don’t feel that I entirely grok the concept, but I think it’s definitely something worth exploring further and it appears that Ruby will be a good vehicle for it.

A nice, slightly quirky feature of the book is that Tate likens each programming language to a film (or TV series in one case) character that he feels epitomises similar characteristics.  In the case of Ruby, the chosen film is Mary Poppins.  Tate describes the titular character (and hence also Ruby) as “sometimes quirky, always beautiful, a little mysterious and absolutely magical.” [Actually, to be pedantic, he applies that description to Ruby and then says “Think Mary Poppins.]

I fairly recently started (or rather restarted, as it’s an old project I started and abandoned several years ago) a programming project using Python, so I’ll probably try to get it finished in that language.  However, the next time I have a real-world problem to solve with a script, there’s a good chance I’ll reach for Ruby.