79
7/15/2019 PROLOG cheatsheet http://slidepdf.com/reader/full/prolog-cheatsheet 1/79 2/20/2013 1 Prolog Programming Introduction to logic programming Declarative vs. procedural Prolog components: facts, rules, queries How logic programming works Sample Prolog programs Prolog exercise

PROLOG cheatsheet

Embed Size (px)

DESCRIPTION

PROLOG cheatsheet

Citation preview

Page 1: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 1/79

2/20/2013

1

Prolog Programming

Introduction to logic programming

Declarative vs. procedural

Prolog components: facts, rules, queries

How logic programming works

Sample Prolog programs

Prolog exercise

Page 2: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 2/79

2/20/2013

2

Prolog (PROgramming in LOGic)

Declarative (though they also have a procedural element)

▪ Knowledge engineer provides logic and specifies a goal to beachieved

▪ Specify what problem needed to be solved rather than how to solve it

▪ Describe properties of objects and relations between them (i.e. usingif-then rules)

▪ colour(roses, red). if cheeky(X) then happy(X)

cheeky(danial)therefore happy(danial)

Non-procedural

▪ Unlike conventional procedural languages (i.e. programmer specifysteps to solve a problem), Prolog interpreter, on the other hand,works out how to achieve the goal.

JAVA PROLOG

Page 3: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 3/79

2/20/2013

3

How logic programming works

Logic programming uses a collection of facts and

rules stored in the knowledge base.

Query as mode of operation

▪ by posing queries (i.e. questions) about information in

the knowledge base

▪ Prolog infers from the facts and rules to answer queries.

Consider the following Prolog program:

and the following queries:

?- animal(fluffy). Is fluffy an animal?

?- cat(X). Is it possible to find cat?, let’s call it X

?- cat(mischief). Is mischief a cat?

?- animal(belle). Is belle an animal?

Explain how Prolog responds to the above queries.

Page 4: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 4/79

2/20/2013

4

For the 1st query: given that any X is an animal if X is a cat, and fluffy is a cat, Prolog infers fluffyis an animal.

For the 3rd query: mischief is not a cat -- on apresumption that, as far as the program isconcerned, what is not currently known to be true

is false (i.e. closed world assumption).

For the 4th query: rules declare things that aretrue depending on a given condition/facts.

Page 5: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 5/79

2/20/2013

5

Write a program to put the following logical

statement into Prolog rule.

X is happy if X is cheeky (for any X)

By adding appropriate facts into a knowledgebase and posing queries, try to understand how

Prolog derives answers to the queries.

cheeky(danial).

% danial is cheeky

happy(X) :- cheeky(X).

% For all X, X is happy if X is cheeky

?- cheeky(X).

X = danial

?- happy(X).

X = danial

?- happy(danial).

yes

?- cheeky(danial).

yes

Page 6: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 6/79

2/20/2013

6

PROLOG: Programming for Artificial Intelligence by Ivan Bratko

(Addison Wesley)

Page 7: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 7/79

5/3/2013

1

Prolog Programming

Prolog consists of data object of different

types:

atoms, numbers, variables, compound terms such

as structures, and lists

▪ they differ by its syntactic form

Also called terms

Page 8: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 8/79

5/3/2013

2

An atom can be written in several ways: begins with a lower-case letter followed by letters

(either case) or digits, or the underscore character.▪ cheeky red_apple wAES2106

using special characters + - * / = < > : . ~ whichmay be in the form of sequence of characters or asingle character▪ :- predefined meaning in Prolog, read as ‘if’

▪ % for insertion of a line of comment

a string of any characters enclosed within singlequotes.▪ ‘WAES2106’ ‘123abc’ ‘Kuala Lumpur’ ‘Suite 12-8’

Consists of sequence of integer or real

numbers

with/without a + or – sign

may contain a single decimal point, anywhere

except before a + or - sign

▪ -123 +25 12.3 -.123 +246.

Page 9: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 9/79

5/3/2013

3

Begins with a capital letter, which can befollowed by letters (either case) or digits, orthe underscore character.

▪ X123 Y Zara Kuala_Lumpur

Anonymous variables

using the underscore character ‘_’ for such case

where we are not interested to know the value of aparticular variable

for all Y, Y is a mother if Y has a daughter

▪ mother(Y) :- daughter(_,Y).

Begins with an atom, known as functor ,

followed by one or more arguments (separated

by a comma) and enclosed within parentheses.

Each argument is a term of any kind

(including a compound term)

Number of arguments is called its arity

▪ colour(roses, red) happy(X) likes(myra, durian)

▪ student('Sarah Ahmad', dob(12, february, 2000), age(12))

▪ substract(10, 2, 8)

Page 10: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 10/79

5/3/2013

4

An ordered collection of terms of any kind

(separated by a comma) and enclosed within

square brackets

▪ [ ] an empty list

▪ ['kuala lumpur', 'shah alam', kuching]

▪ [[white, blue, green], [yellow, orange, red]]

▪ [prolog, ali, publication_year(22, january, 2012)]

To satisfy a goal, Prolog works through the

clauses in the KB in turn, from top to bottom

until a match is found.

happy(X) and happy(danial) are identical, thus can

be matched/unified by instantiating variable X to

danial (i.e. giving X the value)

Page 11: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 11/79

5/3/2013

5

If Term1 and Term2 are constants, then they

match only if they are the same object

apples = apples

If Term1 is a variable and Term2 is anything,

then they match. Term1 is instantiated toTerm2 (and vice versa)

likes(myra, X) = likes(Y, chocolate)

▪ X = chocolate Y = myra

If Term1 and Term2 are structures, then they

match only if 

Term1 and Term2 have the same principal functor

