Spaceorc.Z3Wrap 0.0.5-beta.11

This is a prerelease version of Spaceorc.Z3Wrap.
dotnet add package Spaceorc.Z3Wrap --version 0.0.5-beta.11
                    
NuGet\Install-Package Spaceorc.Z3Wrap -Version 0.0.5-beta.11
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Spaceorc.Z3Wrap" Version="0.0.5-beta.11" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Spaceorc.Z3Wrap" Version="0.0.5-beta.11" />
                    
Directory.Packages.props
<PackageReference Include="Spaceorc.Z3Wrap" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Spaceorc.Z3Wrap --version 0.0.5-beta.11
                    
#r "nuget: Spaceorc.Z3Wrap, 0.0.5-beta.11"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Spaceorc.Z3Wrap@0.0.5-beta.11
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Spaceorc.Z3Wrap&version=0.0.5-beta.11&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Spaceorc.Z3Wrap&version=0.0.5-beta.11&prerelease
                    
Install as a Cake Tool

Z3Wrap

Write x + y == 10 instead of verbose Z3 API calls. Natural C# syntax for Microsoft's Z3 theorem prover with unlimited precision arithmetic.

NuGet Tests Coverage

using var context = new Z3Context();
using var scope = context.SetUp();

var x = context.IntConst("x");
var y = context.IntConst("y");

using var solver = context.CreateSolver();
solver.Assert(x + y == 10);        // Natural syntax
solver.Assert(x * 2 == y - 1);     // Mathematical operators

if (solver.Check() == Z3Status.Satisfiable)
{
    var model = solver.GetModel();
    Console.WriteLine($"x = {model.GetIntValue(x)}");  // BigInteger
    Console.WriteLine($"y = {model.GetIntValue(y)}");
}

Why Z3Wrap?

Natural Syntax vs Z3 API

// Z3Wrap - readable
solver.Assert(x + y == 10);
solver.Assert(p.Implies(q & r));

// Raw Z3 - verbose
solver.Assert(ctx.MkEq(ctx.MkAdd(x, y), ctx.MkInt(10)));
solver.Assert(ctx.MkImplies(p, ctx.MkAnd(q, r)));

Unlimited Precision

using System.Numerics;

// BigInteger - no integer overflow
var huge = BigInteger.Parse("999999999999999999999999999999");
solver.Assert(x == huge);

// Exact rationals - no floating point errors
var r = context.RealConst("r");
solver.Assert(r == new Real(1, 3));  // Exactly 1/3
solver.Assert(r * 3 == 1);           // Perfect arithmetic

Type Safety

// Compile-time type checking
var prices = context.ArrayConst<IntExpr, RealExpr>("prices");
solver.Assert(prices[0] == 10.5m);   // Index: Int, Value: Real
solver.Assert(prices[1] > prices[0]); // Type-safe comparisons

// Seamless conversions
solver.Assert(x.ToReal() + r == 5.5m);  // Int → Real

Custom .NET Data Types

// Real class - exact rational arithmetic (not decimal/double)
var oneThird = new Real(1, 3);
var twoThirds = new Real(2, 3);
Console.WriteLine(oneThird + twoThirds);  // "1" (exact)

// Bv class - compile-time sized bitvectors with type safety
var bv8 = new Bv<Size8>(0b10101010);
var bv16 = bv8.Resize<Size16>(signed: false);  // Resize to 16 bits
Console.WriteLine(bv8.ToULong());               // 170
Console.WriteLine(bv8.ToBinaryString());        // "10101010"

// Direct arithmetic and bitwise operations
var result = bv8 + new Bv<Size8>(5);   // Type-safe arithmetic
var masked = bv8 & new Bv<Size8>(0xFF); // Bitwise operations

Installation

dotnet add package Spaceorc.Z3Wrap
brew install z3  # macOS (or apt-get install libz3-4 on Linux)

Z3 library is auto-discovered on Windows/macOS/Linux.

Complete Feature Set

  • Booleans: p & q, p | q, !p, p.Implies(q)
  • Integers: BigInteger arithmetic with +, -, *, /, %
  • Reals: Exact rational arithmetic with Real(1,3) fractions
  • BitVectors: Type-safe compile-time sizes with Bv<Size32>, overflow detection
  • Arrays: Generic ArrayExpr<TIndex, TValue> with natural indexing
  • Quantifiers: First-order logic with ForAll and Exists
  • Functions: Uninterpreted functions with type-safe signatures
  • Optimization: Maximize/minimize objectives with soft constraints
  • Solver: Push/pop scopes for constraint backtracking

Advanced Examples

BitVector Operations

var bv = context.BvConst<Size32>("bv");

solver.Assert((bv & 0xFF) == 0x42);      // Bitwise operations
solver.Assert(bv << 2 == bv * 4);        // Shift equivalence
solver.Assert((bv ^ 0xFFFFFFFF) == ~bv); // XOR/NOT relationship

// Overflow detection
solver.Assert(context.AddNoOverflow(bv, 100U));

Quantifiers (First-Order Logic)

var x = context.IntConst("x");
var y = context.IntConst("y");

// Universal quantification: ∀x. x + 0 = x
var identity = context.ForAll(x, x + 0 == x);
solver.Assert(identity);

