mk: phase 7 — naive ground-arg tabling, Fibonacci canary green
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 58s

`table-2` wraps a 2-arg (input, output) relation. On a ground input
walk, looks up the (string-encoded) cache key; on miss, runs the
relation, drains the answer stream, extracts walk*-output values from
each subst, stores them, and replays. On hit, replays the cached
values directly — no recomputation.

Cache lifetime: a single global mk-tab-cache (mutated via set!).
mk-tab-clear! resets between independent queries.

Canonical demo: tabled fib(25) = 75025 in ~5 seconds; the same naive
fib-o times out at 60s. Memoization collapses the exponential redundant
recomputation in the binary recursion.

Limitations (deferred to future SLG work): cyclic recursive calls with
the same ground key still diverge — naive memoization populates the
cache only AFTER computation completes, so a recursive call inside its
own computation can't see the in-progress entry. The brief's "tabled
patho on cyclic graphs" use case requires producer/consumer
scheduling and is left for a future iteration.

12 new tests, fib(0..20) + ground-term predicate + cache-replay
verification. 638/638 cumulative.
This commit is contained in:
2026-05-08 22:27:10 +00:00
parent 8644668fc9
commit adc8467c78
3 changed files with 164 additions and 4 deletions

View File

@@ -180,10 +180,19 @@ Key semantic mappings:
- [ ] Tests: send-more-money, N-queens with CLP(FD), map coloring, cryptarithmetic
### Phase 7 — tabling (memoization of relations)
- [ ] `tabled` annotation: memoize calls to a relation using a hash table
- [ ] Prevents infinite loops in recursive relations like `patho` on cyclic graphs
- [ ] Producer/consumer scheduling for tabled relations (variant of SLG resolution)
- [ ] Tests: cyclic graph reachability, mutual recursion, Fibonacci via tabling
- [x] `table-2` wrapper: ground-arg memoization for 2-arg relations.
Cache keyed by walked input; on miss runs underlying relation,
collects all output values from the answer stream, stores, and
replays. Subsequent calls with the same ground input replay the
cached values (no recomputation).
- [x] Fibonacci canary green: tabled `fib(25) = 75025` in seconds;
naive `fib(25)` times out at 60s. Memoization turns exponential
recursion into linear.
- [ ] Producer/consumer SLG scheduling — required to handle recursive
tabled calls with the SAME ground key (e.g. cyclic `patho` with a
shared key); naive memoization deferred to a future iteration.
- [ ] Tests: cyclic graph reachability via tabled patho (deferred —
requires SLG); mutual recursion (deferred).
## Blockers