all their corresponding components match

date(D,F, 2012) = date(D1, february, Y1)

▪ D = D1 (binds two differently named variables to single,

unique variable)

▪ F = february (F is instantiated to february )

▪ Y1 = 2012 (Y1 is instantiated to 2012)

Page 12: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 12/79

5/3/2013

6

Term 1 Term 2 Match?

doll X

parent(X, sarah) Y

colour (blue, green, yellow) colour(X, Y)

colour (a, b, c) col(a, b, c)

a b

parent(X, Y, Z) parent(ali, 35, doctor)

[a, cake(X, Y), B) [Q, R, c]

Term 1 Term 2 Match?

doll X Succeed

X = doll

parent(X, sarah) Y Succeed

Y = parent(X, sarah)

X is still unbound

colour (blue, green, yellow) colour(X, Y) Fails. The terms have different arity

colour (a, b, c) col(a, b, c) Fails. The terms have different functor 

a b Fails as they are not identical

parent(X, Y, Z) parent(ali,35, doctor) Succeed.X, Y, Z are instantiated to ali,

35, doctor, respectively

[a, cake(X, Y), B] [Q, R, c] Succeed.

Q = a

R = cake(X, Y)

B = c

Page 13: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 13/79

5/3/2013

7

chases(X,Y) :- cat(X), bird(Y).

Procedural reading:

▪ determines how Prolog satisfies a list of goals

▪ depend on the order of goals and clauses

▪ The procedural meaning is to satisfy chases(X,Y), first satisfy

cat(X), then satisfy bird(X)

big(bear).

big(elephant).

small(cat).

brown(bear).

black(cat).

gray(elephant).

dark(Z) :- black(Z).

dark(Z) :- brown(Z).

?- dark(X), big(X).

Page 14: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 14/79

5/3/2013

8

Consider the following rule:

chases(X,Y) :- cat(X), bird(Y).

Declarative reading:

▪ Determines whether a given goal is true and if so, for

what values of variables it is true.

▪ The declarative meaning says chases(X,Y) is true if X and Y are

true

A variant of a clause C

an instance of the clause C where each variable is substituted by another

variable

An instance of a clause C the clause C with each of its variables substituted by some term

father_of(X,Y) :- parent(X,Y), male(X).

father_of(A,B) :- parent(A,B), male(A). % variant of clause

father_of(tom,Y) :- parent(tom,Y), male(tom). % an instance of clause

Page 15: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 15/79

5/3/2013

9

Given a program and a goal G, the declarative

meaning says:

G is true if and only if:

(1) there is a clause (i.e. C) in the program such that

(2) there is a clause instance (i.e. I) of C such that

(a) the head of I is identical to G, and

(b) all the goals in the body of I are true

male(tom).

parent_of(tom,sarah).

father_of(X,Y):-parent_of(X,Y), male(X).

G is father_of(tom,Y)

The program has C (i.e. rule) father_of(X,Y):-parent_of(X,Y), male(X)

and C (i.e. facts) parent_of(tom,sarah)and male(tom)

then father_of(tom,Y):-parent_of(tom,Y), male(tom)

and then

I is an instance of C father_of(tom,sarah) :- parent_of(tom, sarah), male(tom)

Page 16: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 16/79

5/3/2013

10

Write a program to put the following logical statement intoProlog rule.

For all X and Y,X will be jealous of Y if X likes Z andY likes Z

By adding appropriate facts into a knowledge base andposing the query, ?- jealous(X,Y), try to understandand trace how Prolog derives answer(s) to the query.

likes(irfan, zara).likes(danial, zara).

jealous(X, Y):-likes(X, Z),

likes(Y, Z).

Consider the following query,?- jealous(X, Y). There are four

ways Prolog can satisfy the query. Work through the program carefully

and explain how does Prolog answer the above query by giving four

different solutions.

Page 17: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 17/79

5/3/2013

11

PROLOG: Programming for Artificial Intelligence by Ivan Bratko (4th

edition), Addison Wesley.

Page 18: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 18/79

15/3/2013

1

Prolog Programming

Proof search (and search tree)

How Prolog searches a KB to see if a query issatisfied.

Constructing Prolog program

family.pl

Page 19: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 19/79

15/3/2013

2

Prolog process query, searching the KB in left

to right depth-first order to find out whether

a query can be satisfied.

p(a).p(b).q(a).q(b).r(b).

w(X) :- p(X), q(X), r(X).

?- w(What).

?- w(What).

Page 20: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 20/79

15/3/2013

3

p(a).p(b).q(a).q(b).r(b).w(X) :- p(X), q(X), r(X).

What = X

X = a X = b

Fails – backtrack!

?- w(What).What = b

?- w(What).

?- p(X), q(X), r(X).

?- q(a), r(a).

?- r(a).

?- q(b), r(b).

?- r(b).

likes(irfan, zara).likes(danial, zara).

 jealous(X, Y):-likes(X, Z),

likes(Y, Z).

?- jealous(A, B).

?- jealous(X,Y).

Page 21: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 21/79

15/3/2013

4

likes(irfan, zara).likes(danial, zara).

 jealous(X, Y):-likes(X, Z),likes(Y, Z).

X = A Y = B

X = irfan Y = zara X = danial Y = zara

Y = irfan Y = danial Y = irfan Y = danial

?- jealous(A, B).

X= Y = irfan

X = irfan Y = danialX = danial Y = irfanX = Y = danial

?- jealous(X,Y).

?- likes(X,Z), likes(Y,Z).

?- likes(Y,zara) ?- likes(Y,zara)

Consider the following Prolog clauses:

v('A').v(a).x(b).w(c,'A').

