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:
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:
- Structural validation: Syntax, variable safety
- Semantic validation: Preventing circular rules while allowing undefined predicates
- 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 factsrule grandparent X Z :- parent X Y, parent Y Z- Add rulesquery grandparent X alice- Run queriesdream 3- Run sleep cyclesshow- Display knowledge base
The Philosophy
DreamLog embodies the principle that intelligence emerges from the interplay of exploration and exploitation:
- Consolidation: Strengthen important patterns
- Abstraction: Find general principles
- Compression: Achieve more with less
- 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
- Repository: github.com/queelius/dreamlog
- Technical Report: DreamLog paper
- Project page: /projects/dreamlog/
Discussion