06-25433 – Logic Programming
Writing and running
simple Prolog programs
This lecture will cover writing and
querying facts without and with unbound
variables, and show how rules are a way of
querying more than one fact at a time.
06-25433 – Logic Programming
A question from last week
Why can’t Prolog have the ++ operator, for instance:
A_Variable++
Answer:
A_Variable ++
is the equivalent of:
A_Variable is A_Variable + 1
Prolog doesn’t allow variables to be reassigned.
2 - Writing and running simple Prolog programs 1
06-25433 – Logic Programming
Last time ...
Prolog’s built-in predicates and user-defined
predicates look the same and behave the same.
Every predicate in Prolog returns true (yes) or fail
(no).
Variables can’t be reassigned - it’s not logical.
Var is Var +1
is as logical as saying:
1 is 1 + 1.
2 - Writing and running simple Prolog programs 2
06-25433 – Logic Programming
Last time ...
For your rules to communicate with other rules, you
have to:
– send information in through the parameter list;
– send information back out through the parameter
list.
2 - Writing and running simple Prolog programs 3
06-25433 – Logic Programming
Last time ...
When there is a choice of rules,
– Prolog tries the first and, if it works, returns
“true” and carries on
If the rule fails,
– it tries the next, and the next and the next, etc,
until:
– either it finds a true rule or it fails
(catastrophically)
2 - Writing and running simple Prolog programs 4
06-25433 – Logic Programming
Today
This lecture will cover:
– revise unification
– introduce facts
– show that Prolog uses unification when searching
for clauses
– writing rules that use facts
– how Prolog works – its “hidden” data structure
2 - Writing and running simple Prolog programs 5
06-25433 – Logic Programming
Unification revisited - 1
Tutorial Sheet 1 had a number of matching and
unification questions.
Prolog has several predicates that match or compare
two terms:
==/2 e.g. 1 == 1
\==/2 e.g. 1 \== 2
=:=/2 e.g. 5 =:= 2 + 3
=\=/2 e.g. 7 =\= 16 / 2
>=/2 e.g. 5 >= 3
etc
2 - Writing and running simple Prolog programs 6
06-25433 – Logic Programming
Unification revisited - 2
None of these predicates assigns values to a variable:
==/2, \==/2,=:=/2, =\=/2, >=/2, etc
Only two predicates assign values:
Arithmetic only:
is/2 e.g. Var1 is 3 * 4
Unification:
=/2 e.g. Var2 = an_atom
2 - Writing and running simple Prolog programs 7
06-25433 – Logic Programming
Unification revisited - 3
When is unification true?
Unifying a variable with a literal:
Var1 = an_atom.
Var2 = 300.045.
Var3 = an_object(an_atom, 300.045).
2 - Writing and running simple Prolog programs 8
06-25433 – Logic Programming
Unification revisited - 3
When is unification true?
Unifying a variable with a literal:
an_atom = Var1.
300.045 = Var2.
an_object(an_atom, 300.045) = Var3.
The “empty” variable does not
have to be on the left-hand
side.
2 - Writing and running simple Prolog programs 9
06-25433 – Logic Programming
Unification revisited - 4
When is unification true?
Unifying a variable with another variable:
| ?- Var1 = Var2.
Var2 = Var1 ? ;
no
The “semi-colon” in
Prolog show theProlog
Tvariable binding
means “or”.
– the “contents”We’re
of thesaying
variable
“Or is there
All unification really does another solution...?”
is to make variables share
memory locations.
2 - Writing and running simple Prolog programs 10
06-25433 – Logic Programming
Unification revisited - 4
We can run a conjunction of unifications:
2 - Writing and running simple Prolog programs 11
06-25433 – Logic Programming
Writing facts - 1
In the last lecture, we saw rules such as:
display_balance(bank_account(No, Name,
Balance)) :-
write(Name),
write(' account no: '),
write(No),
nl,
write('Your balance is: '),
write(Balance),
nl.
2 - Writing and running simple Prolog programs 12
06-25433 – Logic Programming
Writing facts - 2
Facts are rules without a body:
likes(max, julia).
likes(max, amabel).
They are always true, so we could also write them
as:
likes(max, julia) :-
true.
likes(max, amabel) :-
true.
2 - Writing and running simple Prolog programs 13
06-25433 – Logic Programming
Queries with these facts
2 - Writing and running simple Prolog programs 14
06-25433 – Logic Programming
What facts represent
Facts with arguments are used to describe
relationships between arguments.
e.g.
lives_in(max, london).
likes(max, amabel).
child(charles, amy, brian).
price(template, 3, 4.75).
assembly(arm, joint(ball,3)).
2 - Writing and running simple Prolog programs 15
06-25433 – Logic Programming
An insight
Prolog matches our query with facts using
unification.
Prolog uses unification wherever it has to match
anything with anything.
2 - Writing and running simple Prolog programs 16
06-25433 – Logic Programming
Consolidation moment
Facts are rules that are always true.
Prolog will attempt to return every solution – in the
order that they occur in the program.
Prolog uses unification to match queries with rule
heads and facts.
2 - Writing and running simple Prolog programs 17
06-25433 – Logic Programming
Jealousy: the jealous/2 rule
This is a simple program:
jealous(Jealous, Victim) :-
likes(Person, Jealous),
likes(Person, Victim).
likes(max, julia).
likes(max, amabel).
2 - Writing and running simple Prolog programs 18
06-25433 – Logic Programming
Jealousy: the jealous/2 rule
2 - Writing and running simple Prolog programs 19
06-25433 – Logic Programming
States of the stack - 1
Step 1 - add to stack
jealous(julia, Victim) :-
likes(Person, julia),
likes(Person, Victim).
Step 2 - match first subgoal
jealous(julia, Victim) :-
likes(max, julia),
likes(max, Victim).
2 - Writing and running simple Prolog programs 20
06-25433 – Logic Programming
States of the stack - 2
Step 3 - match second subgoal
jealous(julia, julia) :-
likes(max, julia),
likes(max, julia).
jealous(julia, amabel) :-
likes(max, julia),
likes(max, amabel).
2 - Writing and running simple Prolog programs 21
06-25433 – Logic Programming
States of the stack - 3
Step 4 - report success to the user
| ?- jealous(julia, Who).
Who = julia ;
jealous(julia, amabel) :-
likes(max, julia),
likes(max, amabel).
2 - Writing and running simple Prolog programs 22
06-25433 – Logic Programming
States of the stack - 4
Step 5 - report success to the user
| ?- jealous(julia, Who).
Who = julia ;
Who = amabel ;
2 - Writing and running simple Prolog programs 23
06-25433 – Logic Programming
States of the stack - 5
Step 6 - report no more solutions
| ?- jealous(julia, Who).
Who = julia ;
Who = amabel ;
no
2 - Writing and running simple Prolog programs 24
06-25433 – Logic Programming
Consolidation moment
Prolog will attempt to find all solutions – if you ask.
Finding multiple solutions is based on an internal
stack – which you’ll never see but you’ll see the
effects.
2 - Writing and running simple Prolog programs 25
06-25433 – Logic Programming
Some terminology
jealous(Jealous, Victim) :-
likes(Person, Jealous),
likes(Person, Victim).
likes(max, julia). 1 procedure
likes(max, amabel).
1 procedure
2 - Writing and running simple Prolog programs 26
06-25433 – Logic Programming
Some terminology
jealous(Jealous, Victim) :-
likes(Person, Jealous),
likes(Person, Victim).
1 clause
likes(max, julia).
likes(max, amabel).
2 clauses
2 - Writing and running simple Prolog programs 27
06-25433 – Logic Programming
Some terminology
jealous(Jealous, Victim) :-
likes(Person, Jealous),
likes(Person, Victim).
1 rule
likes(max, julia).
likes(max, amabel).
2 facts
2 - Writing and running simple Prolog programs 28
06-25433 – Logic Programming
Friendship - 1
Suppose we define our friend as being either:
an immediate friend of ourselves
or
a friend of a friend
2 - Writing and running simple Prolog programs 29
06-25433 – Logic Programming
Friendship - 2
We could code this as:
friend_of(max, julia).
friend_of(max, amabel).
friend_of(amabel, richard). % etc
% 1
friend(Pers, Friend) :-
friend_of(Pers, Friend).
% 2
friend(Pers, Friend) :-
friend_of(Pers, Inter),
friend_of(Inter, Friend).
2 - Writing and running simple Prolog programs 30
06-25433 – Logic Programming
A question
How many subgoals would the longest friend/2 rule
have to have?
or, to put it another way:
What is the maximum number of friends you could
have?
2 - Writing and running simple Prolog programs 31