1// Copyright (c) Microsoft Corporation 2025
2// Z3 Go API: Fixedpoint solver for Datalog and CHC (Constrained Horn Clauses)
16// Fixedpoint represents a fixedpoint solver for Datalog/CHC queries
17type Fixedpoint struct {
22// newFixedpoint creates a new Fixedpoint solver with proper memory management
23func newFixedpoint(ctx *Context, ptr C.Z3_fixedpoint) *Fixedpoint {
24 fp := &Fixedpoint{ctx: ctx, ptr: ptr}
25 C.Z3_fixedpoint_inc_ref(ctx.ptr, ptr)
26 runtime.SetFinalizer(fp, func(f *Fixedpoint) {
27 C.Z3_fixedpoint_dec_ref(f.ctx.ptr, f.ptr)
32// NewFixedpoint creates a new fixedpoint solver
33func (ctx *Context) NewFixedpoint() *Fixedpoint {
34 ptr := C.Z3_mk_fixedpoint(ctx.ptr)
35 return newFixedpoint(ctx, ptr)
38// GetHelp returns a string describing all available fixedpoint solver parameters
39func (f *Fixedpoint) GetHelp() string {
40 cstr := C.Z3_fixedpoint_get_help(f.ctx.ptr, f.ptr)
41 return C.GoString(cstr)
44// SetParams sets the fixedpoint solver parameters
45func (f *Fixedpoint) SetParams(params *Params) {
46 C.Z3_fixedpoint_set_params(f.ctx.ptr, f.ptr, params.ptr)
49// GetParamDescrs retrieves parameter descriptions for the fixedpoint solver
50func (f *Fixedpoint) GetParamDescrs() *ParamDescrs {
51 ptr := C.Z3_fixedpoint_get_param_descrs(f.ctx.ptr, f.ptr)
52 return newParamDescrs(f.ctx, ptr)
55// Assert adds a constraint into the fixedpoint solver
56func (f *Fixedpoint) Assert(constraint *Expr) {
57 C.Z3_fixedpoint_assert(f.ctx.ptr, f.ptr, constraint.ptr)
60// RegisterRelation registers a predicate as a recursive relation
61func (f *Fixedpoint) RegisterRelation(funcDecl *FuncDecl) {
62 C.Z3_fixedpoint_register_relation(f.ctx.ptr, f.ptr, funcDecl.ptr)
65// AddRule adds a rule (Horn clause) to the fixedpoint solver
66// The rule should be an implication of the form body => head
67// where head is a relation and body is a conjunction of relations
68func (f *Fixedpoint) AddRule(rule *Expr, name *Symbol) {
69 var namePtr C.Z3_symbol
75 C.Z3_fixedpoint_add_rule(f.ctx.ptr, f.ptr, rule.ptr, namePtr)
78// AddFact adds a table fact to the fixedpoint solver
79func (f *Fixedpoint) AddFact(pred *FuncDecl, args []int) {
81 C.Z3_fixedpoint_add_fact(f.ctx.ptr, f.ptr, pred.ptr, 0, nil)
85 cArgs := make([]C.uint, len(args))
86 for i, arg := range args {
87 cArgs[i] = C.uint(arg)
89 C.Z3_fixedpoint_add_fact(f.ctx.ptr, f.ptr, pred.ptr, C.uint(len(args)), &cArgs[0])
92// Query queries the fixedpoint solver with a constraint
93// Returns Satisfiable if there is a derivation, Unsatisfiable if not
94func (f *Fixedpoint) Query(query *Expr) Status {
95 result := C.Z3_fixedpoint_query(f.ctx.ptr, f.ptr, query.ptr)
106// QueryRelations queries the fixedpoint solver with an array of relations
107// Returns Satisfiable if any relation is non-empty, Unsatisfiable otherwise
108func (f *Fixedpoint) QueryRelations(relations []*FuncDecl) Status {
109 if len(relations) == 0 {
113 cRelations := make([]C.Z3_func_decl, len(relations))
114 for i, rel := range relations {
115 cRelations[i] = rel.ptr
118 result := C.Z3_fixedpoint_query_relations(f.ctx.ptr, f.ptr, C.uint(len(relations)), &cRelations[0])
129// UpdateRule updates a named rule in the fixedpoint solver
130func (f *Fixedpoint) UpdateRule(rule *Expr, name *Symbol) {
131 var namePtr C.Z3_symbol
137 C.Z3_fixedpoint_update_rule(f.ctx.ptr, f.ptr, rule.ptr, namePtr)
140// GetAnswer retrieves the satisfying instance or instances of solver,
141// or definitions for the recursive predicates that show unsatisfiability
142func (f *Fixedpoint) GetAnswer() *Expr {
143 ptr := C.Z3_fixedpoint_get_answer(f.ctx.ptr, f.ptr)
147 return newExpr(f.ctx, ptr)
150// GetReasonUnknown retrieves explanation why fixedpoint engine returned status Unknown
151func (f *Fixedpoint) GetReasonUnknown() string {
152 cstr := C.Z3_fixedpoint_get_reason_unknown(f.ctx.ptr, f.ptr)
153 return C.GoString(cstr)
156// GetNumLevels retrieves the number of levels explored for a given predicate
157func (f *Fixedpoint) GetNumLevels(predicate *FuncDecl) int {
158 return int(C.Z3_fixedpoint_get_num_levels(f.ctx.ptr, f.ptr, predicate.ptr))
161// GetCoverDelta retrieves the cover delta for a given predicate and level
162func (f *Fixedpoint) GetCoverDelta(level int, predicate *FuncDecl) *Expr {
163 ptr := C.Z3_fixedpoint_get_cover_delta(f.ctx.ptr, f.ptr, C.int(level), predicate.ptr)
167 return newExpr(f.ctx, ptr)
170// AddCover adds a cover constraint to a predicate at a given level
171func (f *Fixedpoint) AddCover(level int, predicate *FuncDecl, property *Expr) {
172 C.Z3_fixedpoint_add_cover(f.ctx.ptr, f.ptr, C.int(level), predicate.ptr, property.ptr)
175// String returns the string representation of the fixedpoint solver
176func (f *Fixedpoint) String() string {
177 cstr := C.Z3_fixedpoint_to_string(f.ctx.ptr, f.ptr, 0, nil)
178 return C.GoString(cstr)
181// GetStatistics retrieves statistics for the fixedpoint solver
182func (f *Fixedpoint) GetStatistics() *Statistics {
183 ptr := C.Z3_fixedpoint_get_statistics(f.ctx.ptr, f.ptr)
184 return newStatistics(f.ctx, ptr)
187// GetRules retrieves the current rules as a string
188func (f *Fixedpoint) GetRules() string {
192// GetAssertions retrieves the fixedpoint assertions as an AST vector
193func (f *Fixedpoint) GetAssertions() *ASTVector {
194 ptr := C.Z3_fixedpoint_get_assertions(f.ctx.ptr, f.ptr)
195 return newASTVector(f.ctx, ptr)
198// SetPredicateRepresentation sets the predicate representation for a given relation
199func (f *Fixedpoint) SetPredicateRepresentation(funcDecl *FuncDecl, kinds []C.Z3_symbol) {
201 C.Z3_fixedpoint_set_predicate_representation(f.ctx.ptr, f.ptr, funcDecl.ptr, 0, nil)
204 C.Z3_fixedpoint_set_predicate_representation(f.ctx.ptr, f.ptr, funcDecl.ptr, C.uint(len(kinds)), &kinds[0])
207// FromString parses a Datalog program from a string
208func (f *Fixedpoint) FromString(s string) {
210 defer C.free(unsafe.Pointer(cstr))
211 C.Z3_fixedpoint_from_string(f.ctx.ptr, f.ptr, cstr)
214// FromFile parses a Datalog program from a file
215func (f *Fixedpoint) FromFile(filename string) {
216 cstr := C.CString(filename)
217 defer C.free(unsafe.Pointer(cstr))
218 C.Z3_fixedpoint_from_file(f.ctx.ptr, f.ptr, cstr)
221// Statistics represents statistics for Z3 solvers
222type Statistics struct {
227// newStatistics creates a new Statistics object with proper memory management
228func newStatistics(ctx *Context, ptr C.Z3_stats) *Statistics {
229 stats := &Statistics{ctx: ctx, ptr: ptr}
230 C.Z3_stats_inc_ref(ctx.ptr, ptr)
231 runtime.SetFinalizer(stats, func(s *Statistics) {
232 C.Z3_stats_dec_ref(s.ctx.ptr, s.ptr)
237// String returns the string representation of statistics
238func (s *Statistics) String() string {
239 cstr := C.Z3_stats_to_string(s.ctx.ptr, s.ptr)
240 return C.GoString(cstr)
243// Size returns the number of statistical data entries
244func (s *Statistics) Size() int {
245 return int(C.Z3_stats_size(s.ctx.ptr, s.ptr))
248// GetKey returns the key (name) of a statistical data entry at the given index
249func (s *Statistics) GetKey(idx int) string {
250 cstr := C.Z3_stats_get_key(s.ctx.ptr, s.ptr, C.uint(idx))
251 return C.GoString(cstr)
254// IsUint returns true if the statistical data at the given index is unsigned integer
255func (s *Statistics) IsUint(idx int) bool {
256 return bool(C.Z3_stats_is_uint(s.ctx.ptr, s.ptr, C.uint(idx)))
259// IsDouble returns true if the statistical data at the given index is double
260func (s *Statistics) IsDouble(idx int) bool {
261 return bool(C.Z3_stats_is_double(s.ctx.ptr, s.ptr, C.uint(idx)))
264// GetUintValue returns the unsigned integer value at the given index
265func (s *Statistics) GetUintValue(idx int) uint64 {
266 return uint64(C.Z3_stats_get_uint_value(s.ctx.ptr, s.ptr, C.uint(idx)))
269// GetDoubleValue returns the double value at the given index
270func (s *Statistics) GetDoubleValue(idx int) float64 {
271 return float64(C.Z3_stats_get_double_value(s.ctx.ptr, s.ptr, C.uint(idx)))