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.



Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: