Predicate cache

Is there a Prolog implementation or library that caches predicates?

Or you implement, say, a FIFO cache using assertz / 1 and retract / 1, for example:

:- dynamic cache/1. ccall(G) :- cache(G). ccall(G) :- \+ cache(G), call(G), ( findall(G0,cache(G0),Gs), length(Gs,N), N =:= 100 -> once retract(cache(_)) ; true ), assertz(cache(G)). 

In ECLiPSe-CLP, you can at least replace the findall / 3 line with additional logical variables:

 ... ( getval(cache_size) =:= 100 -> once retract(cache(_)) ; incval(cache_size) ), ... 

In my box, 1000 calls of this ccall / 1 take> 4.00 cpu sec, while the actual CPU time cannot be negligible (0.04 cpu sec). Therefore, I assume that the cache (especially the LRU cache or so) implemented inside the interpreter will still exceed assertz / 1 and retract / 1.

I do not want to have caching for each predicate, of course, only for very few. The scenario may be this: p([H|T], E) :- q(H,E) ; p(T,E) p([H|T], E) :- q(H,E) ; p(T,E) with q/2 without side effects. p/2 is called for an ever-growing list, but always / often for the same E

+6
source share
1 answer

Do you want a tab / demo ?
XSB offers automatic tabulation (you declare which predicates you want to have in the table)

and yes, assertz / 1, etc. slow

+4
source

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


All Articles