PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/src/api/dotnet/Solver.cs

https://bitbucket.org/chyh1990/z3
C# | 301 lines | 174 code | 31 blank | 96 comment | 18 complexity | e3d9d97e76cdcca8cdfe1a65ea4a7ced MD5 | raw file
  1. /*++
  2. Copyright (c) 2012 Microsoft Corporation
  3. Module Name:
  4. Solver.cs
  5. Abstract:
  6. Z3 Managed API: Solvers
  7. Author:
  8. Christoph Wintersteiger (cwinter) 2012-03-22
  9. Notes:
  10. --*/
  11. using System;
  12. using System.Diagnostics.Contracts;
  13. namespace Microsoft.Z3
  14. {
  15. /// <summary>
  16. /// Solvers.
  17. /// </summary>
  18. [ContractVerification(true)]
  19. public class Solver : Z3Object
  20. {
  21. /// <summary>
  22. /// A string that describes all available solver parameters.
  23. /// </summary>
  24. public string Help
  25. {
  26. get
  27. {
  28. Contract.Ensures(Contract.Result<string>() != null);
  29. return Native.Z3_solver_get_help(Context.nCtx, NativeObject);
  30. }
  31. }
  32. /// <summary>
  33. /// Sets the solver parameters.
  34. /// </summary>
  35. public Params Parameters
  36. {
  37. set
  38. {
  39. Contract.Requires(value != null);
  40. Context.CheckContextMatch(value);
  41. Native.Z3_solver_set_params(Context.nCtx, NativeObject, value.NativeObject);
  42. }
  43. }
  44. /// <summary>
  45. /// Retrieves parameter descriptions for solver.
  46. /// </summary>
  47. public ParamDescrs ParameterDescriptions
  48. {
  49. get { return new ParamDescrs(Context, Native.Z3_solver_get_param_descrs(Context.nCtx, NativeObject)); }
  50. }
  51. /// <summary>
  52. /// The current number of backtracking points (scopes).
  53. /// </summary>
  54. /// <seealso cref="Pop"/>
  55. /// <seealso cref="Push"/>
  56. public uint NumScopes
  57. {
  58. get { return Native.Z3_solver_get_num_scopes(Context.nCtx, NativeObject); }
  59. }
  60. /// <summary>
  61. /// Creates a backtracking point.
  62. /// </summary>
  63. /// <seealso cref="Pop"/>
  64. public void Push()
  65. {
  66. Native.Z3_solver_push(Context.nCtx, NativeObject);
  67. }
  68. /// <summary>
  69. /// Backtracks <paramref name="n"/> backtracking points.
  70. /// </summary>
  71. /// <remarks>Note that an exception is thrown if <paramref name="n"/> is not smaller than <c>NumScopes</c></remarks>
  72. /// <seealso cref="Push"/>
  73. public void Pop(uint n = 1)
  74. {
  75. Native.Z3_solver_pop(Context.nCtx, NativeObject, n);
  76. }
  77. /// <summary>
  78. /// Resets the Solver.
  79. /// </summary>
  80. /// <remarks>This removes all assertions from the solver.</remarks>
  81. public void Reset()
  82. {
  83. Native.Z3_solver_reset(Context.nCtx, NativeObject);
  84. }
  85. /// <summary>
  86. /// Assert a constraint (or multiple) into the solver.
  87. /// </summary>
  88. public void Assert(params BoolExpr[] constraints)
  89. {
  90. Contract.Requires(constraints != null);
  91. Contract.Requires(Contract.ForAll(constraints, c => c != null));
  92. Context.CheckContextMatch(constraints);
  93. foreach (BoolExpr a in constraints)
  94. {
  95. Native.Z3_solver_assert(Context.nCtx, NativeObject, a.NativeObject);
  96. }
  97. }
  98. /// <summary>
  99. /// The number of assertions in the solver.
  100. /// </summary>
  101. public uint NumAssertions
  102. {
  103. get
  104. {
  105. ASTVector ass = new ASTVector(Context, Native.Z3_solver_get_assertions(Context.nCtx, NativeObject));
  106. return ass.Size;
  107. }
  108. }
  109. /// <summary>
  110. /// The set of asserted formulas.
  111. /// </summary>
  112. public BoolExpr[] Assertions
  113. {
  114. get
  115. {
  116. Contract.Ensures(Contract.Result<BoolExpr[]>() != null);
  117. ASTVector ass = new ASTVector(Context, Native.Z3_solver_get_assertions(Context.nCtx, NativeObject));
  118. uint n = ass.Size;
  119. BoolExpr[] res = new BoolExpr[n];
  120. for (uint i = 0; i < n; i++)
  121. res[i] = new BoolExpr(Context, ass[i].NativeObject);
  122. return res;
  123. }
  124. }
  125. /// <summary>
  126. /// Checks whether the assertions in the solver are consistent or not.
  127. /// </summary>
  128. /// <remarks>
  129. /// <seealso cref="Model"/>
  130. /// <seealso cref="UnsatCore"/>
  131. /// <seealso cref="Proof"/>
  132. /// </remarks>
  133. public Status Check(params Expr[] assumptions)
  134. {
  135. Z3_lbool r;
  136. if (assumptions == null)
  137. r = (Z3_lbool)Native.Z3_solver_check(Context.nCtx, NativeObject);
  138. else
  139. r = (Z3_lbool)Native.Z3_solver_check_assumptions(Context.nCtx, NativeObject, (uint)assumptions.Length, AST.ArrayToNative(assumptions));
  140. switch (r)
  141. {
  142. case Z3_lbool.Z3_L_TRUE: return Status.SATISFIABLE;
  143. case Z3_lbool.Z3_L_FALSE: return Status.UNSATISFIABLE;
  144. default: return Status.UNKNOWN;
  145. }
  146. }
  147. /// <summary>
  148. /// The model of the last <c>Check</c>.
  149. /// </summary>
  150. /// <remarks>
  151. /// The result is <c>null</c> if <c>Check</c> was not invoked before,
  152. /// if its results was not <c>SATISFIABLE</c>, or if model production is not enabled.
  153. /// </remarks>
  154. public Model Model
  155. {
  156. get
  157. {
  158. IntPtr x = Native.Z3_solver_get_model(Context.nCtx, NativeObject);
  159. if (x == IntPtr.Zero)
  160. return null;
  161. else
  162. return new Model(Context, x);
  163. }
  164. }
  165. /// <summary>
  166. /// The proof of the last <c>Check</c>.
  167. /// </summary>
  168. /// <remarks>
  169. /// The result is <c>null</c> if <c>Check</c> was not invoked before,
  170. /// if its results was not <c>UNSATISFIABLE</c>, or if proof production is disabled.
  171. /// </remarks>
  172. public Expr Proof
  173. {
  174. get
  175. {
  176. IntPtr x = Native.Z3_solver_get_proof(Context.nCtx, NativeObject);
  177. if (x == IntPtr.Zero)
  178. return null;
  179. else
  180. return Expr.Create(Context, x);
  181. }
  182. }
  183. /// <summary>
  184. /// The unsat core of the last <c>Check</c>.
  185. /// </summary>
  186. /// <remarks>
  187. /// The unsat core is a subset of <c>Assertions</c>
  188. /// The result is empty if <c>Check</c> was not invoked before,
  189. /// if its results was not <c>UNSATISFIABLE</c>, or if core production is disabled.
  190. /// </remarks>
  191. public Expr[] UnsatCore
  192. {
  193. get
  194. {
  195. Contract.Ensures(Contract.Result<Expr[]>() != null);
  196. ASTVector core = new ASTVector(Context, Native.Z3_solver_get_unsat_core(Context.nCtx, NativeObject));
  197. uint n = core.Size;
  198. Expr[] res = new Expr[n];
  199. for (uint i = 0; i < n; i++)
  200. res[i] = Expr.Create(Context, core[i].NativeObject);
  201. return res;
  202. }
  203. }
  204. /// <summary>
  205. /// A brief justification of why the last call to <c>Check</c> returned <c>UNKNOWN</c>.
  206. /// </summary>
  207. public string ReasonUnknown
  208. {
  209. get
  210. {
  211. Contract.Ensures(Contract.Result<string>() != null);
  212. return Native.Z3_solver_get_reason_unknown(Context.nCtx, NativeObject);
  213. }
  214. }
  215. /// <summary>
  216. /// Solver statistics.
  217. /// </summary>
  218. public Statistics Statistics
  219. {
  220. get
  221. {
  222. Contract.Ensures(Contract.Result<Statistics>() != null);
  223. return new Statistics(Context, Native.Z3_solver_get_statistics(Context.nCtx, NativeObject));
  224. }
  225. }
  226. /// <summary>
  227. /// A string representation of the solver.
  228. /// </summary>
  229. public override string ToString()
  230. {
  231. return Native.Z3_solver_to_string(Context.nCtx, NativeObject);
  232. }
  233. #region Internal
  234. internal Solver(Context ctx, IntPtr obj)
  235. : base(ctx, obj)
  236. {
  237. Contract.Requires(ctx != null);
  238. }
  239. internal class DecRefQueue : Z3.DecRefQueue
  240. {
  241. public override void IncRef(Context ctx, IntPtr obj)
  242. {
  243. Native.Z3_solver_inc_ref(ctx.nCtx, obj);
  244. }
  245. public override void DecRef(Context ctx, IntPtr obj)
  246. {
  247. Native.Z3_solver_dec_ref(ctx.nCtx, obj);
  248. }
  249. };
  250. internal override void IncRef(IntPtr o)
  251. {
  252. Context.Solver_DRQ.IncAndClear(Context, o);
  253. base.IncRef(o);
  254. }
  255. internal override void DecRef(IntPtr o)
  256. {
  257. Context.Solver_DRQ.Add(o);
  258. base.DecRef(o);
  259. }
  260. #endregion
  261. }
  262. }