1.1.6. Upravleniye prosessom resheniya zadachi.
1.1.6.1 Ispolzovaniye predikata fail. V PROLOGe realizovan mexanizm poiska s vozvratom (backtracking), pri kotorom sistema pыtayetsya otыskat vse vozmojnыye resheniya zadachi. Mexanizm vыvoda programmы zapominayet te tochki prosessa unifikasii, v kotorыx ne bыli ispolzovanы vse alternativnыye resheniya, a zatem vozvraщayetsya v eti tochki i iщyet resheniye po inomu puti.
Odnako poisk s vozvratom vыpolnyayetsya avtomaticheski tolko v tex sluchayax, kogda programma reshayet zadachu v rezultate dialoga s polzovatelem. Yesli je sel ukazana v razdele goal programmы, to poisk okanchivayetsya posle naxojdeniya pervogo resheniya zadachi. V etom sluchaye dlya vыvoda vsex resheniy ispolzuyetsya predikat fail.
Predikat fail nazыvayut otkatom posle neudachi. On vыzыvayet iskusstvennoye neuspeshnoye zaversheniye poiska, chto pozvolyayet poluchit vse vozmojnыye resheniya zadachi.
Programma 7
PREDICATES
gorod(symbol)
show
GOAL
write(“Eto goroda:”),nl,show.
CLAUSES
gorod(“moskva”). gorod(“minsk”).
gorod(“kiyev”). gorod(“omsk”).
show :- gorod(X), write(X),nl,fail.
Posle zapuska budut napechatanы vse nazvaniya gorodov. Proverte, kak budet rabotat programma bez predikata fail.
1.1.6.2. Ispolzovaniye predikata cut. Predikat otsecheniya cut oboznachayetsya s pomoщyu simvola !. On pozvolyayet poluchit dostup tolko k chasti dannыx, ustranyaya dalneyshiye poiskovыye deystviya. V obщyem vide mojno zapisat, naprimer:
R : – A,B, !, C
Eto oznachayet, chto yesli dlya seley A i V naydeno xotya bы odno resheniye, to dalneyshiy perebor vozmojnыx variantov znacheniy A i V ne nujen. Programmы 8 i 9 illyustriruyut deystviye predikata cut.
Programma 8
DOMAINS
person=symbol
PREDICATES
deti(person)
show
make_cut(person)
GOAL
write(“Eto malchiki:”),nl,show.
CLAUSES
deti(“Petya”).deti(“Vasya”).deti(“Oleg”).
deti(“Masha”).deti(“Olya”).deti(“Natasha”).
show :- deti(X),write(X),nl,make_cut(X),!.
make_cut(X) :- X=”Masha”.
Programma 8 napechatayet tolko imena malchikov.
Programma 9
PREDICATES
buy_car(symbol,symbol)
car(symbol,symbol,integer)
color(symbol,symbol)
CLAUSES
buy_car(Model,Color) :-
car(Model,Color,Price),color(Color,”svetlыy”),!,Price<25000.
Car(“moskvich”,”siniy”,12000).
Car(“jiguli”,”zelenыy”,26000).
Car(“volvo”,”siniy”,24000).
Car(“volga”,”siniy”,20000).
Car(“audi”,”zelenыy”,20000).
Color(“siniy”,”temnыy”).
Color(“zelenыy”,”svetlыy”).
Eta programma ne naydet ni odnogo resheniya, poskolku posle dorogix zelenыx «jiguley» poisk zakanchivayetsya, i boleye deshevыye «audi» ne budut naydenы.
Do'stlaringiz bilan baham: |