Z3
Solver.cs
Go to the documentation of this file.
1/*++
2Copyright (c) 2012 Microsoft Corporation
3
4Module Name:
5
6 Solver.cs
7
8Abstract:
9
10 Z3 Managed API: Solvers
11
12Author:
13
14 Christoph Wintersteiger (cwinter) 2012-03-22
15
16Notes:
17
18--*/
19
20using System;
21using System.Diagnostics;
22using System.Linq;
23using System.Collections.Generic;
24
25namespace Microsoft.Z3
26{
30 public class Solver : Z3Object
31 {
35 public string Help
36 {
37 get
38 {
39
40 return Native.Z3_solver_get_help(Context.nCtx, NativeObject);
41 }
42 }
43
48 {
49 set
50 {
51 Debug.Assert(value != null);
52
53 Context.CheckContextMatch(value);
54 Native.Z3_solver_set_params(Context.nCtx, NativeObject, value.NativeObject);
55 }
56 }
57
61 public void Set(string name, bool value)
62 {
63 using var parameters = Context.MkParams().Add(name, value);
64 Parameters = parameters;
65 }
66
70 public void Set(string name, uint value)
71 {
72 using var parameters = Context.MkParams().Add(name, value);
73 Parameters = parameters;
74 }
75
79 public void Set(string name, double value)
80 {
81 using var parameters = Context.MkParams().Add(name, value);
82 Parameters = parameters;
83 }
84
88 public void Set(string name, string value)
89 {
90 using var parameters = Context.MkParams().Add(name, value);
91 Parameters = parameters;
92 }
93
97 public void Set(string name, Symbol value)
98 {
99 using var parameters = Context.MkParams().Add(name, value);
100 Parameters = parameters;
101 }
102
106 public void Set(Symbol name, bool value)
107 {
108 using var parameters = Context.MkParams().Add(name, value);
109 Parameters = parameters;
110 }
111
115 public void Set(Symbol name, uint value)
116 {
117 using var parameters = Context.MkParams().Add(name, value);
118 Parameters = parameters;
119 }
120
124 public void Set(Symbol name, double value)
125 {
126 using var parameters = Context.MkParams().Add(name, value);
127 Parameters = parameters;
128 }
129
133 public void Set(Symbol name, string value)
134 {
135 using var parameters = Context.MkParams().Add(name, value);
136 Parameters = parameters;
137 }
138
142 public void Set(Symbol name, Symbol value)
143 {
144 using var parameters = Context.MkParams().Add(name, value);
145 Parameters = parameters;
146 }
147
148
149
154 {
155 get { return new ParamDescrs(Context, Native.Z3_solver_get_param_descrs(Context.nCtx, NativeObject)); }
156 }
157
158
164 public uint NumScopes
165 {
166 get { return Native.Z3_solver_get_num_scopes(Context.nCtx, NativeObject); }
167 }
168
173 public void Push()
174 {
175 Native.Z3_solver_push(Context.nCtx, NativeObject);
176 }
177
183 public void Pop(uint n = 1)
184 {
185 Native.Z3_solver_pop(Context.nCtx, NativeObject, n);
186 }
187
192 public void Reset()
193 {
194 Native.Z3_solver_reset(Context.nCtx, NativeObject);
195 }
196
200 public void Assert(params BoolExpr[] constraints)
201 {
202 Debug.Assert(constraints != null);
203 Debug.Assert(constraints.All(c => c != null));
204
205 Context.CheckContextMatch<BoolExpr>(constraints);
206 foreach (BoolExpr a in constraints)
207 {
208 Native.Z3_solver_assert(Context.nCtx, NativeObject, a.NativeObject);
209 }
210 }
211
215 public void Add(params BoolExpr[] constraints)
216 {
217 Assert(constraints);
218 }
219
223 public void Add(IEnumerable<BoolExpr> constraints)
224 {
225 Assert(constraints.ToArray());
226 }
227
239 public void AssertAndTrack(BoolExpr[] constraints, BoolExpr[] ps)
240 {
241 Debug.Assert(constraints != null);
242 Debug.Assert(constraints.All(c => c != null));
243 Debug.Assert(ps.All(c => c != null));
244 Context.CheckContextMatch<BoolExpr>(constraints);
245 Context.CheckContextMatch<BoolExpr>(ps);
246 if (constraints.Length != ps.Length)
247 throw new Z3Exception("Argument size mismatch");
248
249 for (int i = 0 ; i < constraints.Length; i++)
250 Native.Z3_solver_assert_and_track(Context.nCtx, NativeObject, constraints[i].NativeObject, ps[i].NativeObject);
251 }
252
264 public void AssertAndTrack(BoolExpr constraint, BoolExpr p)
265 {
266 Debug.Assert(constraint != null);
267 Debug.Assert(p != null);
268 Context.CheckContextMatch(constraint);
269 Context.CheckContextMatch(p);
270
271 Native.Z3_solver_assert_and_track(Context.nCtx, NativeObject, constraint.NativeObject, p.NativeObject);
272 }
273
277 public void FromFile(string file)
278 {
279 Native.Z3_solver_from_file(Context.nCtx, NativeObject, file);
280 }
281
285 public void FromString(string str)
286 {
287 Native.Z3_solver_from_string(Context.nCtx, NativeObject, str);
288 }
289
293 public uint NumAssertions
294 {
295 get
296 {
297 using ASTVector assertions = new ASTVector(Context, Native.Z3_solver_get_assertions(Context.nCtx, NativeObject));
298 return assertions.Size;
299 }
300 }
301
306 {
307 get
308 {
309
310 using ASTVector assertions = new ASTVector(Context, Native.Z3_solver_get_assertions(Context.nCtx, NativeObject));
311 return assertions.ToBoolExprArray();
312 }
313 }
314
319 {
320 get
321 {
322
323 using ASTVector assertions = new ASTVector(Context, Native.Z3_solver_get_units(Context.nCtx, NativeObject));
324 return assertions.ToBoolExprArray();
325 }
326 }
327
336 public Status Check(params Expr[] assumptions)
337 {
338 Z3_lbool r;
339 if (assumptions == null || assumptions.Length == 0)
340 r = (Z3_lbool)Native.Z3_solver_check(Context.nCtx, NativeObject);
341 else
342 r = (Z3_lbool)Native.Z3_solver_check_assumptions(Context.nCtx, NativeObject, (uint)assumptions.Length, AST.ArrayToNative(assumptions));
343 return lboolToStatus(r);
344 }
345
354 public Status Check(IEnumerable<BoolExpr> assumptions)
355 {
356 Z3_lbool r;
357 BoolExpr[] asms = assumptions.ToArray();
358 if (asms.Length == 0)
359 r = (Z3_lbool)Native.Z3_solver_check(Context.nCtx, NativeObject);
360 else
361 r = (Z3_lbool)Native.Z3_solver_check_assumptions(Context.nCtx, NativeObject, (uint)asms.Length, AST.ArrayToNative(asms));
362 return lboolToStatus(r);
363 }
364
380 public Status Consequences(IEnumerable<BoolExpr> assumptions, IEnumerable<Expr> variables, out BoolExpr[] consequences)
381 {
382 using ASTVector result = new ASTVector(Context);
383 using ASTVector asms = new ASTVector(Context);
384 using ASTVector vars = new ASTVector(Context);
385 foreach (var asm in assumptions) asms.Push(asm);
386 foreach (var v in variables) vars.Push(v);
387 Z3_lbool r = (Z3_lbool)Native.Z3_solver_get_consequences(Context.nCtx, NativeObject, asms.NativeObject, vars.NativeObject, result.NativeObject);
388 consequences = result.ToBoolExprArray();
389 return lboolToStatus(r);
390 }
391
400 {
401 get
402 {
403 IntPtr x = Native.Z3_solver_get_model(Context.nCtx, NativeObject);
404 if (x == IntPtr.Zero)
405 return null;
406 else
407 return new Model(Context, x);
408 }
409 }
410
418 public Expr Proof
419 {
420 get
421 {
422 IntPtr x = Native.Z3_solver_get_proof(Context.nCtx, NativeObject);
423 if (x == IntPtr.Zero)
424 return null;
425 else
426 return Expr.Create(Context, x);
427 }
428 }
429
439 {
440 get
441 {
442
443 using ASTVector core = new ASTVector(Context, Native.Z3_solver_get_unsat_core(Context.nCtx, NativeObject));
444 return core.ToBoolExprArray();
445 }
446 }
447
451 public string ReasonUnknown
452 {
453 get
454 {
455
456 return Native.Z3_solver_get_reason_unknown(Context.nCtx, NativeObject);
457 }
458 }
459
463 public uint BacktrackLevel { get; set; }
464
468 public BoolExpr[] CubeVariables { get; set; }
469
470
474 public IEnumerable<BoolExpr[]> Cube()
475 {
476 using ASTVector cv = new ASTVector(Context);
477 if (CubeVariables != null)
478 foreach (var b in CubeVariables) cv.Push(b);
479
480 while (true) {
481 var lvl = BacktrackLevel;
482 BacktrackLevel = uint.MaxValue;
483 using ASTVector r = new ASTVector(Context, Native.Z3_solver_cube(Context.nCtx, NativeObject, cv.NativeObject, lvl));
484 var v = r.ToBoolExprArray();
485 CubeVariables = cv.ToBoolExprArray();
486 if (v.Length == 1 && v[0].IsFalse) {
487 break;
488 }
489 yield return v;
490 if (v.Length == 0) {
491 break;
492 }
493 }
494 }
495
500 {
501 Debug.Assert(ctx != null);
502 return new Solver(ctx, Native.Z3_solver_translate(Context.nCtx, NativeObject, ctx.nCtx));
503 }
504
508 public void ImportModelConverter(Solver src)
509 {
510 Native.Z3_solver_import_model_converter(Context.nCtx, src.NativeObject, NativeObject);
511 }
512
517 {
518 get
519 {
520
521 return new Statistics(Context, Native.Z3_solver_get_statistics(Context.nCtx, NativeObject));
522 }
523 }
524
528 public override string ToString()
529 {
530 return Native.Z3_solver_to_string(Context.nCtx, NativeObject);
531 }
532
533 #region Internal
534 internal Solver(Context ctx, IntPtr obj)
535 : base(ctx, obj)
536 {
537 Debug.Assert(ctx != null);
538 this.BacktrackLevel = uint.MaxValue;
539 }
540
541 internal class DecRefQueue : IDecRefQueue
542 {
543 public DecRefQueue() : base() { }
544 public DecRefQueue(uint move_limit) : base(move_limit) { }
545 internal override void IncRef(Context ctx, IntPtr obj)
546 {
547 Native.Z3_solver_inc_ref(ctx.nCtx, obj);
548 }
549
550 internal override void DecRef(Context ctx, IntPtr obj)
551 {
552 Native.Z3_solver_dec_ref(ctx.nCtx, obj);
553 }
554 };
555
556 internal override void IncRef(IntPtr o)
557 {
558 Context.Solver_DRQ.IncAndClear(Context, o);
559 base.IncRef(o);
560 }
561
562 internal override void DecRef(IntPtr o)
563 {
564 Context.Solver_DRQ.Add(o);
565 base.DecRef(o);
566 }
567
568 private Status lboolToStatus(Z3_lbool r)
569 {
570 switch (r)
571 {
572 case Z3_lbool.Z3_L_TRUE: return Status.SATISFIABLE;
573 case Z3_lbool.Z3_L_FALSE: return Status.UNSATISFIABLE;
574 default: return Status.UNKNOWN;
575 }
576 }
577
578 #endregion
579 }
580}
The abstract syntax tree (AST) class.
Definition: AST.cs:31
Vectors of ASTs.
Definition: ASTVector.cs:29
BoolExpr[] ToBoolExprArray()
Translates an ASTVector into a BoolExpr[]
Definition: ASTVector.cs:127
uint Size
The size of the vector
Definition: ASTVector.cs:34
Boolean expressions
Definition: BoolExpr.cs:32
The main interaction with Z3 happens via the Context.
Definition: Context.cs:34
Params MkParams()
Creates a new ParameterSet.
Definition: Context.cs:3458
IDecRefQueue Solver_DRQ
Solver DRQ
Definition: Context.cs:4948
Expressions are terms.
Definition: Expr.cs:31
A Model contains interpretations (assignments) of constants and functions.
Definition: Model.cs:30
A ParamDescrs describes a set of parameters.
Definition: ParamDescrs.cs:29
A Params objects represents a configuration in the form of Symbol/value pairs.
Definition: Params.cs:29
Params Add(Symbol name, bool value)
Adds a parameter setting.
Definition: Params.cs:33
void Set(string name, uint value)
Sets parameter on the solver
Definition: Solver.cs:70
BoolExpr[] Assertions
The set of asserted formulas.
Definition: Solver.cs:306
void Set(string name, double value)
Sets parameter on the solver
Definition: Solver.cs:79
Model Model
The model of the last Check(params Expr[] assumptions).
Definition: Solver.cs:400
string Help
A string that describes all available solver parameters.
Definition: Solver.cs:36
void Set(string name, string value)
Sets parameter on the solver
Definition: Solver.cs:88
void Pop(uint n=1)
Backtracks n backtracking points.
Definition: Solver.cs:183
Solver Translate(Context ctx)
Create a clone of the current solver with respect to ctx.
Definition: Solver.cs:499
void Add(IEnumerable< BoolExpr > constraints)
Alias for Assert.
Definition: Solver.cs:223
void Reset()
Resets the Solver.
Definition: Solver.cs:192
void AssertAndTrack(BoolExpr constraint, BoolExpr p)
Assert a constraint into the solver, and track it (in the unsat) core using the Boolean constant p.
Definition: Solver.cs:264
void Set(Symbol name, Symbol value)
Sets parameter on the solver
Definition: Solver.cs:142
void Push()
Creates a backtracking point.
Definition: Solver.cs:173
void Assert(params BoolExpr[] constraints)
Assert a constraint (or multiple) into the solver.
Definition: Solver.cs:200
IEnumerable< BoolExpr[]> Cube()
Return a set of cubes.
Definition: Solver.cs:474
void FromString(string str)
Load solver assertions from a string.
Definition: Solver.cs:285
uint NumAssertions
The number of assertions in the solver.
Definition: Solver.cs:294
Params Parameters
Sets the solver parameters.
Definition: Solver.cs:48
Statistics Statistics
Solver statistics.
Definition: Solver.cs:517
uint NumScopes
The current number of backtracking points (scopes).
Definition: Solver.cs:165
Status Check(IEnumerable< BoolExpr > assumptions)
Checks whether the assertions in the solver are consistent or not.
Definition: Solver.cs:354
void Set(Symbol name, string value)
Sets parameter on the solver
Definition: Solver.cs:133
void ImportModelConverter(Solver src)
Import model converter from other solver.
Definition: Solver.cs:508
ParamDescrs ParameterDescriptions
Retrieves parameter descriptions for solver.
Definition: Solver.cs:154
Expr Proof
The proof of the last Check(params Expr[] assumptions).
Definition: Solver.cs:419
Status Check(params Expr[] assumptions)
Checks whether the assertions in the solver are consistent or not.
Definition: Solver.cs:336
BoolExpr[] UnsatCore
The unsat core of the last Check.
Definition: Solver.cs:439
BoolExpr[] Units
Currently inferred units.
Definition: Solver.cs:319
uint BacktrackLevel
Backtrack level that can be adjusted by conquer process
Definition: Solver.cs:463
override string ToString()
A string representation of the solver.
Definition: Solver.cs:528
void FromFile(string file)
Load solver assertions from a file.
Definition: Solver.cs:277
void Set(string name, bool value)
Sets parameter on the solver
Definition: Solver.cs:61
string ReasonUnknown
A brief justification of why the last call to Check returned UNKNOWN.
Definition: Solver.cs:452
void Set(Symbol name, double value)
Sets parameter on the solver
Definition: Solver.cs:124
void Set(Symbol name, bool value)
Sets parameter on the solver
Definition: Solver.cs:106
void Add(params BoolExpr[] constraints)
Alias for Assert.
Definition: Solver.cs:215
BoolExpr[] CubeVariables
Variables available and returned by the cuber.
Definition: Solver.cs:468
void AssertAndTrack(BoolExpr[] constraints, BoolExpr[] ps)
Assert multiple constraints into the solver, and track them (in the unsat) core using the Boolean con...
Definition: Solver.cs:239
Status Consequences(IEnumerable< BoolExpr > assumptions, IEnumerable< Expr > variables, out BoolExpr[] consequences)
Retrieve fixed assignments to the set of variables in the form of consequences. Each consequence is a...
Definition: Solver.cs:380
void Set(string name, Symbol value)
Sets parameter on the solver
Definition: Solver.cs:97
void Set(Symbol name, uint value)
Sets parameter on the solver
Definition: Solver.cs:115
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
Internal base class for interfacing with native Z3 objects. Should not be used externally.
Definition: Z3Object.cs:33
Context Context
Access Context object
Definition: Z3Object.cs:120
Z3_lbool
Lifted Boolean type: false, undefined, true.
Definition: z3_api.h:60
Status
Status values.
Definition: Status.cs:29