w(c, b).m(X,Y,Z):-v(X), x(Y), w(Z,X).

Give all the variable instantiations during Prolog execution for the query?- m(X,Y,_). Use backtracking to force Prolog to find more than onesolution.Where relevant, indicates whether the instantiations lead tofailures.

Draw the proof search tree for the query.

Page 22: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 22/79

15/3/2013

5

v('A').v(a).x(b).w(c,'A').w(c, b).m(X,Y,Z):-v(X), x(Y), w(Z,X).

?- m(X,Y,_).

X = ‘A’

Y = b ;

no.

Variable instantiations during Prolog execution.

Step 1

X = ‘A’

Y = bZ = c

Step 2 (backtrack)

X = ‘A’

Y = bZ = c

b\= ‘A’

Step 3 (backtrack)

X = a

Y = b

‘A’ \= a

b \= a

Write a Prolog program called family.pl which contains information about family of 

three generations as shown below.

Define the following relations using rules in Prolog.- father- mother- sister- aunty- cousin

Page 23: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 23/79

15/3/2013

6

What will be Prolog’s answers to the following queries?

sister(X,Y).

father_of(david,_).

aunty(X, james).

aunty(patricia, dora).

cousin(james, X).

mother_of(X,betty), aunty(X, james).

\+parent_of(tom, patricia).

parent_of(max,_); cousin(max, betty).

Page 24: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 24/79

15/3/2013

7

?- sister(X, Y).

X = sarah ,

Y = max ;

X = sarah ,

Y = patricia ;

X = patricia ,

Y = max ;

X = patricia ,

Y = sarah ;

X = dora ,

Y = betty ;

X = betty ,

Y = dora ;

no

?- father_of(david, _).

yes

?- aunty(X, james).X = sarah ;

no

?- aunty(patricia, dora).yes

?- cousin(james, X).

X = dora ;

X = betty ;

no

Conjoined goals  AND

?- mother_of(X, betty), aunty(X, james).X = sarah ;

no

Negative goal NOT 

?- \+parent_of(tom, patricia).

no

Disjoint goals OR

?- parent_of(max, _); cousin(max, betty).

noBacktracking producedduplicate results! Prolog

regards that each answersas separate pieces of data.

Consider the following clause:play(dora, doll).

?- play(dora, What). % Dora plays with what?What = doll.

go:-play(dora, What),

write('Dora likes to play with '), write(What), nl.

predicate go/0 (with no argument) enables query to be entered brief, i.e.

?- go.

Dora likes to play with doll

yes

Page 25: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 25/79

27/3/2013

1

Prolog Programming

So far, we used functor, followed by a number of arguments inparentheses to represent fact, e.g. likes(myra, durian)

As alternative, the fact can be written using infix operator .

This enables functor (i.e. likes) to be written between the two

arguments with no parentheses: myra likes durian.

To aid readability, you may write rule such as:likes(dora, X) :- cat(X)

as dora likes X :- X is a cat.

Page 26: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 26/79

27/3/2013

2

To perform arithmetic calculations:

+ addition

- subtraction

* multiplication

 / division The result is the whole number part of a division

mod remainder The result is the remainder of a division

sqrt(X)

The built-in predicate 'is' is used to evaluate an expression. Examples:

?- X is 1 + 2.

X = 3

?- X is 3 + 5

X = 8

X > Y X is greater than YX < Y X is less than YX >= Y X is greater than or equal to YX =< Y X is less than or equal to YX =:= Y the values of X and Y are equalX is Y X is Y, if X matches the value of Y (where X is a variable or a constant

and Y is an arithmetic expressionX \== Y X is not literally equal to YX = Y X is equal to Y, if X and Y match

Examples:

?- 4 > 3. ?- 2 =:= 2.yes yes

?- X is 2 + 2, X < 2. ?- 2 \== 3.no yes

?- X is 2 + 2. ?- zara = zara.X = 4 yes

Page 27: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 27/79

27/3/2013

3

?- Y is 10, Z is Y+1.

?- X is sqrt(36).

?- 10 is 7+13-11+9.

?- 88+15-3=:=110-5*2.

?- 6+4=:=6*3-8.

Consider the following rules:

greater_than(X,Y) :- X > Y.

add_3_and_double(X,Y) :- Y is (X+3)*2.

and the following queries: ?- greater_than(2,5).

?- add_3_and_double(2,Y)

Define a predicate sum that holds only when its 3rd argument is the sum of 

the first two arguments. For example, sum(1,2,3) should hold, but

sum(2,2,5) should not

Y = 10 , Z = 11

X = 6

no

yes

yes no

Y = 10

sum(X, Y, Z) :- Z is X+Y.

Page 28: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 28/79

27/3/2013

4

In ordinary arithmetic, 1+2*3 means (1+(2*3)) and 6-3-1 means (6-3)-1

(i.e. from left to right - equal precedence). Prolog follows the ordinary

arithmetic standard usage.

1+2*3 expression has the structure +(1,*(2,3))

6-3-1 has the structure -(-(6,3),1)

Consider the following expression:

a + b * c

In Prolog, the convention is that, operators with lower precedence bind

stronger

The operator with the highest precedence in a term is the principle functor of the term.

Therefore in Prolog, the precedence of + is defined higher than the

precedence of * (and therefore * binds stronger than +).

The precedence of operators decides what is the correct interpretation of an

expression, and so the expression a + b * c is understood as +(a,*(b, c))

and not as *(+a,b),c).

Page 29: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 29/79

27/3/2013

5

Prolog operators: binary

Infix: An infix operator appears between its two arguments (e.g. 2+3)

xfx non-associative

xfy right to left

yfx left to right

Prolog operators: unary

