3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2026-02-16 05:41:43 +00:00

Add benchmark export to C# and TypeScript APIs (#8228)

* Initial plan

* Add benchmark export functionality to C# and TypeScript APIs

Co-authored-by: NikolajBjorner <3085284+NikolajBjorner@users.noreply.github.com>

* Fix TypeScript build error: remove redundant array length parameter

The Z3 TypeScript wrapper auto-generates array length parameters from the array itself, so passing assumptions.length explicitly causes a parameter count mismatch. Removed the redundant parameter.

Co-authored-by: NikolajBjorner <3085284+NikolajBjorner@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: NikolajBjorner <3085284+NikolajBjorner@users.noreply.github.com>
This commit is contained in:
Copilot 2026-01-17 13:02:19 -08:00 committed by GitHub
parent 11851c2e4c
commit 1be52d95a5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 106 additions and 0 deletions

View file

@ -3491,6 +3491,32 @@ namespace Microsoft.Z3
AST.ArrayLength(decls), Symbol.ArrayToNative(declNames), AST.ArrayToNative(decls)));
return assertions.ToBoolExprArray();
}
/// <summary>
/// Convert a benchmark into SMT-LIB2 formatted string.
/// </summary>
/// <param name="name">Name of the benchmark. May be null.</param>
/// <param name="logic">The benchmark logic. May be null.</param>
/// <param name="status">Status string, such as "sat", "unsat", or "unknown".</param>
/// <param name="attributes">Other attributes, such as source, difficulty or category. May be null.</param>
/// <param name="assumptions">Auxiliary assumptions.</param>
/// <param name="formula">Formula to be checked for consistency in conjunction with assumptions.</param>
/// <returns>A string representation of the benchmark in SMT-LIB2 format.</returns>
public string BenchmarkToSmtlibString(string name, string logic, string status, string attributes, BoolExpr[] assumptions, BoolExpr formula)
{
Debug.Assert(assumptions != null);
Debug.Assert(formula != null);
return Native.Z3_benchmark_to_smtlib_string(
nCtx,
name,
logic,
status,
attributes,
(uint)(assumptions?.Length ?? 0),
AST.ArrayToNative(assumptions),
formula.NativeObject);
}
#endregion
#region Goals

View file

@ -590,6 +590,34 @@ namespace Microsoft.Z3
return Native.Z3_solver_to_string(Context.nCtx, NativeObject);
}
/// <summary>
/// Convert the solver assertions to SMT-LIB2 format as a benchmark.
/// </summary>
/// <param name="status">Status string, such as "sat", "unsat", or "unknown". Default is "unknown".</param>
/// <returns>A string representation of the solver's assertions in SMT-LIB2 format.</returns>
public string ToSmt2(string status = "unknown")
{
BoolExpr[] assertions = Assertions;
BoolExpr formula;
BoolExpr[] assumptions;
if (assertions.Length > 0)
{
// Use last assertion as formula and rest as assumptions
assumptions = new BoolExpr[assertions.Length - 1];
Array.Copy(assertions, assumptions, assertions.Length - 1);
formula = assertions[assertions.Length - 1];
}
else
{
// No assertions, use true
assumptions = new BoolExpr[0];
formula = Context.MkTrue();
}
return Context.BenchmarkToSmtlibString("", "", status, "", assumptions, formula);
}
#region Internal
internal Solver(Context ctx, IntPtr obj)
: base(ctx, obj)