Prolog

My Experience Learning Prolog

I first heard about the Prolog programming language in the book Seven Languages in Seven Weeks. The book, as promised, introduces the reader to the syntax and paradigms of seven different languages. What attracted me to Prolog was its similarity to constraint programming (programs are defined by and solved using their constraints), its similarity to SQL (data-focused and declarative), and its recursive nature.

Normally, I would learn a new language by playing with it, but Prolog was too foreign and intimidating, so I looked for a more structured learning approach. I did some research on recommended first textbooks to work through and settled on The Art of Prolog (AoP). This was a terrible decision. AoP is structured in four parts: logic programming, Prolog, advanced Prolog, and applications. Over two weeks, I completed Part I and worked through all the exercises using pen and paper. I was inspired by the beauty and simplicity of logic programs, and was excited to start Part II, which introduces the Prolog language and its shortcomings as a logic programming language. The exercises in Part II were essentially the same as in Part I, except using Prolog instead of pure logic. The solutions were ugly. I felt disillusioned at being introduced to beauty and then being denied it. I put AoP away and gave up on Prolog.

TALE-SPIN in Prolog

TALE-SPIN

TALE-SPIN1 is an AI program developed in 1977 by James R. Meehan. It works by using a knowledge base of actions to solve and modify goals. As actions are performed, they output text that form a story. In the words of the author:

TALE-SPIN is a problem solver, top-down and goal-directed. Its output may be regarded as a trace through problem-solving procedures.

Meehan rejected previous approaches with clean formulations that involved defining “one kind” of knowledge but sometimes resulted in bizarre stories. He instead defined and incorporated multiple kinds of knowledge to create more natural stories.

Chopsticks in Prolog

The Game

Chopsticks is a simple game of many variations played by children and college students. The variation I play is:

  1. two players start by each sticking both index fingers out to represent a count of one on each hand
  2. players alternate turns until someone loses by having a count of zero on both hands
  3. on a player’s turn, he must either:
    1. use one of his hands with positive count $H1$ to tap one of the other player’s hands $H2$. The other player updates the count on $H2$ to $(H1+H2) \bmod 5$ by changing the number of fingers sticking out
    2. redistribute aka “split” his fingers in one of two ways: $(4,0)–>(2,2)$ or $(2,0)–>(1,1)$

For example, we can visualize the first turn of the game like so: