230 likes | 335 Views
Chapter 8. Arithmetic Program Examples. Factorial1.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) :-
E N D
Chapter 8 Arithmetic Program Examples
Factorial1.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”). Chapter 8
Factorial2.pro predicates factorial(integer, integer) clauses factorial(1, 1) :- !. factorial(N, Result) :- M = N-1, factorial(M, RET), Result = N*RET. Chapter 8
Factorial Ex07EX03.pro /* Recursive program to compute factorials. */ predicates factorial(integer, real) clauses factorial(1, 1) :- !. factorial(X, FactX) :- Y = X-1, factorial(Y, FactY), FactX = X*FactY. Chapter 8
Factorial Ex07EX07.pro predicates factorial(integer, real) factorial_aux(integer, real, integer, real) /* Numbers likely to exceed 32767 are declared as reals. */ clauses factorial(N, FactN) :- factorial_aux(N, FactN, 1, 1). factorial_aux(N, FactN, I, P) :- I <= N, !, NewP = P * I, NewI = I + 1, factorial_aux(N, FactN, NewI, NewP). factorial_aux(N, FactN, I, FactN) :- I > N. Chapter 8
Factorial Ex07EX08.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,FactN):- factorial(N,FactN,1,1). factorial(N, FactN, N, FactN):- !. factorial(N, FactN, I, P):- NewI = I+1, NewP = P*NewI, factorial(N, FactN, NewI, NewP). Chapter 8
Status Ex3.6status.pro /* Ex3.6 from thai book */ predicates status(symbol). sex(symbol,symbol). father(symbol,symbol). husband (symbol,symbol) clauses husband (suchart,malee). husband (somchai,monta). father(suchart,poo). father(somchai,tik). father(somchai,tum). father(somchai,ta). father(somchai,tu). sex(female,malee). sex(female,monta). sex(male,suchart). sex(male,somchai). sex(female,susy). sex(male,mike). Chapter 8
Status Ex3.6status.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 "). Chapter 8
โจทย์ จงเขียนโปรแกรมภาษาโปรล็อกเพื่อคำนวณหาค่า A = (X/B +B)/2 Chapter 8
Ex5.2 Solution.pro /* Ex5.2 from thai book */ /* Find A = (X/B +B)/2 */ predicates solve(real,real,real). clauses solve(A,X,B) :- A = (X/B + B)/2. Chapter 8
โจทย์ จงเขียนโปรแกรมภาษาโปรล็อกเพื่อคำนวณหาค่า AX^2 + BX + C = 0 X = ( -B +- sqrt(B*B -4AC))/2A Chapter 8
Ex5.3 Solution.pro /* Ex5.3 from thai book */ /* Find AX^2 =BX + C = 0 */ /* Find X = ( -B +-sqrt(B*B -4AC))/2A */ predicates solve(real,real,real). run(real,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), X1 = (-B + S)/(2*A), X2 = (-B - S)/(2*A), write(" x1 = ",X1," x2 = ",X2). Chapter 8
โจทย์ จงเขียนโปรแกรมภาษาโปรล็อกเพื่อคำนวณหาค่า ระยะห่างจากเมือง 2 เมืองใดๆ route(town, town, distance) road(town1, town2, 200) Chapter 8
Ex18Ex02.pro : Distance /*Turbo Prolog 2.0 Chapter 18, Example Program 2 */ domains town = symbol distance = integer predicates road(town, town, distance) route(town, 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(Town1, Town2, Distance) :- road(Town1, Town2, Distance). route(Town1, Town2, Distance) :- road(Town1, X, Dist1), route(X, Town2, Dist2), Distance=Dist1+Dist2, !. Chapter 8
EX18EX01.pro : Animal goal: run predicates animal_is(symbol) it_is(symbol) ask(symbol, 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). Chapter 8
EX18EX01.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). Chapter 8
EX18EX01.pro : Animal (cont.) it_is(mammal) :- positive(has, hair). it_is(mammal) :- positive(does, give_milk). it_is(bird) :- positive(has, feathers). it_is(bird) :- 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). Chapter 8
EX18EX01.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("\n\nPlease press the space bar to exit\n"), readchar(_). run :- animal_is(X), !, write("\nAnswer.... => Your animal may be a (an) ",X), nl, nl, clear_facts. run :- write("\n Answer.... => Unable to determine what"), write("your animal is.\n\n"),clear_facts. Chapter 8
Symbolic Expert System Logic Programming VB Propositional Predicate Logic Prolog True False Fact Rule pred_name(att1,att2,var1) if....then... Chapter 8
+ - * / mod Symbolic factorial Turbo Prolog interface ! cut, fail write read and, or, not window recursive predicates clauses goal domains string variable matching symbol fact rule integer List pred_name(att1,att2,var1) member reverse append head/tail length Chapter 8
Symbolic Artificial Intelligence Knowledge Representation Expert System Knowledge Engineer Expert Specific Domain User Interface Inference Engine Easy to use Tool Explanation Fact Rule Prolog VB Others if....then... Chapter 8
Symbolic Expert System Knowledge Representation semantic network frame conceptual graph predicate Logic slot value isa inheritant pred_name(att1,att2) Chapter 8
DEMO • EXPERT SYSTEMS Chapter 8