Z3
 
Loading...
Searching...
No Matches
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
332 {
333 get
334 {
335 using ASTVector assertions = new ASTVector(Context, Native.Z3_solver_get_non_units(Context.nCtx, NativeObject));
336 return assertions.ToBoolExprArray();
337 }
338 }
339
344 {
345 get
346 {
347 using ASTVector trail = new ASTVector(Context, Native.Z3_solver_get_trail(Context.nCtx, NativeObject));
348 return trail.ToBoolExprArray();
349 }
350 }
351
358 {
359 Debug.Assert(t != null);
360 Context.CheckContextMatch(t);
361 return Expr.Create(Context, Native.Z3_solver_congruence_root(Context.nCtx, NativeObject, t.NativeObject));
362 }
363
370 {
371 Debug.Assert(t != null);
372 Context.CheckContextMatch(t);
373 return Expr.Create(Context, Native.Z3_solver_congruence_next(Context.nCtx, NativeObject, t.NativeObject));
374 }
375
380 {
381 Debug.Assert(a != null);
382 Debug.Assert(b != null);
383 Context.CheckContextMatch(a);
384 Context.CheckContextMatch(b);
385 return Expr.Create(Context, Native.Z3_solver_congruence_explain(Context.nCtx, NativeObject, a.NativeObject, b.NativeObject));
386 }
387
396 public Status Check(params Expr[] assumptions)
397 {
398 Z3_lbool r;
399 if (assumptions == null || assumptions.Length == 0)
400 r = (Z3_lbool)Native.Z3_solver_check(Context.nCtx, NativeObject);
401 else
402 r = (Z3_lbool)Native.Z3_solver_check_assumptions(Context.nCtx, NativeObject, (uint)assumptions.Length, AST.ArrayToNative(assumptions));
403 return lboolToStatus(r);
404 }
405
414 public Status Check(IEnumerable<BoolExpr> assumptions)
415 {
416 Z3_lbool r;
417 BoolExpr[] asms = assumptions.ToArray();
418 if (asms.Length == 0)
419 r = (Z3_lbool)Native.Z3_solver_check(Context.nCtx, NativeObject);
420 else
421 r = (Z3_lbool)Native.Z3_solver_check_assumptions(Context.nCtx, NativeObject, (uint)asms.Length, AST.ArrayToNative(asms));
422 return lboolToStatus(r);
423 }
424
440 public Status Consequences(IEnumerable<BoolExpr> assumptions, IEnumerable<Expr> variables, out BoolExpr[] consequences)
441 {
442 using ASTVector result = new ASTVector(Context);
443 using ASTVector asms = new ASTVector(Context);
444 using ASTVector vars = new ASTVector(Context);
445 foreach (var asm in assumptions) asms.Push(asm);
446 foreach (var v in variables) vars.Push(v);
447 Z3_lbool r = (Z3_lbool)Native.Z3_solver_get_consequences(Context.nCtx, NativeObject, asms.NativeObject, vars.NativeObject, result.NativeObject);
448 consequences = result.ToBoolExprArray();
449 return lboolToStatus(r);
450 }
451
460 {
461 get
462 {
463 IntPtr x = Native.Z3_solver_get_model(Context.nCtx, NativeObject);
464 if (x == IntPtr.Zero)
465 return null;
466 else
467 return new Model(Context, x);
468 }
469 }
470
478 public Expr Proof
479 {
480 get
481 {
482 IntPtr x = Native.Z3_solver_get_proof(Context.nCtx, NativeObject);
483 if (x == IntPtr.Zero)
484 return null;
485 else
486 return Expr.Create(Context, x);
487 }
488 }
489
499 {
500 get
501 {
502
503 using ASTVector core = new ASTVector(Context, Native.Z3_solver_get_unsat_core(Context.nCtx, NativeObject));
504 return core.ToBoolExprArray();
505 }
506 }
507
511 public string ReasonUnknown
512 {
513 get
514 {
515
516 return Native.Z3_solver_get_reason_unknown(Context.nCtx, NativeObject);
517 }
518 }
519
523 public uint BacktrackLevel { get; set; }
524
528 public BoolExpr[] CubeVariables { get; set; }
529
530
534 public IEnumerable<BoolExpr[]> Cube()
535 {
536 using ASTVector cv = new ASTVector(Context);
537 if (CubeVariables != null)
538 foreach (var b in CubeVariables) cv.Push(b);
539
540 while (true) {
541 var lvl = BacktrackLevel;
542 BacktrackLevel = uint.MaxValue;
543 using ASTVector r = new ASTVector(Context, Native.Z3_solver_cube(Context.nCtx, NativeObject, cv.NativeObject, lvl));
544 var v = r.ToBoolExprArray();
545 CubeVariables = cv.ToBoolExprArray();
546 if (v.Length == 1 && v[0].IsFalse) {
547 break;
548 }
549 yield return v;
550 if (v.Length == 0) {
551 break;
552 }
553 }
554 }
555
560 {
561 Debug.Assert(ctx != null);
562 return new Solver(ctx, Native.Z3_solver_translate(Context.nCtx, NativeObject, ctx.nCtx));
563 }
564
568 public void ImportModelConverter(Solver src)
569 {
570 Native.Z3_solver_import_model_converter(Context.nCtx, src.NativeObject, NativeObject);
571 }
572
577 {
578 get
579 {
580
581 return new Statistics(Context, Native.Z3_solver_get_statistics(Context.nCtx, NativeObject));
582 }
583 }
584
588 public override string ToString()
589 {
590 return Native.Z3_solver_to_string(Context.nCtx, NativeObject);
591 }
592
593 #region Internal
594 internal Solver(Context ctx, IntPtr obj)
595 : base(ctx, obj)
596 {
597 Debug.Assert(ctx != null);
598 this.BacktrackLevel = uint.MaxValue;
599 }
600
601 internal override void IncRef(IntPtr o)
602 {
603 Native.Z3_solver_inc_ref(Context.nCtx, o);
604 }
605
606 internal override void DecRef(IntPtr o)
607 {
608 lock (Context)
609 {
610 if (Context.nCtx != IntPtr.Zero)
611 Native.Z3_solver_dec_ref(Context.nCtx, o);
612 }
613 }
614
615 private Status lboolToStatus(Z3_lbool r)
616 {
617 switch (r)
618 {
619 case Z3_lbool.Z3_L_TRUE: return Status.SATISFIABLE;
620 case Z3_lbool.Z3_L_FALSE: return Status.UNSATISFIABLE;
621 default: return Status.UNKNOWN;
622 }
623 }
624
625 #endregion
626 }
627}
The abstract syntax tree (AST) class.
Definition AST.cs:31
Vectors of ASTs.
Definition ASTVector.cs:29
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:3470
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.
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:460
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:559
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
BoolExpr[] NonUnits
Non-unit literals in the solver state.
Definition Solver.cs:332
IEnumerable< BoolExpr[]> Cube()
Return a set of cubes.
Definition Solver.cs:534
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:577
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:414
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:568
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:479
Status Check(params Expr[] assumptions)
Checks whether the assertions in the solver are consistent or not.
Definition Solver.cs:396
BoolExpr[] UnsatCore
The unsat core of the last Check.
Definition Solver.cs:499
BoolExpr[] Units
Currently inferred units.
Definition Solver.cs:319
Expr CongruenceExplain(Expr a, Expr b)
Explain congruence of a and b relative to the current search state.
Definition Solver.cs:379
uint BacktrackLevel
Backtrack level that can be adjusted by conquer process.
Definition Solver.cs:523
override string ToString()
A string representation of the solver.
Definition Solver.cs:588
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:512
Expr CongruenceNext(Expr t)
Retrieve congruence closure sibling of the term t relative to the current search state....
Definition Solver.cs:369
void Set(Symbol name, double value)
Sets parameter on the solver.
Definition Solver.cs:124
BoolExpr[] Trail
Trail of the solver state after a check() call.
Definition Solver.cs:344
Expr CongruenceRoot(Expr t)
Retrieve congruence closure root of the term t relative to the current search state....
Definition Solver.cs:357
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:528
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:440
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:31
Symbols are used to name several term and type constructors.
Definition Symbol.cs:30
The exception base class for error reporting from Z3.
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:111
Z3_lbool
Lifted Boolean type: false, undefined, true.
Definition z3_api.h:58
Status
Status values.
Definition Status.cs:29