Skip to main content

Overview

In Z3, all terms, formulas, and types are represented as Abstract Syntax Trees (ASTs). The expression system is the foundation for building and manipulating logical formulas.

AST Hierarchy

Z3’s AST nodes form a hierarchy:

Core AST Types

Represents types in Z3’s type system:
  • Basic sorts: Bool, Int, Real
  • Bit-vector sorts: (_ BitVec n)
  • Array sorts: (Array Domain Range)
  • Datatypes: user-defined algebraic types
  • Uninterpreted sorts: abstract types
Declares functions and constants:
  • Constants: 0-arity functions
  • Uninterpreted functions: no specified meaning
  • Interpreted functions: built-in operations (+, *, etc.)
Applications of functions to arguments:
  • Constants are 0-arity applications
  • Operations like x + y are applications of + to [x, y]

Creating Sorts

Creating Expressions

Constants and Variables

Numerals

Function Applications

Expression Kinds

From z3_api.h:170, Z3 defines these AST kinds:

Boolean Formulas

Arithmetic Expressions

Bit-Vector Operations

Array Expressions

Expression Traversal

From examples/c++/example.cpp:807:

Expression Simplification

If-Then-Else Terms

Substitution

From examples/c++/example.cpp:1147:

Memory Management

Z3 uses reference counting for memory management. In C/C++:
  • Always increment refs with Z3_inc_ref
  • Decrement with Z3_dec_ref when done
  • The C++ API handles this automatically
  • Python API is fully garbage-collected

SMT Solving

Learn about satisfiability checking

Solvers

Using expressions with solvers

Quantifiers

Quantified expressions

References

  • Z3 API: src/api/z3_api.h:7-179
  • AST implementation: src/api/api_ast.cpp
  • Examples: examples/c++/example.cpp