Prefix: Prefix operator appears before its single argument (e.g. –8)

▪ fx non-associative

▪ fy left to right

Postfix: Postfix operator appears after its single argument (e.g. 2 factorial)

▪ xf non-associative

▪ yf right to left

The definition of an operator includes three parts and is accomplished by issuing a directive

to Prolog.

op(<Precedence>, <Type>, <Operator>)

<Precedence> is an integer between 1 and 1200, the higher the number, the higher the

precedence of the operator. Atoms always have precedence 0.

<Type> is one of 

xfx, xfy, yfx for binary operators,

fx, fy for prefix unary operators,

xf, yf for postfix unary operators

<Operator> is an atom or a list of atoms that represents the operator symbol(s). It is possible

to define two operators with the same symbol, as long as the type of each is different i.e.

infix, prefix or postfix.

xfx is non associative operator, which suggests that the operator is between two arguments.

‘f’ stands for the operator which acts as a point of reference to distinguish between the left

and right arguments (left or right for unary operators).

'x' and 'y' denote arguments.

xfy defines a right-associative operator and the type yfx, a left-associative operator.

Page 30: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 30/79

27/3/2013

6

:-op(300,yfx,-).

?- display(4-1-2).

-(-(4,1),2)

yes

?- X is 4-1-2.

X = 1

:-op(300,xfy,-).

?- display(4-1-2).

-(4,-(1,2))

yes

?- X is 4-1-2.

X = 5 wrong

:-op(500,yfx,-).

:-op(400,yfx,/).

?- display(5-6/2).

-(5,/(6,2))

yes

?- X is 5-6/2.

X = 2

:-op(300,yfx,-).

:-op(400,yfx,/).

?- display(5-6/2).

 /(-(5,6),2)

yes

?- X is 5-6/2.

X = -0.5 wrong

Page 31: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 31/79

27/3/2013

7

:- op(500, yfx, [+,-]).

?- display(3-2+1).

+(-(3,2),1)

yes

?- X is 3-2+1.

X = 2

:- op(500, xfy, [+,-]).

?- display(3-2+1).

-(3,+(2,1))

yes

?- X is 3-2+1.

X = 0 wrong

:-op(500,yfx,+).

:-op(400,yfx,*).

?- display(1+2*3).

+(1,*(2,3))

yes

?- X is 1+2*3.

X = 7

:-op(400,yfx,+).

:-op(500,yfx,*).

?- display(1+2*3).

*(+(1,2),3)

yes

?- X is 1+2*3.

X = 9 wrong

Page 32: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 32/79

27/3/2013

8

