Chapter 8 Arithmetic Program Examples Factorial 1 pro

  • Slides: 23
Download presentation
Chapter 8 Arithmetic Program Examples

Chapter 8 Arithmetic Program Examples

Factorial 1. pro predicates factorial(integer, integer) clauses factorial(1, 1). factorial(N, Result) : N >

Factorial 1. pro predicates factorial(integer, integer) clauses factorial(1, 1). factorial(N, Result) : N > 1, M = N-1, factorial(M, RET), Result = N*RET. factorial(N, M) : M = N, write(“end of datatbase”). 344 -302 LP and Prolog 2 Chapter 8

Factorial 2. pro predicates factorial(integer, integer) clauses factorial(1, 1) : - !. factorial(N, Result)

Factorial 2. pro predicates factorial(integer, integer) clauses factorial(1, 1) : - !. factorial(N, Result) : M = N-1, factorial(M, RET), Result = N*RET. 344 -302 LP and Prolog 3 Chapter 8

Factorial Ex 07 EX 03. pro /* Recursive program to compute factorials. */ predicates

Factorial Ex 07 EX 03. pro /* Recursive program to compute factorials. */ predicates factorial(integer, real) clauses factorial(1, 1) : - !. factorial(X, Fact. X) : - Y = X-1, factorial(Y, Fact. Y), Fact. X = X*Fact. Y. 344 -302 LP and Prolog 4 Chapter 8

Factorial Ex 07 EX 07. pro predicates factorial(integer, real) factorial_aux(integer, real, integer, real) /*

Factorial Ex 07 EX 07. pro predicates factorial(integer, real) factorial_aux(integer, real, integer, real) /* Numbers likely to exceed 32767 are declared as reals. */ clauses factorial(N, Fact. N) : factorial_aux(N, Fact. N, 1, 1). factorial_aux(N, Fact. N, I, P) : I <= N, !, New. P = P * I, New. I = I + 1, factorial_aux(N, Fact. N, New. I, New. P). factorial_aux(N, Fact. N, I, Fact. N) : I > N. 344 -302 LP and Prolog 5 Chapter 8

Factorial Ex 07 EX 08. pro /*Turbo Prolog 2. 0 Chapter 7, Example Program

Factorial Ex 07 EX 08. pro /*Turbo Prolog 2. 0 Chapter 7, Example Program 8*/ predicates factorial(integer, real) factorial(integer, real, integer, real) /* Numbers likely to exceed 32767 are declared as reals. */ clauses factorial(N, Fact. N): factorial(N, Fact. N, 1, 1). factorial(N, Fact. N, N, Fact. N): - !. factorial(N, Fact. N, I, P): New. I = I+1, New. P = P*New. I, factorial(N, Fact. N, New. I, New. P). 344 -302 LP and Prolog 6 Chapter 8

Status Ex 3. 6 status. pro /* Ex 3. 6 from thai book */

Status Ex 3. 6 status. pro /* Ex 3. 6 from thai book */ predicates status(symbol). sex(symbol, symbol). father(symbol, symbol). husband (symbol, symbol) clauses husband (suchart, malee). husband (somchai, monta). sex(female, malee). sex(female, monta). sex(male, suchart). father(suchart, poo). father(somchai, tik). father(somchai, tum). father(somchai, ta). father(somchai, tu). 344 -302 LP and Prolog sex(male, somchai). sex(female, susy). sex(male, mike). 7 Chapter 8

