# z3prover/z3 architecture

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

- Interactive diagram: https://gitdiagram.com/z3prover/z3
- Repository: https://github.com/z3prover/z3

## Overview

Based on your review of the file tree and README, the project that we are diagramming is the Z3 theorem prover. Z3 is a complex, high-performance SMT solver developed by Microsoft Research. It is written mainly in C++ and supports multiple language bindings (C, C++, .NET, Java, Python, OCaml, Julia, JavaScript/TypeScript, etc.) and builds using a mix of CMake, custom Python build scripts (mk_make.py), and even traditional Makefiles on some platforms.

##### Main Components
- Core Solver and Theorem Proving Engine:
– This includes many interrelated modules, such as the AST (abstract syntax tree) management, rewriting and simplification (found in src/ast and src/ast/rewriter), and proof handling.
– The SAT and SMT solvers are implemented in directories such as src/sat and src/smt.
– Optimization and tactic modules (src/opt, src/tactic) that control the decision procedures and strategies.
- Math and Theory Modules:
– Various specialized modules for rational arithmetic, bit-vectors (theory_bv), arrays (theory_array), and differential/inequality logic.
– Other mathematical components (found in src/math, src/model, and related submodules).
- APIs and Bindings:
– The “src/api” directory shows that Z3 provides higher-level interfaces for C++, .NET, Java, Python, Julia, and JavaScript.
– Each language binding is a layer on top of the core solver functionality.
- Build and Integration Components:
– The build system (mk_make.py in scripts, various CMakeLists.txt scattered across the tree, and cmake modules) is central for managing configuration, compiler flags, build targets, and installation.
– There are also directories for continuous integration configurations (.github, azure-pipelines.yml) and docker configurations.
- External Interfaces:
– The default input format is SMTLIB2, but there is also support for native APIs and a command-line interface in the “src/shell” directory.
– Other “frontends” (for SMTLIB, DIMACS, DRAT, etc.) are provided.
- Documentation and Examples:
– The README and doc directories, along with examples for all supported languages (e.g., examples/c++, examples/python, etc.), indicate how the tool is used.

##### Relationships and Interactions
- The core modules (AST, solvers, tactics) interact closely. Data flows from the input (usually in SMTLIB2 format) into the AST layer where formulas are constructed.
- The rewriting and simplification modules and tactic managers operate on these ASTs to prepare and transform logical formulas, which are then passed to the SAT and SMT solvers.
- The model construction and evaluation parts (located in src/model and util) then use the results to produce outputs or proofs.
- Language bindings serve as a façade, translating user calls in languages like Python or Java into corresponding core API calls implemented in C/C++.
- The command line, shell, and various frontends (src/shell, src/cmd_context) provide user interfaces to interact with the solver.
- The build system orchestrates the integration and configuration across modules and language bindings.

##### Architectural Patterns and Design Principles
- Modular design: Z3 is split into many interdependent yet compartmentalized modules (e.g., AST, rewriting, SAT, SMT, tactics) which are built as separate libraries/components.
- Layered architecture: There is a clear separation between the core solver engine and the API layers.
- Extensibility: The design supports plugging in different theories, solvers, and even language-specific configuration via build flags.
- Multi-language interoperability: The presence of a unified core (written in C/C++) and multiple thin API layers highlights a pattern of a native backend with various front-end interfaces.

##### Relevant Technologies, Frameworks, and Tools
- C++ (C++20), used widely in the core engine.
- CMake and custom Python build scripts (mk_make.py) to manage the enormous codebase.
- Usage of extensive testing (src/test) and CI configuration (GitHub Actions, Azure Pipelines).
- A wide array of language-specific dependencies (e.g., .NET runtime, JVM, Python packages, etc.) as described in the README.

