chapter 8 arithmetic program examples. 344-302 lp and prolog chapter 82 factorial1.pro predicates...
TRANSCRIPT
Chapter 8 2344-302 LP and Prolog
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 3344-302 LP and Prolog
Factorial2.pro
predicates factorial(integer, integer)clauses
factorial(1, 1) :- !. factorial(N, Result) :-
M = N-1, factorial(M, RET),
Result = N*RET.
Chapter 8 4344-302 LP and Prolog
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 5344-302 LP and Prolog
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.
Factorial Ex07EX07.pro
Chapter 8 6344-302 LP and Prolog
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 7344-302 LP and Prolog
/* 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).
Status Ex3.6status.pro
sex(female,malee).
sex(female,monta).
sex(male,suchart).
sex(male,somchai).
sex(female,susy).
sex(male,mike).
Chapter 8 8344-302 LP and Prolog
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 ").
Status Ex3.6status.pro
Chapter 8 9344-302 LP and Prolog
โจทย์�
จงเขี�ยนโปรแกรมภาษาโปรล็�อกเพื่��อคำ�านวณหาคำ�า
A = (X/B +B)/2
Chapter 8 10344-302 LP and Prolog
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 11344-302 LP and Prolog
โจทย์�จงเขี�ยนโปรแกรมภาษาโปรล็�อกเพื่��อคำ�านวณหาคำ�า
AX^2 + BX + C = 0
X = ( -B +- sqrt(B*B -4AC))/2A
Chapter 8 12344-302 LP and Prolog
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 13344-302 LP and Prolog
โจทย์�จงเขี�ยนโปรแกรมภาษาโปรล็�อกเพื่��อ
คำ�านวณหาคำ�าระยะห�างจากเม�อง 2 เม�องใดๆ
route(town, town, distance)
road(town1, town2, 200)
Chapter 8 14344-302 LP and Prolog
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 15344-302 LP and Prolog
EX18EX01.pro : Animalpredicates animal_is(symbol) it_is(symbol) ask(symbol, symbol, symbol) positive(symbol, symbol) negative(symbol, symbol) clear_facts runclauses 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).
goal:
run
Chapter 8 16344-302 LP and Prolog
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 17344-302 LP and Prolog
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).
EX18EX01.pro : Animal (cont.)
Chapter 8 18344-302 LP and Prolog
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.
EX18EX01.pro : Animal (cont.)
Chapter 8 19344-302 LP and Prolog
Logic Programming
Propositional Predicate Logic
True False
Prolog
Fact Rule
pred_name(att1,att2,var1) if....then...
Expert System
VB
Symbolic
Chapter 8 20344-302 LP and Prolog
Turbo Prolog
domains predicates clauses
fact rule
Listinteger
stringsymbol
Symbolic
recursive
variable matching
pred_name(att1,att2,var1)
and, or, not
! cut, fail
+ - * / mod
interface
member appendreverse
read
factorial
write
window
head/tail length
goal
Chapter 8 21344-302 LP and Prolog
Expert System
Specific Domain User Interface Inference Engine
Fact Rule
if....then...
Artificial Intelligence
Explanation
Knowledge Engineer
Expert
Easy to use Tool
VBProlog Others
Symbolic Knowledge Representation
Chapter 8 22344-302 LP and Prolog
Knowledge Representation
semantic network
frame conceptual graph
slot
isa inheritant
Symbolic
predicate
Logic
pred_name(att1,att2)
Expert System
value