CSCI 431 Programming Languages
Fall 2003
Prolog’s Lists, Negation and
Imperative Control Flow
(Section 11.3)
A modification of slides developed by Felix
Hernandez-Campos at UNC Chapel Hill
1
Lists
• Constructors
– []
–.
Empty list constant
Constructor functor
• Example
– .(a, .(b, .(c, [])))
– [a, b, c] (syntactic sugar)
• Tail notation:
– [a | [b, c]]
– [a, b | [c]]
Head
Tail
2
Lists
Examples
No notion of
input or output
parameters
3
Tic-Tac-Toe Example
• 3x3 grid
• Two Players:
– X (computer)
– O (human)
• Fact x(n)indicates a
movement by X
– E.g. x(5), x(9)
• Fact o(n) indicates a
movement by O
– E.g. o(1), o(6)
4
Tic-Tac-Toe Example
• Winning condition
5
Tic-Tac-Toe Example
Strategy: good moves
Ordered List
of Choices
6
Winning Split
Tic-Tac-Toe Example
X
7
Imperative Control Flow
The cut
• Prolog has a number of explicit control flow features
•!
Known as the cut
– This is a zero-argument predicate that always succeeds
– It commits the interpreter to the unification made between
the parent goal and the left-hand side of the current rules
• Example
member(X, [X|T]).
member may
member(X, [H|T]) :- member(X, T). succeed n times
member may succeed
member(X, [X|T]) :- !.
member(X, [H|T]) :- member(X, T). at most one time
If this rule succeeded, do not try to use the following ones
8
Imperative Control Flow
• Alternative
member(X, [X|T]).
member(X, [H|T]) :- not(X=H), member(X, T).
• How does not work?
not(P) :- call(P), !, fail.
not(P).
– call attempts to satisfy the goal P.
– fail always fails.
9
Prolog Database Manipulation
• Two built-in predicates can be used to modify the
database of known facts
• assert(P) adds a new fact.
– E.g. assert(parent(kevin, john))
• retract(P) removes a known fact.
– E.g. retract(parent(kevin, john))
10
Logic Puzzles
An Example (courtesy of Axel Schreiner):
1. There are 3 boys and 3 girls.
2. One girl is dressed in red, one in green, one in blue.
3. One boy is dressed in red, one in green, one in blue.
4. The boy in red danced with the girl in green.
5. No boy danced with a girl who was dressed in the same color.
6. Which boy danced with the girl dressed in red?
11
A Manual Solution
(courtesy of Axel Schreiner)
A constraint applies to
property values and is entered
into the appropriate matrix.
An assertion such as (4) is
entered as a dot relating two
values for two properties:
12
Manual Solution
Exclusions such as (5) are
entered as crosses into the
matrix. A cell may only
contain a dot or a cross, not
both:
Each row and column can only
contain a single dot because
the relationship between
values is one-to-one. This can
lead to further exclusions:
13
Manual Solution
It can also lead to further dots
if only a single cell remains
unfilled in a row or column:
The process becomes more complicated once there
are more properties and therefore more matrices;
each property must be related to every other in a
separate matrix.
Eventually the solution can be read from the
matrices: The boy in blue danced with the girl in
red.
14
Prolog Solution
(courtesy of Axel Schreiner)
/* properties and values */
/* constraints */
boy(red).
boy(green).
boy(blue).
a(red, green).
c(red, X) :- X \= red.
d(green, X) :- X \= green.
e(blue, X) :- X \= blue.
girl(red).
girl(green).
girl(blue).
15
Prolog Solution
solve(Boy1,Girl1, Boy2,Girl2, Boy3,Girl3) :/* freeze first property */
/* assert constraints */
Boy1 = red,
Boy2 = green,
Boy3 = blue,
( a(Boy1, Girl1);
a(Boy2, Girl2);
a(Boy3, Girl3) ),
/* make values of other properties distinct */
( c(Boy1, Girl1);
c(Boy2, Girl2);
c(Boy3, Girl3) ),
girl(Girl1),
girl(Girl2),
girl(Girl3),
Girl1 \= Girl2, Girl1 \= Girl3,
Girl2 \= Girl3,
( d(Boy1, Girl1);
d(Boy2, Girl2);
d(Boy3, Girl3) ),
( e(Boy1, Girl1);
e(Boy2, Girl2);
e(Boy3, Girl3) ).
16
Prolog Solution
main :solve(Boy1,Girl1, Boy2,Girl2, Boy3,Girl3),
/* print a solution */
write(Boy1), write('\t'),
write(Girl1), write('\n'),
write(Boy2), write('\t'),
write(Girl2), write('\n'),
write(Boy3), write('\t'),
write(Girl3), write('\n'),
/* backtrack for all solutions */
fail.
17
Prolog Interpreter
?- ['c:/classes/431/boy.pl'].
% c:/classes/431/boy.pl compiled 0.00 sec, 3,032 bytes
Yes
?- main.
red green
green blue
blue red
No
?-
18
© Copyright 2026 Paperzz