3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2026-03-02 03:36:53 +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

@ -193,3 +193,15 @@ func (o *Optimize) FromString(s string) {
defer C.free(unsafe.Pointer(cStr))
C.Z3_optimize_from_string(o.ctx.ptr, o.ptr, cStr)
}
// Translate creates a copy of the optimize context in the target context.
// This is useful when working with multiple Z3 contexts.
func (o *Optimize) Translate(target *Context) *Optimize {
ptr := C.Z3_optimize_translate(o.ctx.ptr, o.ptr, target.ptr)
newOpt := &Optimize{ctx: target, ptr: ptr}
C.Z3_optimize_inc_ref(target.ptr, ptr)
runtime.SetFinalizer(newOpt, func(opt *Optimize) {
C.Z3_optimize_dec_ref(opt.ctx.ptr, opt.ptr)
})
return newOpt
}

54
src/api/go/simplifier.go Normal file
View file

@ -0,0 +1,54 @@
package z3
/*
#include "z3.h"
#include <stdlib.h>
*/
import "C"
import (
"runtime"
"unsafe"
)
// Simplifier represents a Z3 simplifier for pre-processing solver assertions.
type Simplifier struct {
ctx *Context
ptr C.Z3_simplifier
}
// newSimplifier creates a new Simplifier and manages its reference count.
func newSimplifier(ctx *Context, ptr C.Z3_simplifier) *Simplifier {
s := &Simplifier{ctx: ctx, ptr: ptr}
C.Z3_simplifier_inc_ref(ctx.ptr, ptr)
runtime.SetFinalizer(s, func(simp *Simplifier) {
C.Z3_simplifier_dec_ref(simp.ctx.ptr, simp.ptr)
})
return s
}
// MkSimplifier creates a simplifier with the given name.
func (c *Context) MkSimplifier(name string) *Simplifier {
cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))
return newSimplifier(c, C.Z3_mk_simplifier(c.ptr, cName))
}
// AndThen creates a simplifier that applies s followed by s2.
func (s *Simplifier) AndThen(s2 *Simplifier) *Simplifier {
return newSimplifier(s.ctx, C.Z3_simplifier_and_then(s.ctx.ptr, s.ptr, s2.ptr))
}
// UsingParams creates a simplifier that uses the given parameters.
func (s *Simplifier) UsingParams(params *Params) *Simplifier {
return newSimplifier(s.ctx, C.Z3_simplifier_using_params(s.ctx.ptr, s.ptr, params.ptr))
}
// GetHelp returns help information for the simplifier.
func (s *Simplifier) GetHelp() string {
return C.GoString(C.Z3_simplifier_get_help(s.ctx.ptr, s.ptr))
}
// GetParamDescrs returns parameter descriptions for the simplifier.
func (s *Simplifier) GetParamDescrs() *ParamDescrs {
return newParamDescrs(s.ctx, C.Z3_simplifier_get_param_descrs(s.ctx.ptr, s.ptr))
}

View file

@ -357,6 +357,47 @@ func (dst *Solver) ImportModelConverter(src *Solver) {
C.Z3_solver_import_model_converter(dst.ctx.ptr, src.ptr, dst.ptr)
}
// Translate creates a copy of the solver in the target context.
// This is useful when working with multiple Z3 contexts.
func (s *Solver) Translate(target *Context) *Solver {
ptr := C.Z3_solver_translate(s.ctx.ptr, s.ptr, target.ptr)
newSolver := &Solver{ctx: target, ptr: ptr}
C.Z3_solver_inc_ref(target.ptr, ptr)
runtime.SetFinalizer(newSolver, func(solver *Solver) {
C.Z3_solver_dec_ref(solver.ctx.ptr, solver.ptr)
})
return newSolver
}
// GetProof returns the proof of unsatisfiability from the last check.
// Returns nil if no proof is available (e.g. the result was not UNSAT,
// or proof production is disabled).
func (s *Solver) GetProof() *Expr {
result := C.Z3_solver_get_proof(s.ctx.ptr, s.ptr)
if result == nil {
return nil
}
return newExpr(s.ctx, result)
}
// AddSimplifier creates a new solver with the given simplifier attached for
// pre-processing assertions before solving.
func (s *Solver) AddSimplifier(simplifier *Simplifier) *Solver {
ptr := C.Z3_solver_add_simplifier(s.ctx.ptr, s.ptr, simplifier.ptr)
newSolver := &Solver{ctx: s.ctx, ptr: ptr}
C.Z3_solver_inc_ref(s.ctx.ptr, ptr)
runtime.SetFinalizer(newSolver, func(solver *Solver) {
C.Z3_solver_dec_ref(solver.ctx.ptr, solver.ptr)
})
return newSolver
}
// Dimacs converts the solver's Boolean formula to DIMACS CNF format.
// If includeNames is true, variable names are included in the output.
func (s *Solver) Dimacs(includeNames bool) string {
return C.GoString(C.Z3_solver_to_dimacs_string(s.ctx.ptr, s.ptr, C.bool(includeNames)))
}
// Model represents a Z3 model (satisfying assignment).
type Model struct {
ctx *Context