Using sum in subsumptive tabling

I’ve solved day 11, part 2 (advent of code 2025), with subsumptive tabling, but cannot find the right syntax/semantic to use the table sum specification.

That is this works

:- table count_paths(+,+,+,-).
count_paths(T,T,_,1).
count_paths(S,T,U,N) :-
    memberchk(S-Cs,U),
    aggregate_all(sum(V), (
       member(C,Cs),
       count_paths(C,T,U,V)
    ),N).

while this one doesn’t:

:- table count_paths(+,+,+,sum).
count_paths(T,T,_,1).
count_paths(S,T,U,N) :-
    memberchk(S-Cs,U),
    maplist(count_path(T,U),Cs,Ns),
    sumlist(Ns,N).
count_path(T,U,C,N) :-
    count_paths(C,T,U,N).

How to take advantage of :- table count_paths(+,+,+,sum). ?

1 Like

Without knowing exactly what this should do it gets a little hard to answer. The sum operation is pretty dubious though. This thing is called subsumptive tabling because the final answer subsumes all individual answers. This works fine for e.g., max. sum however sums all answers and thus depends on whether or not the predicate produces duplicate answers. Removing duplicates (variants) is a key property of tabling. Duplicates on the summed argument are not removed (this cannot be done as the purpose is typically to end up with a single answer and thus save space, so the individual answers are not saved). Thus

:- table p(sum).

p(1).
p(1).
p(2).

Results in ?- p(X).X = 4.

It should have been called aggregative tabling :slight_smile: That still does not fix the fact that duplicates are not well defines in the context of tabling.

4 Likes