# souffle-lang/souffle architecture

> Architecture diagram of the GitHub repository souffle-lang/souffle, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/souffle-lang/souffle
- Repository: https://github.com/souffle-lang/souffle

## Overview

#### 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: [`src/include/souffle/SouffleInterface.h`](https://github.com/souffle-lang/souffle/blob/master/src/include/souffle/SouffleInterface.h) (Soufflé runtime API), [`src/ast2ram/TranslationStrategy.h`](https://github.com/souffle-lang/souffle/blob/master/src/ast2ram/TranslationStrategy.h) (AST-to-RAM strategies), [`src/ram/transform/MakeIndex.cpp`](https://github.com/souffle-lang/souffle/blob/master/src/ram/transform/MakeIndex.cpp) (RAM optimization plan), [`src/interpreter/Engine.cpp`](https://github.com/souffle-lang/souffle/blob/master/src/interpreter/Engine.cpp) (RAM interpreter)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Front end"]
  node_datalog["Datalog programs<br/>source input"]
  node_cli["Soufflé CLI<br/>compiler driver<br/>[souffle.cpp]"]
  node_parser["Flex/Bison parser<br/>[parser.yy]"]
  node_parser_driver["Parser driver<br/>parse orchestration<br/>[ParserDriver.cpp]"]
  node_ast["AST translation unit<br/>language IR<br/>[TranslationUnit.h]"]
end

subgraph group_semantic["Semantic compilation"]
  node_ast_analysis["Semantic analyses<br/>AST analysis<br/>[TypeSystem.cpp]"]
  node_ast_pipeline["AST transform pipeline<br/>validation and lowering<br/>[Pipeline.h]"]
  node_ast2ram["AST-to-RAM strategies<br/>IR lowering"]
end

subgraph group_ram["RAM planning"]
  node_ram["RAM translation unit<br/>relational algebra IR<br/>[TranslationUnit.h]"]
  node_ram_analysis["RAM analyses<br/>physical analysis<br/>[Index.cpp]"]
  node_ram_pipeline["RAM optimization plan<br/>physical optimization<br/>[MakeIndex.cpp]"]
end

subgraph group_execution["Execution and runtime"]
  node_interpreter["RAM interpreter<br/>execution backend<br/>[Engine.cpp]"]
  node_synthesiser["C++ synthesiser<br/>code generation backend<br/>[Synthesiser.cpp]"]
  node_compiler_helper["Compiler helper<br/>generated-code build"]
  node_runtime["Soufflé runtime API<br/>embedded runtime<br/>[SouffleInterface.h]"]
  node_relation_io["Relation I/O<br/>data integration<br/>[IOSystem.h]"]
  node_provenance["Provenance explanations<br/>explainability"]
end

subgraph group_tools["Tooling and delivery"]
  node_profiler["Profiler<br/>observability tool<br/>[souffle_prof.cpp]"]
  node_bindings["Java/Python bindings<br/>language integration<br/>[SwigInterface.i]"]
  node_build["CMake and packaging<br/>build delivery<br/>[CMakeLists.txt]"]
end

node_datalog -->|"invoked with"| node_cli
node_cli -->|"parses"| node_parser_driver
node_parser_driver -->|"drives"| node_parser
node_parser -->|"constructs"| node_ast
node_ast -->|"analyzed"| node_ast_analysis
node_ast_analysis -->|"informs"| node_ast_pipeline
node_ast_pipeline -->|"canonical AST"| node_ast2ram
node_ast2ram -->|"lowers to"| node_ram
node_ram -->|"analyzed"| node_ram_analysis
node_ram_analysis -->|"index requirements"| node_ram_pipeline
node_ram_pipeline -->|"interpret path"| node_interpreter
node_ram_pipeline -->|"compile path"| node_synthesiser
node_synthesiser -->|"generated C++"| node_compiler_helper
node_compiler_helper -->|"links against"| node_runtime
node_interpreter -->|"uses relations and tables"| node_runtime
node_relation_io -->|"relation import/export"| node_runtime
node_ast2ram -.->|"provenance mode"| node_provenance
node_interpreter -.->|"execution events"| node_profiler
node_runtime -->|"wrapped by"| node_bindings
node_build -->|"builds"| node_cli
node_build -->|"builds"| node_runtime

