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 >
Text File  |  1996-06-04  |  1KB  |  57 lines

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