Chapter 3 Input and Output Turbo Prolog Editor
Chapter 3 Input and Output
Turbo Prolog Editor Ctrl + K + B Ctrl + K + C Ctrl + K + V Ctrl + K + Y Ctrl + Y 344 -302 LP and Prolog = = = Start block End block Copy block Move block Delete line 2 Chapter 3
AND, OR, NOT and use , or use ; not HELP Command file ? PROLOG open with notepad 344 -302 LP and Prolog 3 Chapter 3
EX 04 EX 05 is letter predicates isletter(char) clauses goal : isletter(‘K’) yes /* When applied to characters, '<=' means "alphabetically precedes or is the same as" */ l isletter(Ch) : - 'a' <= Ch, Ch <= 'z'. isletter(Ch) : - 'A' <= Ch, Ch <= 'Z'. 344 -302 LP and Prolog 4 arithematics < > <= > = + - * / not l char : put ‘. . ’ when asking Chapter 3
Setup makewindow(1, 120, 35, "Left operand", 2, 5, 5, 25) Location 1 : window number 2 : background color and text color in the window 3 : frame color 4 : text center of the border 5 : row 6 : column 7 : Height 8 : Width cursor(row, column) cursor(25, 80) 344 -302 LP and Prolog 5 Chapter 3
Setup color HOW TO CALCULATE SCREEN ATTRIBUTES. 1. 2. 3. Choose ONE foreground color and ONE background color. Add the corresponding integer values below. Add 128 if you want whatever is displayed with that attribute to blink. BACKGROUND-COLORS (Screen) FOREGROUND-COLORS (Characters) ------------+------------+------Black 0 Blue 16 Blue 1 Green 32 Green 2 Cyan 48 Cyan 3 Red 64 Red 4 Magenta 80 Magenta 5 Brown 96 Brown 6 White 112 White 7 Grey 8 Light Blue 9 Light Green 10 Light Cyan 11 23 = 16 + 7 344 -302 LP and Prolog Light Red Light Magenta Yellow White (High Intensity) 6 12 13 14 15 Chapter 3
EX 14 EX 01. PRO : makewindow predicates run clauses run : makewindow(1, 20, 7, "A blue window", 2, 5, 10, 50), write("The characters are red"), nl, makewindow(2, 176, 7, "A light cyan window", 14, 25, 10, 40), write("This window is light cyan and the "), write("letters are black and blink. "), nl, write("Please type an integer to exit. "), nl, readint(_), removewindow. 344 -302 LP and Prolog 7 Chapter 3
Window 1. PRO : EX 04 EX 03. PRO predicates run(char) goal run(X). /* This is a internal goal. */ clauses run(X) : makewindow(1, 7, 7, " Hello World Program ", 0, 0, 25, 80), write("Hello World (first)"), readchar(X), removewindow. run(X) : write("hello World (second)"), readchar(X). 344 -302 LP and Prolog 8 Chapter 3
Window 2. PRO predicates run(char) goal run(X). /* This is a internal goal. */ clauses run(X) : makewindow(1, 7, 7, "hello", 0, 0, 25, 80), write("hello World (second)"), readchar(X). run(X) : makewindow(1, 7, 7, " Hello World Program ", 0, 0, 25, 80), write("Hello World (first)"), readchar(X), removewindow. 344 -302 LP and Prolog 9 Chapter 3
Window 3. PRO : show 2 windows predicates run 1(char) run 2(char) goal run 1(X), run 2(Y). /* This is a internal goal. */ clauses run 2(X) : makewindow(1, 7, 7, "hello", 0, 0, 25, 80), write("hello World (second)"), readchar(X). run 1(X) : makewindow(1, 7, 7, " Hello World Program ", 0, 0, 25, 80), write("Hello World (first)"), readchar(X), removewindow. 344 -302 LP and Prolog 10 Chapter 3
EX 14 EX 02. pro : SUM 2 NUMBERS predicates start run(integer) do_sums set_up_windows clear_windows clauses goal: start : - set_up_windows, do_sums. set_up_windows : makewindow(1, 7, 7, makewindow(2, 7, 7, nl, write(" PLUS"), makewindow(2, 7, 7, makewindow(3, 7, 7, makewindow(4, 7, 7, do_sums 344 -302 LP and Prolog "", 0, 0, 25, 80), "Left operand", 2, 5, 5, 25), "", 2, 35, 5, 10), "Right operand", 2, 50, 5, 25), "Gives", 10, 27, 5, 25), "", 17, 22, 5, 35). : - run(_), clear_windows, 11 do_sums. Chapter 3
EX 14 EX 02. pro : SUM 2 NUMBERS (cont. ) run(Z) : shiftwindow(1), cursor(2, 1), readint(X), shiftwindow(2), cursor(2, 10), readint(Y), shiftwindow(3), Z=X+Y, cursor(2, 10), write(Z), shiftwindow(4), write(" Please press the space bar"), readchar(_). clear_windows : shiftwindow(1), shiftwindow(2), shiftwindow(3), shiftwindow(4), 344 -302 LP and Prolog clearwindow, clearwindow. 12 Chapter 3
goal: LISTEX 01. pro ownes(X, Y) domains thing = book(title, author) ; car(name); shoe author = author(firstname, lastname) person, title, name, firstname, lastname, shoe = symbol predicates ownes(person, thing) clauses ownes(siri, book("AI book", author(tom, brook))). ownes(siri, car(honda)). ownes(siri, shoe). 344 -302 LP and Prolog 13 Chapter 3
EX 12 EX 07. PRO : Read Person Information domains person = p(name, age, telno, job) age = integer telno, name, job = string predicates readperson(person) run goal run. clauses readperson(p(Name, Age, Telno, Job)) : write("Which name ? "), readln(Name), write("Job ? "), readln(Job), write("Age ? "), readint(Age), write("Telephone no ? "), readln(Telno). run : run readperson(P), nl, write(P), nl, write("Is this compound object OK (y/n)"), readchar(Ch), Ch='y'. : nl, write("Alright, try again"), nl, 344 -302 LP and Prolog 14 run. Chapter 3
EX 07 EX 02. PRO : Typewriter /* Uses repeat to keep accepting characters and printing them until the user presses Enter. */ predicates repeat goal: clauses typewriter repeat : - repeat. typewriter : - repeat, readchar(C), /* Read a char, bind C to it */ write(C), char_int(C, 13). /* Is C = ASCII 13? */ 344 -302 LP and Prolog 15 Chapter 3
EX 05 EX 09 Interactive window predicates write("Selection? "), repeat readint(Int), nl, action(integer, string) test(string) action(Int, Text), goal Int = 0, !, /* this cut will prevent backtracking even if you have not created a string */ makewindow(1, 7, 7, "interaction window", 0, 2, 11, 43), repeat, test(Text). shiftwindow(1), clearwindow, clauses write("0. Enter 0 to endn"), action(0, "EXIT"): - !, /* this cut prevents Turbo Prolog from looking at other options. */ write("1. Enter 1 to create a window and inputn a new stringn"), write("2. Enter 2 to remove the window and textn"), exit. write("3. Enter 3 to write to existing windownn"), 344 -302 LP and Prolog 16 Chapter 3
EX 05 EX 09 Interactive window action(1, Str): - action(2, "window removed"): - existwindow(2), !, /* If the window has been input, this cut will prevent the second action(2) clause from executing */ write("You have a window that already exists. n"), shiftwindow(2), write("Do you wish to clear it. (y, n) "), removewindow, readchar(Ans), !, clearwindow. . . . Ans='y', /* If you answer yes to the question this cut prevents the backtracking to the second action(1) clause. */ action(3, Str): existwindow(2), !, shiftwindow(2), nl, shiftwindow(2), clearwindow, write("Enter your stringn"), readln(Str). action(1, Str): - !, /* this cut prevents Turbo Prolog from looking at other options. */ nl, makewindow(2, 7, 7, " simple window control ", 12, 3, 12, 40), clearwindow, write("Enter your stringn"), readln(Str). . . . action(_, "ERROR"): write("not a valid optionn"), write("press any key to continue"). test(Text): - write(Text). write("Enter your stringn"), repeat. readln(Str). repeat: -repeat. 344 -302 LP and Prolog 17 Chapter 3
EX 16 EX 02. PRO : show time predicates timer clauses timer: time(H 1, M 1, S 1, D 1), nl, write("Start time is: ", H 1, ": ", M 1, ": ", S 1, " and ", D 1, "/100 sec"), nl, /* This is the activity that is being timed */ system("dir *. *"), time(H 2, M 2, S 2, D 2), Time = (D 2 -D 1) + 100 * ((S 2 - S 1) + 60 * ((M 2 - M 1) + 60 * (H 2 - H 1))), write("Elapsed time: ", Time, "/100 sec"), nl, time(H 3, M 3, S 3, D 3), write("The time now is: ", H 3, ": ", M 3, ": ", S 3, " and ", D 3, "/100 sec"), nl. goal makewindow(1, 7, 7, " Timer ", 8, 10, 12, 60), write("Press any key to start"), readchar(_), timer. 344 -302 LP and Prolog 18 Chapter 3
EX 05 l. We can set the program to run automatic by using the command at. . . goal predicates type(symbol, symbol) is_a(symbol, symbol) lives(symbol, symbol) can_swim(symbol) goal lives(zebra, on_land). lives(frog, in_water). lives(shark, in_water). can_swim(Y) : type(X, animal) , is_a(Y, X) , lives(Y, in_water). can_swim(What) , write("A ", What, " can swim. n"). clauses type(ungulate, animal). type(fish, animal). l is_a(zebra, ungulate). is_a(herring, fish). is_a(shark, fish). 344 -302 LP and Prolog l 19 write(“. . . ” ) write(“n. . . . ”, Variable , ”. . . nnn”) Chapter 3
cswim 1 l. Fail : use : prolog will find all of the solutions. is_a(zebra, ungulate). is_a(herring, fish). is_a(shark, fish). predicates type(symbol, symbol) is_a(symbol, symbol) lives(symbol, symbol) can_swim(symbol) goal is_a(tuna, fish). lives(zebra, on_land). lives(frog, in_water). lives(shark, in_water). lives(herring, in_water). lives(tuna, in_water). can_swim(What) , write("A ", What, " can swim. n"), fail. clauses type(ungulate, animal). type(fish, animal). 344 -302 LP and Prolog to make the rule not true can_swim(Y) : type(X, animal) , is_a(Y, X) , lives(Y, in_water). 20 Chapter 3
l. We can_swim can set the program to run automatic by using the command at. . . predicates type(symbol, symbol) is_a(symbol, symbol) lives(symbol, symbol) can_swim(symbol) run. goal run. clauses type(ungulate, animal). type(fish, animal). goal lives(zebra, on_land). lives(frog, in_water). lives(shark, in_water). lives(tuna, in_water). can_swim(Y) : type(X, animal) , is_a(Y, X) , lives(Y, in_water). run : write("n Hello n"), can_swim(What), write("A ", What, " can swim. n"), fail. is_a(zebra, ungulate). is_a(tuna, fish). is_a(shark, fish). 344 -302 LP and Prolog 21 Chapter 3
EX 05 EX 06 father domains name = symbol predicates father(name, name) l Some predicate may not have attributes Ex : everybody clauses father(leonard, katherine). father(carl, jason). father(carl, marilyn). everybody : - father(X, Y), write(X, " is ", Y, "'s fathern"), fail. 344 -302 LP and Prolog 22 Chapter 3
- Slides: 22