click node_cli "https://github.com/souffle-lang/souffle/blob/master/src/souffle.cpp"
click node_parser "https://github.com/souffle-lang/souffle/blob/master/src/parser/parser.yy"
click node_parser_driver "https://github.com/souffle-lang/souffle/blob/master/src/parser/ParserDriver.cpp"
click node_ast "https://github.com/souffle-lang/souffle/blob/master/src/ast/TranslationUnit.h"
click node_ast_analysis "https://github.com/souffle-lang/souffle/blob/master/src/ast/analysis/typesystem/TypeSystem.cpp"
click node_ast_pipeline "https://github.com/souffle-lang/souffle/blob/master/src/ast/transform/Pipeline.h"
click node_ast2ram "https://github.com/souffle-lang/souffle/blob/master/src/ast2ram/TranslationStrategy.h"
click node_ram "https://github.com/souffle-lang/souffle/blob/master/src/ram/TranslationUnit.h"
click node_ram_analysis "https://github.com/souffle-lang/souffle/blob/master/src/ram/analysis/Index.cpp"
click node_ram_pipeline "https://github.com/souffle-lang/souffle/blob/master/src/ram/transform/MakeIndex.cpp"
click node_interpreter "https://github.com/souffle-lang/souffle/blob/master/src/interpreter/Engine.cpp"
click node_synthesiser "https://github.com/souffle-lang/souffle/blob/master/src/synthesiser/Synthesiser.cpp"
click node_compiler_helper "https://github.com/souffle-lang/souffle/blob/master/src/souffle-compile.template.py"
click node_runtime "https://github.com/souffle-lang/souffle/blob/master/src/include/souffle/SouffleInterface.h"
click node_relation_io "https://github.com/souffle-lang/souffle/blob/master/src/include/souffle/io/IOSystem.h"
click node_provenance "https://github.com/souffle-lang/souffle/blob/master/src/include/souffle/provenance/ExplainProvenance.h"
click node_profiler "https://github.com/souffle-lang/souffle/blob/master/src/souffle_prof.cpp"
click node_bindings "https://github.com/souffle-lang/souffle/blob/master/src/include/souffle/swig/SwigInterface.i"
click node_build "https://github.com/souffle-lang/souffle/blob/master/CMakeLists.txt"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_datalog,node_cli,node_parser,node_parser_driver,node_ast toneBlue
class node_ast_analysis,node_ast_pipeline,node_ast2ram toneAmber
class node_ram,node_ram_analysis,node_ram_pipeline toneMint
class node_interpreter,node_synthesiser,node_compiler_helper,node_runtime,node_relation_io,node_provenance toneRose
class node_profiler,node_bindings,node_build toneIndigo
```

## Components

### Front end

- **Datalog programs**: source input · User .dl programs and included sources.
- [Soufflé CLI](https://github.com/souffle-lang/souffle/blob/master/src/souffle.cpp): compiler driver · `src/souffle.cpp`
- [Flex/Bison parser](https://github.com/souffle-lang/souffle/blob/master/src/parser/parser.yy): parser · Builds location-aware ASTs with include-aware input. · `src/parser/parser.yy`
- [Parser driver](https://github.com/souffle-lang/souffle/blob/master/src/parser/ParserDriver.cpp): parse orchestration · `src/parser/ParserDriver.cpp`
- [AST translation unit](https://github.com/souffle-lang/souffle/blob/master/src/ast/TranslationUnit.h): language IR · Models Datalog programs, rules, types, directives, and components. · `src/ast/TranslationUnit.h`

### Semantic compilation

- [Semantic analyses](https://github.com/souffle-lang/souffle/blob/master/src/ast/analysis/typesystem/TypeSystem.cpp): AST analysis · Derives types, dependencies, SCCs, scheduling, I/O use, and relation properties. · `src/ast/analysis/typesystem/TypeSystem.cpp`
- [AST transform pipeline](https://github.com/souffle-lang/souffle/blob/master/src/ast/transform/Pipeline.h): validation and lowering · Validates and canonicalizes source semantics through rewrites and optimizations. · `src/ast/transform/Pipeline.h`
- [AST-to-RAM strategies](https://github.com/souffle-lang/souffle/blob/master/src/ast2ram/TranslationStrategy.h): IR lowering · Selects semi-naïve or provenance-aware relational-algebra lowering. · `src/ast2ram/TranslationStrategy.h`

### RAM planning

- [RAM translation unit](https://github.com/souffle-lang/souffle/blob/master/src/ram/TranslationUnit.h): relational algebra IR · Program of scans, joins, aggregates, loops, I/O, and parallel operations. · `src/ram/TranslationUnit.h`
- [RAM analyses](https://github.com/souffle-lang/souffle/blob/master/src/ram/analysis/Index.cpp): physical analysis · Derives index requirements and complexity information. · `src/ram/analysis/Index.cpp`
- [RAM optimization plan](https://github.com/souffle-lang/souffle/blob/master/src/ram/transform/MakeIndex.cpp): physical optimization · Adds indexes, reorders conditions, hoists aggregates, and marks parallel work. · `src/ram/transform/MakeIndex.cpp`

### Execution and runtime

- [RAM interpreter](https://github.com/souffle-lang/souffle/blob/master/src/interpreter/Engine.cpp): execution backend · Executes optimized RAM directly. · `src/interpreter/Engine.cpp`
- [C++ synthesiser](https://github.com/souffle-lang/souffle/blob/master/src/synthesiser/Synthesiser.cpp): code generation backend · Emits parallel C++ from optimized RAM. · `src/synthesiser/Synthesiser.cpp`
- [Compiler helper](https://github.com/souffle-lang/souffle/blob/master/src/souffle-compile.template.py): generated-code build · Compiles generated C++ programs. · `src/souffle-compile.template.py`
- [Soufflé runtime API](https://github.com/souffle-lang/souffle/blob/master/src/include/souffle/SouffleInterface.h): embedded runtime · Provides program interfaces, relation storage, tables, and parallel utilities. · `src/include/souffle/SouffleInterface.h`
- [Relation I/O](https://github.com/souffle-lang/souffle/blob/master/src/include/souffle/io/IOSystem.h): data integration · Reads and writes CSV, JSON, SQLite, and compressed streams. · `src/include/souffle/io/IOSystem.h`
- [Provenance explanations](https://github.com/souffle-lang/souffle/blob/master/src/include/souffle/provenance/ExplainProvenance.h): explainability · Provides provenance lowering and runtime explanation APIs. · `src/include/souffle/provenance/ExplainProvenance.h`

### Tooling and delivery

- [Profiler](https://github.com/souffle-lang/souffle/blob/master/src/souffle_prof.cpp): observability tool · Records execution events for CLI, TUI, and HTML reporting. · `src/souffle_prof.cpp`
- [Java/Python bindings](https://github.com/souffle-lang/souffle/blob/master/src/include/souffle/swig/SwigInterface.i): language integration · `src/include/souffle/swig/SwigInterface.i`
- [CMake and packaging](https://github.com/souffle-lang/souffle/blob/master/CMakeLists.txt): build delivery · Builds the toolchain and supports CI and platform packaging. · `CMakeLists.txt`

## Connections

- 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
