1 / 35

Artificial Intelligence

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

ashton
Télécharger la présentation

Artificial Intelligence

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Artificial Intelligence Lecture 8

  2. 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).

  3. Adding a cut

  4. 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

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

  6. 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.

  7. 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

  8. Operators that compare Integers

  9. A simple example of their use would be the following two predicates: • positive(N) :- N>0. • non_zero(N) :- N<0;N>0.

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

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

  12. Input/Output in Prolog

  13. I/O in Prolog - Example:

  14. 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)

  15. 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

  16. Backslash commands • \n Carriage return/line feed • \tTab • \b Backspace • These commands are enclosed with double quotation

  17. 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)

  18. Using window • Clearwindow to clean dialog window

  19. 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.

  20. Prolog has four database manipulation commands:  • assert, retract, asserta, and assertz.

  21. 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

  22. 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

  23. 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).

  24. 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).

  25. 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

More Related