home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
lifeos2.zip
/
LIFE-1.02
/
TESTS
/
LF
/
BRUNO_DI.LF
< prev
next >
Wrap
Text File
|
1996-06-04
|
1KB
|
57 lines
%
%
% a little program to show the backtrack problem
%
%
% cond_funct is a function that replaces the usual "cond" function written in C
cond_funct(true,X,Y) -> X.
cond_funct(false,X,Y) -> Y.
% choicept(N) creates a choice-point, and deletes it on backtracking.
choicept(N) :- write("create CP number",N),nl.
choicept(N) :- write("delete CP number",N),nl, fail.
%
% The 3 programs below create choice points, starting at number 1, 2 or 3, and
% up to number 9.
%
% Manual backtracking (bad behaviour)
%
main1 :- write(X:{1;2;3}),nl, create_CP1(X,1).
create_CP1(X,N) :- choicept(N), cond_funct(X=:=9, nl, create_CP1(X+1,N+1)).
%
% Automatic backtracking: backtracking is enforced by the fail in the second
% argument of cond_funct (good behaviour)
%
main2 :- write(X:{1;2;3}),nl, create_CP2(X,1).
create_CP2(X,N) :- choicept(N),
cond_funct(X=:=9, (nl,fail),create_CP2(X+1,N+1)).
%
% using a predicate for cond instead of a function (good behaviour)
%
cond_pred(true,X,Y) :- !, X.
cond_pred(false,X,Y) :- Y.
main3 :- write(X:{1;2;3}),nl, create_CP3(X,1).
create_CP3(X,N) :- choicept(N), cond_pred(X=:=9, nl, create_CP3(X+1,N+1)).