Transcript Chapter

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”).
344-302 LP and Prolog
2
Chapter 8
Factorial2.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 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.
344-302 LP and Prolog
4
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.
344-302 LP and Prolog
5
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).
344-302 LP and Prolog
6
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).
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 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 ").
344-302 LP and Prolog
8
Chapter 8
โจทย์
จงเขียนโปรแกรมภาษาโปรล็อกเพือ่ คานวณหาค่ า
A = (X/B +B)/2
344-302 LP and Prolog
9
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.
344-302 LP and Prolog
10
Chapter 8
โจทย์
จงเขียนโปรแกรมภาษาโปรล็อกเพือ่ คานวณหาค่ า
AX^2 + BX + C = 0
X = ( -B +- sqrt(B*B -4AC))/2A
344-302 LP and Prolog
11
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).
344-302 LP and Prolog
12
Chapter 8
โจทย์
จงเขียนโปรแกรมภาษาโปรล็อกเพือ่ คานวณหาค่ า
ระยะห่ างจากเมือง 2 เมืองใดๆ
route(town, town, distance)
road(town1, town2, 200)
344-302 LP and Prolog
13
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),
344-302 LP and Prolog
Distance=Dist1+Dist2, !.
14
Chapter 8
EX18EX01.pro : Animal
predicates
goal:
animal_is(symbol)
it_is(symbol)
ask(symbol, symbol, symbol)
positive(symbol, symbol)
negative(symbol, symbol)
clear_facts
run
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
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).
344-302 LP and Prolog
16
Chapter 8
EX18EX01.pro : Animal (cont.)
it_is(mammal) :- positive(has, hair).
it_is(mammal) :- positive(does, give_milk).
it_is(bird)
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
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.
344-302 LP and Prolog
18
Chapter 8
Symbolic
Expert System
Logic Programming
Propositional
True
Predicate Logic
False
Fact
pred_name(att1,att2,var1)
344-302 LP and Prolog
19
VB
Prolog
Rule
if....then...
Chapter 8
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(att1,att2,var1)
20
Chapter 8
Artificial Intelligence
Symbolic
Expert System
Knowledge
Representation
Knowledge
Engineer
Expert
Specific Domain
Easy to use
User Interface
Tool
Prolog
344-302 LP and Prolog
VB
Explanation
Others
21
Inference Engine
Fact
Rule
if....then...
Chapter 8
Expert System
Symbolic
Knowledge Representation
semantic
network
frame
slot
isa
conceptual
graph
predicate
Logic
value
inheritant
pred_name(att1,att2)
344-302 LP and Prolog
22
Chapter 8
DEMO
EXPERT SYSTEMS
344-302 LP and Prolog
23
Chapter 8