##### Specific Elements
- Clear labels for each major component:
– Core Engine: AST, Rewriter, SAT/Solver, Theories, Tactics, Model Building.
– Interface Layer: API bindings for C++, .NET, Java, Python, etc.
– Build and Configuration: CMake, Python scripts, CI pipelines.
– Examples and Documentation layers.
– For illustrating the transformation of logical formulas from parsing to solving and model generation.
– For showing interactions between language bindings and the core engine.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Input
    Inp["Input (SMTLIB2)"]
    
    %% Core Engine Subgraph
    subgraph "Core Engine"
        AST_REWR["AST & Rewriting"]:::core
        SAT["SAT Solver"]:::core
        SMT["SMT Solver & Theories"]:::core
        TACTIC["Tactic Modules"]:::core
        MODEL["Model Construction"]:::core
    end

    %% Math & Theory Modules Subgraph
    subgraph "Math & Theory Modules"
        MATH["Math Modules"]:::core
    end

    %% API Bindings Subgraph
    subgraph "API Bindings"
        API["Unified Core API"]:::api
        API_CPP["C++ API"]:::api
        API_DOTNET[".NET API"]:::api
        API_JAVA["Java API"]:::api
        API_PY["Python API"]:::api
        API_JS["JavaScript API"]:::api
        API_JULIA["Julia API"]:::api
        API_ML["ML API"]:::api
    end

    %% Build & Integration Subgraph
    subgraph "Build & Integration"
        BUILD_PY["Python Build Script"]:::build
        BUILD_CMAKE["CMake Config"]:::build
        BUILD_CMAKE_MODULES["CMake Modules"]:::build
        CI_DOCKER["CI & Docker Configs"]:::build
    end

    %% External Interfaces Subgraph
    subgraph "External Interfaces"
        SHELL["Shell Frontend"]:::external
    end

    %% Documentation & Examples Subgraph
    subgraph "Documentation & Examples"
        DOC["Documentation"]:::doc
        EXAMPLES["Examples"]:::doc
    end

    %% Connections: Data Flow in Core Engine
    Inp -->|"parses"| AST_REWR
    AST_REWR -->|"generates"| SAT
    AST_REWR -->|"generates"| SMT
    AST_REWR -->|"feeds"| TACTIC
    TACTIC -->|"controls"| MODEL
    SAT -->|"contributes to"| MODEL
    SMT -->|"contributes to"| MODEL
    MATH -->|"supports"| SMT

    %% API Bindings calling Core Engine
    API -->|"calls"| AST_REWR
    API_CPP -->|"calls"| AST_REWR
    API_DOTNET -->|"calls"| AST_REWR
    API_JAVA -->|"calls"| AST_REWR
    API_PY -->|"calls"| AST_REWR
    API_JS -->|"calls"| AST_REWR
    API_JULIA -->|"calls"| AST_REWR
    API_ML -->|"calls"| AST_REWR

    %% Build integration connections
    BUILD_PY -->|"builds"| AST_REWR
    BUILD_CMAKE -->|"configures"| AST_REWR
    BUILD_CMAKE_MODULES -->|"includes"| AST_REWR
    CI_DOCKER -->|"integrates"| AST_REWR
    BUILD_PY -->|"builds"| API
    BUILD_CMAKE -->|"configures"| API

    %% External Interfaces connections
    SHELL -->|"provides CLI to"| AST_REWR

    %% Documentation & Examples pointers
    DOC -->|"describes"| API
    EXAMPLES -->|"demonstrates usage of"| API

    %% Class definitions for colouring
    classDef core fill:#AED6F1,stroke:#1B4F72,stroke-width:2px;
    classDef api fill:#D4EFDF,stroke:#0E6251,stroke-width:2px;
    classDef build fill:#F9E79F,stroke:#B7950B,stroke-width:2px;
    classDef external fill:#E8DAEF,stroke:#6C3483,stroke-width:2px;
    classDef doc fill:#F5B7B1,stroke:#922B21,stroke-width:2px;

    %% Click Events for Core Engine Components
    click AST_REWR "https://github.com/z3prover/z3/tree/master/src/ast"
    click AST_REWR "https://github.com/z3prover/z3/tree/master/src/ast/rewriter"
    click SAT "https://github.com/z3prover/z3/tree/master/src/sat"
    click SMT "https://github.com/z3prover/z3/tree/master/src/smt"
    click TACTIC "https://github.com/z3prover/z3/tree/master/src/tactic"
    click MODEL "https://github.com/z3prover/z3/tree/master/src/model"
    click MATH "https://github.com/z3prover/z3/tree/master/src/math"

    %% Click Events for API Bindings
    click API "https://github.com/z3prover/z3/tree/master/src/api"
    click API_CPP "https://github.com/z3prover/z3/tree/master/src/api/c++"
    click API_DOTNET "https://github.com/z3prover/z3/tree/master/src/api/dotnet"
    click API_JAVA "https://github.com/z3prover/z3/tree/master/src/api/java"
    click API_PY "https://github.com/z3prover/z3/tree/master/src/api/python"
    click API_JS "https://github.com/z3prover/z3/tree/master/src/api/js"
    click API_JULIA "https://github.com/z3prover/z3/tree/master/src/api/julia"
    click API_ML "https://github.com/z3prover/z3/tree/master/src/api/ml"

    %% Click Events for Build and Integration
    click BUILD_PY "https://github.com/z3prover/z3/blob/master/scripts/mk_make.py"
    click BUILD_CMAKE "https://github.com/z3prover/z3/blob/master/CMakeLists.txt"
    click BUILD_CMAKE_MODULES "https://github.com/z3prover/z3/tree/master/cmake"
    click CI_DOCKER "https://github.com/z3prover/z3/blob/master/.github"

    %% Click Event for External Interfaces
    click SHELL "https://github.com/z3prover/z3/tree/master/src/shell"

    %% Click Events for Documentation & Examples
    click DOC "https://github.com/z3prover/z3/tree/master/doc"
    click EXAMPLES "https://github.com/z3prover/z3/tree/master/examples"
```
