Lecture 5 PROLOG
Lecture 5 PROLOG
Artificial Intelligence
Fundamentals
Lecture 5 and 6
Automated Reasoning -
PROLOG
PROLOG
The most commonly used programming languages in AI.
PROLOG facilitates
◼ Propositional logic reasoning
daughter(X, Y) X is a daughter of Y
son(X, Y) X is a son of Y
parent(X, Y) X is a parent of Y
mother (X, Y) X is the mother of Y
father(X, Y) X is father of Y
male(X) X is male
An Example
?_ grandMother(X, samantha).
?_ grandFather(X, Y).
can be answered.
Constants, Variables, Terms,
Predicates and Atoms
In PROLOG, datatypes are not defined, though numerical calculations
are supported.
1 Constants
A constant in PROLOG is a value which can never be changed.
A constant can be a value of an integer, a decimal number, a sequence
of symbols, a string, a list or a structure.
In PROLOG, a constant may/may not have an identifier. The
identifier of a constant may contain letters, digits, underscores, and
should start with a lower-case letter.
Example
a12_3, xY, 50, 3.1415926, pi, samantha and jack are constants.
_a, Mary, _50 are not constants.
Constants, Variables, Terms,
Predicates and Atoms
2 Variables
Example
_X, Mary, Z100
are valid variable identifiers/names.
x, 10XY, ???
are invalid variable names.
Constants, Variables, Terms,
Predicates and Atoms
3 Terms
Example
greater_than(1, 5), greater_than(a, b), positive(1), positive(b) and
parent(micheal, jack) are atoms.
Logical Connectives
Logic PROLOG
conjunction ,
disjunction ;
implication :-
negation not
Logical Connectives
Example
In the previous PROLOG program,
are equivalent to
parent(X, Y) daughter(Y, X)
mother(X, Y) parent(X, Y) male(X)
Logic Program
1. Logic Program
Example
daughter(samantha, rebecca).
parent(rebecca, anna).
parent(elizabeth, michael).
are facts.
Logic Program
3. Rule
Example
?_ mother(rebecca, jack).
is a goal.
1 Arithmetic Operators
◼ “=:=“, “=“ and “==“ are the operators for equal testing.
“=:=“(arithmetic comparison – the values to be compared
must be numerical. )
“=“ (general comparison – compares the values on the
left and right sides. If one of them hasn’t been bound,
then the value of the other side will be bound to it )
“==“ (strict comparison - compares the values on the
left and right sides. The two values must be the same.
No value binding will be conducted.)
Arithmetic Calculations
/*maxL2.pl */
/*max(X, Y, Z) finds the larger one of X and Y, and binds it to Z*/
max(X, X, X).
max(X, Y, X) :- X > Y.
max(X, Y, Y) :- Y > X.
This program finds the larger one of the first and second
parameters, and binds the larger value to the third parameter.
Arithmetic Calculations
Queries can be made to the PROLOG executor
?_ max(2, 2, X).
X=2
?- max(X, 6, 8).
X=8
?- max(X, 6, 4).
False
Arithmetic Calculations
Example
Define a predicate f(N, F) to calculate the factorial of N,
N! = 1 * 2 * ... * N
and to bind N! to F.
/* factL2.pl */
/* f(N, F) calculates the factorial of N, and binds it to F */
f(0, 1).
f(N, F) :- N > 0, N1 is N - 1, f(N1, Temp), F is N * Temp.
Homework Exercise
Define the predicate sumOddEven(N, S) where N and S are
positive integers. The predicate calculates
1 + 3 + ... + N (if N is odd)
2 + 4 + ... + N (if N is even)
and binds the sum to S.
Homework Exercise (Answer)
/*sumOddEven.pl*/
sumOddEven(1, 1).
sumOddEven(2, 2).
sumOddEven(N, S) :- N > 2, NN is N - 2, sumOddEven(NN, Temp), S
is N + Temp.
Input and Output
1 Input
read(X)
reads a term from the standard input or a file, and uses the
input value to instantiate X. The input is terminated by a full
stop.
Input and Output
2 Output
Example
write(X).
write(100).
write(‘Hello’).
Input and Output - Examples
Example
/* readL2.pl */
test1 :- read(X), Y = X, write(Y).
test2 :- read(X), Y == X, write(Y).
test3 :- read(X), read(Y), X == Y.
?- test1.
12.
12
true
(Y was unbound, but now is bound to the value of X)
Input and Output - Examples
?- test1.
/* readL2.pl */
anna.
test1 :- read(X), Y = X, write(Y).
anna test2 :- read(X), Y == X, write(Y).
true test3 :- read(X), read(Y), X == Y.
?- test2.
12.
false
(Y was unbound, and hence Y == X returns false)
Input and Output - Examples
?- test3.
/* readL2.pl */
12. test1 :- read(X), Y = X, write(Y).
12. test2 :- read(X), Y == X, write(Y).
true test3 :- read(X), read(Y), X == Y.
?- test3.
12.
13.
false
Input and Output
Example
/* readL2.pl */
test1 :- read(X), Y = X, tab(12), write(Y).
test2 :- read(X), Y = X, nl, tabl(12), write(Y).
test3 :- read(X), read(Y), X == Y.
Class Exercise
/*abstract.pl*/
abstract :- nl, write('enter X '), read(X), positive(X).
positive(X) :- X < 0, Y is 0 - X, write(Y).
positive(X) :- X >= 0, write(X).
Derivation in PROLOG
Given a logic program (which is a set of facts and rules) and a
goal, the PROLOG executor evaluates the goal by selecting a
fact/rule that matches the goal.
◼ A fact matching the goal means the fact and goal have the
student(a).
person(b).
person(c).
studying(c).
student(X) :- person(X), studying(X).
student(a).
person(b).
person(c).
studying(c).
student(X) :- person(X), studying(X).
Answers X=a
student(a).
person(b).
person(c).
studying(c).
student(X) :- person(X), studying(X).
Answers X=a
student(a).
person(b).
person(c).
studying(c).
student(X) :- person(X), studying(X).
Answers X=a
student(a).
person(b).
person(c).
studying(c).
student(X) :- person(X), studying(X).
Answers X=a
student(a).
person(b).
person(c).
studying(c).
student(X) :- person(X), studying(X).
Answers X=a
student(a).
person(b).
person(c).
studying(c).
student(X) :- person(X), studying(X).
Answers X=a
student(a).
person(b).
person(c).
studying(c).
student(X) :- person(X), studying(X).
Answers X=a
student(a).
person(b).
person(c).
studying(c).
student(X) :- person(X), studying(X).
Answers X=a
student(a).
person(b).
person(c).
studying(c).
student(X) :- person(b), studying(b).
Answers X=a
student(a).
person(b).
person(c).
studying(c).
student(X) :- person(b), studying(b).
Answers X=a
student(a).
person(b).
person(c).
studying(c).
student(X) :- person(b), studying(b).
Answers X=a
student(a).
person(b).
person(c).
studying(c).
student(X) :- person(b), studying(b).
Answers X=a
student(a).
person(b).
person(c).
studying(c).
student(X) :- person(b), studying(b).
Answers X=a
student(a).
person(b).
person(c).
studying(c).
student(X) :- person(b), studying(b).
Answers X=a
student(a).
person(b).
person(c).
studying(c).
student(X) :- person(b), studying(b).
Answers X=a
student(a).
person(b).
person(c).
studying(c).
student(X) :- person(X), studying(X).
Answers X=a
student(a).
person(b).
person(c).
studying(c).
student(X) :- person(c), studying(c).
Answers X=a
student(a).
person(b).
person(c).
studying(c).
student(X) :- person(c), studying(c).
Answers X=a
student(a).
person(b).
person(c).
studying(c).
student(X) :- person(c), studying(c).
Answers X=a
student(a).
person(b).
person(c).
studying(c).
student(X) :- person(c), studying(c).
Answers X=a
student(a).
person(b).
person(c).
studying(c).
student(X) :- person(c), studying(c).
Answers X=a;
X=c
student(a).
person(b).
person(c).
studying(c).
student(X) :- person(X), studying(X).
Answers X=a;
X=c
student(a).
person(b).
person(c).
studying(c).
student(X) :- person(X), studying(X).
Answers X=a;
X=c
/*powerL3.pl*/
p(X, 0, 1).
p(X, Y, Z) :- Y > 0, A is Y - 1, p(X, A, B), Z is B * X.
In this program,
p(X, 0, 1).
defines a terminate case/condition
p(X, Y, Z) :- Y > 0, A is Y - 1, p(X, A, B), Z is B * X.
is the recursive rule.
Recursion - How does it work
p(X, 0, 1).
p(X, Y, Z) :- Y > 0, A is Y - 1, p(X, A, B), Z is B * X.
?-p(5, 2, Z).
5 2
p(X, Y, Z) :- Y > 0, A is Y - 1, p(X, A, B), Z is B * X.
Recursion - How does it work
p(X, 0, 1).
p(X, Y, Z) :- Y > 0, A is Y - 1, p(X, A, B), Z is B * X.
?-p(5, 2, Z).
5 2 2 1 2 5 1
p(X, Y, Z) :- Y > 0, A is Y - 1, p(X, A, B), Z is B * X.
Recursion - How does it work
p(X, 0, 1).
p(X, Y, Z) :- Y > 0, A is Y - 1, p(X, A, B), Z is B * X.
?-p(5, 2, Z).
5 2 2 1 2 5 1
p(X, Y, Z) :- Y > 0, A is Y - 1, p(X, A, B), Z is B * X.
5 1
p(X, Y, Z) :- Y > 0, A is Y - 1, p(X, A, B), Z is B * X.
Recursion - How does it work
p(X, 0, 1).
p(X, Y, Z) :- A > 0, A is Y - 1, p(X, A, B), Z is B * X.
?-p(5, 2, Z).
5 2 2 1 2 5 1
p(X, Y, Z) :- Y > 0, A is Y - 1, p(X, A, B), Z is B * X.
5 1 1 0 1 5 0
p(X, Y, Z) :- Y > 0, A is Y - 1, p(X, A, B), Z is B * X.
Recursion - How does it work
p(X, 0, 1).
p(X, Y, Z) :- Y > 0, A is Y - 1, p(X, A, B), Z is B * X.
?-p(5, 2, Z).
5 2 2 1 2 5 1
p(X, Y, Z) :- Y > 0, A is Y - 1, p(X, A, B), Z is B * X.
5 1 1 0 1 5 0
p(X, Y, Z) :- Y > 0, A is Y - 1, p(X, A, B), Z is B * X.
5 0 1
p(X, Y, Z).
Recursion - How does it work
p(X, 0, 1).
p(X, Y, Z) :- Y > 0, A is Y - 1, p(X, A, B), Z is B * X.
?-p(5, 2, Z).
5 2 2 1 2 5 1
p(X, Y, Z) :- Y > 0, A is Y - 1, p(X, A, B), Z is B * X.
5 1 1 0 1 5 0 1
p(X, Y, Z) :- Y > 0, A is Y - 1, p(X, A, B), Z is B * X.
5 0 1
p(X, Y, Z).
Recursion - How does it work
p(X, 0, 1).
p(X, Y, Z) :- Y > 0, A is Y - 1, p(X, A, B), Z is B * X.
?-p(5, 2, Z).
5 2 2 1 2 5 1
p(X, Y, Z) :- Y > 0, A is Y - 1, p(X, A, B), Z is B * X.
5 1 5 1 0 1 5 0 1 5 1 5
p(X, Y, Z) :- Y > 0, A is Y - 1, p(X, A, B), Z is B * X.
5 0 1
p(X, Y, Z).
Recursion - How does it work
p(X, 0, 1).
p(X, Y, Z) :- Y > 0, A is Y - 1, p(X, A, B), Z is B * X.
?-p(5, 2, Z).
5 2 2 1 2 5 1 5
p(X, Y, Z) :- Y > 0, A is Y - 1, p(X, A, B), Z is B * X.
5 1 5 1 0 1 5 0 1 5 1 5
p(X, Y, Z) :- Y > 0, A is Y - 1, p(X, A, B), Z is B * X.
5 0 1
p(X, Y, Z).
Recursion - How does it work
p(X, 0, 1).
p(X, Y, Z) :- Y > 0, A is Y - 1, p(X, A, B), Z is B * X.
?-p(5, 2, Z).
5 2 25 2 1 2 5 1 5 25 5 5
p(X, Y, Z) :- Y > 0, A is Y - 1, p(X, A, B), Z is B * X.
5 1 5 1 0 1 5 0 1 5 1 5
p(X, Y, Z) :- Y > 0, A is Y - 1, p(X, A, B), Z is B * X.
5 0 1
p(X, Y, Z).
Class Exercise
fib(1) = 1
fib(2) = 1
fib(n) = fib(n-1) + fib(n-2)
Class Exercise (Answer)
Write a program to calculate the nth fibonacci number.
fib(1) = 1
fib(2) = 1
fib(n) = fib(n-1) + fib(n-2)
/* fibL3.pl */
fib(1, 1).
fib(2, 1).
fib(N, Z) :- N > 2, N1 is N-1, N2 is N-2, fib(N1, Temp1),
fib(N2, Temp2), Z is Temp1+Temp2.
Iteration
Iteration (a loop) can be created by using recursive rules.
Iteration
Example
/* loopL3.pl */
loop(Index, Index) :- body(Index).
loop(Index, End) :- Index < End, body(Index), Index1 is Index + 1,
loop(Index1, End).
body(Index) :- write('In the loop with index = '), write(Index), nl.
?- loop(1, 3).
In the loop with index = 1
In the loop with index = 2
In the loop with index = 3
true
Iteration
?- loop(2, 2).
In the loop with index = 2
true
Iteration
A loop is normally created using two clauses. The two clauses
have the same head. One clause defines the terminating case
while the other executes the body, and then move the loop
into the next iteration.
Change index
Start a new iteration
Iteration
Example
loop(X) :- X == end.
loop(X) :- X \== end, body(X), write('enter X '), read(Y), loop(Y).
body(X) :- write(' X = '), write(X), nl.
[]
is an empty list (ie. a list contains no element).
Lists
The pre-defined predicate nth1/3 can be used to extract an
element of specific position.
Example
?- nth1(2, [1, 2, 3, 4], X).
X=2
A list can be divided into two components: the head and the
tail. The first element of the list is the head; the rest is the
tail of the list. The head is a term, and the tail is a list.
?-weekdays(X).
X = [monday, tuesday, wednesday, thursday, friday].
?-weekdays([First_day|Rest_of_days]).
First_day = m onday.
Rest_of_days = [tuesday, wednesday, thursday, friday].
Lists
?-weekdays([First_day,Second_day|Rest_of_days]).
First_day = monday.
Second_day = Tuesday.
Rest_of_Days = [wednesday, thursday, friday].
?-like([X|Y]).
X = pear.
Y = [].
Lists
Example
/* memberL.pl */
memberL(X, [X|T]).
memberL(X,[Y|T]) :-memberL(X,T).
Example
?- member(4, [3, 5, 4, 6]).
true
?- week_days(X), writeL(X).
monday
tuesday
wednesday
thursday
friday
X = [monday,tuesday,wednesday,thursday,friday] ;
false
Lists
Example
week_days([monday, tuesday, wednesday, thursday, friday]).
lengthL([],0).
lengthL([H|T], N):-lengthL(T, N1), N is N1+1.
[a, b, c] [g, h]
[b, c] [a, g, h]
[c] [a, b, g, h]
[] [a, b, c, g, h]
Lists
Example
week_days([monday, tuesday, wednesday, thursday, friday]).
appendL([], L2, L2).
appendL([H|T], L2, [H|L3]) :- appendL(T, L2, L3).
Example
?- forall(member(X, [purple, blue, green, orange, red]),
(write(X),tab(4))).
reverseL([X], [X]) :- !.
reverseL([H|T], R) :- reverseL(T, R1), appendL(R1, [H], R).
Strings
A string is a sequence of symbols enclosed in a pair of single
quotation marks.
[104,101,108,108,111]
[116,104,101,114,101]
Strings
A built-in predicate name/2 can be utilized to convert between
a string and a list of ASCII codes.
st1(‘hello’).
st2(‘there’).
X = [104,101,108,108,111]
Y = [116,104,101,114,101]
Z = [104,101,108,108,111,116,104,101,114,101]
W = hellothere.
For example
?- convert(‘abba’, Y).
Y = ABBA;
false
Class Exercise (Answer)
/* convert.pl */
modify([], X, []).
modify([H|T], X, [NH|NT]) :- NH is H - X, modify(T, X, NT).
Example
postive(X) :- X > 0.
greaterThan(X, Y) :- greaterThan(X, Z), greaterThan(Z, Y).
The X in the first clause and the X in the second clause are
irrelevant.
Example
student(may, smith, 21061985, sbcs, 2021).
book(prolog, ivan, bratko, cs, ai, ai_language).
borrow (21061985, prolog).
?- studnet(X, Y, Z, 2021).
X = names(may, smith),
Y = 21061985,
Z = sbcs;
?- book(prolog, X, Y).
X = author(ivan, bratko),
Y = category(cs, area(ai, subject(ai_language)));
Structures
functor/3 is a built-in predicate that can be utilized to examine
a structure.
Example
into
Example
?- person(names(may, smith), 670903) =.. Y.
Y = [person, names(may, smith), 670903] ;
?- clause(body(X), Y).
Example
1 ?- assert(rain(yesterday)).
true.
2 ?- assert(rain(today)).
true.
3 ?- rain(X).
X = yesterday ;
X = today.
Built-in Predicates
4 ?- assert(rain(X)).
true.
5 ?- rain(anyDay).
true.
Built-in Predicates
The built-in predicate retract/1 functions in the opposite way as
assert/1. retract(X) deletes the occurrence of X in the database
of the PROLOG executor.
Built-in Predicates
Example
1 ?- assert(rain(yesterday)), assert(rain(today)).
true.
2 ?- rain(X).
X = yesterday ;
X = today.
2 ?- retract(rain(X)).
X = yesterday ;
X = today.
3 ?- rain(X).
false.