Skip to main content

Python API Reference

Comprehensive documentation for the Z3 Python API.

Core Classes

Context

Manages Z3 objects and configuration options.
Description: A Context manages all Z3 objects and global configuration. Z3Py uses a default global context for most applications. Multiple contexts can be created, but objects from different contexts cannot be mixed. Parameters:
  • *args: Configuration options as key-value pairs
  • **kws: Configuration options as keyword arguments
Example:
Methods:

ref()

Returns reference to the C pointer to the Z3 context.

interrupt()

Interrupt a solver performing a satisfiability test. Can be called from a different thread.

param_descrs()

Return the global parameter description set.

Solver

Main interface for SMT solving.
Description: The Solver API provides methods for implementing SMT 2.0 commands: push, pop, check, get-model, etc. Parameters:
  • solver: Optional existing solver
  • ctx: Context (uses global context if None)
  • logFile: Optional file path for logging
Methods:

add(*constraints)

Add constraints to the solver.

check(*assumptions)

Check satisfiability of current constraints. Returns: sat, unsat, or unknown

model()

Get a model for the last satisfiable check.

push()

Save the current solver state.

pop(num=1)

Restore a previously saved state. Parameters:
  • num: Number of scopes to pop (default: 1)

reset()

Remove all assertions from the solver.

assert_and_track(constraint, label)

Add a constraint with a tracking label (for unsat cores).

unsat_core()

Get the unsatisfiable core after an unsat result.

set(param, value)

Set a solver parameter.

statistics()

Get solver statistics from the last check.

Variable Creation Functions

Integer and Real Variables

Int(name, ctx=None)

Create an integer constant.

Ints(names, ctx=None)

Create multiple integer constants.

Real(name, ctx=None)

Create a real constant.

Reals(names, ctx=None)

Create multiple real constants.

IntVal(val, ctx=None)

Create an integer value.

RealVal(val, ctx=None)

Create a real value.

Boolean Variables

Bool(name, ctx=None)

Create a Boolean constant.

Bools(names, ctx=None)

Create multiple Boolean constants.

BoolVal(val, ctx=None)

Create a Boolean value.

Bitvector Variables

BitVec(name, size, ctx=None)

Create a bitvector constant. Parameters:
  • name: Variable name
  • size: Bit width (e.g., 8, 16, 32, 64)

BitVecs(names, size, ctx=None)

Create multiple bitvector constants.

BitVecVal(val, size, ctx=None)

Create a bitvector value.

String Variables

String(name, ctx=None)

Create a string constant.

StringVal(val, ctx=None)

Create a string value.

Expression Classes

ArithRef

Base class for arithmetic expressions (Int and Real). Supported Operations:

BoolRef

Base class for Boolean expressions. Supported Operations:

BitVecRef

Base class for bitvector expressions. Supported Operations:

Logical Operators

And(*args)

Logical conjunction.

Or(*args)

Logical disjunction.

Not(expr)

Logical negation.

Implies(p, q)

Logical implication (p → q).

Xor(p, q)

Exclusive OR.

Quantifiers

ForAll(vars, body)

Universal quantification. Parameters:
  • vars: List of bound variables
  • body: Formula body

Exists(vars, body)

Existential quantification.

Array Operations

Array(name, domain, range)

Create an array.

Select(array, index)

Array read operation (can also use array[index]).

Store(array, index, value)

Array write operation.

String Operations

Concat(*strings)

String concatenation.

Length(string)

String length.

SubString(string, offset, length)

Extract substring.

PrefixOf(prefix, string)

Check if string starts with prefix.

SuffixOf(suffix, string)

Check if string ends with suffix.

Contains(string, substring)

Check if string contains substring.

Utility Functions

simplify(expr, *args, **kwargs)

Simplify an expression.

substitute(expr, *substitutions)

Substitute variables in an expression.

solve(*constraints, show=False)

Quick solver for simple constraints.

prove(claim, show=False)

Attempt to prove a claim.

Sort Functions

IntSort(ctx=None)

Return the integer sort.

RealSort(ctx=None)

Return the real sort.

BoolSort(ctx=None)

Return the Boolean sort.

BitVecSort(size, ctx=None)

Return a bitvector sort.

StringSort(ctx=None)

Return the string sort.

ArraySort(domain, range)

Return an array sort.

DeclareSort(name, ctx=None)

Declare an uninterpreted sort.

Function Declarations

Function(name, *sig)

Declare an uninterpreted function. Parameters:
  • name: Function name
  • *sig: Argument sorts followed by return sort

RecFunction(name, *sig)

Declare a recursive function.

Model Class

Model

Represents a satisfying assignment. Methods:

__getitem__(var)

Get value of a variable.

eval(expr, model_completion=False)

Evaluate an expression in the model.

decls()

Get all declarations in the model.

Version Information

get_version()

Get Z3 version as a tuple.

get_version_string()

Get Z3 version as a string.

get_full_version()

Get full Z3 version string with build info.

Configuration

set_param(*args, **kwargs)

Set global Z3 parameters.

Common Parameters

  • timeout: Timeout in milliseconds
  • random_seed: Random seed for reproducibility
  • verbose: Verbosity level (0-10)
  • proof: Enable proof generation
  • unsat_core: Enable unsat core generation

Type Checking Functions


Additional Resources