Labsco
rikarazome logo

prolog-reasoner

β˜… 9

from rikarazome

SWI-Prolog execution for LLMs with CLP(FD) and recursion β€” boosts logic/constraint accuracy from 73% to 90% on a 30-problem benchmark.

πŸ”₯πŸ”₯πŸ”₯πŸ”₯βœ“ VerifiedAccount requiredAdvanced setup

prolog-reasoner

SWI-Prolog as a "logic calculator" for LLMs β€” available as an MCP server and a Python library. Eliminate the black box from LLM logical reasoning.

LLMs excel at natural language but struggle with formal logic. Prolog excels at logical reasoning but can't process natural language. prolog-reasoner bridges this gap by exposing SWI-Prolog execution to LLMs.

Does it help?

On the built-in 30-problem logic benchmark:

PipelineAccuracy
LLM-only (claude-sonnet-4-6)22/30 (73.3%)
LLM + prolog-reasoner27/30 (90.0%)

The gap concentrates in constraint satisfaction and multi-step reasoning β€” the combinatorial territory LLMs are weak on and Prolog is strong on. Full breakdown below.

Why it works

LLMs pattern-match; Prolog actually searches and solves. When the LLM writes its problem down as Prolog, two things happen at once:

  • Prolog handles the combinatorial work LLMs are weak on β€” constraint satisfaction, multi-step inference, exhaustive search.
  • The reasoning exists as code you can read, re-run, and debug. When it goes wrong, you see the exact Prolog that failed and why.

Two ways to use it

  • MCP server β€” Claude (or any MCP client) calls it as a logic solver during conversation. Rule bases let the LLM save stable domain rules once and reference them by name per call.
  • Python library β€” full NLβ†’Prolog pipeline with self-correction. Requires OpenAI or Anthropic.

Features

  • MCP tools: execute_prolog for arbitrary SWI-Prolog execution, plus list_rule_bases / get_rule_base / save_rule_base / delete_rule_base for reusable named rule bases (v14)
  • Rule bases: save stable Prolog rules once (e.g. chess move rules, legal axioms) and reference them by name from execute_prolog so the LLM only writes the situation-specific facts per call
  • Transparent intermediate representation: the Prolog code is the audit trail β€” inspect, modify, or verify before execution
  • CLP(FD) support: constraint logic programming for scheduling and optimization
  • Negation-as-failure, recursion, all standard SWI-Prolog features
  • Library mode: NLβ†’Prolog translation with self-correction loop (OpenAI / Anthropic)

Benchmark

benchmarks/ contains 30 logic problems across 5 categories (deduction, transitive, constraint, contradiction, multi-step) to compare LLM-only reasoning vs LLM+Prolog reasoning. The benchmark exercises the library path (translator + executor), since it requires the NL→Prolog step.

Results

Measured on anthropic/claude-sonnet-4-6, single run over 30 problems:

PipelineAccuracyAvg latency
LLM-only22/30 (73.3%)1.7s
LLM + Prolog27/30 (90.0%)3.8s

Per-category breakdown:

CategoryLLM-onlyLLM + Prolog
deduction6/66/6
transitive6/65/6
constraint3/76/7
contradiction4/43/4
multi-step3/77/7

The gap is concentrated in constraint (SEND+MORE, 6-queens, knapsack, K4 coloring, Einstein-lite) and multi-step (Nim game theory, 3-person knights-and-knaves, TSP-4, zebra puzzle) β€” exactly the combinatorial/search-heavy territory where symbolic solvers outperform pattern completion. On purely deductive or transitive questions the LLM is already strong and Prolog adds latency without accuracy gains.

All 3 LLM+Prolog failures were Prolog execution errors from malformed LLM-generated code (missing predicate definitions, unbound CLP(FD) variables) rather than reasoning errors β€” addressable via prompt tuning. Notably, every failure is inspectable: you can see the exact Prolog that failed and why, rather than a wrong natural-language answer with no explanation.

Running it yourself

docker run --rm -e PROLOG_REASONER_LLM_API_KEY=sk-... \
    prolog-reasoner-dev python benchmarks/run_benchmark.py

Results are saved to benchmarks/results.json.

Comparison with other Prolog MCPs

Several Prolog MCP servers exist, each with different design choices. prolog-reasoner is intentionally stateless and spot-use β€” Prolog is a calculator you call when logic matters, not the backbone of your agent's memory.

prolog-reasonerStateful Prolog MCPs
Prolog's rolePer-call reasoning toolProject-wide knowledge base
StateStateless execution (each call independent); optional named rule bases for reusable static rules, no inter-call session memoryPersistent sessions / layered KBs
ReproducibilitySame input (incl. same rule bases) β†’ same output, alwaysDepends on accumulated state
Integration effortUse where logic matters, skip where it doesn'tArchitectural commitment
A/B testable vs LLM-onlyYes (each call is a controlled experiment)Structurally not comparable

This is also why accuracy benchmarks are published here and not elsewhere: statelessness is what makes a side-by-side comparison possible.

If you need persistent agent memory, hallucination-safeguarded fact storage, or a full neuro-symbolic substrate, other projects may fit better:

We're the spot-use option.

Development

# Build dev image
docker build -f docker/Dockerfile -t prolog-reasoner-dev .

# Run tests (no API key needed β€” LLM calls are mocked)
docker run --rm prolog-reasoner-dev

# With coverage
docker run --rm prolog-reasoner-dev pytest tests/ -v --cov=prolog_reasoner

# Or via docker compose
docker compose -f docker/docker-compose.yml run --rm test