.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Declarative Programming
Introduction to Prolog
Geraint A. Wiggins
Professor of Computational Creativity
Department of Computer Science
Vrije Universiteit Brussel
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
General module information
▶ Module consists of 13 2-hour lectures and 13 tutorials
▶ Lectures at 8am on Friday of Term 1
▶ All course materials will be made available on the Web
(Pointcarré & ai.vub.ac.uk)
▶ There are course notes, giving a quick introduction to
Prolog
▶ There is an auto-tutorial, which you can use on line to
get you started
▶ Please note: this module starts slowly – but it SPEEDS
UP!
ai.vub.ac.uk
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
General module information
▶ There will be 5 practical exercises, one of which (5)
counts for 50% of the course mark
▶ All practical exercises are compulsory, and 2-5 form the
module’s tutorial work
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Books
▶ The photocopied notes are necessary but NOT
SUFFICIENT
▶ The recommended book is
Programming in Prolog
W. Clocksin & C. S. Mellish
▶ Another good book is
The Art of Prolog
L. Sterling & E. Shapiro
▶ You should also refer to the lecture slides on the Web
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Introduction to Prolog
▶ Programming languages are of two kinds:
▶ procedural (BASIC, ForTran, C++, Pascal)
▶ declarative (Lisp, Prolog, ML, Miranda, Haskell)
▶ In procedural programming, we tell the computer how to
solve a problem
▶ In declarative programming, we tell the computer what
problem we want solved
▶ (However, in Prolog, we are often forced to give clues as
to the solution method)
▶ Declarative languages are usually either functional (e.g.,
Lisp) or logical (e.g., Prolog)
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Basic Elements of Prolog
▶ We give a database of facts:
▶ some are always true;
▶ some are dependent from others
▶ To run a program, we ask questions about the facts.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Prolog in English
John is the father of Jim.
Jane is the mother of Jim.
Jack is the father of John.
Person 1 is a parent of Person 2 if
Person 1 is the father of Person 2 or
Person 1 is the mother of Person 2.
Person 1 is a grandparent of Person 2 if
some Person 3 is a parent of Person 2 and
Person 1 is a parent of Person 3.
Who is Jim’s father?
Is Jane the mother of Fred?
Is Jane the mother of Jim?
Does Jack have a grandchild?
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Prolog in Prolog
father( john, jim ).
mother( jane, jim ).
father( jack, john ).
parent( Person1, Person2 ) :-
father( Person1, Person2 ).
parent( Person1, Person2 ) :-
mother( Person1, Person2 ).
grandparent( Person1, Person2 ) :-
parent( Person3, Person2 ),
parent( Person1, Person3 ).
?- father( Who, jim ).
?- mother( jane, fred ).
?- mother( jane, jim ).
?- grandparent( jack, _ ).
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Using Prolog (1)
▶ First, write your program
▶ Then, type it into a Unix file, with a .pl extension
▶ Then, type
swipl
▶ Then, “consult” your file (omitting the .pl):
[yourfilename].
▶ Then, you can ask your questions.
▶ Sometimes, you can get multiple answers, by typing ‘;’
▶ If you edit your program file (eg to correct something), be
sure to consult it again afterwards!
▶ Or you can use the menu-driven version
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Using Prolog (2)
▶ To exit from Prolog, type
halt.
or
Control/D
▶ The Prolog comment characters:
▶ Single line comments:
% This is a comment
This not a comment, but an error
▶ Multiple line comments:
/* This is a multi-line comment
which must be closed with a */
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Anatomy
▶ Prolog programs consist of predicate definitions
– like parent/2
▶ Predicate definitions consist of clauses
parent( P1, P2 ) :- mother( P1, P2 ).
▶ Clauses consist of a head
– e.g., parent( P1, P2 )
and sometimes a body
– e.g., mother( P1, P2 )
▶ A clause head has a predicate name and sometimes some
arguments
▶ The body is a collection of literals, which match clause
heads.
▶ Arguments consist of terms, which are similar in form to
literals.