Title: Prolog
1Prolog
- Overview
- Syntax
- Mechanism
- Summary
2Overview
- PROLOG Programming with Logic
- Uses predicate (first-order) calculus
- History
- Roots J.A. Robinson (1965) created algorithms
- for resolution refutation.
3Resolution
P V Q P V A
Q V A
star(A75) V galaxy(andromeda)
star(X) V
bright(X)
galaxy(andromeda) V bright(A75) T X/A75
4History
- First Prolog Program written in France early
1970s - as part of a project in natural language
understanding. - Theoretical background defined by Kowalski,
Hayes, - and others.
- Major development of the language done from
1975-1979 - at the dept. of artificial intelligence of the
University of - Edinburgh.
5Applications
- Automatic code generation
- Program verification
- Design of high-level specification languages
- Example Mathematica
6Examples
Everyone likes Susie likes(X,susie) George
likes Kate and George likes Susie likes(george,ka
te) , likes(george,susie) George likes Kate or
George likes Susie likes(george,kate)
likes(george,susie) George likes Susie if
George does not like Kate likes(george,susie) -
not(likes(george,kate))
7Prolog
- Overview
- Syntax
- Mechanism
- Summary
8Syntax
- There are many dialects of Prolog.
- We will use C-Prolog (Clocksin and Mellish 1984)
- Prolog uses Predicate Calculus as its language.
9Prolog
- Overview
- Syntax
- Mechanism
- Summary
10Prologs Mechanism
- A Prolog program is a set of specifications in
predicate - calculus.
- The specification is known as the database of
the system. - Prolog is an interactive language (the user
enters queries - in response to a prompt).
11Example 1
likes(george,kate). likes(george,susie). likes(geo
rge,wine). likes(susie,wine). likes(kate,gin). lik
es(kate,susie). ?- likes(george,kate) yes ?-
likes(george,beer) no
12Example 2
likes(george,kate). likes(george,susie). likes(geo
rge,wine). ?- likes(george,X) X kate X
susie X wine no
13Points to Consider
- Successive user prompts cause the
interpreter to - return all terms that can be substituted for X.
- They are returned in the order found.
- ORDER IS IMPORTANT
- PROLOG adopts the closed-world assumption
- All knowledge of the world is present in the
database. - If a term is not in the database, assume it is
false.
14Defining Rules
To define a rule use logical implication -
- Example
- friends(X,Y) - likes(X,Z), likes(Y,Z).
- Only one predicate is allowed on the left of the
implication - The predicate on the left cannot be negated
- This is Horn Clause Logic
15Example Rule
likes(george,kate). likes(george,susie). likes(geo
rge,wine). likes(susie,wine). likes(kate,gin). lik
es(kate,susie). friends(X,Y) - likes(X,Z),
likes(Y,Z). ?- friends(george,susie) yes
16Quantifiers
- When a variable appears in the specification of a
database, - the variable is universally quantified. Example
- likes(susie,Y) Susie likes everyone
- For the existentially quantifier one may do two
things - Enter the value directly into the database
- likes(george,Z) becomes likes(george,wine)
- b. Query the interpreter
- ?- likes(george,Z) returns a value for Z
if one exists.
17Recursion in Prolog
Recursion is the primary control structure in
Prolog. Lets look at an example with
lists Elements in lists are enclosed by
brackets 1,2,3,4 george,kate,allen,amy,d
on,pat
18lists
The first element of a list can be separated from
the tail using operator Example Match the
list tom,dick,harry,fred to XY
then X tom and Y dick,harry,fred X,YZ
then X tom, Y dick, and Z
harry,fred V,W,X,Y,ZU will not
match tom,Xharry,fred gives X dick
19List membership
We want to write a function member that works as
follows ?- member(a,a,b,c,d,e) yes ?-
member(a,1,2,3,4) no ?- member(X,a,b,c) X
a X b X c no
20Function membership
Define two predicates member(X,XT). member(X,
YT) - member(X,T). Trace the following
call ?- member(c,a,b,c) yes
21Function membership
A more elegant definition uses anonymous
variables member(X,X,_). member(X,_T) -
member(X,T). The symbol _ indicates that the
contents of that variable is unimportant.
22Function write list
writelist(). writelist(HT) - write(H), nl,
writelist(T). A reverse write of a list is as
follows reverse_writelist(). reverse_writelist
(HT) - reverse_writelist(T), write(H), nl.
23Other functions
?- assertz(likes(david,sarah)) Adds this
predicate to the database. To remove a
predicate p use ?- retract(p)
24Other functions
To download a database of sentences ?-
consult(myfile) yes Or ?- myfile To read and
write from console ?- read(X) ?- write(X)
25Prolog
- Overview
- Syntax
- Mechanism
- Summary
26Summary
- There are many dialects of Prolog we will use
- C-Prolog (Clocksin and Mellish 1984). Prolog
uses - Predicate Calculus as its language.
- A Prolog program is a set of specifications in
predicate - calculus. The specification is known as the
database - of the system.
- Prolog is an interactive language (the user
enters queries - in response to a prompt).
- Recursion is the primary control structure in
Prolog. - PROLOG adopts the closed-world assumption
- All knowledge of the world is present in the
database. - If a term is not in the database assume it is
false.