Quick Prolog Primer

Overview: Prolog is a very fun and useful language for exploring declarative programming. Aside from its long history of academic (research and teaching) use, it has actually been used quite extensively in real production, most notably as the basis for many high-performance parallel processing initiatives (e.g. Parlog). There are many different implementations of Prolog out there; among these, SWI-Prolog is arguably the most popular and (!!) is available for all common platforms, which makes it convenient for the purposes of a class like this.

The documentation available for SWI-Prolog is IMMENSE, with not only an extensive list of manuals, tutorials, and other links on the SWIPL home page (http://www.swi-prolog.org), but also many many forums online dedicated to discussion centered around swipl. As upper division students, you should be more than capable of piecing together the knowledge you need for a project by a focused exploration of these resources. Nonetheless, this page is an attempt to just get you started and give you a nudge in the right direction. More generally, the "Getting started" tutorial linked off the SWIPL home page covers what I'm going to say here in much more detail.

Installing SWIPL.

The first thing you'll need to do is to get SWIPL installed on your machine. Go to the SWIPL download page and grab whatever the latest stable version is for your platform. Depending on your platform, what exactly this process looks like might be slightly different. For Mac, they now have it packaged as a GUI application, meaning you just drag it into your "Applications" folder and run it, which then pops up a window with a Prolog command line. For some other platforms, you might run an installer (Mac used to be that way) and it'll install the "swipl" binary somewhere in your system path. Each downloadable package comes with a nice README file, so do whatever it says to get going!

Ok, so let's assume that you've done whatever needs doing and are now looking at a Prolog command prompt, which looks like " ?- ". That means it's up and waiting for you to ask it to prove something! So let's get programming...Now you can just type "swipl" at the command line to start up Prolog inside your terminal window. That's it. No IDE, no fancy GUI. Prolog is old school: the interpreter fires up right there in the terminal window and waits for you to do something!

How to "program" in Prolog.

Now that you've got Prolog installed and running, the edit-test development cycle is simple: fire up your favorite text editor. Yup, you read correctly: No IDE, no fancy GUI. Prolog is old school: it just gives you and interpreter; you edit code in your text editor and load it into the interpreter to run it. Ideally your text editor is one with a Prolog "mode", so that it will color predicates and all that pretty stuff to help you out, but any basic text editor will do (see below for comments on what *real* programmers use). So now you write your code in the text editor, save it as a ".pl" file. Then you load that file (containing your facts and rules) into Prolog, and start posing queries. Repeat as necessary. Here are some detailed pointers:

Ok, that should be enough to help you avoid the main "gotchas". Your next move should be to do the nifty Learn Prolog NOW Tutorial linked from the SWIPL home page. Do a few more tutorials, bookmark the prolog online manual, and you should be well on your way to being a Prolog expert!

How to see what Prolog is doing.

We have made the claim that Prolog implements a pretty close version of the backward-chaining FOL proof algorithm detailed in Chapter 9...which in turn just implements the basic backward-chaining proof concept introduced for prepositional logic in Ch7. But how do you know it's actually doing that? More generally, how can you see what Prolog is doing....not just to understand it, but to debug your Prolog programs? In fact, there are a number of ways to make Prolog be "verbose" and show you what it's doing. First and foremost among these is "trace.", which shows every predicate invoked as Prolog tries to prove something. Read all about Trace and other tools to look under the hood of Prolog on the SWIPL manual page regarding tracing and debugging.

Comments on text editor for Prolog

The big popularity of IDEs these days has made much of the discussion about text editors moot, seeing as an IDE will generally have a text editing solution built in. But Prolog is still one of those areas of CS that's "old school", i.e., there isn't enough mass usage to warrant creating a fancy special IDE (like Dr. Racket for Scheme) for Prolog. And it's not all that vital: Prolog is super simple to run using the basic command-line interpreter plus any old text editor. It can be nice, however, to have your editor help you out a little with some code coloring (comments, predicates, etc. and other goodies).

What I use for most of my text editing is GNU Emacs (or Xemacs is fine too). You might have heard of emacs and even tried it out (command-line version comes built in for all unix/linux/mac systems)...and decided "ewww, it's fully command-line based! Where's my fancy GUI?" Hey, no problem, I'm that way too these days! Fortunately, there are plenty of great open-source GUI wrappers that wrap standard emacs into a nice GUI interface --- that's where GNU Emacs and Xemacs help bridge the gap. What I use is Aquamacs, which works super on Mac...but there are tons of other GUI emacs editors for Windows, Linux, whatever. Because they are all *wrappers* for standard emacs, the way to get them to Prolog mode will be fairy identical. "Prolog mode?", you say? Yes, Emacs looks at the extension of the text file your are editing, decides what kind of file that is, and puts you in the right mode, i.e., with appropriate coloring and other support for that language.

Depending on what version/brand of emacs editor you install, you very well might have prolog mode installed by default. Just open a "something.pl" file in the editor and see if it says "prolog" for the mode at the bottom. If so, you're done, happy editing. Often there is no prolog mode defined in the standard distro though, and you'll need to add it. This is no problem: Emacs is written in lisp...and so anyone can extend it at will by adding more code...on the fly even! And many people over the years have perfected a sweet "prolog mode" file for emacs. Just go to https://bruda.ca/emacs/prolog_mode_for_emacs , download the prolog.el file, and follow the instructions for installing it in the right place and making sure prolog knows where to find it. Simple!

Do yourself a favor and learn to use emacs. If nothing else, you'll be able to impress all the geeks drinking with you about how much of a "true programmer" you really are!