07 Prolog Programming PDF
07 Prolog Programming PDF
slides written by
Dr W.F. Clocksin
The Plan
An example program Syntax of terms Some simple programs Terms as data structures, unification The Cut Writing real programs
What is Prolog?
Prolog is the most widely used language to have been inspired by logic programming research. Some features: Prolog uses logical variables. These are not the same as variables in other languages. Programmers can use them as holes in data structures that are gradually filled in as computation proceeds.
More
Unification is a built-in termmanipulation method that passes parameters, returns results, selects and constructs data structures. Basic control flow model is backtracking. Program clauses and data have the same form. The relational form of procedures makes it possible to define reversible procedures.
More
Clauses provide a convenient way to express case analysis and nondeterminism. Sometimes it is necessary to use control features that are not part of logic. A Prolog program can also be seen as a relational database containing rules as well as facts.
In a functional language
In a declarative language
Constant
Names an individual
Compound Term
Names an individual that has parts likes(john, mary) book(dickens, Z, cricket) f(x) [1, 3, g(a), 7, 9] -(+(15, 17), t) 15 + 17 - t
Variable
Stands for an individual unable to be named when program is written X Gross_pay Diagnosis _257 _
Atom
alpha17 gross_pay john_smith dyspepsia + =/= 12Q&A
Number
0 1 57 1.618 2.04e-27 -13.6
Compound Terms
The parents of Spot are Fido and Rover.
parents(spot, fido, rover)
Some atoms have built-in operator declarations so they may be written in a syntactically convenient form. The meaning is not affected. This example looks like an arithmetic expression, but might not be. It is just a term. =/=(15+X, (0*a)+(2<<5)) + 15 X 0 * a 2 =/= + << 5
Compound Terms
Associativity: left, right, none. X+Y+Z is parsed as (X+Y)+Z because addition is left-associative.
Precedence: an integer. X+Y*Z is parsed as X+(Y*Z) because multiplication has higher precedence.
Structure of Programs
Programs consist of procedures. Procedures consist of clauses. Each clause is a fact or a rule. Programs are executed by posing queries.
An example
Example
Predicate Procedure for elephant Facts elephant(george). elephant(mary). elephant(X) :- grey(X), mammal(X), hasTrunk(X).
Clauses Rule
Example
Queries
Replies
no
Goals
Exercise: Identify all the parts of Prolog text you have seen so far.
Interpretation of Clauses
Clauses can be given a declarative reading or a procedural reading. Form of clause: Declarative reading: H :- G1, G2, , Gn. That H is provable follows from goals G1, G2, , Gn being provable.
Procedural reading:
To execute procedure H, the procedures called by goals G1, G2, , Gn are executed first.
?- pair(percival, X). ?- pair(apollo, daphne). ?- pair(camilla, X). ?- pair(X, lucinda). ?- pair(X, X). ?- pair(bertram, lucinda). ?- pair(X, daphne). ?- pair(X, Y).
Worksheet 2
drinks(john, martini). drinks(mary, gin). drinks(susan, vodka). drinks(john, gin). drinks(fred, gin). pair(X, Y, Z) :drinks(X, Z), drinks(Y, Z). ?- pair(X, john, martini). ?- pair(mary, susan, gin). ?- pair(john, mary, gin). ?- pair(john, john, gin). ?- pair(X, Y, gin). ?- pair(bertram, lucinda). ?- pair(bertram, lucinda, vodka). ?- pair(X, Y, Z).
Worksheet 3
berkshire
surrey kent
wiltshire
hampshire
sussex
WS3
This relation represents one direction of border:
border(sussex, kent). border(sussex, surrey). border(surrey, kent). border(hampshire, sussex). border(hampshire, surrey). border(hampshire, berkshire). border(berkshire, surrey). border(wiltshire, hampshire). border(wiltshire, berkshire).
WS3
Now a somewhat strange type of discount ticket. For the ticket to be valid, one must pass through an intermediate county. A valid ticket between a start and end county obeys the following rule: valid(X, Y) :- adjacent(X, Z), adjacent(Z, Y)
WS3
border(sussex, kent). border(sussex, surrey). border(surrey, kent). border(hampshire, sussex). border(hampshire, surrey). border(hampshire, berkshire). border(berkshire, surrey). border(wiltshire, hampshire). border(wiltshire, berkshire). adjacent(X, Y) :- border(X, Y). adjacent(X, Y) :- border(Y, X).
?- valid(wiltshire, sussex). ?- valid(wiltshire, kent). ?- valid(hampshire, hampshire). ?- valid(X, kent). ?- valid(sussex, X). ?- valid(X, Y).
Worksheet 4
arc
a(g, h). a(g, d). a(e, d). a(h, f). a(e, f). a(a, e). a(a, b). a(b, f). a(b, c). a(f, c).
a e g h
b f
d Note that Prolog can distinguish between the 0-ary constant a (the name of a node) and the 2-ary functor a (the name of a relation).
path(X, X). path(X, Y) :- a(X, Z), path(Z, Y). ?- path(f, f). ?- path(a, c). ?- path(g, e). ?- path(g, X). ?- path(X, h).
a d g e h
b f
This program works only for acyclic graphs. The program may infinitely loop given a cyclic graph. We need to leave a trail of visited nodes. This is accomplished with a data structure (to be seen later).
Unification
Two terms unify if substitutions can be made for any variables in the terms so that the terms are made identical. If no such substitution exists, the terms do not unify. The Unification Algorithm proceeds by recursive descent of the two terms. Constants unify if they are identical Variables unify with any term, including other variables Compound terms unify if their functors and components unify.
Examples
The terms f(X, a(b,c)) and f(d, a(Z, c)) unify.
f f X b a c d Z a c
The terms are made equal if d is substituted for X, and b is substituted for Z. We also say X is instantiated to d and Z is instantiated to b, or X/d, Z/b.
Examples
The terms f(X, a(b,c)) and f(Z, a(Z, c)) unify.
f f X b a c Z Z a c
Examples
The terms f(c, a(b,c)) and f(Z, a(Z, c)) do not unify. f
f c b a c Z Z a c
No matter how hard you try, these two terms cannot be made identical by substituting terms for variables.
Exercise
Do terms g(Z, f(A, 17, B), A+B, 17) and g(C, f(D, D, E), C, E) unify?
g Z f B A + B 17 C f E g C E
A 17
D D
Exercise
First write in the co-referring variables.
g Z f B A + B 17 C f E g C E
A 17
D D
Exercise
Z/C, C/Z
g Z f B A + B 17
Now proceed by recursive descent We go top-down, left-to-right, but the order does not matter as long as it is systematic and complete. g C f E C E
A 17
D D
Exercise
Z/C, C/Z, A/D, D/A
g Z f B A + B 17 C f E
g C E
A 17
D D
Exercise
Z/C, C/Z, A/17, D/17
g Z f B A + B 17 C f E
g C E
A 17
D D
Exercise
Z/C, C/Z, A/17, D/17, B/E, E/B
g Z f B A + B 17 C f E
g C E
A 17
D D
Exercise
Z/17+B, C/17+B, A/17, D/17, B/E, E/B
g Z f B A + B 17 C f E
g C E
A 17
D D
Exercise
Z/17+17, C/17+17, A/17, D/17, B/17, E/17
g Z f B A + B 17 C f E
g C E
A 17
D D
A 17
D D
A 17
D D
A 17
D D
D 17
D D
17 17
17 17
17 17
17 17
17 17
17 17
17 17
17 17
E 17 17
E 17 17
17 17 17
Lists
Lists are the same as other languages (such as ML) in that a list of terms of any length is composed of list cells that are consed together. The list of length 0 is called nil, written []. The list of length n is .(head,tail), where tail is a list of length n-1. So a list cell is a functor . of arity 2. Its first component is the head, and the second component is the tail.
Examples of lists
nil .(a, nil) .(a, .(b, nil) .(a, .(b, .(c, .(d, .(e. nil))))) .(a,b) (note this is a pair, not a proper list) .(a, X) (this might be a list, or might not!) .(a, .(b, nil)), .(c, nil))
Exercises
Identify the heads and tails of these lists (if any): [a, b, c] [a] [] [[the, cat], sat] [[the, cardinal], [pulled, [off]], [each, [plum, coloured], shoe]
Exercises
Identify the heads and tails of these lists (if any): a [b, c] [a] [] [[the, cat], sat] [[the, cardinal], [pulled, [off]], [each, [plum, coloured], shoe]
Exercises
Identify the heads and tails of these lists (if any): [a, b, c] a [] [] [[the, cat], sat] [[the, cardinal], [pulled, [off]], [each, [plum, coloured], shoe]
Exercises
Identify the heads and tails of these lists (if any): [a, b, c] [a] [] (not a list, so doesnt have head and tail. nil is a constant) [[the, cat], sat] [[the, cardinal], [pulled, [off]], [each, [plum, coloured], shoe]
Exercises
Identify the heads and tails of these lists (if any): [a, b, c] [a] [] [the, cat] [sat] [[the, cardinal], [pulled, [off]], [each, [plum, coloured], shoe]
Exercises
Identify the heads and tails of these lists (if any): [a, b, c] [a] [] [[the, cat], sat] [the, cardinal] [pulled, [off]], [each, [plum, coloured], shoe]
Exercises
For each pair of terms, determine whether they unify, and if so, to which terms are the variables instantiated? [X, Y, Z] [cat] [X,Y|Z] [[the,Y]|Z] [X, Y, X] [[X], [Y], [X]] [john, likes, fish] [X|Y] [mary, likes, wine] (picture on next slide) [[X,answer], [is, here]] [a, Z, Z] [[a], [X], [X]]
Remember
A variable may be instantiated to any term.
X mary likes wine [] Y Z
[X,Y|Z]
Exercises
Here is a mystery predicate. What does it do?
mystery(X, A, B) :- member(X, A), member(X, B).
?- mystery(a, [b, c, a], [p, a, l]). ?- mystery(b, [b, l, u, e], [y, e, l, l, o, w]). ?- mystery(X, [r, a, p, i, d], [a, c, t, i, o, n]). ?- mystery(X, [w, a, l, n, u, t], [c, h, e, r, r, y]).
member(X, [H|T]) :- member(X, T). member(X, [X|_]). member(X, [_|T]) :- member(X, T).
is
But is cannot solve equations, so the expression must be ground (contain no free variables. ?- Y is 2 * X. no ?- X is 15, Y is 2 * X. X = 15, Y= 30.
Worksheet 6
/* length(List, Length) */
Tail-recursive method:
length(L, N) :- acc(L, 0, N). /* acc(List, Before, After) */ acc([], A, A). acc([H|T], A, N) :- A1 is A + 1, acc(T, A1, N).
Exercises
?- length([apple, pear], N). ?- length([alpha], 2). ?- length(L, 3). Modify length to give a procedure sum such that sum(L,N) succeeds if L is a list of integers and N is their sum.
As you might expect, there are nave and tail-recursive ways to compute this.
Worksheet 7
The nave method:
Worksheet 7
Tail-recursive method: inner(A, B, N) :- dotaux(A, B, 0, N). dotaux([], [], V, V). dotaux([A|As],[B|Bs],N,Z) :N1 is N + (A * B), dotaux(As, Bs, N1, Z).
Worksheet 8
maximum(L, M) :- max(L, -10000, M). Magic numbers are a bad idea. maximum(L, M) :- max(L, minint, M) Need to change definitions of arithmetic. maximum([H|T], M) :- max(T, H, M). And know that ?- maximum([],X) fails.
Worksheet 9
arc
a(g, h). a(d, a). a(g, d). a(e, d). a(h, f). a(e, f). a(a, e). a(a, b). a(b, f). a(b, c). a(f, c).
a d g e h
b f
Here we have added a new arc from d to a. If you use the program from WS 4, some goals will cause an infinite loop.
Keep a trail of nodes visited so far. Visit only legal nodes, not already on the trail. Represent the trail as an accumulator, an extra argument of path.
Worksheet 9
path(X, X, T). path(X, Y, T) :a(X, Z), legal(Z, T), path(Z, Y, [Z|T]). legal(Z, []). legal(Z, [H|T]) :- Z \== H, legal(Z, T). Notice that legal is like the negation of member.