home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lifeos2.zip / LIFE-1.02 / TESTS / LF / T3207.LF < prev    next >
Text File  |  1996-06-04  |  1KB  |  58 lines

  1. %
  2. %
  3. % Backtrack test program
  4. %
  5. %
  6.  
  7.  
  8. % choicept(N) creates a choice-point, and deletes it on backtracking.
  9.  
  10. choicept(N) :- write("create CP number",N),nl.
  11. choicept(N) :- write("delete CP number",N),nl, fail.
  12.  
  13.  
  14. %
  15. %  The 3 programs below create choice points, starting at number 1, 2 or 3, and
  16. %  up to number 9.
  17.  
  18.  
  19. main :- ( ( nl, nl, write("MANUAL BACKTRACKING"), nl, nl, main1,fail) ; 
  20.           ( nl, nl, write("AUTOMATIC  BACKTRACKING"), nl, nl, main2 ); 
  21.           ( nl, nl, write("COND PREDICATE"), nl, nl, main3,fail);
  22.             succeed ).
  23.  
  24.  
  25. %
  26. %  Manual backtracking
  27. %
  28.  
  29. main1  :- write(X:{1;2;3}),nl, create_CP1(X,1).
  30.  
  31. create_CP1(X,N) :-  choicept(N), cond(X=:=9, nl, create_CP1(X+1,N+1)).
  32.  
  33.  
  34. %
  35. %   Automatic backtracking: backtracking is enforced by the fail in the second
  36. %   argument of cond.
  37. %
  38.  
  39. main2  :- write(X:{1;2;3}),nl, create_CP2(X,1).
  40.  
  41. create_CP2(X,N) :-  choicept(N),
  42.                     cond(X=:=9, (nl,fail),create_CP2(X+1,N+1)). 
  43.  
  44.  
  45. %
  46. %  using a predicate for cond instead of a function
  47. %
  48.  
  49. cond_pred(true,X,Y) :- !, X.
  50. cond_pred(false,X,Y) :- Y.
  51.  
  52.  
  53. main3  :- write(X:{1;2;3}),nl, create_CP3(X,1).
  54.  
  55. create_CP3(X,N) :-  choicept(N), cond_pred(X=:=9, nl, create_CP3(X+1,N+1)).
  56.  
  57. main?
  58.