Getting Started with Prolog
Prolog is rather unusual when compared to other programming languages. Rather than describing a sequence of steps as in a procedural language, it’s a declarative language that uses formal logic as its primary metaphor. Prolog is used predominantly in academic settings for language research and artificial intelligence. That being said it is a general purpose programming language and used in commercial settings where the system must be able to reason about complex data like logistics, language processing and data mining. Some examples can be found at SICStus’s Website.
Although it’s a general purpose programming language, generally other languages are used to do GUI and interface coding. There are GUI packages available though, so I’ll give one of them a try in Week 4. With a little luck we’ll see Prolog’s strength in Weeks 2 and 3.
What to Expect
When this article is complete:
- You will have:
- An installation of SWI-Prolog
- A program that’ll say “Hello World”
- You will know:
- How to compile a program in SWI-Prolog.
- How to use the interactive interpreter to make logical queries from a logic base.
- That Socrates is mortal.
Files Used in this Project
- It works on Windows. (All projects on this blog do)
- It has a GUI toolkit. (necessary for week 4)
- It’s free, under the LGPL. (Please remember to include a link to SWI’s page if you use it for your applications)
- It has an interactive interpreter. (I like interactive interpreters)
- Download the latest version of SWI-Prolog from their download page.
- Run the installer accepting all the defaults.
"C:\Program Files\pl\bin"to your PATH in environment variables. How to change your path.
helloworld.txt (Rename to helloworld.pl):
% Say Hello Prolog main :- write('Hello World!'), nl, halt.
In Prolog comments are preceded by a
Prolog programs are composed of clauses separated by periods. A clause is divided into the head and the body with the format
<head> :- <body> or just
<head>. When just the head is given it’s treated like
<head> :- true. The body of a clause is a statement composed of either a term, or sequence of terms composed by operators and ending in a period.
In Prolog a
, is equivalent to boolean
and. In the above, should any of the statements return false, then the others will not be executed. Each of the terms:
write('Hello World!'): Writes “Hello World!” to the screen.
nl: Writes a new line to the screen.
halt: Causes Prolog to shutdown.
Running/Compiling Your Code
SWI-Prolog has an interactive interpreter, an interpreter and a compiler.
The Interactive Interpreter
A session with the interactive interpreter looks like:
C:\>swipl Welcome to SWI-Prolog (Multi-threaded, 32 bits, Version 5.10.2) Copyright (c) 1990-2010 University of Amsterdam, VU Amsterdam SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. Please visit http://www.swi-prolog.org for details. For help, use ?- help(Topic). or ?- apropos(Word). 1 ?- write('Hello World!'), nl. Hello World! true. 2 ?- halt. C:\Documents and Settings\fberthold\My Documents\My Dropbox\VirtuousProgrammer\Prolog\1 - Getting Started>
You can also interact with the interpreter through SWI-Prolog’s IDE. I’m a command line junky so haven’t played with it too much yet, but you can find it in the Start menu.
To run your programmer through the interpreter type:
swipl -g main helloworld.pl
Where “helloworld.pl” is your Prolog program, commonly called a logicbase. “main” is what you’ve declared to be the main clause in your program.
You can compile it with:
swipl -g main -o helloworld.exe -c helloworld.pl
This will generate a windows executable “helloworld.exe” by compiling “helloworld.pl”.
Prolog’s Hello World
It’s a little hard to do Prolog justice from this first example, because the primary function of Prolog is to describe and evaluate relationships, which don’t come up in ‘hello world’. To give a little more flavor of what Prolog does, here’s a simple relationship:
socrates.txt (Rename to socrates.pl):
% All men are mortal, Socrates is a man. mortal(X) :- man(X). man(socrates).
Here we have two assertions. That men are mortal and that Socrates is a man. We can how use Prolog to draw a couple of conclusions. The first step is to load “socrates.pl” into the interactive interpreter with:
Here’s a sample session in which we can find that Socrates is mortal and that if you are mortal, you are Socrates (Add more facts to the logicbase if you want more mortals):
1 ?- mortal(socrates). true. 2 ?- mortal(X). X = socrates.
Prolog’s single data type is a
term. Prolog terms can be(with examples from the above code):
- atoms: socrates
- numbers: no example in the above code
- variables: X
- compound terms: man(socrates)
manis called a functor
Prolog is unlike any programming language I’ve worked with to date. Not only is it’s model entirely different, but it’s syntax was designed before it was obvious Algol style syntax was going to predominate, so it’s syntax is more influenced by it’s logical roots than what is currently considered normal looking syntax.
Next week I’ll be putting Prolog through it’s paces. The fizzbuzz problem should be interesting to solve. It feels like it should naturally be able to deal with conditional statements, but it doesn’t have any direct looping facilities other than recursion. Unit tests on the other hand also feel like a natural fit.
- Prolog on Wikipedia
- Prolog Syntax and Semantics on Wikipedia
- Adventures in Prolog: A tutorial that uses writing an text adventure game to teach Prolog.
- Introduction to Prolog: A simple, but clear tutorial on Prolog.
- Despite being based on an entirely different semantic set than other programming languages, Prolog is Turing complete.
- Prolog was created at the University of Aix-Marseille by Alain Colmerauer and Phillipe Roussel, collaborating with Robert Kowalski of the University of Edinburgh in 1972.
- If you enter
X.at the interactive interpreter, SWI-Prolog gives Douglas Adams fans the answer.