:- op(1200, xfx, [:-, -->]):- op(1200, fx, [:-, ?-]):- op(1100, xfy, ';’):- op(1050, xfy, ->):- op(1000, xfy, ‘,’):- op(700, xfx, [=, \=, ==, @<. @=<. @>, @>=, is,

=:=, =\=, <, >, =< <=, >=]):- op(500, yfx, [+, -]):- op(400, yfx, [*, /, //, mod]):- op(200, xfx, **)

Using Prolog predefined arithmetic operators as follows:

:-op(500,yfx,[+, -]).

:-op(400,yfx,[/, *]).

What is the structure of the following arithmetic expression?

6/3-1*2+3

Page 33: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 33/79

27/3/2013

9

?- display(6/3-1*2+3).

+(-(/(6,3),*(1,2)),3)

yes

?- Answer is 6/3-1*2+3.

Answer = 3

:-op(600, xfx, likes).

likes(myra,durian).

?- myra likes What.

What = durian

:-op(100,fy,turn_on).

turn_on(the_tv).

?- turn_on the_tv.

yes

?- turn_on What

What = the_tv

Page 34: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 34/79

27/3/2013

10

Consider the following statement:

danial likes drawing and reading

and the following operator definitions:

:- op(300,xfx,likes).:- op(200,xfy,and).

Note that, an operator for 'and' can be defined as right-associative operatoraccording to Bratko (Prolog Pogramming for Artificial Intelligence).

How is this this statement understood by Prolog? What is theirstructure?

Give 3 Prolog queries to demonstrate your answer.

likes(danial, and(drawing, reading))

danial likes What.What = drawing and reading

danial likes What and reading.What = drawing

danial likes drawing and What.What = reading

Page 35: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 35/79

27/3/2013

11

Define appropriate operators (‘and’, ‘inside’) to be able to write

sentence as follow:

ball and marble inside blue_box inside red_box

and then ask Prolog the following query:

?- What and What2 inside blue_box inside red_boxWhat = ball

What2 = marble

:-op(300,xfy,and).

:-op(400,yfx,inside). % left associative

inside(inside(and(ball, marble), blue_box), red_box)

?- What and What2 inside blue_box inside red_box.

What = ball ,

What2 = marble

Page 36: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 36/79

27/3/2013

12

Change the following statement into Prolog rule.

For all X and Y,

X chases Y if X is a dog and Y is a cat.

Define appropriate operator ‘chases’ so that Prolog can answer

the following query:

?- Who chases Whom.

:-op(700,yfx,chases).

cat(tom).

dog(buddy).

chases(X,Y) :- dog(X), cat(Y).

?- Who chases Whom. display

Who = buddy ,

Whom = tom

Page 37: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 37/79

27/3/2013

13

PROLOG: Programming for Artificial Intelligence by Ivan Bratko

(Addison Wesley)

Page 38: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 38/79

29/3/2013

1

Prolog Programming

Prolog offers 2 ways to perform computations repetitively:

Backtracking (more on this in the next few lectures)

Recursion

Recursive programming One of the most basic and most important concepts in computer

science (and mathematics) in general.

Page 39: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 39/79

29/3/2013

2

The basic idea of recursion principle is the following:

To solve a complex problem, provide the solution for the simplestproblem of its kind (i.e. base case) and provide a rule for transformingsuch a (complex) problem into a slightly simpler problem (i.e. a recursionrule)

Recursive rule means a rule calls itself until some final point is reached (i.e.the base case).

In Prolog what this means is that we have a first fact that acts as somestopping condition followed up by some rule(s) that performs someoperation before reinvoking itself.

Suppose we want to print a table of the integers 1 to 5 and theirsquares:

1 1

2 43 9

4 16

5 25

Page 40: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 40/79

29/3/2013

3

The recursive algorithm for the problem is as follows:

To print square beginning with N:

If N > 5,

print bye

Otherwise,

print N and N2, then print squares beginning with N + 1.

Exercise

Now, transform the above algorithm into Prolog clauses.

In Prolog:

print_squares(N) :- N > 5, write(‘bye’), nl.

print_squares(N) :-

S is N * N,

write(N), write(' '), write(S), nl,

M is N + 1,

print_squares(M).

Page 41: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 41/79

29/3/2013

4

The sample of query is as follows:

| ?- print_squares(1).

1 1

2 4

3 9

4 16

5 25bye

yes

Write a Prolog program to print integers from Last to First inclusive. Thesample run is as follow:

?- print_integers(5,2).

5

4

3

2

end

yes

Page 42: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 42/79

29/3/2013

5

print_integers(First, First) :- write(First), nl,

write('end'), nl.

print_integers(Last, First) :- Last=\=First,

write(Last), nl,

N is Last-1,

print_integers(N, First).

Consider facts and a rule given below:

en_route(kajang).

en_route(Place):-

travel(Place,Travelling_by,NewPlace),

en_route(NewPlace).

travel(brighton,train,london).

travel(london,plane,dubai).

travel(dubai,plane,kuala_lumpur).

travel(kuala_lumpur,car,kajang).

?- en_route(brighton).

Page 43: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 43/79

29/3/2013

6

What happens when we submit this query :

?- en_route(brighton).

Explain your answer.

What is Factorial?

The factorial N! of a natural number N is defined as the productof all natural numbers from 1 to N .

The factorial of 1 is 1.

The factorial any larger integer N  is N times the factorial of N-1.

Page 44: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 44/79

29/3/2013

7

Here’s a more formal, recursive definition:

1! = 1 (base case)

n! = (n − 1)! · n for n > 1 (recursion rule)

What is the answer for 5! ?

5! = 5 x 4 x 3 x 2 x 1 = 120

The definition of a Prolog predicate to compute factorials:

factorial(1, 1). % base case

factorial(N, Result) :- % recursion step

N > 1,

N1 is N - 1,

factorial(N1, Temp),

Result is Temp * N.

Provide Prolog trace for:?- factorial(3, Result).

Page 45: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 45/79

29/3/2013

8

How it works?

To compute the 3! we have to pass through the second part ofthat definition 2 times until we get to the base case and are ableto calculate the overall result.

The procedure factorial calls itself to compute the factorial of thenext smaller integer, then uses the result to compute the factorialof the integer, 3.

The recursion stops when the number whose factorial is to becomputed is 1.

Page 46: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 46/79

18/4/2013

1

Prolog Programming

Lists is a sequence of Prolog objects of any kind and

can also be lists.

An empty list in Prolog:

[] A non empty:

[violet, indigo, green, yellow].

The first item (i.e. violet) is the head of the list.

The remaining part of the list is the tail.

The head can be any Prolog data objects, however,

the tail has to be a list.

Page 47: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 47/79

18/4/2013

2

What are the head and tail for the following lists?

[do, [re, mi]]

[do, re, [mi]]

[[do]]

[do, [re], mi]

 Example of prolog query:

?- [Head | Tail] = [do, re, mi].

Head = do, Tail = [re, mi]

Page 48: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 48/79

18/4/2013

3

[do, [re, mi]]Head = do,Tail = [[re, mi]]

[do, re, [mi]].Head = do,Tail = [re, [mi]]

[[do]]Head = [do],

Tail = []

[do, [re], mi]Head = do,Tail = [[re], mi]

Given the lists 1 and 2 below, what are the values for the instantiated

variables?

 Lists 1 Lists 2

[X, Y | Z] [do, re, mi]

[re] [X | Y]

[Orange | Apple] [[do], re, mi]

[X | [Y | [Z | W]]] [do, re, mi]

 Example of prolog query:

?- [X, Y | Z] = [do, re, mi].

X = do, Y = re, Z = [mi]

Page 49: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 49/79

18/4/2013

4

[re] = [X | Y].X = re,Y = []

[Orange | Apple] = [[do], re, mi].Orange = [do],

 Apple = [re,mi]

[X | [Y | [Z | W]]] = [do, re, mi].X = do,Y = re,Z = mi,

 W = []

Checking if some object is an element of a list. Concatenating of two lists to obtain a third list. Counting the length of a list (i.e. number of 

elements the list contains). Adding a new object to a list Deleting some object from a list. Sublist such that a list S occurs within L as its

sublist. Permutating a list such that one list is a

permutation of the other list.

Page 50: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 50/79

18/4/2013

5

The membership relation can be based on either of thefollowing cases:

X is the head of L

X is a member of the tail of L, if it can be proved thatX is a member of the Tail

In Prolog clauses:

member(X, [X | Tail]).

member(X, [Head | Tail]):-

member(X, Tail).

Describe how Prolog solve the following

query:

?- my_member(b, [a, b, c]).

Page 51: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 51/79

18/4/2013

6

The concatenation relation can be based

on either of the following cases:

If the 1st argument is the empty list

then the 2nd

and the 3rd

argumentmust be the same list, i.e.

conc([], L, L).

If the first argument of is non-empty

list then it has a head and a tail, i.e.[X|L1]

For example:conc([1, 2], [3, 4], [1, 2, 3, 4])

Page 52: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 52/79

18/4/2013

7

In Prolog clauses:conc([X | L1], L2, [X | L3]):-

conc(L1, L2, L3)

conc can be used (in the inverse direction) to decompose a givenlist into two lists, e.g.

?- conc(L1, L2, [1, 2, 3]).

L1 = []

L2 = [1, 2, 3];

L1 = [1]

L2 = [2, 3];

L1 = [1, 2]

L2 = [3];

:

Describe how Prolog solve the following

query:

?- my_conc([a,b],[c,d],R).

Page 53: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 53/79

18/4/2013

8

Can be based on either of the following cases:

If the list is empty, then its length is 0

If the list is not empty then List = [Head | Tail];

then its length is equal to 1 plus to the length of the

tail Tail

In Prolog clauses:

length([],0).

length([_| Tail], N):-

length(Tail,N1), N is N1+1.

Page 54: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 54/79

18/4/2013

9

To add an item, i.e. X in front of the list, thus becomes thenew head of the list.

In Prolog clause: add(X, L, [X | L]).

For example:

?- add(2,[3,4],[X|L]).

X = 2,

L = [3,4]

?- add(2,[3,4,5],N).

N = [2,3,4,5]

Can be based on either of the following cases:

If X is the head of the list then the result (after the deletion)

is the tail of the list

If X is in the tail then it is deleted from there In Prolog clauses:

del(X, [X | Tail], Tail).

del(X, [Y | Tail], [Y | Tail1]):-

del(X, Tail, Tail1).

For example:

del(a, [a, b, a], L).

L = [b, a];

L = [a, b];

Page 55: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 55/79

18/4/2013

10

del can be used (in the inverse direction) to inserta new item anywhere in the list.

For example:

?- del(z, L, [1, 2, 3]).

L = [z, 1, 2, 3];

L = [1, z, 2, 3];

L = [1, 2, z, 3];

L = [1, 2, 3, z];

no

The sublist relation can be based on either of the

following cases:

L can be decomposed into two lists, giving L1 and

L2

L2 can be further decomposed into two lists, giving

S and some L3.

In Prolog clauses:sublist(S, L) :- conc(L1, L2, L),

conc(S, L3, L2).

Page 56: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 56/79

18/4/2013

11

For example:sublist([1, 2, 3], [0, 0, 1, 2, 3, 4])

Can also be used to find all sublists of a given list, for example:

?- sublist(S, [1, 2, 3]).

S = [];

S = [1];

S = [1, 2];

S = [1, 2, 3];

S = [];

S = [2];

:

:

The permutation relation can be based on either of the followingcases:

If the first list is empty then the second list is also empty

If the first list is not empty -- first permute L obtaining L1 and

then insert X at any position into L1.

In Prolog clauses:

permutation ([], []).

permutation ([X | L], P):-

permutation(L, L1),

insert(X, L1, P).

insert(X,List,AnotherList):-

del(X,AnotherList,List).

Page 57: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 57/79

18/4/2013

12

For example:

?- permutation([apple, berry, cherry], P).

P = [apple,berry,cherry] ;

P = [berry,apple,cherry] ;

P = [berry,cherry,apple] ;

P = [apple,cherry,berry] ;

P = [cherry,apple,berry] ;P = [cherry,berry,apple] ;

no

PROLOG: Programming for Artificial

Intelligence by Ivan Bratko (Addison Wesley)

Page 58: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 58/79

25/05/2013

1

Prolog Programming

The ‘Cut’ Operator !

Problems with Cuts

Negation as Failure

Using fail

Backtracking using fail

Cut using fail

Page 59: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 59/79

25/05/2013

2

Sometimes we need a way to prevent Prolog finding all solutions,i.e. a way to stop backtracking.

There are cases, however, where backtracking is not desirable.

The cut operator ! is a predefined Prolog predicate that preventsbacktracking and can be placed anywhere inside a rule’s body.

Executing the subgoal ! will always succeed, but afterwards,backtracking into subgoals placed before the cut inside the samerule body is not possible anymore.

Consider the following facts in the KB

p(1).

p(2) :- !.

p(3).

Write all Prolog’s answers to the following queries:?- p(X).

?- p(X), p(Y).

?- p(X),!,p(Y).

Page 60: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 60/79

25/05/2013

3

?- p(X).

X = 1 ;

X = 2

?- p(X), p(Y).

X = Y = 1 ;

X = 1 ,Y = 2 ;

X = 2 ,

Y = 1 ;

X = Y = 2

?- p(X),!,p(Y).

X = Y = 1 ;

X = 1 ,

Y = 2

max, without cut:

max(A, B, A) :- A > B.

max(A, B, B) :- A =< B.

?- max(2, 5, X).

X = 5

?- max(3, 1, X).

X = 3 ;

These two rules are mutually exclusive. If the first one succeeds then thesecond one will fails, and vice versa.

Page 61: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 61/79

25/05/2013

4

max, with cut:

max(A, B, A) :- A > B, !.

max(A, B, B).

?- max(3, 5, X).

X = 5

?- max(3, 1, X).

X = 3

Represent the following facts in Prolog form.

The query, marks(80, Grade) should succeed, thus Grade = A

Page 62: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 62/79

25/05/2013

5

marks(N,A) :- N>=80,!.

marks(N,B) :- N>=65,!.

marks(N,C) :- N>=55,!.

marks(N,D) :- N>=50,!.

marks(N,pass) :- N>=40,!.

marks(N,fail) :- N<40.

Consider the following Prolog clauses:

% shopping day on weekend in fair weather and on public

% holiday

holiday(friday, feb1).

holiday(wednesday,may1).

weekend(saturday).

weather(saturday,fair_weather).

shopping(Day) :- holiday(Day,_), !.

shopping(Day) :- weather(Day, fair_weather), weekend(Day).

1. Explain how Prolog respond to the query, ?- shopping(When).

2. What can you conclude from Prolog program in exercise 2 and 3?

Page 63: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 63/79

25/05/2013

6

Cut changes the declarative meaning of the program.

Need to use it with care only where needed.

Green cuts can be inserted for efficiency (i.e. programin exercise 2).

Red cuts should be considered carefully (i.e. program

in exercise 3).

Cuts are very useful to “guide” the Prolog interpreter towards asolution.

However, by introducing cuts, we give up some of the declarativemeaning of Prolog towards a more procedural meaning.

This can sometimes lead to unexpected results. Two types of cut:

Green cut: makes a program more efficient without affecting theset of solutions that the program generates

Red cuts: prevents the program from generating solution it wouldotherwise give

Page 64: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 64/79

25/05/2013

7

Sometimes we might not want to ask whether a certain goalsucceeds, but whether it fails.

In Prolog this is possible using the \+ operator.

In other words, \+ Goal succeeds, if Prolog fails to derive aproof for Goal.

Prolog’s negation is defined as the failure to provide a proof.

Consider the following facts:

married(ahmad, lina).

married(ben, nancy).

married(chong, julia).

married(dani, damia).

Then we can define single/1 that succeeds if the argument given

can neither be found as the first nor as the second argument in anyof the married/2 facts.

Page 65: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 65/79

25/05/2013

8

Consider the following predicate, single/1:single(Person) :-

\+ married(Person, _),

\+ married(_, Person).

% use anonymous variable because its value would be

% irrelevant.

and the following queries:

?- single(damia).

no

?- single(nour).

yes

“Nour is assumed to be single, because she cannot be proved to be married”.

Where to use \+:

The \+ operator can be applied to any valid Prolog goal.

Goals are either:

▪ (sub)goals of a query or

▪ (sub)goals of a rule-body.

Facts and rule-heads aren’t goals. Hence, it is not possible tonegate a fact or the head of a rule.

Page 66: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 66/79

25/05/2013

9

fail is a symbol that will immediately fail when Prolog encounters

it as a goal. When fails, Prolog tries to backtrack.

fail used in combination with automatic backtracking allow Prolog

to search through the KB to find all possible values of X that satisfythe goal, say for example, list_all(X).

fail used in combination with cut, blocks backtracking.

Consider the following KB:

candy(vanilla).

candy(strawberry).

candy(chocolate).

list_all :- candy(X), write(X),

write(‘ flavoured candy’), nl, fail.

list_all.

How does Prolog respond to the query, ?- list_all.

The second clause list_all. ensure that, after the KB has been

searched, the goal succeeds. However, with only the first line, any callto list_all. will eventually fail.

Page 67: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 67/79

25/05/2013

10

Suppose the KB contains the following clauses.

bird(robin).bird(parrot).bird(penguin).can_fly(X) :- bird(X).

The following query, ?- can_fly(penguin) gives

undesired result. Why? If we add new clause, can_fly(penguin):- fail. and

having the clause, can_fly(X) :- bird(X).immediately following the new clause, the query still givesundesired result. Why?

How can we solve this problem using fail?

The desired result can be achieved by defining the new clause asfollow:

bird(robin).

bird(parrot).

bird(penguin).

can_fly(penguin):- !, fail. % cut with failurecan_fly(X) :- bird(X).

Cut using fail helps to specify few exceptions to general rules, inthis case, penguin cannot fly.

But, if we pose the query, ?- can_fly(X) -- the answer is no.

How do we solve the problem?

Page 68: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 68/79

25/05/2013

11

bird(robin).

bird(parrot).

bird(penguin).

can_fly(X) :- bird(X), X \== penguin.

Write a program that repeatedly prompts the user to enter series ofnumbers and outputs a message indicating whether each is an oddor even number. The program stops when the user has entered 10numbers.

Use recursion and ! operator.

Page 69: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 69/79

25/05/2013

12

Write a program that repeatedly prompts the user to enter series ofnumbers and outputs a message indicating whether each is an oddor even number. The program stops when the user has entered anumber “10”.

Use repeat, ! and =:= operators.

Write appropriate Prolog clauses that says:

Lina likes all candy flavours except orange flavour.

Use appropriate queries to demonstrate your solution.

Page 70: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 70/79

25/05/2013

13

likes(lina,X) :- candy(X), X == 'Orange', !, fail.

likes(lina,X) :- candy(X).

candy(X) :- candy_flav(X).

candy_flav('Strawberry').

candy_flav('Lemon').

candy_flav('Mint').

The queries, ?- likes(lina, ‘Mint’), yes

?- likes(lina, ‘Orange‘), no

Explain how Prolog responds to the above queries.

Page 71: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 71/79

5/25/2013

1

Prolog Programming

Testing the types of terms

Constructing and decomposing terms: =.., functor , arg 

Testing for equality and comparisons (see lecture note on arithmeticand operations)

Control facilities: ! , fail , true , not(P), repeat , if-else construct 

Database manipulation

Finding solutions: bagof , setof , findall  Input and output

Page 72: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 72/79

5/25/2013

2

Predicates: var, nonvar, atom, integer, float, number, atomic,

compound 

var(Y): succeeds if Y is currently an uninstantiated variable.

nonvar(Y): succeeds if Y is not a variable, or Y is an alreadyinstantiated variable.

atom(Y): checks if Y is instantiated to an atom.

integer(Y): checks if Y is instantiated to an integer.

float(Y): checks if Y is instantiated to a real number

number(Y): checks if Y is instantiated to a number.

atomic(Y): true if Y is either an atom or a number

compound(Y): checks if Y is instantiated to a compound term (i.e.a structure).

?- var(Y), Y =2.

Y = 2

?- X = 2, var(X).

no

?- atom(cake).

yes

?- atom(2).

no

?- atom('cake').

yes

?- atomic(22.5).

no

?- compound(mm(s)).

yes

Page 73: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 73/79

5/25/2013

3

Predicates: =.. , functor, arg 

Term =.. L

Written as an infix operator and reads as ‘univ’. The goal Term =.. Lis true if L is a list that contains the principle functor of Term,followed by its arguments.

▪ Term =.. [Functor | ArgumentList]

?- a(b,c) =.. L.

L = [a,b,c]

?- Z =..[p,1,2].

Z = p(1,2).

functor(Term, F, N)

True if F is the principal functor of Term and N is the arity of F.

?- functor(t(a, b, c), X, Arity)

X = t

Arity = 3

Page 74: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 74/79

5/25/2013

4

arg(N, Term, A)

True if A is the Nth argument of Term, numbering left to right from 1.

?- arg(2, f(a, b, c, d), X).

X = b

?- arg(3, f(a, x(b), y(c)), Z).

Z = y(c)

!

reads as cut, prevents backtracking

fail

a goal that always fails

true

a goal that always succeeds

not(P)

negation as failure

Page 75: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 75/79

5/25/2013

5

Repeat

Is use to do a loop. The predicate in which it is used is repeateduntil every goal succeeds.

pword :- repeat,write('Please enter a password'),

read(X),(X == prolog).

?- pword.

Please enter a password|: hello.

Please enter a password|: world.

Please enter a password|: prolog.

yes

(P -> Q; R)

Is an if-then-else construct. It read as: if P then Q else R. Thearrow ‘ -> ‘ is an infix operator whose priority is less than that of ‘ ; ’and greater than that of a comma.

?- X = 10, Y = 15, (X >= Y -> Colour = pink; Colour

= green).

X = 10 ,

Y = 15 ,

Colour = green

Page 76: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 76/79

5/25/2013

6

Prolog 'throws away' old solutions (and is not accessible any more)as it backtracks to generate new solution. The predicates bagof ,setof and findall can be used if we prefer to have all generatedsolutions available.

bagof( X, P, L)

This produces a list L of all the objects X such that a goal P issatisfied.

setof( X, P, L) This produces a list L of all the objects X that satisfy P. This time,

duplicate items will be eliminated and it defines the orderingamong terms.

findall( X, P, L)

Similar to bagof.

▪ The difference: findall will return an empty list if a goal P hasno solutions, whereas bagof would fail in such situation.

Consider the following KB:

likes(myra, durian).

likes(danial, strawberry).

likes(zara, orange).

likes(irfan, durian).

likes(lana, apple).

?- bagof(Child, likes(Child, durian), L).

Child = _ ,

L = [myra,irfan]

?- bagof(Child, Fruit ^ likes(Child, Fruit), L).

Child = _ ,

Fruit = _ ,

L = [myra,danial,zara,irfan,lana]

Page 77: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 77/79

5/25/2013

7

?- bagof(Child, likes(Child, Fruit), L).

Child = _ ,Fruit = apple ,L = [lana] ;

Child = _ ,Fruit = durian ,L = [myra,irfan] ;

Child = _ ,Fruit = orange ,L = [zara] ;

Child = _ ,Fruit = strawberry ,L = [danial] ;

?- bagof(Fruit, likes(Child, Fruit), L).

Fruit = _ ,Child = danial ,L = [strawberry] ;

Fruit = _ ,Child = irfan ,L = [durian] ;

Fruit = _ ,Child = lana ,L = [apple] ;

Fruit = _ ,Child = myra ,L = [durian] ;

Fruit = _ ,Child = zara ,L = [orange] ;

Page 78: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 78/79

5/25/2013

8

likes(myra, durian).likes(danial, strawberry).

likes(zara, orange).likes(irfan, durian).

likes(lana, apple).

?- setof(Child, Fruit ^ likes(Child, Fruit), CList),setof(Fruit, Child ^ likes(Child, Fruit), FList).

Child = _ ,

Fruit = _ ,CList = [danial,irfan,lana,myra,zara] ,FList = [apple,durian,orange,strawberry]

?- setof(Fruit/Child, likes(Child, Fruit), L).

Fruit = _ ,Child = _ ,

L = [apple/lana, durian/irfan, durian/myra,

orange/zara, strawberry/danial]

likes(myra, durian).likes(danial, strawberry).

likes(zara, orange).likes(irfan, durian).

likes(lana, apple).

?- findall(Child, likes(Child, Fruit), L).

Child = _ ,Fruit = _ ,

L = [myra,danial,zara,irfan,lana]

?- findall(Child, likes(Child, banana), L).

Child = _ ,

L = []

If there is no object X that satisfy condition P, then findall will succeed withL = [ ]. Note that if the query ?- bagof(Child, likes(Child,

banana), L), then bagof will not succeed.

Page 79: PROLOG cheatsheet

7/15/2019 PROLOG cheatsheet

http://slidepdf.com/reader/full/prolog-cheatsheet 79/79

5/25/2013

PROLOG: Programming for Artificial Intelligence by Ivan Bratko,3rd edition, Addison Wesley