Writing and Running Simple Prolog Programs
Writing and Running Simple Prolog Programs
Answer:
A_Variable ++
is the equivalent of:
A_Variable is A_Variable + 1
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
Unification revisited - 1
Tutorial Sheet 1 had a number of matching and
unification questions.
Unification revisited - 2
None of these predicates assigns values to a variable:
==/2, \==/2,=:=/2, =\=/2, >=/2, etc
Arithmetic only:
is/2 e.g. Var1 is 3 * 4
Unification:
=/2 e.g. Var2 = an_atom
Unification revisited - 3
When is unification true?
Unification revisited - 3
When is unification true?
Unification revisited - 4
When is unification true?
Unification revisited - 4
We can run a conjunction of unifications:
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).
An insight
Prolog matches our query with facts using
unification.
Consolidation moment
jealous(Jealous, Victim) :-
likes(Person, Jealous),
likes(Person, Victim).
likes(max, julia).
likes(max, amabel).
jealous(julia, amabel) :-
likes(max, julia),
likes(max, amabel).
jealous(julia, amabel) :-
likes(max, julia),
likes(max, amabel).
| ?- jealous(julia, Who).
Who = julia ;
Who = amabel ;
| ?- jealous(julia, Who).
Who = julia ;
Who = amabel ;
no
Consolidation moment
Some terminology
jealous(Jealous, Victim) :-
likes(Person, Jealous),
likes(Person, Victim).
1 procedure
Some terminology
jealous(Jealous, Victim) :-
likes(Person, Jealous),
likes(Person, Victim).
1 clause
likes(max, julia).
likes(max, amabel).
2 clauses
Some terminology
jealous(Jealous, Victim) :-
likes(Person, Jealous),
likes(Person, Victim).
1 rule
likes(max, julia).
likes(max, amabel).
2 facts
Friendship - 1
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