scryer-prolog icon indicating copy to clipboard operation
scryer-prolog copied to clipboard

Inference counter would benefit from fine-tuning

Open triska opened this issue 2 years ago • 3 comments

For example, with:

count(Goal, Count) :-
        '$inference_count'(C0),
        Goal,
        '$inference_count'(C),
        Count is C - C0.

We get:

?- count(true, C).
   C = 1.

?- count((true,true),C).
   C = 3.

?- count((true,true,true), C).
   C = 4.

So, adding the same conjunct sometimes increments the count by 1, and sometimes by 2. Maybe there is a way to tune this.

Similarly, we get:

?- count((true;true), C).
   C = 2
;  C = 264.

Why so high?

Further, we have:

?- count((false;true), C).
   C = 2.

So, false or (;)/2 has influence on the the counter.

Yet, we also get:

?- count((false;false;false;true), C).
   C = 2.

So, further false and (;)/2 apparently have no influence.

triska avatar Nov 24 '23 07:11 triska

The global inference count includes the intermediary calls in the toplevel when backtracking. That's the reason for the gap 2 - 264.

mthom avatar Nov 24 '23 17:11 mthom

Oh interesting. That means that disjuctions in general will have inflated inference counts because they invoke the top level.

To fix this I guess you'd have to call count/2 for each disjuction and handle backtracking internally or something.

infogulch avatar Nov 24 '23 18:11 infogulch

call_with_inference_limit/3 avoids it by bracketing a local inference count around its goal. time/1 could do something similar but it's tricky.. you don't want to time the inferences made in the count setup, for one.

mthom avatar Nov 24 '23 19:11 mthom