How souffle-lang/souffle works
Purpose and entry points
Soufflé is a C++ implementation of a Datalog-like language for program analysis. It accepts .dl programs, validates and optimizes them, then either interprets an internal relational algebra program or generates parallel C++ for compilation. Primary executables are the Soufflé CLI, compiler helper, and profiler:
• src/souffle.cpp
• src/MainDriver.cpp
• src/souffle-compile.template.py
• src/souffle_prof.cpp
Front end: source to AST
The parser uses Flex/Bison grammar and scanner definitions to construct an AST, preserving source locations and supporting virtualized/include-aware files. The AST models programs, relations, clauses, types, records/ADTs, aggregates, directives, components, and execution plans.
• src/parser/parser.yy
• src/parser/scanner.ll
• src/parser/ParserDriver.cpp
• src/ast/Program.h
• src/ast/TranslationUnit.h
Semantic analysis and AST transformation
The AST translation unit is analyzed for types, grounding, dependencies, SCCs, scheduling, I/O usage, and relation properties. A configurable transformation pipeline then performs validation and lowering/optimization: component instantiation, semantic/type checking, magic-set rewriting, inlining, aggregate materialization, relation minimization, and normalization. This boundary converts user-level Datalog semantics into a canonical program suitable for execution planning.
• src/ast/analysis/typesystem/TypeSystem.cpp
• src/ast/analysis/SCCGraph.cpp
• src/ast/transform/Pipeline.h
• src/ast/transform/MagicSet.cpp
• src/ast/transform/SemanticChecker.cpp
AST-to-RAM lowering
AST rules are translated into RAM, Soufflé’s relational-algebra intermediate representation. Translation strategies select standard semi-naïve evaluation or provenance-aware lowering; clause/value/constraint translators build RAM operations and statements. This is the central compiler boundary between language semantics and physical execution.
• src/ast2ram/TranslationStrategy.h
• src/ast2ram/seminaive/TranslationStrategy.cpp
• src/ast2ram/provenance/TranslationStrategy.cpp
• src/ram/Program.h
• src/ram/TranslationUnit.h
RAM optimization and execution planning
RAM represents scans, indexed scans, joins, aggregates, inserts, loops, parallel operators, I/O, and provenance operations. RAM analyses derive relation/index requirements and complexity information; transforms introduce indexes, reorder conditions, collapse filters, hoist aggregates, and mark parallel work. The resulting plan can be interpreted or synthesized.
• src/ram/analysis/Index.cpp
• src/ram/transform/MakeIndex.cpp
• src/ram/transform/Parallel.cpp
• src/ram/transform/ReorderConditions.cpp
• src/ram/IndexScan.h
Execution backends and runtime
The interpreter executes RAM directly against relation/index implementations. The synthesis backend generates C++ programs using the public runtime, which supplies relation storage (B-tree, Brie, equivalence relations), symbol/record tables, parallel utilities, and callable C++ interfaces. Generated/embedded programs expose SouffleProgram-style interfaces.
• src/interpreter/Engine.cpp
• src/interpreter/Generator.cpp
• src/synthesiser/Synthesiser.cpp
• src/include/souffle/SouffleInterface.h
• src/include/souffle/datastructure/BTree.h
I/O, observability, and integrations
Relation I/O supports CSV, JSON, SQLite, and compressed streams. Provenance has dedicated AST-to-RAM translation and runtime explanation APIs. Profiling records program/relation/rule events and offers CLI/TUI/HTML reporting. SWIG headers enable Java and Python bindings.
• src/include/souffle/io/IOSystem.h
• src/include/souffle/io/ReadStreamSQLite.h
• src/include/souffle/provenance/ExplainProvenance.h
• src/include/souffle/profile/ProfileDatabase.h
• src/include/souffle/swig/SwigInterface.i
Build and delivery
CMake builds the C++ toolchain and runtime; optional external dependencies include LibFFI and SQLite-oriented I/O support. CI builds across Linux distributions and MSVC, while Debian, Chocolatey, and container definitions support packaging. There is no service-side infrastructure or required remote runtime.
• CMakeLists.txt
• src/CMakeLists.txt
• cmake/FindLibFFI.cmake
• .github/workflows/CI-Tests.yml
• debian/control
Main entry points: SouffleInterface.h Soufflé runtime API · TranslationStrategy.h AST-to-RAM strategies · MakeIndex.cpp RAM optimization plan · Engine.cpp RAM interpreter
Components 20
Front end
- Datalog programs · User .dl programs and included sources.
- Soufflé CLI
src/souffle.cpp - Flex/Bison parser · Builds location-aware ASTs with include-aware input.
src/parser/parser.yy - Parser driver
src/parser/ParserDriver.cpp - AST translation unit · Models Datalog programs, rules, types, directives, and components.
src/ast/TranslationUnit.h
Semantic compilation
- Semantic analyses · Derives types, dependencies, SCCs, scheduling, I/O use, and relation properties.
src/ast/analysis/typesystem/TypeSystem.cpp - AST transform pipeline · Validates and canonicalizes source semantics through rewrites and optimizations.
src/ast/transform/Pipeline.h - AST-to-RAM strategies · Selects semi-naïve or provenance-aware relational-algebra lowering.
src/ast2ram/TranslationStrategy.h
RAM planning
- RAM translation unit · Program of scans, joins, aggregates, loops, I/O, and parallel operations.
src/ram/TranslationUnit.h - RAM analyses · Derives index requirements and complexity information.
src/ram/analysis/Index.cpp - RAM optimization plan · Adds indexes, reorders conditions, hoists aggregates, and marks parallel work.
src/ram/transform/MakeIndex.cpp
Execution and runtime
- RAM interpreter · Executes optimized RAM directly.
src/interpreter/Engine.cpp - C++ synthesiser · Emits parallel C++ from optimized RAM.
src/synthesiser/Synthesiser.cpp - Compiler helper · Compiles generated C++ programs.
src/souffle-compile.template.py - Soufflé runtime API · Provides program interfaces, relation storage, tables, and parallel utilities.
src/include/souffle/SouffleInterface.h - Relation I/O · Reads and writes CSV, JSON, SQLite, and compressed streams.
src/include/souffle/io/IOSystem.h - Provenance explanations · Provides provenance lowering and runtime explanation APIs.
src/include/souffle/provenance/ExplainProvenance.h
Tooling and delivery
- Profiler · Records execution events for CLI, TUI, and HTML reporting.
src/souffle_prof.cpp - Java/Python bindings
src/include/souffle/swig/SwigInterface.i - CMake and packaging · Builds the toolchain and supports CI and platform packaging.
CMakeLists.txt
Connections 21
- Datalog programs → Soufflé CLI: invoked with
- Soufflé CLI → Parser driver: parses
- Parser driver → Flex/Bison parser: drives
- Flex/Bison parser → AST translation unit: constructs
- AST translation unit → Semantic analyses: analyzed
- Semantic analyses → AST transform pipeline: informs
- AST transform pipeline → AST-to-RAM strategies: canonical AST
- AST-to-RAM strategies → RAM translation unit: lowers to
- RAM translation unit → RAM analyses: analyzed
- RAM analyses → RAM optimization plan: index requirements
- RAM optimization plan → RAM interpreter: interpret path
- RAM optimization plan → C++ synthesiser: compile path
- C++ synthesiser → Compiler helper: generated C++
- Compiler helper → Soufflé runtime API: links against
- RAM interpreter → Soufflé runtime API: uses relations and tables
- Relation I/O → Soufflé runtime API: relation import/export
- AST-to-RAM strategies → Provenance explanations: provenance mode
- RAM interpreter → Profiler: execution events
- Soufflé runtime API → Java/Python bindings: wrapped by
- CMake and packaging → Soufflé CLI: builds
- CMake and packaging → Soufflé runtime API: builds