CSc 372 - Comparative Programming Languages
16 : Prolog -- Introduction

Christian Collberg

Department of Computer Science

University of Arizona

1 What is Prolog?

2 What is Prolog?


		     Algorithm = Logic + Control 		 Robert A. Kowalski

Prescriptive Languages:
Descriptive Languages:

Kowalski's equation says that

3 Objects & Relationships

Prolog programs deal with
English:
``Christian likes the record''
Prolog:
\begin{gprogram}
\x likes(christian, record).
\end{gprogram}

4 Record Database


\begin{gprogram}
is\_record(planet\_waves). \\
is\_record(desire). \\
is\_reco...
...ding\_year(desire, 1975). \\
recording\_year(slow\_train, 1979).
\end{gprogram}

5 Record Database...

6 Conditional Relationships

English:

``C. likes Bob Dylan records recorded before 1979''

Prolog:
\begin{gprogram}
likes(christian, X) :- \\
\xx is\_record(X), \\
\xx recorded...
...bob\_dylan),\\
\xx recording\_year(X, Year),\\
\xx Year < 1979.
\end{gprogram}

7 Conditional Relationships...

8 Asking Questions

Prolog programs
English:
``Does Christian like the albums Planet Waves & Slow Train?'
Prolog:
\begin{gprogram}
\redtt{?- likes(christian, planet\_waves).}\\
yes\\
\redtt{?- likes(christian, slow\_train).}\\
no
\end{gprogram}

9 Asking Questions...

English:
``Was Planet Waves recorded by Bob Dylan?''

``When was Planet Waves recorded?''

``Which album was recorded in 1974?''
Prolog:
\begin{gprogram}
\redtt{?- recorded\_by(planet\_waves, bob\_dylan).} \\
\x yes\...
...\\
\redtt{?- recording\_year(X, 1974).} \\
\x X = planet\_waves
\end{gprogram}

10 Asking Questions...

In Prolog
English:
``Did Bob Dylan record an album in 1974?''
Prolog:
\begin{gprogram}
\redtt{?- is\_record(X),} \\
\xx \redtt{recorded\_by(X, bob\_dylan),} \\
\xx \redtt{recording\_year(X, 1974).} \\
yes
\end{gprogram}

11 Asking Questions...

Sometimes a query has more than one answer:
English:
``What does Christian like?''
Prolog:
\begin{gprogram}
\redtt{?- likes(christian, X).} \\
\x X = planet\_waves ; \\
\\
\x X = desire ; \\
\\
no
\end{gprogram}

12 Asking Questions...

Sometimes answers have more than one part:

English:

``List the albums and their artists!''
Prolog:
\begin{gprogram}
\redtt{?- is\_record(X), recorded\_by(X, Y).}\\
X = planet\_wa...
...\
Y = bob\_dylan ;\\
X = slow\_train,\\
Y = bob\_dylan ;\\
no
\end{gprogram}

13 Recursive Rules

``People are influenced by the music they listen to.

People are influenced by the music listened to by the people they listen to.''

\begin{gprogram}
listens\_to(bob\_dylan, woody\_guthrie). \\
listens\_to(arlo\_...
...nced\_by(X, Y) :- \=listens\_to(X,Z), \\
\> influenced\_by(Z,Y).
\end{gprogram}

14 Asking Questions...

English:
``Is Björk influenced by Bob Dylan?''

``Is Björk influenced by Woody Guthrie?''

``Is Bob Dylan influenced by Bruce Springsteen?''
Prolog:
\begin{gprogram}
\redtt{?- influenced\_by(bjork, bob\_dylan).} \\
yes \\
\redt...
...
yes \\
\redtt{?- influenced\_by(bob\_dylan, bruce\_s).} \\
no
\end{gprogram}

15 Visualizing Logic

16 Visualizing Logic...

AND OR
   
and or

17 Visualizing Logic...

AND OR
   
and-example or-example

18 Visualizing Logic...

19 Answering Questions


\begin{gprogram}
(1) \xx scientist(helder). \\
(2) \xx scientist(ron). \\
(3) ...
...:- scientist(X). \\
(6) \xx \redtt{?- logician(X), american(X).}
\end{gprogram}

20 Answering Questions...

logician-3

21 Answering Questions...

logician


\begin{gprogram}
(1) \xx scientist(helder). \\
(2) \xx scientist(ron). \\
(3) ...
...:- scientist(X). \\
(6) \xx \redtt{?- logician(X), american(X).}
\end{gprogram}

22 Answering Questions...

logician-2

23 Answering Questions...


\begin{gprogram}
is\_record(planet\_waves). is\_record(desire). \\
is\_record(s...
..._by(X, bob\_dylan),\\
\xx recording\_year(X, Year), Year < 1979.
\end{gprogram}

24 Answering Questions...

likes

25 Answering Questions...


\begin{gprogram}
listens\_to(bob\_dylan, woody\_guthrie). \\
listens\_to(arlo\_...
...rk, bob\_dylan).} \\
\redtt{?- inf\_by(bjork, woody\_guthrie).}
\end{gprogram}

26 Answering Questions...

bjork1

27 Answering Questions...

bjork2

28 Map Coloring

ColoringMap1

``Color a planar map with at most four colors, so that contiguous regions are colored differently.''

29 Map Coloring...

A coloring is OK iff

  1. The color of Region 1 $\neq$ the color of Region 2, and
  2. The color of Region 1 $\neq$ the color of Region 3,...

\begin{gprogram}
color(R1, R2, R3, R4, R5, R6) :- \\
\x diff(R1, R2), diff(R1, ...
...ow). \\
diff(yellow, red).diff(yellow,blue). diff(yellow,green).
\end{gprogram}

30 Map Coloring...


\begin{gprogram}
\redtt{?- color(R1, R2, R3, R4, R5, R6).} \\
R1 = R4 = red, R2...
...
\\
R1 = red, R2 = blue, \\
R3 = R5 = green, R4 = R6 = yellow
\end{gprogram}

ColoringMap2

31 Map Coloring - Backtracking

ColoringMap3

32 Map Coloring - Backtracking

ColoringMap4

33 Working with gprolog

34 Working with gprolog...

35 Working with gprolog...


\scalebox{0.25}{\includegraphics{PS/gprolog.ps}}

36 Readings and References

Prolog by Example Coelho & Cotta
Prolog: Programming for AI Bratko
Programming in Prolog Clocksin & Mellish
The Craft of Prolog O'Keefe
Prolog for Programmers Kluzniak & Szpakowicz
Prolog Alan G. Hamilton
The Art of Prolog Sterling & Shapiro

37 Readings and References...

Computing with Logic Maier & Warren
Knowledge Systems Through Prolog Steven H. Kim
Natural Language Processing in Prolog Gazdar & Mellish
Language as a Cognitive Process Winograd
Prolog and Natural Language Analysis Pereira and Shieber
Computers and Human Language George W. Smith
Introduction to Logic Irving M. Copi
Beginning Logic E.J.Lemmon

38 Prolog So Far

39 Prolog So Far...

40 Prolog So Far...



Christian S. Collberg
2005-10-05