Skip to content
Back to skills

Prolog Tabling

ASecurity

Guidelines for tabling, memoization, and SLG resolution in Prolog. Use when handling recursive graph queries, transitive closures, Datalog queries, reachability graphs, dynamic programming, and preventing infinite recursion loops.

  • 4 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added October 1, 2026
ai-agentsgodatabase

Works with

  • cli

Security analysis

A100/100

Scanned October 1, 2026

npx -y skills add dougransom/prolog-agent-toolkit --skill prolog-tabling --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Prolog Tabling?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Prolog Tabling
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/dougransom-prolog-tabling/badge)](https://www.skillsdirectory.com/skills/dougransom-prolog-tabling)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: prolog-tabling
description: Guidelines for tabling, memoization, and SLG resolution in Prolog. Use when handling recursive graph queries, transitive closures, Datalog queries, reachability graphs, dynamic programming, and preventing infinite recursion loops.
---

# Prolog Tabling & SLG Resolution Guidelines

Use this skill when implementing recursive graph algorithms, transitive closures, Datalog queries, or dynamic programming in Prolog engines that support tabling (SLG resolution).

## 1. When to Use Tabling

Tabling (memoization of subgoals and answers) guarantees termination for left-recursive and cyclic graph queries:

- **Graph Reachability & Transitive Closures** (avoiding infinite loops in cyclic graphs)
- **Grammar Parsing with Left Recursion**
- **Datalog-style Deductive Database Queries**
- **Dynamic Programming** (e.g. shortest path, Fibonacci, editing distances)

---

## 2. Declaration Syntax

Declare tabling directives before predicate definitions:

```prolog
% SWI-Prolog / Scryer / Trealla (where supported)
:- table reach/2.

% Cyclic graph reachability
reach(X, Y) :- edge(X, Y).
reach(X, Y) :- reach(X, Z), edge(Z, Y).
```

---

## 3. Answer Subsumption & Mode Directed Tabling

When computing min/max/sum costs over graphs, use mode-directed tabling or answer subsumption:

```prolog
% SWI-Prolog mode-directed tabling for shortest path:
:- table shortest_path(?, ?, min).

shortest_path(X, Y, Dist) :- edge(X, Y, Dist).
shortest_path(X, Y, Dist) :-
    shortest_path(X, Z, D1),
    edge(Z, Y, D2),
    Dist is D1 + D2.
```

---

## 4. Portability & Fallbacks

For Prolog engines without tabling support (or when writing portable ISO code), fall back to an explicit **visited list accumulator**:

```prolog
% Portable fallback for reachability in cyclic graphs without tabling
reach_portable(X, Y) :-
    reach_path(X, Y, [X]).

reach_path(X, Y, _Visited) :- edge(X, Y).
reach_path(X, Y, Visited) :-
    edge(X, Z),
    \+ member(Z, Visited),
    reach_path(Z, Y, [Z|Visited]).
```

---

## 5. Table Management & Invalidation

When facts change dynamically during program execution, clear existing tabled answers:

```prolog
% SWI-Prolog table cleanup
:- retract(edge(a, b)), abolishing_all_tables.
```

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.