Chapter 4

Prolog

Sally Dibbs, Dibbs Sally. 461-0192.

Raymond

Ah, Prolog. Sometimes spectacularly smart, other times just as frustrating. You’ll get astounding answers only if you know how to ask the question. Think Rain Man.[7] I remember watching Raymond, the lead character, rattle off Sally Dibbs’ phone number after reading a phone book the night before, without thinking about whether he should. With both Raymond and Prolog, I often find myself asking, in equal parts, “How did he know that?” and “How didn’t he know that?” He’s a fountain of knowledge, if you can only frame your questions in the right way.

Prolog represents a serious departure from the other languages we’ve encountered so far. Both Io and Ruby are called imperative languages. Imperative languages are recipes. You tell the computer exactly how to do a job. Higher-level imperative languages might give you a little more leverage, combining many longer steps into one, but you’re basically putting together a shopping list of ingredients and describing a step-by-step process for baking a cake.

It took me a couple of weeks of playing with Prolog before I could make an attempt at this chapter. I used several tutorials as I ramped up, including a tutorial by J. R. Fisher[8] for some examples to wade through and another primer by A. Aaby[9] to help the structure and terminology gel for me, and lots of experimentation.

Prolog is a declarative language. You’ll throw some facts and inferences at Prolog and let it do the reasoning for you. It’s more like going to a good baker. You describe the characteristics of cakes that you like and let the baker pick the ingredients and bake the cake for you, based on the rules you provided. With Prolog, you don’t have to know how. The computer does the reasoning for you.

With a casual flip through the Internet, you can find examples to solve a Sudoku with fewer than twenty lines of code, crack Rubik’s Cube, and solve famous puzzles such as the Tower of Hanoi (around a dozen lines of code). Prolog was one of the first successful logic programming languages. You make assertions with pure logic, and Prolog determines whether they are true. You can leave gaps in your assertions, and Prolog will try to fill in the holes that would make your incomplete facts true.