Virtuous Programmer Adventures of an Autodidact

3Jan/110

Prolog 1/4: Getting Started

Getting Started with Prolog

The Plan

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

SWI-Prolog

There are a large variety of implementations of Prolog. From these I chose SWI-Prolog because:

  • 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)

Installation

  1. Download the latest version of SWI-Prolog from their download page.
  2. Run the installer accepting all the defaults.
  3. Add "C:\Program Files\pl\bin" to your PATH in environment variables. How to change your path.

The Code

helloworld.txt (Rename to helloworld.pl):

  1. % Say Hello Prolog
  2. main :-
  3.   write('Hello World!'), nl,
  4.   halt.
  5.  

Comments

In Prolog comments are preceded by a %.

Clause Structure

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:

  1. C:\>swipl
  2. Welcome to SWI-Prolog (Multi-threaded, 32 bits, Version 5.10.2)
  3. Copyright (c) 1990-2010 University of Amsterdam, VU Amsterdam
  4. SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
  5. and you are welcome to redistribute it under certain conditions.
  6. Please visit http://www.swi-prolog.org for details.
  7.  
  8. For help, use ?- help(Topic). or ?- apropos(Word).
  9.  
  10. 1 ?- write('Hello World!'), nl.
  11. Hello World!
  12. true.
  13.  
  14. 2 ?- halt.
  15.  
  16. C:\Documents and Settings\fberthold\My Documents\My Dropbox\VirtuousProgrammer\Prolog\1 - Getting Started>
  17.  

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.

The Interpreter

To run your programmer through the interpreter type:

  1. swipl -g main helloworld.pl
  2.  

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.

The Compiler

You can compile it with:

  1. swipl -g main -o helloworld.exe -c helloworld.pl
  2.  

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):

  1. % All men are mortal, Socrates is a man.
  2. mortal(X) :- man(X).
  3. man(socrates).
  4.  

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:

  1. swipl socrates.pl
  2.  

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. 1 ?- mortal(socrates).
  2.  
  3. 2 ?- mortal(X).
  4. X = socrates.
  5.  

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)
    • where man is called a functor

Summary

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.

Coming Up

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.

Resources

Trivia

  • 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.

Posted by Frank Berthold

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

No trackbacks yet.