350 likes | 481 Views
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
E N D
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 • can be used in the following way: • a(X) :- b(X), c(X), !, d(X).
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
Example • predicate count(integer) clauses count(9). count(N):- write(N), NN=N+1, count(NN).
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.
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
A simple example of their use would be the following two predicates: • positive(N) :- N>0. • non_zero(N) :- N<0;N>0.
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
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)
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)
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
Backslash commands • \n Carriage return/line feed • \tTab • \b Backspace • These commands are enclosed with double quotation
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)
Using window • Clearwindow to clean dialog window
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.
Prolog has four database manipulation commands: • assert, retract, asserta, and assertz.
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
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)).yesassert(happy(marcellus)).yesassert(happy(butch)).yesassert(happy(vincent)).yesSuppose we then ask for a listing: • listing.happy(mia).happy(vincent).happy(marcellus).happy(butch).happy(vincent).yes
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).
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).
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