Overlapping facts in Prolog

Suppose it f(X)is a dynamic fact, which can be asserted and retracted, and let Xthere always be a number. Suppose that the most complete query is to find f(X)one that is minimal. In SWI-Prolog, I can write:

min_f(R) :- aggregate(min(X), f(X), R).

But apparently this always forces Prolog to perform a linear search on all the facts. Now suppose there will be many such facts (for example, 1,000,000). Since I know in advance that I often do min_f/1:

  • Can I put things in order f/1so that the engine can find a minimum in O (1)?
  • I can explicitly specify a assertmini-heap containing all the facts, and then look into the head; could facts be stored in an inactive heap?

I have no restrictions on Prolog dialogs, so any alternative Prolog implementation will be fine.

+5
source share
2 answers

An alternative to data structures that can quickly deliver minimum or maximum would be to use a directional tabulation mode. In the tabulation mode in the mode, you can specify the necessary aggregate functions:

f(5).
f(4).
f(6).

:- table min_f(min).
min_f(X) :- f(X).

The minimum will be calculated only once, here is an example request. It works in SWI-Prolog, and from version 1.4.0 also in Jekejeke Prolog:

?- min_f(X).
X = 4.

Theoretically, this remains with the implementation of the tab, as it calculates the min. At present, developments are also taking place, additional tabulations that will make f / 1 dynamic, and then track changes.

+1
:- dynamic(f/1).

min_f(X) :-
    f(X),
    !.
assert_f(X) :- 
    min_f(Min),
    Min<X,
    assertz(f(X)),
    !.
assert_f(X) :-
    asserta(f(X)).

assert_f/1 assert/1. f/1 , , f/1 .

0

Source: https://habr.com/ru/post/1611572/


All Articles