Prolog 2
Prolog 2
Programming in Prolog
An Introduction
Part 2
Lecturer: Saeid Abrishami
What is a Term?
Term
Atom Number X
mary 57 Person Structures Lists
ball 1.618 Stu_Avg
_257 date(2010,may,3) []
ball22 2.04e-27 mother(ali)
stu_avg -13.6 _ [4,6,7]
‘Mary’ [ali,reza]
‘john smith’ [[3,4],[7,8]]
Structures
• To create a single data element from a collection of
related terms we use a structure.
• A structure is constructed from a functor (a constant
symbol) and one or more components.
• Examples:
– date(12, may, 2010)
– father(ali)
– student(ali, ahmadi, computer, 18.13)
– person(reza, razavi, date(15,november,1978))
• Same as functions in FOL
3
Structures
birth(ali, date(15,october,1996)).
birth(ahmad,date(21,december,2002)).
birth(reza,date(14,may,1992)).
?- birth(ahmad,X).
X = date(21, december,2002)
?-birth(reza,date(X,Y,Z)).
X=14, Y=may, Z=1992
?-birth(ali,date(_,_,X)).
X=1996
?-birth(X,date(_,december,_)).
X=ahmad
4
Lists
• A collection of ordered data
• Has zero or more elements enclosed by square
brackets (‘[ ]’) and separated by commas (‘,’).
• Examples
– [10,13,5]
– [ali,reza,ahmad]
– []
– [ali,6,12,[reza,4],4.56]
• Like any object, a list can be unified with a variable
– L = [4,8,12,6,23].
5
Lists
• Internally, lists are just structures
• To represent a list as a structure, we have to
consider two cases:
– Empty list: written as a prolog atom []
– Non-empty list: it consists of two parts:
• the first item called head
• the remaining part called tail
– the head and the tail are then combined into a
structure by a special functor . (dot)
– . (head, tail)
– [a,b,c] is represented as .( a, .(b, .(c, [])))
6
Lists
• Bar Notation
– [Head | Tail]
• Head must unify with a single term
• Tail must unify with a list, including empty list
• It can be used to extract head and/or tail from a list:
– ?- [5, 12, 3, 6] = [H | T]
H=5
T = [12, 3, 6]
– ?- [7, 10 , 4, 3] = [X, Y | Rest]
X=7
Y=10
Rest = [4,3]
– ?- [5] = [H | T]
H=5
T=[ ]
7
Lists - Membership
• member(X,L)
– it will be true if object X occurs in list L
• First try
member(X, [X | T]).
member(X, [Y | T]) :- member(X,T).
• Second try
member(X, [X | _ ]).
member(X, [ _ | T]) : - member(X,T).
8
Lists - Membership
member(5,[10,3,5,8,12]) member(X, [X
[ _ || _]).
T]) :- member (X,T).
{X=5, T=[3,5,8,12]}
member(5,[3,5,8,12]) member(X, [X
[ _ || _]).
T]) :- member (X,T).
{X=5, T=[5,8,12]}
{X=5}
[]
9
Lists - Concatenation
• conc(L1, L2, L3)
– it will be true if list L3 is the concatenation of lists
L1 and L2
– example: conc([3,5],[10,2,6],[3,5,10,2,6]) is true.
• Definition
conc([ ] , L, L).
conc([X | L1] , L2, [X | L3]) :- conc(L1, L2, L3).
X L1 L2
L3
10
Lists - Concatenation
• Some Examples:
?- conc([2,3],[5,8,1],[2,3,5,8,1])
Yes
?- conc([2,3],[5,8,1],X)
X=[2,3,5,8,1]
?-conc([2,3], X,[2,3,5,8,1])
X=[5,8,1]
?-conc([2,3],X,[4,3,5,6,1])
No
11
Lists - Concatenation
{L=[5,8,2], L3=[5,8,2]}
[]
12
Lists – Deletion
• del(X, L, L2)
– It will be true if list L2 is equal to list L wit
the item X removed.
• Definition:
del(X, [X | T], T).
del(X, [Y | T], [Y | T2]) :- del (X,T,T2).
13
Lists – Deletion
• Some Examples:
?- del(5,[3,9,5,2,6],[3,9,2,6]).
Yes
?- del(5,[3,9,5,2,6],X).
X=[3,9,2,6]
?-del(X,[3,9,5,2,6],[3,9,2,6]).
X=5
?-del(5,X,[3,9,2,6]).
X=[5,3,9,2,6]
14
Lists – Deletion
del(5,[2,3,5,8,1],D) del(X, [Y | T], [Y | T2]) :- del(X,T,T2).
{X=5, Y=2, T=[3,5,8,1], D=[2 | T2]} D = [2, 3, 8, 1]
[]
15
Lists – Sublist
• sublist(S,L)
– It will be true if list S is a sublist of list L
– Example:
• sublist([3,5],[4,3,5,12,6]) is true.
• sublist([3,5],[4,3,7,2,5,12]) is false.
• Definition: L
sublist(S,L) :- L1 S L3
conc(L1,L2,L), L2
conc(S,L3,L2).
16
Arithmetic
• There are some predefined operators
for arithmetic operations:
Symbol Operation Symbol Operation
+ addition > greater than
- subtraction < less than
* multiplication
>= greater than
/ real division or equal
// integer <= less than or
division equal
mod modulus
=:= equal
** power
=/= not equal
17
Arithmetic
• Clearing up equality
X = Y succeeds if the terms X and Y unify.
18
Arithmetic
| ?- 3+4 = 4+3. | ?- 3+4 =:= 4+3.
no % treats them as terms yes % calculates both values
| ?- 3+4 =\= 4+3.
| ?- 3+4 = 3+4.
no
yes
| ?- 3+4 == 4+3.
| ?- X = 4+3, X=7.
no
no
| ?- 3+4 \== 4+3.
| ?- X is 4+3, X=7. yes
yes | ?- 3+X = 3+4.
| ?- 3+4 is 4+3. X = 4
no % left arg. has to be a term yes
| ?- 3+X == 3+4.
no
19
Arithmetic
• Factorial relation:
factorial(0,1).
factorial(N, F) :-
N > 0,
N2 is N – 1,
factorial(N2, F2),
F is N * F2.
20
Controlling Backtracking
• Sometimes backtracking is not desirable, so we can
prevent it using cut or !
• We use it as a goal within the body of clause.
• It succeeds when called, but fails the parent
goal (the goal that matched the head of the clause containing the
cut) when an attempt is made to redo it on
backtracking.
• It commits to the choices made so far in the
predicate.
– unlimited backtracking can occur before and after the cut
but no backtracking can go through it.
21
Cut !
a:- b, c, d, e, !, f, g, h, I, j .
a:- k.
a:- m .
a:- b, c, d, e, !, f, g, h, I, j .
Treated as if
a:- k. This clause and
these choices
don’t exist a:- m . committed to
22
Cut !
• We should only use a cut if the clauses are mutually
exclusive (if one succeeds the others won’t).
• An Example:
f(X,0):- X < 3.
f(X,1):- 3 =< X, X < 6.
f(X,2):- 6 =< X.
?- f(2,Y), Y>0.
No
23
Cut !
• A better definition:
f(X,0):- X < 3, !.
f(X,1):- 3 =< X, X < 6, !.
f(X,2):- 6 =< X.
?- f(2,Y), Y>0.
No
• Notice that the answer is still the same, with or without the cut.
– This is because the cut does not alter the logical behaviour of the
program.
– It only alters the procedural behaviour: specifying which goals get
checked when.
• This is called a green cut. It is the correct usage of a cut.
24
Cut !
• A more efficient definition:
f(X,0):- X < 3, !.
f(X,1):- X < 6, !.
f(X,2).
25
fail
• As well as specifying conditions under which
a goal can succeed sometimes we also want
to specify when it should fail.
• We can use the built-in predicate fail
which always fails, thus forcing the parent
goal to fail.
• Example: “Mary likes all animals except snakes.”
– like(mary, X) :- snake(X), !, fail.
– like(mary, X) :- animal(X).
26
Negation as Failure
• This example indicates that it would be
useful to have a unary predicate ‘not’ such
that not Goal is true if Goal is not true.
• Example: “Mary likes all animals except snakes.”
like(mary, X) :- animal (X), not snake(X).
• Note that not defined as failure, as here, does not
exactly correspond to negation in mathematical
logic.
• not Goal is the same as \+ Goal
27
Built-in Predicates
• Built-in prdicates for I/O
– write(X)
– nl
– read(X)
• These predicates are always true.
• There are other predicates for files
(reading and writing)
• See chapter 6 and 7 of the book.
28