3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2026-05-25 19:36:20 +00:00

Go/OCaml API gaps: substitution, AST introspection, Spacer, Goal completion (#9277)

* fix: address issues 1,2,4,5 and add Goal API to Go bindings

Issue 2 (Go): Add Substitute, SubstituteVars, SubstituteFuns to Expr
Issue 4 (Go): Add GetDecl, NumArgs, Arg to Expr for AST app introspection
Goal API (Go): Add IsInconsistent and ToDimacsString to Goal
ASTVector (Go): Add public Size, Get, String methods
ASTMap (Go): Add ASTMap type with full CRUD API in spacer.go
Issue 1 (Go): Add Spacer fixedpoint methods QueryFromLvl, GetGroundSatAnswer,
  GetRulesAlongTrace, GetRuleNamesAlongTrace, AddInvariant, GetReachable
Issue 1 (Go): Add context-level QE functions ModelExtrapolate, QeLite,
  QeModelProject, QeModelProjectSkolem, QeModelProjectWithWitness
Issue 5 (OCaml): Add substitute_funs to z3.ml and z3.mli

Agent-Logs-Url: https://github.com/Z3Prover/z3/sessions/afa18588-47af-4720-8cea-55fe0544ae55

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

* fix: add substitute_funs to Expr module sig in z3.ml

The internal sig...end block in z3.ml (the module type declaration for Expr)
was missing val substitute_funs, causing OCaml compiler error:
  The value substitute_funs is required but not provided

Agent-Logs-Url: https://github.com/Z3Prover/z3/sessions/c6662702-46a3-4aa0-b225-d6b73c2a2505

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-04-12 14:00:03 -07:00 committed by GitHub
parent 1566d3cc41
commit 68e528eaf7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 290 additions and 0 deletions

View file

@ -291,6 +291,21 @@ func newASTVector(ctx *Context, ptr C.Z3_ast_vector) *ASTVector {
return v
}
// Size returns the number of ASTs in the vector.
func (v *ASTVector) Size() uint {
return uint(C.Z3_ast_vector_size(v.ctx.ptr, v.ptr))
}
// Get returns the i-th AST in the vector.
func (v *ASTVector) Get(i uint) *Expr {
return newExpr(v.ctx, C.Z3_ast_vector_get(v.ctx.ptr, v.ptr, C.uint(i)))
}
// String returns the string representation of the AST vector.
func (v *ASTVector) String() string {
return C.GoString(C.Z3_ast_vector_to_string(v.ctx.ptr, v.ptr))
}
// ParamDescrs represents parameter descriptions for Z3 objects.
type ParamDescrs struct {
ctx *Context
@ -640,6 +655,74 @@ func (e *Expr) Simplify() *Expr {
return newExpr(e.ctx, C.Z3_simplify(e.ctx.ptr, e.ptr))
}
// GetDecl returns the function declaration of an application expression.
func (e *Expr) GetDecl() *FuncDecl {
return newFuncDecl(e.ctx, C.Z3_get_app_decl(e.ctx.ptr, C.Z3_to_app(e.ctx.ptr, e.ptr)))
}
// NumArgs returns the number of arguments of an application expression.
func (e *Expr) NumArgs() uint {
return uint(C.Z3_get_app_num_args(e.ctx.ptr, C.Z3_to_app(e.ctx.ptr, e.ptr)))
}
// Arg returns the i-th argument of an application expression.
func (e *Expr) Arg(i uint) *Expr {
return newExpr(e.ctx, C.Z3_get_app_arg(e.ctx.ptr, C.Z3_to_app(e.ctx.ptr, e.ptr), C.uint(i)))
}
// Substitute replaces every occurrence of from[i] in the expression with to[i].
// The from and to slices must have the same length.
func (e *Expr) Substitute(from, to []*Expr) *Expr {
n := len(from)
cFrom := make([]C.Z3_ast, n)
cTo := make([]C.Z3_ast, n)
for i := range from {
cFrom[i] = from[i].ptr
cTo[i] = to[i].ptr
}
var fromPtr, toPtr *C.Z3_ast
if n > 0 {
fromPtr = &cFrom[0]
toPtr = &cTo[0]
}
return newExpr(e.ctx, C.Z3_substitute(e.ctx.ptr, e.ptr, C.uint(n), fromPtr, toPtr))
}
// SubstituteVars replaces free variables in the expression with the expressions in to.
// Variable with de-Bruijn index i is replaced with to[i].
func (e *Expr) SubstituteVars(to []*Expr) *Expr {
n := len(to)
cTo := make([]C.Z3_ast, n)
for i, t := range to {
cTo[i] = t.ptr
}
var toPtr *C.Z3_ast
if n > 0 {
toPtr = &cTo[0]
}
return newExpr(e.ctx, C.Z3_substitute_vars(e.ctx.ptr, e.ptr, C.uint(n), toPtr))
}
// SubstituteFuns replaces every occurrence of from[i] applied to arguments
// with to[i] in the expression.
// The from and to slices must have the same length.
func (e *Expr) SubstituteFuns(from []*FuncDecl, to []*Expr) *Expr {
n := len(from)
cFrom := make([]C.Z3_func_decl, n)
cTo := make([]C.Z3_ast, n)
for i := range from {
cFrom[i] = from[i].ptr
cTo[i] = to[i].ptr
}
var fromPtr *C.Z3_func_decl
var toPtr *C.Z3_ast
if n > 0 {
fromPtr = &cFrom[0]
toPtr = &cTo[0]
}
return newExpr(e.ctx, C.Z3_substitute_funs(e.ctx.ptr, e.ptr, C.uint(n), fromPtr, toPtr))
}
// MkTypeVariable creates a type variable sort for use in polymorphic functions and datatypes
func (c *Context) MkTypeVariable(name *Symbol) *Sort {
return newSort(c, C.Z3_mk_type_variable(c.ptr, name.ptr))