Skip to main content

DreamLog: Logic Programming That Dreams to Improve Itself

DreamLog is a logic programming system that learns continuously by alternating between wake and sleep phases—like biological brains. During wake, it uses LLMs to generate missing knowledge; during sleep, it compresses what it knows into more general principles.

The Core Insight: Compression IS Learning

DreamLog operationalizes an insight from algorithmic information theory: the system that explains your data with the shortest program is the one most likely to generalize. This is Solomonoff induction—the mathematical formalization of Occam’s razor.

For logic programming, the sleep phase searches for minimal representations that preserve deductive closure:

minimize KB subject to Closure(KB)=Closure(KB) \text{minimize } |KB'| \text{ subject to } \text{Closure}(KB') = \text{Closure}(KB)

Find the shortest knowledge base that still derives all the same facts.

The Wake-Sleep Architecture

Wake Phase: Exploitation Through Generation

During wake, DreamLog operates as a logic programming engine enhanced with LLM-based knowledge generation:

from dreamlog.pythonic import dreamlog

# Create knowledge base with LLM integration
kb = dreamlog(llm_provider="openai")

# Add some facts
kb.fact("parent", "john", "mary")
kb.fact("parent", "mary", "alice")

# Add a rule
kb.rule("grandparent", ["X", "Z"]) \
  .when("parent", ["X", "Y"]) \
  .and_("parent", ["Y", "Z"])

# Query
for result in kb.query("grandparent", "X", "alice"):
    print(f"{result.bindings['X']} is Alice's grandparent")  # john

The magic happens with undefined predicates:

# Query a predicate we never defined!
for result in kb.query("sibling", "X", "Y"):
    # LLM generates knowledge about siblings on-the-fly
    print(result)

When the evaluator encounters an undefined predicate, it triggers the LLM hook to generate both facts and rules. The system infers primitive properties (like gender from names) and derives rules compositionally.

Sleep Phase: Exploration Through Compression

During sleep, DreamLog reorganizes knowledge through compression operators:

from dreamlog.kb_dreamer import KnowledgeBaseDreamer

# Initialize dreamer
dreamer = KnowledgeBaseDreamer(kb.provider)

# Execute sleep cycle
session = dreamer.dream(
    kb,
    dream_cycles=3,            # Multiple REM cycles
    exploration_samples=10,     # Try different optimizations
    verify=True                # Ensure behavior preservation
)

print(f"Compression: {session.compression_ratio:.1%}")
print(f"Generalization: {session.generalization_score:.2f}")

The compression operators include:

  • Anti-unification: Find general patterns from specific instances
  • Predicate invention: Discover intermediate concepts that simplify rules
  • Subsumption elimination: Remove specific rules subsumed by general ones

KB-Aware RAG with Success-Based Learning

A key innovation is knowledge-base-aware retrieval-augmented generation. The system uses weighted embeddings combining query similarity (70%) with knowledge base context (30%), enabling example selection that considers both the query structure and current reasoning state.

A success-based learning mechanism tracks which examples lead to successful inference, progressively improving retrieval quality through experience.

Multi-Layered Validation

Generated rules and facts undergo:

  1. Structural validation: Syntax, variable safety
  2. Semantic validation: Preventing circular rules while allowing undefined predicates
  3. LLM-as-judge verification: Fact verification checks consistency with common-sense knowledge

Error-tolerant parsing handles common LLM formatting errors with correction-based retry for iterative refinement.

Interactive TUI

DreamLog includes an interactive terminal interface:

python -m dreamlog.tui

Commands include:

  • fact parent john mary - Add facts
  • rule grandparent X Z :- parent X Y, parent Y Z - Add rules
  • query grandparent X alice - Run queries
  • dream 3 - Run sleep cycles
  • show - Display knowledge base

The Philosophy

DreamLog embodies the principle that intelligence emerges from the interplay of exploration and exploitation:

  1. Consolidation: Strengthen important patterns
  2. Abstraction: Find general principles
  3. Compression: Achieve more with less
  4. Creativity: Explore novel reorganizations

This isn’t logic programming with LLMs bolted on—it’s a paradigm where knowledge representation evolves through use.

Quick Start

pip install dreamlog
from dreamlog.pythonic import dreamlog

kb = dreamlog(llm_provider="ollama")

kb.parse("""
(parent john mary)
(parent mary alice)
(grandparent X Z) :- (parent X Y), (parent Y Z)
""")

for result in kb.query("grandparent", "john", "X"):
    print(result.bindings['X'])  # alice

Resources

Discussion