1// Package z3 provides Go bindings for the Z3 theorem prover.
3// Z3 is a high-performance SMT (Satisfiability Modulo Theories) solver
4// developed at Microsoft Research. These bindings wrap the Z3 C API using
5// CGO and provide idiomatic Go interfaces with automatic memory management.
9// Create a context and solver:
11// ctx := z3.NewContext()
12// solver := ctx.NewSolver()
14// Create variables and constraints:
16// x := ctx.MkIntConst("x")
17// y := ctx.MkIntConst("y")
18// solver.Assert(ctx.MkEq(ctx.MkAdd(x, y), ctx.MkInt(10, ctx.MkIntSort())))
19// solver.Assert(ctx.MkGt(x, y))
21// Check satisfiability and get model:
23// if solver.Check() == z3.Satisfiable {
24// model := solver.Model()
25// xVal, _ := model.Eval(x, true)
26// fmt.Println("x =", xVal.String())
31// All Z3 objects are automatically managed using Go finalizers. Reference
32// counting is handled transparently - you don't need to manually free objects.
34// # Supported Features
36// - Boolean logic, integer and real arithmetic
37// - Bit-vectors and floating-point arithmetic
38// - Arrays, sequences, and strings
39// - Regular expressions
40// - Algebraic datatypes
41// - Quantifiers and lambda expressions
42// - Tactics and goal-based solving
43// - Optimization (MaxSMT)
44// - Fixedpoint solver (Datalog/CHC)
46// For more examples, see the examples/go directory in the Z3 repository.
50#cgo CFLAGS: -I${SRCDIR}/..
61// Config represents a Z3 configuration object.
66// NewConfig creates a new Z3 configuration.
67func NewConfig() *Config {
68 cfg := &Config{ptr: C.Z3_mk_config()}
69 runtime.SetFinalizer(cfg, func(c *Config) {
70 C.Z3_del_config(c.ptr)
75// SetParamValue sets a configuration parameter.
76func (c *Config) SetParamValue(paramID, paramValue string) {
77 cParamID := C.CString(paramID)
78 cParamValue := C.CString(paramValue)
79 defer C.free(unsafe.Pointer(cParamID))
80 defer C.free(unsafe.Pointer(cParamValue))
81 C.Z3_set_param_value(c.ptr, cParamID, cParamValue)
84// Context represents a Z3 logical context.
89// NewContext creates a new Z3 context with default configuration.
90func NewContext() *Context {
91 ctx := &Context{ptr: C.Z3_mk_context_rc(C.Z3_mk_config())}
92 C.Z3_enable_concurrent_dec_ref(ctx.ptr)
93 runtime.SetFinalizer(ctx, func(c *Context) {
94 C.Z3_del_context(c.ptr)
99// NewContextWithConfig creates a new Z3 context with the given configuration.
100func NewContextWithConfig(cfg *Config) *Context {
101 ctx := &Context{ptr: C.Z3_mk_context_rc(cfg.ptr)}
102 C.Z3_enable_concurrent_dec_ref(ctx.ptr)
103 runtime.SetFinalizer(ctx, func(c *Context) {
104 C.Z3_del_context(c.ptr)
109// SetParam sets a global or context parameter.
110func (c *Context) SetParam(key, value string) {
111 cKey := C.CString(key)
112 cValue := C.CString(value)
113 defer C.free(unsafe.Pointer(cKey))
114 defer C.free(unsafe.Pointer(cValue))
115 C.Z3_update_param_value(c.ptr, cKey, cValue)
118// Symbol represents a Z3 symbol.
124// newSymbol creates a new Symbol.
125func newSymbol(ctx *Context, ptr C.Z3_symbol) *Symbol {
126 return &Symbol{ctx: ctx, ptr: ptr}
129// MkIntSymbol creates an integer symbol.
130func (c *Context) MkIntSymbol(i int) *Symbol {
133 ptr: C.Z3_mk_int_symbol(c.ptr, C.int(i)),
137// MkStringSymbol creates a string symbol.
138func (c *Context) MkStringSymbol(s string) *Symbol {
140 defer C.free(unsafe.Pointer(cStr))
143 ptr: C.Z3_mk_string_symbol(c.ptr, cStr),
147// String returns the string representation of the symbol.
148func (s *Symbol) String() string {
149 kind := C.Z3_get_symbol_kind(s.ctx.ptr, s.ptr)
150 if kind == C.Z3_INT_SYMBOL {
151 return string(rune(C.Z3_get_symbol_int(s.ctx.ptr, s.ptr)))
153 return C.GoString(C.Z3_get_symbol_string(s.ctx.ptr, s.ptr))
156// AST represents a Z3 abstract syntax tree node.
162// incRef increments the reference count of the AST.
163func (a *AST) incRef() {
164 C.Z3_inc_ref(a.ctx.ptr, a.ptr)
167// decRef decrements the reference count of the AST.
168func (a *AST) decRef() {
169 C.Z3_dec_ref(a.ctx.ptr, a.ptr)
172// String returns the string representation of the AST.
173func (a *AST) String() string {
174 return C.GoString(C.Z3_ast_to_string(a.ctx.ptr, a.ptr))
177// Hash returns the hash code of the AST.
178func (a *AST) Hash() uint32 {
179 return uint32(C.Z3_get_ast_hash(a.ctx.ptr, a.ptr))
182// Equal checks if two ASTs are equal.
183func (a *AST) Equal(other *AST) bool {
184 if a.ctx != other.ctx {
187 return bool(C.Z3_is_eq_ast(a.ctx.ptr, a.ptr, other.ptr))
190// Sort represents a Z3 sort (type).
196// newSort creates a new Sort and manages its reference count.
197func newSort(ctx *Context, ptr C.Z3_sort) *Sort {
198 sort := &Sort{ctx: ctx, ptr: ptr}
199 C.Z3_inc_ref(ctx.ptr, C.Z3_sort_to_ast(ctx.ptr, ptr))
200 runtime.SetFinalizer(sort, func(s *Sort) {
201 C.Z3_dec_ref(s.ctx.ptr, C.Z3_sort_to_ast(s.ctx.ptr, s.ptr))
206// String returns the string representation of the sort.
207func (s *Sort) String() string {
208 return C.GoString(C.Z3_sort_to_string(s.ctx.ptr, s.ptr))
211// Equal checks if two sorts are equal.
212func (s *Sort) Equal(other *Sort) bool {
213 if s.ctx != other.ctx {
216 return bool(C.Z3_is_eq_sort(s.ctx.ptr, s.ptr, other.ptr))
219// MkBoolSort creates the Boolean sort.
220func (c *Context) MkBoolSort() *Sort {
221 return newSort(c, C.Z3_mk_bool_sort(c.ptr))
224// MkBvSort creates a bit-vector sort of the given size.
225func (c *Context) MkBvSort(sz uint) *Sort {
226 return newSort(c, C.Z3_mk_bv_sort(c.ptr, C.uint(sz)))
229// Expr represents a Z3 expression.
235// newExpr creates a new Expr and manages its reference count.
236func newExpr(ctx *Context, ptr C.Z3_ast) *Expr {
237 expr := &Expr{ctx: ctx, ptr: ptr}
238 C.Z3_inc_ref(ctx.ptr, ptr)
239 runtime.SetFinalizer(expr, func(e *Expr) {
240 C.Z3_dec_ref(e.ctx.ptr, e.ptr)
245// intsToCs converts a []int slice to []C.int, returning the slice and
246// a pointer to its first element (nil if empty).
247func intsToCs(ints []int) ([]C.int, *C.int) {
251 cInts := make([]C.int, len(ints))
252 for i, v := range ints {
255 return cInts, &cInts[0]
258// exprsToASTs converts a []*Expr slice to []C.Z3_ast, returning the slice and
259// a pointer to its first element (nil if empty).
260func exprsToASTs(exprs []*Expr) ([]C.Z3_ast, *C.Z3_ast) {
264 cExprs := make([]C.Z3_ast, len(exprs))
265 for i, e := range exprs {
268 return cExprs, &cExprs[0]
271// sortsToCSorts converts a []*Sort slice to []C.Z3_sort, returning the slice and
272// a pointer to its first element (nil if empty).
273func sortsToCSorts(sorts []*Sort) ([]C.Z3_sort, *C.Z3_sort) {
277 cSorts := make([]C.Z3_sort, len(sorts))
278 for i, s := range sorts {
281 return cSorts, &cSorts[0]
284// String returns the string representation of the expression.
285func (e *Expr) String() string {
286 return C.GoString(C.Z3_ast_to_string(e.ctx.ptr, e.ptr))
289// Equal checks if two expressions are equal.
290func (e *Expr) Equal(other *Expr) bool {
291 if e.ctx != other.ctx {
294 return bool(C.Z3_is_eq_ast(e.ctx.ptr, e.ptr, other.ptr))
297// GetSort returns the sort of the expression.
298func (e *Expr) GetSort() *Sort {
299 return newSort(e.ctx, C.Z3_get_sort(e.ctx.ptr, e.ptr))
302// Pattern represents a Z3 pattern for quantifier instantiation.
308// newPattern creates a new Pattern and manages its reference count.
309func newPattern(ctx *Context, ptr C.Z3_pattern) *Pattern {
310 p := &Pattern{ctx: ctx, ptr: ptr}
311 // Patterns are ASTs in the C API
312 C.Z3_inc_ref(ctx.ptr, (C.Z3_ast)(unsafe.Pointer(ptr)))
313 runtime.SetFinalizer(p, func(pat *Pattern) {
314 C.Z3_dec_ref(pat.ctx.ptr, (C.Z3_ast)(unsafe.Pointer(pat.ptr)))
319// ASTVector represents a vector of Z3 ASTs.
320type ASTVector struct {
325// newASTVector creates a new ASTVector and manages its reference count.
326func newASTVector(ctx *Context, ptr C.Z3_ast_vector) *ASTVector {
327 v := &ASTVector{ctx: ctx, ptr: ptr}
328 C.Z3_ast_vector_inc_ref(ctx.ptr, ptr)
329 runtime.SetFinalizer(v, func(vec *ASTVector) {
330 C.Z3_ast_vector_dec_ref(vec.ctx.ptr, vec.ptr)
335// Size returns the number of ASTs in the vector.
336func (v *ASTVector) Size() uint {
337 return uint(C.Z3_ast_vector_size(v.ctx.ptr, v.ptr))
340// Get returns the i-th AST in the vector.
341func (v *ASTVector) Get(i uint) *Expr {
342 return newExpr(v.ctx, C.Z3_ast_vector_get(v.ctx.ptr, v.ptr, C.uint(i)))
345// String returns the string representation of the AST vector.
346func (v *ASTVector) String() string {
347 return C.GoString(C.Z3_ast_vector_to_string(v.ctx.ptr, v.ptr))
350// ParamDescrs represents parameter descriptions for Z3 objects.
351type ParamDescrs struct {
353 ptr C.Z3_param_descrs
356// newParamDescrs creates a new ParamDescrs and manages its reference count.
357func newParamDescrs(ctx *Context, ptr C.Z3_param_descrs) *ParamDescrs {
358 pd := &ParamDescrs{ctx: ctx, ptr: ptr}
359 C.Z3_param_descrs_inc_ref(ctx.ptr, ptr)
360 runtime.SetFinalizer(pd, func(descrs *ParamDescrs) {
361 C.Z3_param_descrs_dec_ref(descrs.ctx.ptr, descrs.ptr)
366// MkTrue creates the Boolean constant true.
367func (c *Context) MkTrue() *Expr {
368 return newExpr(c, C.Z3_mk_true(c.ptr))
371// MkFalse creates the Boolean constant false.
372func (c *Context) MkFalse() *Expr {
373 return newExpr(c, C.Z3_mk_false(c.ptr))
376// MkBool creates a Boolean constant.
377func (c *Context) MkBool(value bool) *Expr {
384// MkNumeral creates a numeral from a string.
385func (c *Context) MkNumeral(numeral string, sort *Sort) *Expr {
386 cStr := C.CString(numeral)
387 defer C.free(unsafe.Pointer(cStr))
388 return newExpr(c, C.Z3_mk_numeral(c.ptr, cStr, sort.ptr))
391// MkConst creates a constant (variable) with the given name and sort.
392func (c *Context) MkConst(name *Symbol, sort *Sort) *Expr {
393 return newExpr(c, C.Z3_mk_const(c.ptr, name.ptr, sort.ptr))
396// MkBoolConst creates a Boolean constant (variable) with the given name.
397func (c *Context) MkBoolConst(name string) *Expr {
398 sym := c.MkStringSymbol(name)
399 return c.MkConst(sym, c.MkBoolSort())
404// MkAnd creates a conjunction.
405func (c *Context) MkAnd(exprs ...*Expr) *Expr {
412 _, cExprsPtr := exprsToASTs(exprs)
413 return newExpr(c, C.Z3_mk_and(c.ptr, C.uint(len(exprs)), cExprsPtr))
416// MkOr creates a disjunction.
417func (c *Context) MkOr(exprs ...*Expr) *Expr {
424 _, cExprsPtr := exprsToASTs(exprs)
425 return newExpr(c, C.Z3_mk_or(c.ptr, C.uint(len(exprs)), cExprsPtr))
428// MkNot creates a negation.
429func (c *Context) MkNot(expr *Expr) *Expr {
430 return newExpr(c, C.Z3_mk_not(c.ptr, expr.ptr))
433// MkImplies creates an implication.
434func (c *Context) MkImplies(lhs, rhs *Expr) *Expr {
435 return newExpr(c, C.Z3_mk_implies(c.ptr, lhs.ptr, rhs.ptr))
438// MkIff creates a bi-implication (if and only if).
439func (c *Context) MkIff(lhs, rhs *Expr) *Expr {
440 return newExpr(c, C.Z3_mk_iff(c.ptr, lhs.ptr, rhs.ptr))
443// MkXor creates exclusive or.
444func (c *Context) MkXor(lhs, rhs *Expr) *Expr {
445 return newExpr(c, C.Z3_mk_xor(c.ptr, lhs.ptr, rhs.ptr))
448// Comparison operations
450// MkEq creates an equality.
451func (c *Context) MkEq(lhs, rhs *Expr) *Expr {
452 return newExpr(c, C.Z3_mk_eq(c.ptr, lhs.ptr, rhs.ptr))
455// MkDistinct creates a distinct constraint.
456func (c *Context) MkDistinct(exprs ...*Expr) *Expr {
460 _, cExprsPtr := exprsToASTs(exprs)
461 return newExpr(c, C.Z3_mk_distinct(c.ptr, C.uint(len(exprs)), cExprsPtr))
464// Pseudo-Boolean / cardinality constraints
466// MkAtMost encodes p1 + p2 + ... + pn <= k.
467func (c *Context) MkAtMost(args []*Expr, k uint) *Expr {
468 _, cArgsPtr := exprsToASTs(args)
469 return newExpr(c, C.Z3_mk_atmost(c.ptr, C.uint(len(args)), cArgsPtr, C.uint(k)))
472// MkAtLeast encodes p1 + p2 + ... + pn >= k.
473func (c *Context) MkAtLeast(args []*Expr, k uint) *Expr {
474 _, cArgsPtr := exprsToASTs(args)
475 return newExpr(c, C.Z3_mk_atleast(c.ptr, C.uint(len(args)), cArgsPtr, C.uint(k)))
478// MkPBLe encodes k1*p1 + k2*p2 + ... + kn*pn <= k.
479func (c *Context) MkPBLe(args []*Expr, coeffs []int, k int) *Expr {
480 if len(args) != len(coeffs) {
481 panic("MkPBLe: args and coeffs must have the same length")
483 _, cArgsPtr := exprsToASTs(args)
484 _, cCoeffsPtr := intsToCs(coeffs)
485 return newExpr(c, C.Z3_mk_pble(c.ptr, C.uint(len(args)), cArgsPtr, cCoeffsPtr, C.int(k)))
488// MkPBGe encodes k1*p1 + k2*p2 + ... + kn*pn >= k.
489func (c *Context) MkPBGe(args []*Expr, coeffs []int, k int) *Expr {
490 if len(args) != len(coeffs) {
491 panic("MkPBGe: args and coeffs must have the same length")
493 _, cArgsPtr := exprsToASTs(args)
494 _, cCoeffsPtr := intsToCs(coeffs)
495 return newExpr(c, C.Z3_mk_pbge(c.ptr, C.uint(len(args)), cArgsPtr, cCoeffsPtr, C.int(k)))
498// MkPBEq encodes k1*p1 + k2*p2 + ... + kn*pn = k.
499func (c *Context) MkPBEq(args []*Expr, coeffs []int, k int) *Expr {
500 if len(args) != len(coeffs) {
501 panic("MkPBEq: args and coeffs must have the same length")
503 _, cArgsPtr := exprsToASTs(args)
504 _, cCoeffsPtr := intsToCs(coeffs)
505 return newExpr(c, C.Z3_mk_pbeq(c.ptr, C.uint(len(args)), cArgsPtr, cCoeffsPtr, C.int(k)))
508// FuncDecl represents a function declaration.
509type FuncDecl struct {
514// newFuncDecl creates a new FuncDecl and manages its reference count.
515func newFuncDecl(ctx *Context, ptr C.Z3_func_decl) *FuncDecl {
516 fd := &FuncDecl{ctx: ctx, ptr: ptr}
517 C.Z3_inc_ref(ctx.ptr, C.Z3_func_decl_to_ast(ctx.ptr, ptr))
518 runtime.SetFinalizer(fd, func(f *FuncDecl) {
519 C.Z3_dec_ref(f.ctx.ptr, C.Z3_func_decl_to_ast(f.ctx.ptr, f.ptr))
524// String returns the string representation of the function declaration.
525func (f *FuncDecl) String() string {
526 return C.GoString(C.Z3_func_decl_to_string(f.ctx.ptr, f.ptr))
529// GetName returns the name of the function declaration.
530func (f *FuncDecl) GetName() *Symbol {
533 ptr: C.Z3_get_decl_name(f.ctx.ptr, f.ptr),
537// GetArity returns the arity (number of parameters) of the function.
538func (f *FuncDecl) GetArity() int {
539 return int(C.Z3_get_arity(f.ctx.ptr, f.ptr))
542// GetDomain returns the sort of the i-th parameter.
543func (f *FuncDecl) GetDomain(i int) *Sort {
544 return newSort(f.ctx, C.Z3_get_domain(f.ctx.ptr, f.ptr, C.uint(i)))
547// GetRange returns the sort of the return value.
548func (f *FuncDecl) GetRange() *Sort {
549 return newSort(f.ctx, C.Z3_get_range(f.ctx.ptr, f.ptr))
552// MkFuncDecl creates a function declaration.
553func (c *Context) MkFuncDecl(name *Symbol, domain []*Sort, range_ *Sort) *FuncDecl {
554 _, domainPtr := sortsToCSorts(domain)
555 return newFuncDecl(c, C.Z3_mk_func_decl(c.ptr, name.ptr, C.uint(len(domain)), domainPtr, range_.ptr))
558// MkRecFuncDecl creates a recursive function declaration.
559// After creating, use AddRecDef to provide the function body.
560func (c *Context) MkRecFuncDecl(name *Symbol, domain []*Sort, range_ *Sort) *FuncDecl {
561 _, domainPtr := sortsToCSorts(domain)
562 return newFuncDecl(c, C.Z3_mk_rec_func_decl(c.ptr, name.ptr, C.uint(len(domain)), domainPtr, range_.ptr))
565// AddRecDef adds the definition (body) for a recursive function created with MkRecFuncDecl.
566func (c *Context) AddRecDef(f *FuncDecl, args []*Expr, body *Expr) {
567 _, argsPtr := exprsToASTs(args)
568 C.Z3_add_rec_def(c.ptr, f.ptr, C.uint(len(args)), argsPtr, body.ptr)
571// MkApp creates a function application.
572func (c *Context) MkApp(decl *FuncDecl, args ...*Expr) *Expr {
573 _, argsPtr := exprsToASTs(args)
574 return newExpr(c, C.Z3_mk_app(c.ptr, decl.ptr, C.uint(len(args)), argsPtr))
577// Quantifier operations
579// MkForall creates a universal quantifier.
580func (c *Context) MkForall(bound []*Expr, body *Expr) *Expr {
581 cBound := make([]C.Z3_app, len(bound))
582 for i, b := range bound {
583 // Z3_app is a subtype of Z3_ast; constants are apps
584 cBound[i] = (C.Z3_app)(unsafe.Pointer(b.ptr))
586 var boundPtr *C.Z3_app
588 boundPtr = &cBound[0]
590 return newExpr(c, C.Z3_mk_forall_const(c.ptr, 0, C.uint(len(bound)), boundPtr, 0, nil, body.ptr))
593// MkExists creates an existential quantifier.
594func (c *Context) MkExists(bound []*Expr, body *Expr) *Expr {
595 cBound := make([]C.Z3_app, len(bound))
596 for i, b := range bound {
597 // Z3_app is a subtype of Z3_ast; constants are apps
598 cBound[i] = (C.Z3_app)(unsafe.Pointer(b.ptr))
600 var boundPtr *C.Z3_app
602 boundPtr = &cBound[0]
604 return newExpr(c, C.Z3_mk_exists_const(c.ptr, 0, C.uint(len(bound)), boundPtr, 0, nil, body.ptr))
607// Simplify simplifies an expression.
608func (e *Expr) Simplify() *Expr {
609 return newExpr(e.ctx, C.Z3_simplify(e.ctx.ptr, e.ptr))
612// GetDecl returns the function declaration of an application expression.
613func (e *Expr) GetDecl() *FuncDecl {
614 return newFuncDecl(e.ctx, C.Z3_get_app_decl(e.ctx.ptr, C.Z3_to_app(e.ctx.ptr, e.ptr)))
617// NumArgs returns the number of arguments of an application expression.
618func (e *Expr) NumArgs() uint {
619 return uint(C.Z3_get_app_num_args(e.ctx.ptr, C.Z3_to_app(e.ctx.ptr, e.ptr)))
622// Arg returns the i-th argument of an application expression.
623func (e *Expr) Arg(i uint) *Expr {
624 return newExpr(e.ctx, C.Z3_get_app_arg(e.ctx.ptr, C.Z3_to_app(e.ctx.ptr, e.ptr), C.uint(i)))
627// Substitute replaces every occurrence of from[i] in the expression with to[i].
628// The from and to slices must have the same length.
629func (e *Expr) Substitute(from, to []*Expr) *Expr {
631 cFrom := make([]C.Z3_ast, n)
632 cTo := make([]C.Z3_ast, n)
633 for i := range from {
634 cFrom[i] = from[i].ptr
637 var fromPtr, toPtr *C.Z3_ast
642 return newExpr(e.ctx, C.Z3_substitute(e.ctx.ptr, e.ptr, C.uint(n), fromPtr, toPtr))
645// SubstituteVars replaces free variables in the expression with the expressions in to.
646// Variable with de-Bruijn index i is replaced with to[i].
647func (e *Expr) SubstituteVars(to []*Expr) *Expr {
648 _, toPtr := exprsToASTs(to)
649 return newExpr(e.ctx, C.Z3_substitute_vars(e.ctx.ptr, e.ptr, C.uint(len(to)), toPtr))
652// SubstituteFuns replaces every occurrence of from[i] applied to arguments
653// with to[i] in the expression.
654// The from and to slices must have the same length.
655func (e *Expr) SubstituteFuns(from []*FuncDecl, to []*Expr) *Expr {
657 cFrom := make([]C.Z3_func_decl, n)
658 cTo := make([]C.Z3_ast, n)
659 for i := range from {
660 cFrom[i] = from[i].ptr
663 var fromPtr *C.Z3_func_decl
669 return newExpr(e.ctx, C.Z3_substitute_funs(e.ctx.ptr, e.ptr, C.uint(n), fromPtr, toPtr))
672// MkTypeVariable creates a type variable sort for use in polymorphic functions and datatypes
673func (c *Context) MkTypeVariable(name *Symbol) *Sort {
674 return newSort(c, C.Z3_mk_type_variable(c.ptr, name.ptr))
677// Quantifier represents a quantified formula (forall or exists)
678type Quantifier struct {
683// newQuantifier creates a new Quantifier with proper memory management
684func newQuantifier(ctx *Context, ptr C.Z3_ast) *Quantifier {
685 q := &Quantifier{ctx: ctx, ptr: ptr}
686 C.Z3_inc_ref(ctx.ptr, ptr)
687 runtime.SetFinalizer(q, func(qf *Quantifier) {
688 C.Z3_dec_ref(qf.ctx.ptr, qf.ptr)
693// AsExpr converts a Quantifier to an Expr
694func (q *Quantifier) AsExpr() *Expr {
695 return newExpr(q.ctx, q.ptr)
698// IsUniversal returns true if this is a universal quantifier (forall)
699func (q *Quantifier) IsUniversal() bool {
700 return bool(C.Z3_is_quantifier_forall(q.ctx.ptr, q.ptr))
703// IsExistential returns true if this is an existential quantifier (exists)
704func (q *Quantifier) IsExistential() bool {
705 return bool(C.Z3_is_quantifier_exists(q.ctx.ptr, q.ptr))
708// GetWeight returns the weight of the quantifier
709func (q *Quantifier) GetWeight() int {
710 return int(C.Z3_get_quantifier_weight(q.ctx.ptr, q.ptr))
713// GetNumPatterns returns the number of patterns
714func (q *Quantifier) GetNumPatterns() int {
715 return int(C.Z3_get_quantifier_num_patterns(q.ctx.ptr, q.ptr))
718// GetPattern returns the pattern at the given index
719func (q *Quantifier) GetPattern(idx int) *Pattern {
720 ptr := C.Z3_get_quantifier_pattern_ast(q.ctx.ptr, q.ptr, C.uint(idx))
721 return newPattern(q.ctx, ptr)
724// GetNumNoPatterns returns the number of no-patterns
725func (q *Quantifier) GetNumNoPatterns() int {
726 return int(C.Z3_get_quantifier_num_no_patterns(q.ctx.ptr, q.ptr))
729// GetNoPattern returns the no-pattern at the given index
730func (q *Quantifier) GetNoPattern(idx int) *Pattern {
731 ptr := C.Z3_get_quantifier_no_pattern_ast(q.ctx.ptr, q.ptr, C.uint(idx))
732 return newPattern(q.ctx, (C.Z3_pattern)(unsafe.Pointer(ptr)))
735// GetNumBound returns the number of bound variables
736func (q *Quantifier) GetNumBound() int {
737 return int(C.Z3_get_quantifier_num_bound(q.ctx.ptr, q.ptr))
740// GetBoundName returns the name of the bound variable at the given index
741func (q *Quantifier) GetBoundName(idx int) *Symbol {
742 ptr := C.Z3_get_quantifier_bound_name(q.ctx.ptr, q.ptr, C.uint(idx))
743 return newSymbol(q.ctx, ptr)
746// GetBoundSort returns the sort of the bound variable at the given index
747func (q *Quantifier) GetBoundSort(idx int) *Sort {
748 ptr := C.Z3_get_quantifier_bound_sort(q.ctx.ptr, q.ptr, C.uint(idx))
749 return newSort(q.ctx, ptr)
752// GetBody returns the body of the quantifier
753func (q *Quantifier) GetBody() *Expr {
754 ptr := C.Z3_get_quantifier_body(q.ctx.ptr, q.ptr)
755 return newExpr(q.ctx, ptr)
758// String returns the string representation of the quantifier
759func (q *Quantifier) String() string {
760 return q.AsExpr().String()
763// MkQuantifier creates a quantifier with patterns
764func (c *Context) MkQuantifier(isForall bool, weight int, sorts []*Sort, names []*Symbol, body *Expr, patterns []*Pattern) *Quantifier {
765 forallInt := C.bool(isForall)
767 numBound := len(sorts)
768 if numBound != len(names) {
769 panic("Number of sorts must match number of names")
772 var cSorts []C.Z3_sort
773 var cNames []C.Z3_symbol
775 cSorts = make([]C.Z3_sort, numBound)
776 cNames = make([]C.Z3_symbol, numBound)
777 for i := 0; i < numBound; i++ {
778 cSorts[i] = sorts[i].ptr
779 cNames[i] = names[i].ptr
783 var cPatterns []C.Z3_pattern
784 var patternsPtr *C.Z3_pattern
785 numPatterns := len(patterns)
787 cPatterns = make([]C.Z3_pattern, numPatterns)
788 for i := 0; i < numPatterns; i++ {
789 cPatterns[i] = patterns[i].ptr
791 patternsPtr = &cPatterns[0]
794 var sortsPtr *C.Z3_sort
795 var namesPtr *C.Z3_symbol
797 sortsPtr = &cSorts[0]
798 namesPtr = &cNames[0]
801 ptr := C.Z3_mk_quantifier(c.ptr, forallInt, C.uint(weight), C.uint(numPatterns), patternsPtr,
802 C.uint(numBound), sortsPtr, namesPtr, body.ptr)
803 return newQuantifier(c, ptr)
806// MkQuantifierConst creates a quantifier using constant bound variables
807func (c *Context) MkQuantifierConst(isForall bool, weight int, bound []*Expr, body *Expr, patterns []*Pattern) *Quantifier {
808 forallInt := C.bool(isForall)
810 numBound := len(bound)
811 var cBound []C.Z3_app
812 var boundPtr *C.Z3_app
814 cBound = make([]C.Z3_app, numBound)
815 for i := 0; i < numBound; i++ {
816 cBound[i] = (C.Z3_app)(unsafe.Pointer(bound[i].ptr))
818 boundPtr = &cBound[0]
821 var cPatterns []C.Z3_pattern
822 var patternsPtr *C.Z3_pattern
823 numPatterns := len(patterns)
825 cPatterns = make([]C.Z3_pattern, numPatterns)
826 for i := 0; i < numPatterns; i++ {
827 cPatterns[i] = patterns[i].ptr
829 patternsPtr = &cPatterns[0]
832 ptr := C.Z3_mk_quantifier_const(c.ptr, forallInt, C.uint(weight), C.uint(numBound), boundPtr,
833 C.uint(numPatterns), patternsPtr, body.ptr)
834 return newQuantifier(c, ptr)
837// Lambda represents a lambda expression
843// newLambda creates a new Lambda with proper memory management
844func newLambda(ctx *Context, ptr C.Z3_ast) *Lambda {
845 l := &Lambda{ctx: ctx, ptr: ptr}
846 C.Z3_inc_ref(ctx.ptr, ptr)
847 runtime.SetFinalizer(l, func(lam *Lambda) {
848 C.Z3_dec_ref(lam.ctx.ptr, lam.ptr)
853// AsExpr converts a Lambda to an Expr
854func (l *Lambda) AsExpr() *Expr {
855 return newExpr(l.ctx, l.ptr)
858// GetNumBound returns the number of bound variables
859func (l *Lambda) GetNumBound() int {
860 return int(C.Z3_get_quantifier_num_bound(l.ctx.ptr, l.ptr))
863// GetBoundName returns the name of the bound variable at the given index
864func (l *Lambda) GetBoundName(idx int) *Symbol {
865 ptr := C.Z3_get_quantifier_bound_name(l.ctx.ptr, l.ptr, C.uint(idx))
866 return newSymbol(l.ctx, ptr)
869// GetBoundSort returns the sort of the bound variable at the given index
870func (l *Lambda) GetBoundSort(idx int) *Sort {
871 ptr := C.Z3_get_quantifier_bound_sort(l.ctx.ptr, l.ptr, C.uint(idx))
872 return newSort(l.ctx, ptr)
875// GetBody returns the body of the lambda expression
876func (l *Lambda) GetBody() *Expr {
877 ptr := C.Z3_get_quantifier_body(l.ctx.ptr, l.ptr)
878 return newExpr(l.ctx, ptr)
881// String returns the string representation of the lambda
882func (l *Lambda) String() string {
883 return l.AsExpr().String()
886// MkLambda creates a lambda expression with sorts and names
887func (c *Context) MkLambda(sorts []*Sort, names []*Symbol, body *Expr) *Lambda {
888 numBound := len(sorts)
889 if numBound != len(names) {
890 panic("Number of sorts must match number of names")
893 var cSorts []C.Z3_sort
894 var cNames []C.Z3_symbol
895 var sortsPtr *C.Z3_sort
896 var namesPtr *C.Z3_symbol
899 cSorts = make([]C.Z3_sort, numBound)
900 cNames = make([]C.Z3_symbol, numBound)
901 for i := 0; i < numBound; i++ {
902 cSorts[i] = sorts[i].ptr
903 cNames[i] = names[i].ptr
905 sortsPtr = &cSorts[0]
906 namesPtr = &cNames[0]
909 ptr := C.Z3_mk_lambda(c.ptr, C.uint(numBound), sortsPtr, namesPtr, body.ptr)
910 return newLambda(c, ptr)
913// MkLambdaConst creates a lambda expression using constant bound variables
914func (c *Context) MkLambdaConst(bound []*Expr, body *Expr) *Lambda {
915 numBound := len(bound)
916 var cBound []C.Z3_app
917 var boundPtr *C.Z3_app
920 cBound = make([]C.Z3_app, numBound)
921 for i := 0; i < numBound; i++ {
922 cBound[i] = (C.Z3_app)(unsafe.Pointer(bound[i].ptr))
924 boundPtr = &cBound[0]
927 ptr := C.Z3_mk_lambda_const(c.ptr, C.uint(numBound), boundPtr, body.ptr)
928 return newLambda(c, ptr)
931// SetGlobalParam sets a global Z3 parameter.
932func SetGlobalParam(id, value string) {
934 cValue := C.CString(value)
935 defer C.free(unsafe.Pointer(cID))
936 defer C.free(unsafe.Pointer(cValue))
937 C.Z3_global_param_set(cID, cValue)
940// GetGlobalParam retrieves the value of a global Z3 parameter.
941// Returns the value and true if the parameter exists, or empty string and false otherwise.
942func GetGlobalParam(id string) (string, bool) {
944 defer C.free(unsafe.Pointer(cID))
945 var cValue C.Z3_string
946 ok := C.Z3_global_param_get(cID, &cValue)
947 if ok == C.bool(false) {
950 return C.GoString(cValue), true
953// ResetAllGlobalParams resets all global Z3 parameters to their default values.
954func ResetAllGlobalParams() {
955 C.Z3_global_param_reset_all()
958// astVectorToExprs converts a Z3_ast_vector to a slice of Expr.
959// This function properly manages the reference count of the vector by
960// incrementing it on entry and decrementing it on exit.
961// The individual AST elements are already reference counted by newExpr.
962func astVectorToExprs(ctx *Context, vec C.Z3_ast_vector) []*Expr {
963 // Increment reference count for the vector since we're using it
964 C.Z3_ast_vector_inc_ref(ctx.ptr, vec)
965 defer C.Z3_ast_vector_dec_ref(ctx.ptr, vec)
967 size := uint(C.Z3_ast_vector_size(ctx.ptr, vec))
968 result := make([]*Expr, size)
969 for i := uint(0); i < size; i++ {
970 result[i] = newExpr(ctx, C.Z3_ast_vector_get(ctx.ptr, vec, C.uint(i)))