# Using sum in subsumptive tabling

**URL:** <https://swi-prolog.discourse.group/t/using-sum-in-subsumptive-tabling/9499>\
**Category:** Predicate\
**Created:** [January 28, 2026, 10:12am UTC](https://swi-prolog.discourse.group/t/using-sum-in-subsumptive-tabling/9499 "2026-01-28T10:12:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![CapelliC](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/capellic/32/17_2.png) [@CapelliC](https://swi-prolog.discourse.group/u/CapelliC)\
**Post date:** [January 28, 2026, 10:12am UTC](https://swi-prolog.discourse.group/t/using-sum-in-subsumptive-tabling/9499/1 "2026-01-28T10:12:01Z")

</div>

I’ve solved day 11, part 2 (advent of code 2025), with [subsumptive tabling](https://github.com/CapelliC/advent_of_code/blob/9463f3e4b3bfce98013afceb46a5029fb07e0e64/swipl/2025/day11/day11_p2_tabled.pl), but cannot find the right syntax/semantic to use the [table sum specification](https://github.com/CapelliC/advent_of_code/blob/9463f3e4b3bfce98013afceb46a5029fb07e0e64/swipl/2025/day11/day11_p2_subsumptive.pl).

That is this works

```prolog
:- 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:

```prolog
:- 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** ). ?

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [January 28, 2026, 10:53am UTC](https://swi-prolog.discourse.group/t/using-sum-in-subsumptive-tabling/9499/2 "2026-01-28T10:53:12Z")

</div>

> [@CapelliC](#):
>
> How to take advantage of :- table count\_paths(+,+,+, **sum** ). ?

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

```prolog
:- table p(sum).

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

```

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

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