Z3
NativeSolver.cs
Go to the documentation of this file.
1/*++
2Copyright (c) 2012 Microsoft Corporation
3
4Module Name:
5
6 NativeSolver.cs
7
8Abstract:
9
10 Z3 Managed API: Native Solver
11
12Author:
13
14 Christoph Wintersteiger (cwinter) 2012-03-22
15 Nikolaj Bjorner (nbjorner) 2022-03-01
16
17Notes:
18
19--*/
20
21using System;
22using System.Diagnostics;
23using System.Collections.Generic;
24using System.Linq;
25
26namespace Microsoft.Z3
27{
28
29 using Z3_ast = System.IntPtr;
30 using Z3_context = System.IntPtr;
31 using Z3_func_decl = System.IntPtr;
32 using Z3_params = System.IntPtr;
33 using Z3_solver = System.IntPtr;
34 using Z3_sort = System.IntPtr;
35 using Z3_symbol = System.IntPtr;
36
41 {
45 public string Help => Native.Z3_solver_get_help(nCtx, z3solver);
46
47 private void SetParam(Action<Z3_params> setter)
48 {
49 Z3_params p = Native.Z3_mk_params(nCtx);
50 Native.Z3_params_inc_ref(nCtx, p);
51 setter(p);
52 Native.Z3_solver_set_params(nCtx, z3solver, p);
53 Native.Z3_params_dec_ref(nCtx, p);
54 }
55
59 public void Set(string name, bool value)
60 {
61 SetParam((Z3_params p) => Native.Z3_params_set_bool(nCtx, p, Native.Z3_mk_string_symbol(nCtx, name), (byte)(value ? 1 : 0)));
62 }
63
67 public void Set(string name, uint value)
68 {
69 SetParam((Z3_params p) => Native.Z3_params_set_uint(nCtx, p, Native.Z3_mk_string_symbol(nCtx, name), value));
70 }
71
75 public void Set(string name, double value)
76 {
77 SetParam((Z3_params p) => Native.Z3_params_set_double(nCtx, p, Native.Z3_mk_string_symbol(nCtx, name), value));
78 }
79
83 public void Set(string name, string value)
84 {
85 var value_sym = Native.Z3_mk_string_symbol(nCtx, value);
86 SetParam((Z3_params p) => Native.Z3_params_set_symbol(nCtx, p, Native.Z3_mk_string_symbol(nCtx, name), value_sym));
87 }
88
89#if false
93 public void Set(string name, Symbol value) { Parameters = Context.MkParams().Add(name, value); }
97 public void Set(Symbol name, bool value) { Parameters = Context.MkParams().Add(name, value); }
101 public void Set(Symbol name, uint value) { Parameters = Context.MkParams().Add(name, value); }
105 public void Set(Symbol name, double value) { Parameters = Context.MkParams().Add(name, value); }
109 public void Set(Symbol name, string value) { Parameters = Context.MkParams().Add(name, value); }
113 public void Set(Symbol name, Symbol value) { Parameters = Context.MkParams().Add(name, value); }
114
118 public ParamDescrs ParameterDescriptions
119 {
120 get { return new ParamDescrs(Context, Native.Z3_solver_get_param_descrs(nCtx, NativeObject)); }
121 }
122#endif
123
129 public uint NumScopes => Native.Z3_solver_get_num_scopes(nCtx, z3solver);
130
135 public void Push() => Native.Z3_solver_push(nCtx, z3solver);
136
142 public void Pop(uint n = 1) => Native.Z3_solver_pop(nCtx, z3solver, n);
143
148 public void Reset() => Native.Z3_solver_reset(nCtx, z3solver);
149
153 public void Assert(params Z3_ast[] constraints)
154 {
155 Debug.Assert(constraints != null);
156 Debug.Assert(constraints.All(c => c != IntPtr.Zero));
157
158 foreach (Z3_ast a in constraints)
159 {
160 Native.Z3_solver_assert(nCtx, z3solver, a);
161 }
162 }
163
167 public void Add(params Z3_ast[] constraints) => Assert(constraints);
168
172 public void Add(IEnumerable<Z3_ast> constraints) => Assert(constraints.ToArray());
173
184 {
185 uint arity = Native.Z3_get_arity(nCtx, f);
186 Z3_sort range = Native.Z3_get_range(nCtx, f);
187 Z3_ast[] vars = new Z3_ast[arity];
188 Z3_sort[] sorts = new Z3_sort[arity];
189 Z3_symbol[] names = new Z3_symbol[arity];
190 for (uint i = 0; i < arity; ++i)
191 {
192 Z3_sort domain = Native.Z3_get_domain(nCtx, f, i);
193 vars[i] = ntvContext.MkBound(arity - i - 1, domain);
194 sorts[i] = domain;
195 names[i] = Native.Z3_mk_int_symbol(nCtx, (int)i);
196 }
197 Z3_ast app_f = IntPtr.Zero; // Context.MkApp(f, vars);
198 for (uint i = 0; i < arity; ++i)
199 {
200 Z3_sort domain = Native.Z3_get_domain(nCtx, f, i);
201 Z3_func_decl proj = ntvContext.MkFreshFuncDecl("inv", new Z3_sort[] { range }, domain);
202 Z3_ast body = ntvContext.MkEq(vars[i], ntvContext.MkApp(proj, app_f));
203 Z3_ast q = ntvContext.MkForall(names, sorts, body);
204 Assert(q);
205 }
206 }
207
219 public void AssertAndTrack(Z3_ast[] constraints, Z3_ast[] ps)
220 {
221 Debug.Assert(constraints != null);
222 Debug.Assert(constraints.All(c => c != IntPtr.Zero));
223 Debug.Assert(ps.All(c => c != IntPtr.Zero));
224 if (constraints.Length != ps.Length)
225 throw new Z3Exception("Argument size mismatch");
226
227 for (int i = 0; i < constraints.Length; i++)
228 Native.Z3_solver_assert_and_track(nCtx, z3solver, constraints[i], ps[i]);
229 }
230
242 public void AssertAndTrack(Z3_ast constraint, Z3_ast p)
243 {
244 Debug.Assert(constraint != null);
245 Debug.Assert(p != null);
246
247 Native.Z3_solver_assert_and_track(nCtx, z3solver, constraint, p);
248 }
249
253 public void FromFile(string file)
254 => Native.Z3_solver_from_file(nCtx, z3solver, file);
255
259 public void FromString(string str)
260 => Native.Z3_solver_from_string(nCtx, z3solver, str);
261
265 public uint NumAssertions
266 => (uint)ntvContext.ToArray(Native.Z3_solver_get_assertions(nCtx, z3solver)).Length;
267
272 => ntvContext.ToArray(Native.Z3_solver_get_assertions(nCtx, z3solver));
273
277 public Z3_ast[] Units
278 => ntvContext.ToArray(Native.Z3_solver_get_units(nCtx, z3solver));
279
288 public Status Check(params Z3_ast[] assumptions)
289 {
290 Z3_lbool r;
291 if (assumptions == null || assumptions.Length == 0)
292 r = (Z3_lbool)Native.Z3_solver_check(nCtx, z3solver);
293 else
294 r = (Z3_lbool)Native.Z3_solver_check_assumptions(nCtx, z3solver, (uint)assumptions.Length, assumptions);
295 return lboolToStatus(r);
296 }
297
306 public Status Check(IEnumerable<Z3_ast> assumptions)
307 {
308 Z3_lbool r;
309 Z3_ast[] asms = assumptions.ToArray();
310 if (asms.Length == 0)
311 r = (Z3_lbool)Native.Z3_solver_check(nCtx, z3solver);
312 else
313 r = (Z3_lbool)Native.Z3_solver_check_assumptions(nCtx, z3solver, (uint)asms.Length, asms);
314 return lboolToStatus(r);
315 }
316
325 {
326 get
327 {
328 IntPtr x = Native.Z3_solver_get_model(nCtx, z3solver);
329 return x == IntPtr.Zero
330 ? null
331 : new NativeModel(ntvContext, x);
332 }
333 }
334
343 => Native.Z3_solver_get_proof(nCtx, z3solver);
344
354 => ntvContext.ToArray(Native.Z3_solver_get_unsat_core(nCtx, z3solver));
355
359 public string ReasonUnknown
360 => Native.Z3_solver_get_reason_unknown(nCtx, z3solver);
361
366 {
367 Debug.Assert(ctx != null);
368 return new NativeSolver(ctx, Native.Z3_solver_translate(nCtx, z3solver, ctx.nCtx));
369 }
370
375 {
376 Debug.Assert(src != null);
377
378 Native.Z3_solver_import_model_converter(nCtx, src.z3solver, z3solver);
379 }
380
385 {
386 get
387 {
388 var stats = Native.Z3_solver_get_statistics(nCtx, z3solver);
389 return ntvContext.GetStatistics(stats);
390 }
391 }
392
396 public override string ToString()
397 {
398 return Native.Z3_solver_to_string(nCtx, z3solver);
399 }
400
401 #region Internal
402 readonly NativeContext ntvContext;
403 Z3_solver z3solver;
404 Z3_context nCtx => ntvContext.nCtx;
405
406 internal NativeSolver(NativeContext nativeCtx, Z3_solver z3solver)
407 {
408 Debug.Assert(nativeCtx != null);
409 Debug.Assert(z3solver != IntPtr.Zero);
410
411 this.ntvContext = nativeCtx;
412 this.z3solver = z3solver;
413
414 Native.Z3_solver_inc_ref(nCtx, z3solver);
415 }
416
420 ~NativeSolver()
421 {
422 Dispose();
423 }
424
428 public void Dispose()
429 {
430 if (z3solver != IntPtr.Zero)
431 {
432 Native.Z3_solver_dec_ref(nCtx, z3solver);
433 z3solver = IntPtr.Zero;
434 }
435 GC.SuppressFinalize(this);
436 }
437
438
439 private Status lboolToStatus(Z3_lbool r)
440 {
441 switch (r)
442 {
443 case Z3_lbool.Z3_L_TRUE: return Status.SATISFIABLE;
444 case Z3_lbool.Z3_L_FALSE: return Status.UNSATISFIABLE;
445 default: return Status.UNKNOWN;
446 }
447 }
448
449 #endregion
450 }
451}
The main interaction with Z3 happens via the Context.
Definition: Context.cs:34
Params MkParams()
Creates a new ParameterSet.
Definition: Context.cs:3458
A Model contains interpretations (assignments) of constants and functions.
Definition: Model.cs:30
The main interaction with Z3 happens via the Context. NativeContext allows for efficient wrapper-redu...
Z3_ast MkApp(Z3_func_decl f, params Z3_ast[] args)
Create a new function application.
Z3_ast MkEq(Z3_ast x, Z3_ast y)
Creates the equality x = y .
Statistics.Entry[] GetStatistics(Z3_stats stats)
Retrieve statistics as an array of entries
Z3_ast MkBound(uint index, Z3_sort sort)
Creates a new bound variable.
Z3_ast MkForall(Z3_sort[] sorts, Z3_symbol[] names, Z3_ast body, uint weight=1, Z3_ast[] patterns=null, Z3_ast[] noPatterns=null, Symbol quantifierID=null, Symbol skolemID=null)
Create a universal Quantifier.
Z3_func_decl MkFreshFuncDecl(string prefix, Z3_sort[] domain, Z3_sort range)
Creates a fresh function declaration with a name prefixed with prefix .
A Model contains interpretations (assignments) of constants and functions.
Definition: NativeModel.cs:37
void Set(string name, uint value)
Sets parameter on the solver
Definition: NativeSolver.cs:67
void ImportModelConverter(NativeSolver src)
Import model converter from other solver.
Status Check(params Z3_ast[] assumptions)
Checks whether the assertions in the solver are consistent or not.
void Set(string name, double value)
Sets parameter on the solver
Definition: NativeSolver.cs:75
string Help
A string that describes all available solver parameters.
Definition: NativeSolver.cs:45
void Set(string name, string value)
Sets parameter on the solver
Definition: NativeSolver.cs:83
void Pop(uint n=1)
Backtracks n backtracking points.
void Add(IEnumerable< Z3_ast > constraints)
Alias for Assert.
void Reset()
Resets the Solver.
void Add(params Z3_ast[] constraints)
Alias for Assert.
void Push()
Creates a backtracking point.
void FromString(string str)
Load solver assertions from a string.
uint NumAssertions
The number of assertions in the solver.
Z3_ast[] UnsatCore
The unsat core of the last Check.
uint NumScopes
The current number of backtracking points (scopes).
void Dispose()
Disposes of the underlying native Z3 object.
void AssertAndTrack(Z3_ast[] constraints, Z3_ast[] ps)
Assert multiple constraints into the solver, and track them (in the unsat) core using the Boolean con...
Z3_ast Proof
The proof of the last Check(params Expr[] assumptions).
void Assert(params Z3_ast[] constraints)
Assert a constraint (or multiple) into the solver.
Z3_ast[] Assertions
The set of asserted formulas.
override string ToString()
A string representation of the solver.
void FromFile(string file)
Load solver assertions from a file.
void Set(string name, bool value)
Sets parameter on the solver
Definition: NativeSolver.cs:59
string ReasonUnknown
A brief justification of why the last call to Check returned UNKNOWN.
Status Check(IEnumerable< Z3_ast > assumptions)
Checks whether the assertions in the solver are consistent or not.
void AssertAndTrack(Z3_ast constraint, Z3_ast p)
Assert a constraint into the solver, and track it (in the unsat) core using the Boolean constant p.
void AssertInjective(Z3_func_decl f)
Add constraints to ensure the function f can only be injective. Example: for function f : D1 x D2 -> ...
NativeSolver Translate(NativeContext ctx)
Create a clone of the current solver with respect to ctx.
Z3_ast[] Units
Currently inferred units.
Params Add(Symbol name, bool value)
Adds a parameter setting.
Definition: Params.cs:33
Statistical data is organized into pairs of [Key, Entry], where every Entry is either a DoubleEntry o...
Definition: Statistics.cs:39
Objects of this class track statistical information about solvers.
Definition: Statistics.cs:33
Symbols are used to name several term and type constructors.
Definition: Symbol.cs:30
The exception base class for error reporting from Z3
Definition: Z3Exception.cs:32
Z3_lbool
Lifted Boolean type: false, undefined, true.
Definition: z3_api.h:60
System.IntPtr Z3_params
Definition: NativeSolver.cs:32
System.IntPtr Z3_context
Definition: Context.cs:29
Status
Status values.
Definition: Status.cs:29
System.IntPtr Z3_func_decl
System.IntPtr Z3_ast
System.IntPtr Z3_solver
System.IntPtr Z3_sort
System.IntPtr Z3_symbol
expr range(expr const &lo, expr const &hi)
Definition: z3++.h:3946
def Length(s)
Definition: z3py.py:11063