// Existential quantification: ∃y. x + y = 10
var existsSolution = context.Exists(y, x + y == 10);
solver.Assert(existsSolution);

// Multiple variables: ∀x,y. x * y = y * x
var commutativity = context.ForAll(x, y, x * y == y * x);
solver.Assert(commutativity);

Uninterpreted Functions

// Define function signature: Int → Int
var func = context.Func<IntExpr, IntExpr>("f");

// Apply function to arguments
solver.Assert(func.Apply(5) == 10);
solver.Assert(func.Apply(x) > 0);

// Consistency: same inputs produce same outputs
solver.Assert(func.Apply(5) == func.Apply(5));

Optimization

using var optimizer = context.CreateOptimizer();

var x = context.IntConst("x");
var y = context.IntConst("y");

// Constraints
optimizer.Assert(x + y <= 100);
optimizer.Assert(x >= 0);
optimizer.Assert(y >= 0);

// Objective: maximize 3x + 2y (returns typed handle)
var objective = optimizer.Maximize(3 * x + 2 * y);

if (optimizer.Check() == Z3Status.Satisfiable) {
    var model = optimizer.GetModel();
    var optimalValue = optimizer.GetUpper(objective);  // Type-safe!
    Console.WriteLine($"Optimal: x={model.GetIntValue(x)}, y={model.GetIntValue(y)}");
    Console.WriteLine($"Max value: {model.GetIntValue(optimalValue)}");
}

Solver Backtracking

solver.Push();
solver.Assert(x < 10);
if (solver.Check() == Z3Status.Unsatisfiable) {
    solver.Pop();  // Backtrack
    solver.Assert(x >= 10);
}

Architecture

Reference-counted contexts with automatic memory management. No manual disposal needed for expressions.

Z3Expr (abstract)
├── BoolExpr       - Boolean logic
├── IntExpr        - BigInteger arithmetic
├── RealExpr       - Exact rational arithmetic
├── BvExpr<TSize>  - Compile-time sized bitvectors
└── ArrayExpr<TIndex, TValue> - Generic type-safe arrays

Requirements: .NET 9.0+, Z3 library (auto-discovered)

License: MIT

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net9.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.0.5-beta.11 118 10/7/2025
0.0.5-beta.10 123 10/1/2025
0.0.5-beta.8 155 9/20/2025
0.0.4 313 9/17/2025
0.0.1 309 9/17/2025

Added
- Complete code generation infrastructure for Z3 C API wrapper
 - Automated generation of all Z3Library methods from Z3 C API using Doxygen XML
 - 34 category-organized generated files (Arrays, BitVectors, Solvers, etc.)
 - Callback delegates with C# naming conventions and nullable reference type annotations
 - XML documentation auto-generated from Z3 API comments with zero warnings enforced
 - Proper handling of out parameters and reference counting methods
- Z3Library safe wrapper with cross-platform support
 - Public API for loading and managing Z3 native library
 - Load(path) and LoadAuto() methods for manual and automatic library discovery
 - Cross-platform native library discovery (Windows, macOS, Linux)
 - Safe error checking and handle validation for all operations
- Type-safe value types for compile-time guarantees
 - BvExpr&lt;TSize&gt; and Bv&lt;TSize&gt; with compile-time sized types (Size8, Size16, Size32, Size64)
 - Real struct for unlimited precision rational arithmetic (exact fractions)
 - Full support for arithmetic, bitwise, and comparison operations
- Complete expression type hierarchy
 - Type-safe expression classes: BoolExpr, IntExpr, RealExpr, BvExpr&lt;TSize&gt;, ArrayExpr&lt;TIndex, TValue&gt;
 - Category-based organization: Arrays/, BitVectors/, Functions/, Logic/, Numerics/, Quantifiers/
 - Natural mathematical syntax via operator overloading and extension methods
- Advanced solver features
 - FuncDecl for uninterpreted functions with dynamic builder pattern
 - Z3Optimize class for minimize/maximize objectives with type-safe OptimizeObjective&lt;TExpr&gt;
 - Hard constraints via Assert() and soft constraints via AssertSoft() with weight support
 - Optimal value bounds retrieval via GetUpper() and GetLower()
 - Solver parameter extensions: SetParam for bool/uint/double/string and SetTimeout(TimeSpan)
 - Push/Pop backtracking support for optimization

Changed
- BREAKING: Renamed expression types: Z3Bool → BoolExpr, Z3IntExpr → IntExpr, Z3Real → RealExpr
- BREAKING: Changed from runtime-sized to compile-time sized bitvectors (BvExpr&lt;Size32&gt; vs old BitVecConst(&quot;x&quot;, 32))
- BREAKING: Renamed BitVector identifiers: BitVecConst → BvConst, BitVec → Bv, ToBitVec → ToBv, Z3SortKind.BV → Z3SortKind.Bv
- BREAKING: Reorganized namespaces from flat structure to category-based organization
- BREAKING: Made Handle properties and factory methods internal

Removed
- BREAKING: Removed runtime-sized BitVector API (replaced with compile-time sized generics)
- BREAKING: Removed BitVector boundary check builder API (replaced with overflow detection in expressions)