Download presentation
Presentation is loading. Please wait.
Published byBertina Bradford Modified over 8 years ago
1
1 TP #4: Control Facilities n Last TP exercises solved; n Don’t care variables « _ »; n Or « ; »; n Cut.
2
© L.Lúcio, 2003 2 Last TP exercises solved n Ex. 1: evenlength and oddlength: my_length(0,[]). my_length(X,[H|T]):- my_length(X1,T),X is X1+1. oddlength(L):- my_length(X,L),(X mod 2)=\=0. evenlength(L):- my_length(X,L),(X mod 2)=:=0. Note: the « length » predicate is already defined in SWI Prolog. n Ex. 2: shift: shiftright([H|T],L):- append(T,[H],L).
3
© L.Lúcio, 2003 3 Last TP exercises solved (cont) n Ex. 3: Relational database: :- dynamic country/1. :- dynamic company/2. :- dynamic person/4. country(portugal). country(france). country(usa). company(cocacola,usa). company(renault,france). company(machintosh,usa). person(costa,joao,cocacola,usa). person(baker,john,renault,france). add_country(X):- assert(country(X)). add_company(X,Y):- country(X), assert(company(X,Y)). add_person(X,Y,T,Z):- country(T), company(Z,_), assert(person(X,Y,T,Z)). remove_person(X,Y,T,Z):- retract(person(X,Y,T,Z)). remove_company(X,Y):- not(person(_,_,X,Y)), retract(company(X,Y)). remove_country(X):- not(person(_,_,_,X)), not(company(_,X)), retract(country(X)).
4
© L.Lúcio, 2003 4 Don’t care Variables n In the Relational Database example the « _ » (don’t care variable) was used to find certain information within facts. For example: add_person(X,Y,T,Z):- country(T), company(Z,_), assert(person(X,Y,T,Z)). Prolog will find all the instances of « company » That have Z as its first argument n The « don’t care » variable is extremely useful while querying fact databases. For example one can replace the variables we don’t care about by « _ » and find facts that obey a given criteria: person(baker,john,married,1960). person(black,ann,single,1976). person(smith,shane,married,1972). ? – person(X,_,_,Y),Y>1970. X = black Y = 1976 ; X = smith Y = 1972 ; No
5
© L.Lúcio, 2003 5 OR n The « or » is expressed in Prolog in two ways. One is already well known: man(jean). woman(marie). person(X) :- man(X). person(X) :- woman(X). ?- person(marie). Yes. There is an implicit « or » between the two rules. Either a person is a man OR a person is a woman. n There is a simpler, more compact way of representing this in Prolog: person(X) :- man(X); woman(X). The goal « person(X) » evaluates to true either if X is a woman or X is a man.
6
© L.Lúcio, 2003 6 Exercise n Code a simple menu that prints in the screen the following options: u 1: Pasta u 2: Fish u 3: Meat u 4: Tofu u 0: Nothing for today n The user will be given a cursor to choose one of the dishes or exit. If one of the dishes is chosen, then the program prints « you have chosen dish … » and succeeds. If ‘0’ (zero) is chosen then the program exits and fails. u Use « write(‘some string’) » to write to the console and « read(X) » to read a term from the console and unify it with X.
7
© L.Lúcio, 2003 7 Exercise solved menu :- write('1: Pasta'),nl, write('2: Meat'),nl, write('3: Fish'),nl, write('4: Tofu'),nl, write('0: Nothing for today'),nl, read(X), ( X=:=0, !, fail ; write('You have chosen: '), ( X=:=1, write('Pasta') ; X=:=2, write('Meat') ; X=:=3, write('Fish') ; X=:=4, write('Tofu') ) ). The « ! » symbol is called « cut ». What is its purpose here?
8
© L.Lúcio, 2003 8 The « Cut » n Motivation: rule to calculate the maximum of two numbers: max(X,Y,X) :- X >= Y. max(X,Y,Y) :- X < Y. n Prolog executes this inneficiently, because the conditions are mutually exclusive: n If the first one is true, then there is no need to backtrack to the second; n If the first one is false, then there is no need to evaluate the second condition because it is surely true; n But, Prolog’s resolution mechanism backtracks automatically…
9
© L.Lúcio, 2003 9 The « Cut » (cont) n More efficient: max(X,Y,X) :- X >= Y,!. max(X,Y,Y) :- X < Y. n Now there will be no backtracking if the first codition is true… n But, we can make it even more efficient: max(X,Y,X) :- X >= Y,!. max(X,Y,Y). n The second condition is useless because it is only reached if the first one is false – in which case the second one is necessarily true.
10
© L.Lúcio, 2003 10 The « Cut » (cont) n This ressembles very much an « if … then … else » structure: max(X,Y,X) :- X >= Y,!. max(X,Y,Y). If X >= Y then the max is X else the max is Y n Of course it only works for mutually exclusive conditions; n And it has problems, e.g. the last argument of the relation has to be instantiated, otherwise we may get strange results like: ?- max(6,3,3). Yes. Why?
11
© L.Lúcio, 2003 11 Exercise n The following relation classifies numbers into three classes: positive, negative and zero. class(Number,positive) :- Number>0. class(0,zero). class(Number,negative) :- Number<0. n How would you define this relation more efficiently using cut? class(0,zero):-!. class(Number,positive):-Number>0,!. class(_,negative). n In natural language this could be expressed as n If the number is ‘0’ then the class is ‘zero’; n Else if the number is greater then zero the class is ‘positive’ n Else the class is ‘negative.
12
© L.Lúcio, 2003 12 The « Cut » - Conclusions n The « Cut » is a facility Prolog provides in order to control backtracking; n when the « Cut » is reached the solutions that have been found are frozen and Prolog stops backtracking through the alternatives that already have been explored at least once; n « Cut » may be used for: u making the execution of Prolog programs more efficient; u adding expressive power to Prolog - the « if…then…else » construct. n Using the « Cut » facility may be dangerous, as it is possible to get secondary effects and to « twist » the declarative meaning of programs.
13
© L.Lúcio, 2003 13 Final question n Why is the cut used in the menu exercise? menu :- write('1: Pasta'),nl, write('2: Meat'),nl, write('3: Fish'),nl, write('4: Tofu'),nl, write('0: Nothing for today'),nl, read(X), ( X=:=0, !, fail ; write('You have chosen: '), ( X=:=1, write('Pasta') ; X=:=2, write('Meat') ; X=:=3, write('Fish') ; X=:=4, write('Tofu') ) ).
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.