3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2026-02-27 18:35:50 +00:00

Add missing API methods: dimacs, translate, proof, addSimplifier, getLower/getUpper, etc.

Co-authored-by: NikolajBjorner <3085284+NikolajBjorner@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot] 2026-02-24 17:22:17 +00:00
parent 08addfa654
commit 9802b32a3e
9 changed files with 280 additions and 6 deletions

View file

@ -2130,6 +2130,24 @@ export function createApi(Z3: Z3Core): Z3HighLevel {
));
}
dimacs(includeNames: boolean = true): string {
return check(Z3.solver_to_dimacs_string(contextPtr, this.ptr, includeNames));
}
translate(target: Context<Name>): Solver<Name> {
const ptr = check(Z3.solver_translate(contextPtr, this.ptr, target.ptr));
return new (target.Solver as unknown as new (ptr: Z3_solver) => Solver<Name>)(ptr);
}
proof(): Expr<Name> | null {
const result = Z3.solver_get_proof(contextPtr, this.ptr);
throwIfError();
if (result === null) {
return null;
}
return _toExpr(result);
}
fromString(s: string) {
Z3.solver_from_string(contextPtr, this.ptr, s);
throwIfError();
@ -2306,12 +2324,42 @@ export function createApi(Z3: Z3Core): Z3HighLevel {
return new AstVectorImpl(check(Z3.optimize_get_assertions(contextPtr, this.ptr)));
}
maximize(expr: Arith<Name> | BitVec<number, Name>) {
check(Z3.optimize_maximize(contextPtr, this.ptr, expr.ast));
maximize(expr: Arith<Name> | BitVec<number, Name>): number {
return check(Z3.optimize_maximize(contextPtr, this.ptr, expr.ast));
}
minimize(expr: Arith<Name> | BitVec<number, Name>) {
check(Z3.optimize_minimize(contextPtr, this.ptr, expr.ast));
minimize(expr: Arith<Name> | BitVec<number, Name>): number {
return check(Z3.optimize_minimize(contextPtr, this.ptr, expr.ast));
}
getLower(index: number): Expr<Name> {
return _toExpr(check(Z3.optimize_get_lower(contextPtr, this.ptr, index)));
}
getUpper(index: number): Expr<Name> {
return _toExpr(check(Z3.optimize_get_upper(contextPtr, this.ptr, index)));
}
unsatCore(): AstVector<Name, Bool<Name>> {
return new AstVectorImpl(check(Z3.optimize_get_unsat_core(contextPtr, this.ptr)));
}
objectives(): AstVector<Name, Expr<Name>> {
return new AstVectorImpl(check(Z3.optimize_get_objectives(contextPtr, this.ptr)));
}
reasonUnknown(): string {
return check(Z3.optimize_get_reason_unknown(contextPtr, this.ptr));
}
fromFile(filename: string): void {
Z3.optimize_from_file(contextPtr, this.ptr, filename);
throwIfError();
}
translate(target: Context<Name>): Optimize<Name> {
const ptr = check(Z3.optimize_translate(contextPtr, this.ptr, target.ptr));
return new (target.Optimize as unknown as new (ptr: Z3_optimize) => Optimize<Name>)(ptr);
}
async check(...exprs: (Bool<Name> | AstVector<Name, Bool<Name>>)[]): Promise<CheckSatResult> {

View file

@ -1394,6 +1394,28 @@ export interface Solver<Name extends string = 'main'> {
*/
toSmtlib2(status?: string): string;
/**
* Convert the solver's Boolean formula to DIMACS CNF format.
*
* @param includeNames - If true, include variable names in the output (default: true)
* @returns A string containing the DIMACS CNF representation
*/
dimacs(includeNames?: boolean): string;
/**
* Translate the solver to a different context.
* @param target - The target context
* @returns A new Solver instance in the target context
*/
translate(target: Context<Name>): Solver<Name>;
/**
* Retrieve a proof of unsatisfiability after a check that returned 'unsat'.
* Requires proof production to be enabled.
* @returns An expression representing the proof, or null if unavailable
*/
proof(): Expr<Name> | null;
/**
* Manually decrease the reference count of the solver
* This is automatically done when the solver is garbage collected,
@ -1446,9 +1468,64 @@ export interface Optimize<Name extends string = 'main'> {
fromString(s: string): void;
maximize(expr: Arith<Name>): void;
/**
* Load SMT-LIB2 format assertions from a file into the optimizer.
*
* @param filename - Path to the file containing SMT-LIB2 format assertions
*/
fromFile(filename: string): void;
minimize(expr: Arith<Name>): void;
/**
* Add a maximization objective.
* @param expr - The expression to maximize
* @returns A numeric handle index that can be used to retrieve bounds via {@link getLower}/{@link getUpper}
*/
maximize(expr: Arith<Name> | BitVec<number, Name>): number;
/**
* Add a minimization objective.
* @param expr - The expression to minimize
* @returns A numeric handle index that can be used to retrieve bounds via {@link getLower}/{@link getUpper}
*/
minimize(expr: Arith<Name> | BitVec<number, Name>): number;
/**
* Retrieve the lower bound for the objective at the given handle index.
* Call this after {@link check} returns 'sat'.
* @param index - The handle index returned by {@link maximize} or {@link minimize}
*/
getLower(index: number): Expr<Name>;
/**
* Retrieve the upper bound for the objective at the given handle index.
* Call this after {@link check} returns 'sat'.
* @param index - The handle index returned by {@link maximize} or {@link minimize}
*/
getUpper(index: number): Expr<Name>;
/**
* Retrieve the unsat core after a check that returned 'unsat'.
* @returns An AstVector containing the subset of assumptions that caused UNSAT
*/
unsatCore(): AstVector<Name, Bool<Name>>;
/**
* Retrieve the set of objective expressions.
* @returns An AstVector containing the objectives
*/
objectives(): AstVector<Name, Expr<Name>>;
/**
* Return a string describing why the last call to {@link check} returned 'unknown'.
*/
reasonUnknown(): string;
/**
* Translate the optimize context to a different context.
* @param target - The target context
* @returns A new Optimize instance in the target context
*/
translate(target: Context<Name>): Optimize<Name>;
check(...exprs: (Bool<Name> | AstVector<Name, Bool<Name>>)[]): Promise<CheckSatResult>;