Glomus - Prolog Help
SWI-Prolog
This course will use a non-commercial version of Prolog called SWI-Prolog. SWI-Prolog is available for both Windows9x/NT and for Linux. You can visit the SWI-Prolog site to download and install it on your machine if you wish.
Online Prolog Resources
Here are some Prolog resources:
Prolog clauses are often called either facts or rules.
A fact is a clause of the form:
A rule is a clause of the form we saw above,
mortal(Thing):- man(Thing), which has unbound, free
variables.
Rules are used to describe how to infer additional facts.
It is important to note that in Prolog, any term beginning with a
capital letter is an unbound variable. Thus we use uppercase
Thing in the clauses above to denote a variable while
lowercase strings such as socrates, mortal, and
true denote "ground" terms - terms that are not variables.
It is important to understand that Prolog is, fundamentally, a simple
pattern-matching engine. For the most part there are no data types,
there are no flow-of-control constructs, etc.
Thus, a Prolog program consisting of the following:
Here is a Prolog program that contains a set
of rules and facts about a portion of my family tree. You can download
this program and use it to test Prolog. I will use this program
for demonstration purposes below.
At the ?- prompt, you enter goals for Prolog
to attempt to prove. The term goals is technically correct,
though there are many sorts of goals you might need to
enter such as asking for help, loading a file, or trying to solve
a particular query. Note that a period (.) is used as the terminator
character in Prolog - Prolog does not evaluate what you have typed
until it encounters a period at the end of your input.
Typically, the first thing you will do is to "consult" a file (i.e.,
load it) that contains a set of Prolog facts and rules. You do this
using the
Prolog includes a large set of built-in predicates such
as consult/1, help/1, etc. See the SWI-Prolog
documentation for more
information on the built-in predicates. You can use the
built-in help/1 predicate to get online help for all
built-in predicates:
To illustrate some particular interactions with prolog,
consider the following sample session. Comments are provided using
the comment syntax, /* ... */ and the number in the comment
corresponds to the notes following this example:
Notes:
Getting Started with Prolog
Prolog "Programs"
A Prolog "program" consists of one or more text files that contain
Prolog predicate clauses which are roughly based on Horn-clauses.
Prolog clauses consist of a head and a body.
The syntax for a Prolog clause is
head :- body
which can be read
head is true if body is true
or alternatively,
body implies the head
For example
mortal(Thing):- man(Thing) declares that anything which is a
man is mortal (Prolog clauses are universally
quantified, thus this clause would read for all Thing such that
man(Thing) is true, mortal(Thing) is true).
head :- true
which states that the head of the clause is asserted to be
true - the truth of the head does not depend on any other clauses.
In Prolog, we could write facts as:
man(socrates):- true, but
Prolog allows us to dispense with the obvious, and merely write:
man(socrates).
as a fact.
essentially declares four terms, two of which are
predicates - man/1 and dog/1, and two of which
are atoms - socrates and lassie. It is up to
the programmer and user to determine whether these predicates and
assignments make any sense.
man(socrates).
dog(lassie).
Running Prolog
The Prolog interpreter is a simple text-oriented shell.
To start the Prolog interpreter:
You will see a "banner" and prompt:
Welcome to SWI-Prolog (Version 3.2.8)
Copyright (c) 1993-1998 University of Amsterdam. All rights reserved.
For help, use ?- help(Topic). or ?- apropos(Word).
?-
consult/1 predicate as your goal (Note:
Prolog predicates are usually written as "name"/"# parameters" so
consult/1 is the name of the consult predicate
that accepts one parameter - in this case the name of the file to
read). For example, this file contains a
bit of my family history as a set of Prolog facts, and some
Prolog rules for inferring relationships. You can download this file
and then, at the Prolog prompt, you can load this set of facts and
rules:
An alternative syntax for consulting files is the more traditional:
?- consult(family).
which has the same effect. Consulting a file loads and compiles the
facts and rules.
?- ['family'].
would give you help on the consult predicate. You can be more
specific and request help on the consult/1 predicate as well.
?- help(consult).
?- ['family']. /* 1. Load a program from a local file*/
yes
?- listing(ancestor/2). /* 2. List ancestor/2 facts and rules to the screen*/
ancestor(A, B) :-
parent(A, B).
ancestor(A, B) :-
parent(C, B),
ancestor(A, C).
Yes
?- ancestor(Who,david). /* 3. Find an ancestor of david */
Who = richard
Yes
?- ancestor(Who,david), married(Who,abelone). /* 4. Conjunction of goals */
Who = augie
Yes
?- sibling(david,Sibling). /* 5. Multiple solutions to a goal */
Sibling = beth ;
Sibling = paul ;
Sibling = beth ;
Sibling = paul ;
No
?- halt. /* 6. Return to OS */
family.pl contains facts about my family history
as well as predicates that can be used to infer additional
facts (e.g., sibling/2, cousin/2, ancestor/2, parent/2).
Note: omitting the period is a common mistake that merely
results in Prolog prompting you for more goals with the
continuation prompt |. Simply enter the period at
any prompt to get Prolog moving.
halt. at the prompt) which always
succeeds and returns the user to the operating system.