Table Of ContentPROLOG
PROGRAMMING
FORA RTIFICIAL
INTELLIGENCE
INTERNATIONAL COMPUTER SCIENCE SERIES
Consultinge ditors A D McGettrick University of Strathclyde
J van Leeuwen University of Utrecht
OTHER TITLES IN THE SERIES
Programmingin Ada (2nd Edn.) J G P Barnes
ComputerS cienceA pplied to BusinessS ystems M J R Shavea nd K N Bhaskar
SoftwareE ngineering( 2nd Edn.) I Sommerville
A StructuredA pproach to FORTRAN 77 Programming T M R Ellis
The CambridgeD istributed ComputingS ystem R M Needhama nd A J Herbert
An Introduction to NumericalM ethodsw ith Pascal L V Atkinson and P J Harlev
The UNIX System S R Bourne
Handbook of Algorithms and Data Structures G H Gonnet
Office Automation: Concepts,T echnologiesa nd Issues R A Hirschheim
Microcomputersin Engineeringa nd Science I F Crainea nd G R Martin
UNIX for Super-Users E Foxley
SoftwareS pecificationT echniques N Gehania nd A D McGettrick( eds.)
Introduction to Expert Systems P Jackson
Data Communicationsfo r Programmers M Purser
Local Area Network Design A Hopper, S Templea nd R C Williamson
Modula-2:D scipline & Design A H J Sale
PROLOG
PROGRAMMING
FORA RTIFICIAL
INTELLIGENCE
lvanB ratko
E.K ardeUlj niversi.t yJ .S tefanIn stitute
Yugoslavia
YV
ADDISON-WESLEY
PUBLISHING
COMPANY
WokinghamE,n gland. R eadingM, assachuset.t isll enloP arkC, alifornta
DonM ills,Ontar.i oA msterdam. S ydney' S ingapor.e T okyo
Madrid. Eogota. Santiag.oS anJ uan
@1986A ddison-WesleyP ublishersL imited
o
1986A ddison-WeslePy ublishingC ompany,I nc.
All rights reserved.N o part of'this publicationm ay be reproduced,s toredi n a
retrieval system,o r transmittedi n any form or by any means,e lectronic,m echanical,
photocopying,r ecording,o r otherwise,w ithout prior written permissiono f the
publisher.
Cover graphicb y kind permissiono f Dicomed (UK) Ltd.
Phototypesebt y Computerse(tM FK) Ltd., Ely, Cambs.
Printedi n Great Britain bv CommercialC olour Press.
British Library Cataloguingi n Publication Data
Bratko. Ivan
Prolog programmingf or artificial intelligence.
1. Artificial intelligence-Data processing
2. Prolog( Computerp rograml anguage)
I. Title
006.3'02855133 Q336
ISBN 0-20r-14224-4
Library of CongressC ataloging-in-PublicatioDna ta
Bratko, Ivan.
Prolog programmingf or artificial intelligence.
Includesi ndex.
1. Artificial intelligence-Data processing.
2. Prolog( Computerp rograml anguage) I. Title.
Q336.874 1986 006.3 86-1092
ISBN 0-20t-1.4224-4
ABCDEF 89876
i,
To Branka, Andrej and Tadej
\
Foreword
In the Middle Ages, knowledgeo f Latin and Greek was essentialf or all
scholarsT. he one-languagsec holarw asn ecessarilay h andicappesdc holarw ho
lacked the perceptiont hat comesf rom seeingt he world from two points of
view. Similarly,t oday'sp ractitionero f Artificial Intelligenceis handicapped
unlessth oroughlyf amiliarw ith bothL isp andP rolog,f or knowledgeo f thet wo
principal languageso f Artificial Intelligencei s essentialf or a broad point of
view.
I am dedicatedt o Lisp, havingg rownu p at MIT whereL isp wasi nvented.
NeverthelessI, can never forget my excitementw hen I saw my first Prolog-
stylep rogrami n action.I t wasp art of Terry Winograd'sf amousS hrdlus ystem,
whoseb locks-worldp roblem solver arrangedf or a simulatedr obot arm to
moveb locksa rounda screens, olvingi ntricatep roblemsin responseto human-
specifiedg oals.
Winograd'sb locks-worldp roblems olverw asw ritten in Microplanner,a
languagew hichw e now recognizea sa sort of Prolog.N everthelessin, spiteo f
the defectso f Microplanner, the blocks-worldp roblem solverw as organized
explicitlya roundg oals,b ecausea Prolog-stylela nguagee ncouragepsr ograrn-
mers to think in terms of goals.T he goal-orientedp roceduresfo r grasping,
clearing,g ettingr id of, moving,a nd ungraspingm adei t possiblef or a clear,
transparentc, oncisep rogramt o seema mazinglyin telligent.
Winograd'sb locks-worldp roblems olverp ermanentlyc hangedth e way I
think aboutp rograms.I evenr ewrotet he blocks-worldp roblems olveri n Lisp
for my Lisp textbook becauseth at programu nalterablyi mpressedm e with the
powero f the goal-orientedp hilosophyo f programminga nd the fun of writing
goal-orientedp rograms.
But learninga bout goal-orientedp rogrammingt hroughL isp programsi s
like readingS hakespearien a languageo thert hanE nglish.S omeo f the beauty
comest hrough,b ut not asp owerfullya si n the original.S imilarly,t he bestw ay
to learn about goal-orientedp rogrammingi s to read and write goal-oriented
programsin Prolog,f or goal-orientedp rogrammingis whatP rologi s all about.
In broader terms, the evolution of computer languagesis an evolution
awayf rom low-levell anguagesi,n which the programmers pecifiesh ow some-
thing is to be done, toward high-levell anguagesi,n which the programmer
specifiess imply what is to be done. With the developmento f Fortran, for
example,p rogrammersw ere no longerf orced to speakt o the computeri n the
procrustianl ow-levell anguageo f addresseas nd registers.I nstead,F ortran
FOREWORD
programmersc oulds peaki n their own languageo, r nearlys o,u singa notation
that made only moderatec oncessiontso the one-dimensional8, 0-column
world.
i
Fortran and nearly all other languagesa re still how-type languages,
however.I n my view,m odernL isp is the championo f thesela nguagesio, t iisp
in its CbmmonL isp form is enormouslye xpreriiu", but how to do somethingis
still what the Lisp programmeris allowedt o be expressivea bout.P rolog,o n
the other hand, is a languaget hat clearly breaks away from the how-type
languagese, ncouragingth e programmert o describes ituationsa nd problems,
not the detailedm eansb y which the problemsa re to be solved.
Consequently,a n introduction to Prolog is important for all studentso f
ComputerS ciencef,o r therei s no betterw ay to seew hat the notion of what-
type programmingis all about.
In particular,t he chapterso f this book clearlyi llustratet he difference
betweenh ow-typea nd what-typet hinking. In the first chapter,f or example,
the differencei s illustrated through problems dealingw ith family relations.
The Prolog programmers traightiorwardlyd escribesth e grandfatherc oncept
in explicit, natural terms: a grandfatheri s a father of a parent. Here is the
Prologn otation:
grandfather(X, Z) :- father( X, Y), parent(Y , Z).
OnceP rologk nowsw hat a grandfatheris , it is easyt o aska questionw: ho are
Patrick'sg randfathersf,o r example.H ere againi s the Prologn otation,a long
with a typicala nswer:
?- grandfather(X , patrick).
X = james;
X : carl
It is Prolog'sj ob to figure out how to solvet he problemb y combingt hrough a
databaseo f knownf athera ndp arentr elations.T he programmers pecifieso nly
what is known and what questioni s to be solved.T he programmeri s more
concernedw ith knowledgea ndl essc oncernedw ith algorithmsth at exploitt he
knowledge.
Given that it is importantt o learn Prolog, the next questioni s how. I
believe that learning a programming language is like learning a naturaf
languagein manyw ays.F or example,a referencem anuali s helpfuli n learnin$
a programmingla nguagej,u st as a dictionaryi s helpful in learninga natural
languageB. ut no one learnsa naturall anguagew ith only a dictionary,f or th(
wordsa reo nly part of whatm ustb e learned.T he studento f a naturall anguagd
mustl earn the conventionsth at governh ow the wordsa rep ut legallyt ogether,
andl ater,t he students houldl earnt he art of thosew ho put the wordst ogether
with style
Similarly,n o one learnsa programmingla nguagefr om only a reference
FOREWORD
manual, for a referencem anual saysl ittle or nothing about the way the
primitiveso f the languagea re put to useb y thosew ho uset he languagew ell.
For this,a textbooki sr equired,a ndt he bestt extbookso fferc opiouse xamples,
for good examplesa re distilled experience,a nd it is principally through
experienceth at we learn.
In this book, the first examplei s on the first page,a nd the remaining
pagesc onstitutea n examplec ornucopiap, ouringf orth Prologp rogramsw rit-
ten by a passionateP rologp rogrammerw ho is dedicatedto the Prologp oint of
view. By carefullys tudyingt hesee xamplest,h e readera cquiresn ot only the
mechanicos f the languageb, ut alsoa personacl ollectiono f precedentsr,e ady
to be takena part,a dapted,a ndr eassembletdo getherin to newp rogramsW. ith
this acquisitiono f precedentk nowledge,t he transitionf rom novicet o skilled
programmeris alreadyu nderw ay.
Of course,a beneficiasl idee ffecto f goodp rogramminge xamplesis that
they exposea bit of interestings ciencea s well as a lot about programming
itself.T he scienceb ehindt he examplesin this book is Artificial Intelligence.
The reader learnsa bout suchp roblem-solvingid easa s problem reduction,
'how' 'why'
forward and backwardc haining, and questioning,a nd various
searcht echniques.
In fact, one of the greatf eatureso f Prologi s that it is simplee noughf or
studentsin introductoryA rtificial Intelligences ubjectsto learnt o usei mmedi-
ately.I expectt hat manyi nstructorsw ill uset hisb ook asp art of their artificial-
intelligences ubjectss o that their studentsc ans eea bstractid easi mmediately
reducedt o concrete,m otivatingf orm.
Among Prologt exts, I expectt his book to be particularlyp opular, not
only becauseo f its examplesb, ut alsob ecauseo f a numbero f other features:
o Carefuls ummariesa ppeart hroughout.
o Numerouse xercisesre inforcea ll concepts.
o Structures electorsin troducet he notion of data abstraction.
o Explicitd iscussionosf programmings tylea ndt echniqueo ccupya n entire
chapter.
o Therei s honesta ttentiont o the problemst o be facedi n Prologp rogram-
ming,a sw ell ast he joys.
Featuresli ke this make this a well done, enjoyable,a nd instructiveb ook.
PatrickH . Winston
Cambridge,M assachusetts
January1 986
Description:Prolog, the logic programming language, is now recognized as a powerful vehicle for non-numeric programming. The syntax and semantics of Prolog are discussed, as well as the power of Prolog in a variety of application areas in artificial intelligence.