artificial intelligence

35
Artificial Intelligence Lecture 8

Upload: ashton

Post on 24-Feb-2016

27 views

Category:

Documents


0 download

DESCRIPTION

Artificial Intelligence. Lecture 8. Cut Predicates. The cut prunes Prolog's search tree. a pure Prolog program without cut and the same program with cuts the only difference is that the program with cuts might spend less time in fruitless branches ! . the cut operator is an atom - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Artificial Intelligence

Artificial Intelligence

Lecture 8

Page 2: Artificial Intelligence

Cut Predicates

• The cut prunes Prolog's search tree. • a pure Prolog program without cut and the

same program with cuts the only difference is that the program with cuts might spend less time in fruitless branches

• !. the cut operator is an atom• can be used in the following way:• a(X) :- b(X), c(X), !, d(X).

Page 3: Artificial Intelligence
Page 4: Artificial Intelligence
Page 5: Artificial Intelligence
Page 6: Artificial Intelligence
Page 7: Artificial Intelligence
Page 8: Artificial Intelligence
Page 9: Artificial Intelligence
Page 10: Artificial Intelligence
Page 11: Artificial Intelligence

Adding a cut

Page 12: Artificial Intelligence
Page 13: Artificial Intelligence
Page 14: Artificial Intelligence

Recursion

• Predicates can be defined recursively. Roughly speaking, a predicate is recursively defined if one or more rules in its definition refers to itself.

• Recursion is usually used in two situations: • when relations are described with the help of

the relations themselves • when compound objects are a part of other

compound objects

Page 15: Artificial Intelligence

Example

• predicate count(integer)clauses count(9). count(N):- write(N), NN=N+1, count(NN).

Page 16: Artificial Intelligence

repeat predicate

• The predicate repeat (user defined predicate) creates a loop (similar to the loops in imperative programming language).

• command_loop:- repeat, write('Enter command (end to exit): '),

read(X), write(X), nl, X = end.

Page 17: Artificial Intelligence

Arithmetic Operations

• Prolog provides a number of basic arithmetic tools for manipulating integers (that is, numbers of the form ...-3, -2, -1, 0, 1, 2, 3, 4...).

• Prolog handles the four basic operations of addition, multiplication, subtraction, and division

Page 18: Artificial Intelligence

Operators that compare Integers

Page 19: Artificial Intelligence

• A simple example of their use would be the following two predicates:

• positive(N) :- N>0. • non_zero(N) :- N<0;N>0.

Page 20: Artificial Intelligence

Evaluable functors• (rem)/2Remainder• (mod)/2Modulus• (-)/1Negation• (abs)/1Absolute value• (sign)/1Sign• (float_integer_part)/1Integer part• (float_fractional_part)/1Fractional part• (float)/1Float coercion• (floor)/1Floor• (truncate)/1Truncate• (round)/1Round• (ceiling)/1Ceiling

Page 21: Artificial Intelligence

Arithmetic and Bitwise functors• (**)/2Power • sin/1Sine• cos/1Cosinea• tan/1Arc tangent• exp/1Exponentiation• log/1Log• sqrt/1Square root • (>>)/2Bitwise right shiftbitright(X,N,Y)• (<<)/2Bitwise left shift bitleft(X,N,Y)• (/\)/2Bitwise and bitand(X,Y,Z)• (\/)/2Bitwise or bitor(X,Y,Z)• (\)/1Bitwise complement bitnot(X,Z)

Page 22: Artificial Intelligence

Input/Output in Prolog

Page 23: Artificial Intelligence

I/O in Prolog - Example:

Page 24: Artificial Intelligence

• writef predicate is used to get formatted output• Example writef(format,variables)• Variable formate %-m.p• - optional hyphen for forcing left justification by

defalt right justification• m optional minimum field width• p optional maximum string length or precision of

decimal floating point number• Writef(%-10 #%5.0 $%3.2\n,fan,23,3.1)

Page 25: Artificial Intelligence

Input predicates

• Readln read string or symbol• Readchar / inkey read single character• Readint read integer• Readreal read real• Keypressed a key is pressed or not

Page 26: Artificial Intelligence

Backslash commands

• \n Carriage return/line feed• \tTab• \b Backspace• These commands are enclosed with double

quotation

Page 27: Artificial Intelligence

Using a device to get output

• Any output to the display can be derected instead to printer or a file

• To redirect output writedevice(device_name)Predicate is used• For example• writedevice(printer) or writedevice(screen)

Page 28: Artificial Intelligence

Using window

• Clearwindow to clean dialog window

Page 29: Artificial Intelligence

Database in prolog

• The database is a non-logical extension to Prolog.

• The core philosophy of Prolog is that is searches for a set of variable bindings that makes a query true.

Page 30: Artificial Intelligence

• Prolog has four database manipulation commands:

• assert, retract, asserta, and assertz.

Page 31: Artificial Intelligence

assertz and asserta

• asserta(fact) predicate stores a fact at the beginning of the database

• assertz(fact) Places asserted material at the end of the database

• All variables are bound before the database predicate is invoked

• assert commands always succeed

Page 32: Artificial Intelligence

Example• Consider an empty

database.If we now give the command:

• listing.• we simply get a yes; the

listing (of course) is empty.• we now give this command:• assert(happy(mia)).• If we now give the

command:• listing.we get the listing:• happy(mia).

• we then made four more assert commands:• assert(happy(vincent)).

yes assert(happy(marcellus)).yes assert(happy(butch)).yes assert(happy(vincent)).yesSuppose we then ask for a listing:

• listing. happy(mia).happy(vincent).happy(marcellus).happy(butch).happy(vincent).yes

Page 33: Artificial Intelligence

• we have only asserted facts into the database, but we can assert new rules as well. Suppose we want to assert the rule that everyone who is happy is naive. That is, suppose we want to assert that:

• naive(X) :- happy(X).We can do this as follows:

• assert( (naive(X) :- happy(X)) ).

• If we now ask for a listing we get:

• happy(mia).happy(vincent).happy(marcellus).happy(butch).happy(vincent). naive(A) :- happy(A).

Page 34: Artificial Intelligence

retract

• Used to remove things form the database when we no longer need them

• retract(happy(marcellus)).

• then list the database we get:

• happy(mia).happy(vincent).happy(butch).happy(vincent). naive(A) :- happy(A).

Page 35: Artificial Intelligence

retract(cont..)• we go on further, and say• retract(happy(vincent)).and

then ask for a listing. We get:• happy(mia).

happy(butch).happy(vincent). naive(A) :- happy(A).

• Note that the first occurrence of happy(vincent) (and only the first occurrence) was removed.

• To remove all of our assertions we can use a variable:

• retract(happy(X)). X = mia ; X = butch ; X = vincent ; no

• A listing reveals that the database is now empty:

• listing.yes