1

I have a predicate check(Data,Res) that checksDats according to some rules and returns Res (a function result on Data, assuming Data answers to several criteria).

I have another function generate(N,Data) which generates a N-size Data.

My main program begins with generating many 1-size Data, then if none answered the criteria, we go on to 2-size Data and so on until we reach a certain M upper limit.

main(M):- next_number(N,M), generate(N,Data), check(Data,Res).

However, the program runs for very long time. I wanted to make sure it does not get stuck. For this, I wanted to print the generated Data each time before its being checked. But adding display did not assist, because it only actually displayed if the entire statement was true.

That's not what I want.

I want to keep track of the progran using display, similarly to System.out.println in Java.

Is there any other function that displays anyway? Or an idea how to use display in a way that will always display, regardless if the Data answered the criteria or not?

I thought to do:

(check(Data,Res) -> display(Data);display(Data)).

But I am not sure. Any ideas?

false
  • 10,264
  • 13
  • 101
  • 209
Dan D-man
  • 131
  • 3

2 Answers2

0

Your long process is likely to be within check - or more precisely, that check fails for most data, causing the system to backtrack repeatedly. If you display a result in check, you'll have line upon line of tracing. Instead, you could add a write statement to generate, or even to your number generation:

main(M):-
    next_number_and_tick(N,M),
    generate(N,Data),
    check(Data,Res).

next_number_and_tick(N,M) :-
    next_number(N,M),
    write('Tick - now doing '),
    writeln(N).

Upon backtracking, the program will signal the data size it is now working on, giving you an idea of the volume of work it is doing.

boisvert
  • 3,679
  • 2
  • 27
  • 53
0

The problem in the way you use display is that the entire statement must be true for it to display. Your idea of using "if-then" is good but not accurate. If you want to use it, you should "trick" prolog the following way:

new_check(Data,Res) :- (check(Data,Res) -> display('Victory!'),!; display('Failed Data: '), display(Data), nl, fail).

This way, if the check fails, you will get a report on which Data failed, and if it succeeded everything stops (assuming you want only 1 solution. If you want more, remoce the ! predicate).