It's me!

Jezen Thomas

Founder of NewBusinessMonitor

Programmer at Zimpler

Prolog Day One

Beginning Prolog is quite exciting for me. It’s the first declarative programming language I’ve looked at, and the syntax is far removed from anything I’ve worked on previously. I like the idea that you don’t describe the solution to a problem, rather you describe the problem in pure logic.

Setting up my environment was quick and painless. The compiler can be installed with Homebrew by doing brew install gnu-prolog, and we can start the interpreter by doing gprolog.

Prolog files have a .pl extension, and I noticed that Vim interprets this extension as a Perl file. A simple workaround is to begin a Prolog file with a comment, denoted with %. Magic, right?

  1. Things to find
  2. Some free Prolog tutorials
  3. A support forum
  4. An online Prolog reference
  5. Things to do
  6. Create a knowledge base
  7. Query the knowledge base
  8. The anonymous variable
  9. Thoughts

Things to find

Some free Prolog tutorials

Finding tutorials for Prolog is easy. Most of them seem to exist in university publications, which I imagine is probably a good thing. Documentation for technology used by more serious circles has always been more comprehensive in my experience. Compare that to documentation written by ‘Web Designers’, which sometimes amounts to “Use jQuery! It’s amazing and does all the things!”.

Here’s a few I found:

I also managed to find a collection of Ninety-Nine Prolog Problems.

Jay-Z, famed rapper and software developer.

Jay-Z, famed rapper and software developer.

A support forum

There are ~17k topics in this Prolog Google Group, there is a discussion forum on PrologCentral.com, and there is also a Prolog mailing list.

An online Prolog reference

The version of Prolog I’m using is Gnu Prolog 1.4.4, and the reference for it can be found here.

Things to do

Create a knowledge base

Make a simple knowledge base. Represent some of your favourite books and authors.

Rather than books and authors, I’ve gone for a music theme with albums and recording artists. The interesting thing about writing facts in Prolog is that it reads almost like the English language.

One of my favourite artists is Björk, but it looks as though Prolog doesn’t support accented characters like ö out of the box so the wonderful Ms Guðmundsdóttir gets excluded.

% albums.pl

recorded(nine_inch_nails, year_zero).
recorded(nine_inch_nails, hesitation_marks).
recorded(wu_tang_clan, the_w).
recorded(ludovico_einaudi, islands).

Query the knowledge base

Find all books in your knowledge base written by one author.

To query our knowledge base of recording artists and albums, we start the interpreter and enter ['albums']. at the prompt. If you forget to mark the end of the statement with a full-stop (.), nothing will happen and the interpreter won’t tell you anything, which is quite frustrating.

The knowledge base quickly compiles, and awaits our query. If we ask recorded(nine_inch_nails, Album)., Prolog returns us the first result, year_zero. Hit a to view all results.

The anonymous variable

Make a knowledge base representing musicians and instruments. Also represent musicians and their genre of music. Find all musicians who play the guitar.

I could create two sets of facts — one for the relationship between musicians and their instruments, and another for the same musicians and the musical genre they’re most commonly associated with — but the queries would be essentially the same as what we saw in the previous problem. This time, we’ll write the facts with a third predicate.

% musicians.pl

plays(vinnie_colaiuta, drums, jazz).
plays(michael_landau, guitar, jazz).
plays(danny_carey, drums, metal).
plays(adam_jones, guitar, metal).

To find all the musicians who play the guitar, I first tried plays(Who, guitar). but the interpreter simply threw an error. I went in search of some sort of wildcard for Prolog, and dominikh on the ##prolog IRC channel recommended the anonymous variable. With this special symbol, we can run the query plays(Who, guitar, _). and surely enough, Michael Landau and Adam Jones are found.

Thoughts

So far, the concept seems intuitive enough to me. I don’t think today’s exercises are representative of the true power of Prolog, but it was a nice, gentle introduction.

I still have many questions echoing around in my head, all along the lines of “But what if I want to do this…”, and “How would I do that…”. The documentation I’ve read on Prolog so far has mostly been quite dry and academic, and I’d appreciate a more human approach.