# Tabling and memoization across answers

**URL:** <https://swi-prolog.discourse.group/t/tabling-and-memoization-across-answers/6661>\
**Category:** Help!\
**Tags:** how-to\
**Created:** [June 26, 2023, 10:10am UTC](https://swi-prolog.discourse.group/t/tabling-and-memoization-across-answers/6661 "2023-06-26T10:10:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![meditans](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/meditans/32/1768_2.png) [@meditans](https://swi-prolog.discourse.group/u/meditans)\
**Post date:** [June 26, 2023, 10:10am UTC](https://swi-prolog.discourse.group/t/tabling-and-memoization-across-answers/6661/1 "2023-06-26T10:10:58Z")

</div>

I’m having problem understanding how tabling could memoize a predicate: here’s  
an example from 2022 advent of code (problem 8):

```prolog
mat([[3,0,3,7,3],
     [2,5,5,1,2],
     [6,5,3,3,2],
     [3,3,5,4,9],
     [3,5,3,9,0]]).

at(Mat, X-Y, Elem) :- 
    length(Mat, Len), [X, Y] ins 1..Len,
    nth1(Y, Mat, Row), nth1(X, Row, Elem).

upper_shadow(Mat, X-1, 0) :-
    length(Mat, Len),
    X in 1..Len.
upper_shadow(Mat, X-Y, N) :-
    length(Mat, Len),
    [X, Y] ins 1..Len,
    Y #> 1,
    Y1 #= Y - 1,
    upper_shadow(Mat, X-Y1, N1),
    at(Mat, X-Y1, C),
    N #= max(N1, C),
    format('~w-~w~n', [X, Y]).

% ?- mat(Mat), upper_shadow(Mat, X-Y, Shadow), false.

```

From the `format` statement, I can see that the predicate upper\_shadow is called many times with the same arguments. In the effort of getting some memoization, I tried including a number of tabling directive (with and without subsumption), but I couldn’t get the memoization properties I want.

My questions are:

- How can I memoize this efficiently with tabling?
- How should I think about tabling from an operational semantic viewpoint?
- Are there ways of inspecting the tables?

any insight is appreciated 🙂

---

<div class="post-metadata">

**Author:** ![meditans](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/meditans/32/1768_2.png) [@meditans](https://swi-prolog.discourse.group/u/meditans)\
**Post date:** [June 26, 2023, 11:53am UTC](https://swi-prolog.discourse.group/t/tabling-and-memoization-across-answers/6661/2 "2023-06-26T11:53:55Z")

</div>

Solved, I’m not sure what was happening before, probably something in the way the predicate was loaded. As soon that was fixed, I got warnings for my use of attributed variables, removed them and now everything works as intended. 🙂

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [June 26, 2023, 9:31pm UTC](https://swi-prolog.discourse.group/t/tabling-and-memoization-across-answers/6661/3 "2023-06-26T21:31:19Z")

</div>

Although it’s not documented, I _think_ that tabling and attributed variables (which is what CLP(FD) uses) don’t play well together. I’m guessing that you removed the use of predicates such as (#=)/2 , to get things to work?
