Printing solutions on the way to a goal


I am playing around with inductive logic programming, getting Prolog to search for theories. Naturally, such searches take a lot of time. Are there any good ways to print out every Nth solution during the search (instead of every solution)? In more concrete terms, and simplifying, suppose I have

  member(X1, List),
  format('~w~w ~w~n', ['X1 and X2 are ', X1, X2]).
  member(X2, List).

test:- p, fail.

Instead of printing every solution, as the format/2 above does, I would like to print, say, every 10th solution. Or maybe a solution every 10 seconds. In my actual program I don’t want to have a recursive loop to conduct the search (in that case I know that I could have a counter that increments on each recursion and that I could use to print at certain intervals).

Could I use, for example, statistics(runtime, [CurrentTime | _]) somehow? I did some dubious experiments with maybe/1 but that did really not feel right :see_no_evil: Any suggestions welcome :slight_smile:

Kind regards, JCR

One way would be to use mod/2 with the real_time value, something like this.

p :- 
  % ...
  % writeln([t1=T1,t2=T2]),
  (T1 mod 10 =:= 0 -> format('~w~w ~w~n', ['X1 and X2 are ', X1, X2]) ; true),

If the program is fast it will print a lot of solutions so you have to play with the specific value of the modulo value. For testing this you can use sleep/1 for make the program slower.

1 Like

Thanks for the suggestion!

If you want it count based rather than time based, use call_nth/2 to get solutions and their count at the same time.

1 Like

Thanks! I’ll also try this.