artificial intelligence
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 PresentationTRANSCRIPT
![Page 1: Artificial Intelligence](https://reader035.vdocuments.net/reader035/viewer/2022070422/56816567550346895dd7f203/html5/thumbnails/1.jpg)
Artificial Intelligence
Lecture 8
![Page 2: Artificial Intelligence](https://reader035.vdocuments.net/reader035/viewer/2022070422/56816567550346895dd7f203/html5/thumbnails/2.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022070422/56816567550346895dd7f203/html5/thumbnails/3.jpg)
![Page 4: Artificial Intelligence](https://reader035.vdocuments.net/reader035/viewer/2022070422/56816567550346895dd7f203/html5/thumbnails/4.jpg)
![Page 5: Artificial Intelligence](https://reader035.vdocuments.net/reader035/viewer/2022070422/56816567550346895dd7f203/html5/thumbnails/5.jpg)
![Page 6: Artificial Intelligence](https://reader035.vdocuments.net/reader035/viewer/2022070422/56816567550346895dd7f203/html5/thumbnails/6.jpg)
![Page 7: Artificial Intelligence](https://reader035.vdocuments.net/reader035/viewer/2022070422/56816567550346895dd7f203/html5/thumbnails/7.jpg)
![Page 8: Artificial Intelligence](https://reader035.vdocuments.net/reader035/viewer/2022070422/56816567550346895dd7f203/html5/thumbnails/8.jpg)
![Page 9: Artificial Intelligence](https://reader035.vdocuments.net/reader035/viewer/2022070422/56816567550346895dd7f203/html5/thumbnails/9.jpg)
![Page 10: Artificial Intelligence](https://reader035.vdocuments.net/reader035/viewer/2022070422/56816567550346895dd7f203/html5/thumbnails/10.jpg)
![Page 11: Artificial Intelligence](https://reader035.vdocuments.net/reader035/viewer/2022070422/56816567550346895dd7f203/html5/thumbnails/11.jpg)
Adding a cut
![Page 12: Artificial Intelligence](https://reader035.vdocuments.net/reader035/viewer/2022070422/56816567550346895dd7f203/html5/thumbnails/12.jpg)
![Page 13: Artificial Intelligence](https://reader035.vdocuments.net/reader035/viewer/2022070422/56816567550346895dd7f203/html5/thumbnails/13.jpg)
![Page 14: Artificial Intelligence](https://reader035.vdocuments.net/reader035/viewer/2022070422/56816567550346895dd7f203/html5/thumbnails/14.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022070422/56816567550346895dd7f203/html5/thumbnails/15.jpg)
Example
• predicate count(integer)clauses count(9). count(N):- write(N), NN=N+1, count(NN).
![Page 16: Artificial Intelligence](https://reader035.vdocuments.net/reader035/viewer/2022070422/56816567550346895dd7f203/html5/thumbnails/16.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022070422/56816567550346895dd7f203/html5/thumbnails/17.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022070422/56816567550346895dd7f203/html5/thumbnails/18.jpg)
Operators that compare Integers
![Page 19: Artificial Intelligence](https://reader035.vdocuments.net/reader035/viewer/2022070422/56816567550346895dd7f203/html5/thumbnails/19.jpg)
• 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](https://reader035.vdocuments.net/reader035/viewer/2022070422/56816567550346895dd7f203/html5/thumbnails/20.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022070422/56816567550346895dd7f203/html5/thumbnails/21.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022070422/56816567550346895dd7f203/html5/thumbnails/22.jpg)
Input/Output in Prolog
![Page 23: Artificial Intelligence](https://reader035.vdocuments.net/reader035/viewer/2022070422/56816567550346895dd7f203/html5/thumbnails/23.jpg)
I/O in Prolog - Example:
![Page 24: Artificial Intelligence](https://reader035.vdocuments.net/reader035/viewer/2022070422/56816567550346895dd7f203/html5/thumbnails/24.jpg)
• 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](https://reader035.vdocuments.net/reader035/viewer/2022070422/56816567550346895dd7f203/html5/thumbnails/25.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022070422/56816567550346895dd7f203/html5/thumbnails/26.jpg)
Backslash commands
• \n Carriage return/line feed• \tTab• \b Backspace• These commands are enclosed with double
quotation
![Page 27: Artificial Intelligence](https://reader035.vdocuments.net/reader035/viewer/2022070422/56816567550346895dd7f203/html5/thumbnails/27.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022070422/56816567550346895dd7f203/html5/thumbnails/28.jpg)
Using window
• Clearwindow to clean dialog window
![Page 29: Artificial Intelligence](https://reader035.vdocuments.net/reader035/viewer/2022070422/56816567550346895dd7f203/html5/thumbnails/29.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022070422/56816567550346895dd7f203/html5/thumbnails/30.jpg)
• Prolog has four database manipulation commands:
• assert, retract, asserta, and assertz.
![Page 31: Artificial Intelligence](https://reader035.vdocuments.net/reader035/viewer/2022070422/56816567550346895dd7f203/html5/thumbnails/31.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022070422/56816567550346895dd7f203/html5/thumbnails/32.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022070422/56816567550346895dd7f203/html5/thumbnails/33.jpg)
• 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](https://reader035.vdocuments.net/reader035/viewer/2022070422/56816567550346895dd7f203/html5/thumbnails/34.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022070422/56816567550346895dd7f203/html5/thumbnails/35.jpg)
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