Tuesday, December 8, 2009

Chapter 6 from E-Book : Looping with PROLOG

6.1 LOOPING A FIXED NUMBER OF TIMES

Loops enable a set of instructions to be executed a fixed number of times. In prolog, looping can be obtained using recursion. In this case, looping function on PROLOG is similiar to another programming language. We can see in the example given below:

loop(0).
loop(N):-N>0,write('The value is: '),write(N),nl,
M is N-1,loop(M).

Those clauses means that ‘the terminating condition of the recurtion is 0. To loop from N, first determine the value of N, then substract it to having M, then loop from M, until the value is 0. If the value is 0, the looping process is stopped there’

6.2 LOOPING UNTIL A CONDITION IS SATISFIED
No facility on PROLOG that directly enable a set of instructions to be executed repeatedly until a given condition is met. But there are two way to obtained the similiar effect :

- Recursion
Recursion read the input from keyboard, and output it to the screen, until the ‘end’ instruction is endountered.
Example of the form :

go:-loop(start). /* start is a dummy value used to get
the looping process started.*/
loop(end).
loop(X):-X\=end,write('Type end to end'),read(Word),
write('Input was '),write(Word),nl,loop(Word).

The last clauses mean: The looping of X will be stopped if the ‘end’ is encountered. Saat looping berlangsung (before ‘end’ had encountered), PROLOG will always ask the user to enter the input by the sentence ‘Type end to end’ .

- Using the ‘repeat’ Predicate
The goal repeat doesn’t repeat anything, it will be succeeds whenever it’s being called. If user enter another term but yes or no, PROLOG will always repeat the instruction until the user enter the term ‘yes’ or ‘no’. This is the example of the form :

get_answer(Ans):-
write('Enter answer to question'),nl,
repeat,write('answer yes or no'),read(Ans),
valid(Ans),write('Answer is '),write(Ans),nl.
valid(yes). valid(no).

In the case of looping, the two goals write(‘answer yes or no’) and read(Ans) will always repeat until the condition valid(Ans) had satisfied.

Repeat predicate can also processing the sequence of terms from a specified file an outputs them until the term ‘end’ is encountered.
The example of the form :

readterms(Infile):-
seeing(S),see(Infile),
repeat,read(X),write(X),nl,X=end,
seen,see(user).

Then, the file that will being outputted containing (for example, the file ‘myfile.txt’ :

'first term'. 'second term'.
'third term'. 'fourth term'.
'fifth term'. 'sixth term'.
'seventh term'.
'eighth term'.
end.

Then, call the goal readterms will produced a result :

?- readterms('myfile.txt').
first term
second term
third term
fourth term
fifth term
sixth term
seventh term
eighth term
end
yes

If ‘end’ hasn’t been encountered, there will be a looping process between the goals repeat and X=end.

6.3 Backtracking with Failure

ex:
Supposing the database contains clauses such as
dog(fido).
dog(fred).
dog(jonathan).

Each dog clause can be processed in turn using the alldogs predicate defined
below.
alldogs:-dog(X),write(X),write(' is a dog'),nl,fail.
alldogs.

The effect is to loop through the database finding all possible values of X that
satisfy the goal dog(X).
?- alldogs.
fido is a dog
fred is a dog
jonathan is a dog
yes


Note the importance of the second clause of the alldogs predicate. It is there to
ensure that, after the database has been searched, the goal succeeds. With only the
first line, any call to alldogs will eventually fail.

The next program is designed to search a database containing clauses
representing the name, age, place of residence and occupation of a number of
people.It should be noted that it is not always necessary to use 'backtracking with
failure' to search the database.


Finding Multiple Solutions

Suppose that a predicate findroute(Town1,Town2,Route) finds a route Route
between two towns Town1 and Town2.

as follows:
find_all_routes(Town1,Town2):-
findroute(Town1,Town2,Route),
write('Possible route: '),write(Route),nl,fail.
find_all_routes(_,_).


So, Backtracking with failure can also be used to find all the ways of satisfying a goal.
And also describes how a set of goals can be evaluated repeatedly in Prolog,
either a fixed number of times or until a specified condition is met, and how
multiple solutions can be arrived at using the technique of 'backtracking with
failure'.



No comments:

Post a Comment