Status Ex 3. 6 status. pro status(X) : - sex(S, X), write(X, “ is

Status Ex 3. 6 status. pro status(X) : - sex(S, X), write(X, “ is “, S, " "), nl, fail. status(X) : husband(X, W), !, write("married wife = ", W), nl, write(" Chiledren : "), nl, father(X, C), write(" ", C), nl, fail. status(X) : husband (H, X), !, write("married husband = ", H), nl, write(" Chiledren : "), nl, father(H, C), write(" ", C), nl, fail. status(X) : write(X, " is single. n "). 344 -302 LP and Prolog 8 Chapter 8

Ex 5. 2 Solution. pro /* Ex 5. 2 from thai book */ /*

Ex 5. 2 Solution. pro /* Ex 5. 2 from thai book */ /* Find A = (X/B +B)/2 */ predicates solve(real, real). clauses solve(A, X, B) : A = (X/B + B)/2. 344 -302 LP and Prolog 10 Chapter 8

�������� ������ AX^2 + BX + C = 0 X = ( -B +-

�������� ������ AX^2 + BX + C = 0 X = ( -B +- sqrt(B*B -4 AC))/2 A 344 -302 LP and Prolog 11 Chapter 8

Ex 5. 3 Solution. pro /* Ex 5. 3 from thai book */ /*

Ex 5. 3 Solution. pro /* Ex 5. 3 from thai book */ /* Find AX^2 =BX + C = 0 */ /* Find X = ( -B +-sqrt(B*B -4 AC))/2 A */ predicates solve(real, real). run(real, real). clauses run(A, B, C) : D = B*B - (4*A*C), solve(A, B, D), nl. solve(_, _, D) : D < 0, write("No solution"). solve(A, B, D) : D = 0, X = -B / (2*A), write(" x = ", X), !. solve(A, B, D) : S = sqrt(D), X 1 = (-B + S)/(2*A), X 2 = (-B - S)/(2*A), write(" x 1 = ", X 1, " x 2 = ", X 2). 344 -302 LP and Prolog 12 Chapter 8

�������� ���������������� 2 ������� route(town, distance) road(town 1, town 2, 200) 344 -302 LP

�������� ���������������� 2 ������� route(town, distance) road(town 1, town 2, 200) 344 -302 LP and Prolog 13 Chapter 8

Ex 18 Ex 02. pro : Distance /*Turbo Prolog 2. 0 Chapter 18, Example

Ex 18 Ex 02. pro : Distance /*Turbo Prolog 2. 0 Chapter 18, Example Program 2 */ domains town = symbol distance = integer predicates road(town, distance) route(town, distance) clauses road(tampa, houston, 200). road(gordon, tampa, 300). road(houston, gordon, 100). road(houston, kansas_city, 120). road(gordon, kansas_city, 130). route(Town 1, Town 2, Distance) : road(Town 1, Town 2, Distance). route(Town 1, Town 2, Distance) : road(Town 1, X, Dist 1), route(X, Town 2, Dist 2), 344 -302 LP and Prolog Distance=Dist 1+Dist 2, !. 14 Chapter 8

EX 18 EX 01. pro : Animal predicates goal: animal_is(symbol) it_is(symbol) ask(symbol, symbol) positive(symbol,

EX 18 EX 01. pro : Animal predicates goal: animal_is(symbol) it_is(symbol) ask(symbol, symbol) positive(symbol, symbol) negative(symbol, symbol) clear_facts run clauses animal_is(cheetah) : - it_is(mammal), it_is(carnivore), positive(has, tawny_color), positive(has, dark_spots). animal_is(tiger) : - it_is(mammal), it_is(carnivore), positive(has, tawny_color), positive(has, black_stripes). 344 -302 LP and Prolog 15 Chapter 8

EX 18 EX 01. pro : Animal (cont. ) animal_is(giraffe) : - it_is(ungulate), positive(has,

EX 18 EX 01. pro : Animal (cont. ) animal_is(giraffe) : - it_is(ungulate), positive(has, long_neck), positive(has, long_legs), positive(has, dark_spots). animal_is(zebra) : - it_is(ungulate), positive(has, black_stripes). animal_is(ostrich) : - it_is(bird), negative(does, fly), positive(has, long_neck), positive(has, long_legs), positive(has, black_and_white_color). animal_is(penguin) : - it_is(bird), negative(does, fly), positive(does, swim), positive(has, black_and_white_color). animal_is(albatross) : it_is(bird), positive(does, fly_well). 344 -302 LP and Prolog 16 Chapter 8

EX 18 EX 01. pro : Animal (cont. ) it_is(mammal) : - positive(has, hair).

EX 18 EX 01. pro : Animal (cont. ) it_is(mammal) : - positive(has, hair). it_is(mammal) : - positive(does, give_milk). it_is(bird) : - positive(has, feathers). : - positive(does, fly), positive(does, lay_eggs). it_is(carnivore) : - positive(does, eat_meat). it_is(carnivore) : -positive(has, pointed_teeth), positive(has, claws), positive(has, forward_eyes). it_is(ungulate) : - it_is(mammal), positive(has, hooves). it_is(ungulate) : - it_is(mammal), positive(does, chew_cud). positive(X, Y) : - ask(X, Y, yes). negative(X, Y) : - ask(X, Y, no). 344 -302 LP and Prolog 17 Chapter 8

EX 18 EX 01. pro : Animal (cont. ) ask(X, Y, yes) : !,

EX 18 EX 01. pro : Animal (cont. ) ask(X, Y, yes) : !, write(“Question > “, X, " it ", Y, “? ”, ’ n’), readln(Reply), frontchar(Reply, 'y', _). ask(X, Y, no) : !, write(“Question > “, X, " it ", Y, “? ”, ’n’), readln(Reply), frontchar(Reply, 'n', _). clear_facts : write("nn. Please press the space bar to exitn"), readchar(_). run : animal_is(X), !, write("n. Answer. . => Your animal may be a (an) ", X), nl, clear_facts. run : write("n Answer. . => Unable to determine what"), write("your animal is. nn"), clear_facts. 344 -302 LP and Prolog 18 Chapter 8

Symbolic Expert System Logic Programming Propositional True Predicate Logic False Fact pred_name(att 1, att

Symbolic Expert System Logic Programming Propositional True Predicate Logic False Fact pred_name(att 1, att 2, var 1) 344 -302 LP and Prolog 19 VB Prolog Rule if. . then. . . Chapter 8

Symbolic interface write read + - * / mod Turbo Prolog factorial ! cut,

Symbolic interface write read + - * / mod Turbo Prolog factorial ! cut, fail and, or, not window domains predicates integer string symbol clauses fact List member head/tail reverse length 344 -302 LP and Prolog append goal rule recursive variable matching pred_name(att 1, att 2, var 1) 20 Chapter 8

Artificial Intelligence Symbolic Expert System Expert Specific Domain Easy to use User Interface Tool

Artificial Intelligence Symbolic Expert System Expert Specific Domain Easy to use User Interface Tool Prolog 344 -302 LP and Prolog VB Explanation Others 21 Knowledge Representation Knowledge Engineer Inference Engine Fact Rule if. . then. . . Chapter 8

Expert System Symbolic Knowledge Representation semantic network frame slot isa conceptual graph predicate Logic

Expert System Symbolic Knowledge Representation semantic network frame slot isa conceptual graph predicate Logic value inheritant pred_name(att 1, att 2) 344 -302 LP and Prolog 22 Chapter 8

DEMO EXPERT SYSTEMS 344 -302 LP and Prolog 23 Chapter 8

DEMO EXPERT SYSTEMS 344 -302 LP and Prolog 23 Chapter 8