Z3
Model.cs
Go to the documentation of this file.
1/*++
2Copyright (c) 2012 Microsoft Corporation
3
4Module Name:
5
6 Model.cs
7
8Abstract:
9
10 Z3 Managed API: Models
11
12Author:
13
14 Christoph Wintersteiger (cwinter) 2012-03-21
15
16Notes:
17
18--*/
19
20using System;
21using System.Diagnostics;
22using System.Collections.Generic;
23
24namespace Microsoft.Z3
25{
29 public class Model : Z3Object
30 {
37 {
38 Debug.Assert(a != null);
39
40 Context.CheckContextMatch(a);
41 return ConstInterp(a.FuncDecl);
42 }
43
50 {
51 Debug.Assert(f != null);
52
53 Context.CheckContextMatch(f);
54 if (f.Arity != 0)
55 throw new Z3Exception("Non-zero arity functions have FunctionInterpretations as a model. Use FuncInterp.");
56
57 IntPtr n = Native.Z3_model_get_const_interp(Context.nCtx, NativeObject, f.NativeObject);
58 if (n == IntPtr.Zero)
59 return null;
60 else
61 return Expr.Create(Context, n);
62 }
63
70 {
71 Debug.Assert(f != null);
72
73 Context.CheckContextMatch(f);
74
75 Z3_sort_kind sk = (Z3_sort_kind)Native.Z3_get_sort_kind(Context.nCtx, Native.Z3_get_range(Context.nCtx, f.NativeObject));
76
77 if (f.Arity == 0)
78 {
79 IntPtr n = Native.Z3_model_get_const_interp(Context.nCtx, NativeObject, f.NativeObject);
80
81 if (sk == Z3_sort_kind.Z3_ARRAY_SORT)
82 {
83 if (n == IntPtr.Zero)
84 return null;
85 else
86 {
87 if (Native.Z3_is_as_array(Context.nCtx, n) == 0)
88 throw new Z3Exception("Argument was not an array constant");
89 IntPtr fd = Native.Z3_get_as_array_func_decl(Context.nCtx, n);
90 using var decl = new FuncDecl(Context, fd);
91 return FuncInterp(decl);
92 }
93 }
94 else
95 {
96 throw new Z3Exception("Constant functions do not have a function interpretation; use ConstInterp");
97 }
98 }
99 else
100 {
101 IntPtr n = Native.Z3_model_get_func_interp(Context.nCtx, NativeObject, f.NativeObject);
102 if (n == IntPtr.Zero)
103 return null;
104 else
105 return new FuncInterp(Context, n);
106 }
107 }
108
112 public uint NumConsts
113 {
114 get { return Native.Z3_model_get_num_consts(Context.nCtx, NativeObject); }
115 }
116
121 {
122 get
123 {
124
125 uint n = NumConsts;
126 FuncDecl[] res = new FuncDecl[n];
127 for (uint i = 0; i < n; i++)
128 res[i] = new FuncDecl(Context, Native.Z3_model_get_const_decl(Context.nCtx, NativeObject, i));
129 return res;
130 }
131 }
132
136 public IEnumerable<KeyValuePair<FuncDecl, Expr>> Consts
137 {
138 get
139 {
140 uint nc = NumConsts;
141 for (uint i = 0; i < nc; ++i)
142 {
143 var f = new FuncDecl(Context, Native.Z3_model_get_const_decl(Context.nCtx, NativeObject, i));
144 IntPtr n = Native.Z3_model_get_const_interp(Context.nCtx, NativeObject, f.NativeObject);
145 if (n == IntPtr.Zero) continue;
146 yield return new KeyValuePair<FuncDecl, Expr>(f, Expr.Create(Context, n));
147 }
148 }
149 }
150
154 public uint NumFuncs
155 {
156 get { return Native.Z3_model_get_num_funcs(Context.nCtx, NativeObject); }
157 }
158
163 {
164 get
165 {
166
167 uint n = NumFuncs;
168 FuncDecl[] res = new FuncDecl[n];
169 for (uint i = 0; i < n; i++)
170 res[i] = new FuncDecl(Context, Native.Z3_model_get_func_decl(Context.nCtx, NativeObject, i));
171 return res;
172 }
173 }
174
179 {
180 get
181 {
182
183 uint nFuncs = NumFuncs;
184 uint nConsts = NumConsts;
185 uint n = nFuncs + nConsts;
186 FuncDecl[] res = new FuncDecl[n];
187 for (uint i = 0; i < nConsts; i++)
188 res[i] = new FuncDecl(Context, Native.Z3_model_get_const_decl(Context.nCtx, NativeObject, i));
189 for (uint i = 0; i < nFuncs; i++)
190 res[nConsts + i] = new FuncDecl(Context, Native.Z3_model_get_func_decl(Context.nCtx, NativeObject, i));
191 return res;
192 }
193 }
194
199 {
203 public ModelEvaluationFailedException() : base() { }
204 }
205
220 public Expr Eval(Expr t, bool completion = false)
221 {
222 Debug.Assert(t != null);
223
224 IntPtr v = IntPtr.Zero;
225 if (Native.Z3_model_eval(Context.nCtx, NativeObject, t.NativeObject, (byte)(completion ? 1 : 0), ref v) == (byte)0)
227 else
228 return Expr.Create(Context, v);
229 }
230
234 public Expr Evaluate(Expr t, bool completion = false)
235 {
236 Debug.Assert(t != null);
237
238 return Eval(t, completion);
239 }
240
244 public double Double(Expr t) {
245 using var r = Eval(t, true);
246 return Native.Z3_get_numeral_double(Context.nCtx, r.NativeObject);
247 }
248
252 public uint NumSorts { get { return Native.Z3_model_get_num_sorts(Context.nCtx, NativeObject); } }
253
264 public Sort[] Sorts
265 {
266 get
267 {
268
269 uint n = NumSorts;
270 Sort[] res = new Sort[n];
271 for (uint i = 0; i < n; i++)
272 res[i] = Sort.Create(Context, Native.Z3_model_get_sort(Context.nCtx, NativeObject, i));
273 return res;
274 }
275 }
276
284 {
285 Debug.Assert(s != null);
286
287 using ASTVector av = new ASTVector(Context, Native.Z3_model_get_sort_universe(Context.nCtx, NativeObject, s.NativeObject));
288 return av.ToExprArray();
289 }
290
295 public override string ToString()
296 {
297 return Native.Z3_model_to_string(Context.nCtx, NativeObject);
298 }
299
300 #region Internal
301 internal Model(Context ctx, IntPtr obj)
302 : base(ctx, obj)
303 {
304 Debug.Assert(ctx != null);
305 }
306
307 internal class DecRefQueue : IDecRefQueue
308 {
309 public DecRefQueue() : base() { }
310 public DecRefQueue(uint move_limit) : base(move_limit) { }
311 internal override void IncRef(Context ctx, IntPtr obj)
312 {
313 Native.Z3_model_inc_ref(ctx.nCtx, obj);
314 }
315
316 internal override void DecRef(Context ctx, IntPtr obj)
317 {
318 Native.Z3_model_dec_ref(ctx.nCtx, obj);
319 }
320 };
321
322 internal override void IncRef(IntPtr o)
323 {
324 Context.Model_DRQ.IncAndClear(Context, o);
325 base.IncRef(o);
326 }
327
328 internal override void DecRef(IntPtr o)
329 {
330 Context.Model_DRQ.Add(o);
331 base.DecRef(o);
332 }
333 #endregion
334 }
335}
Vectors of ASTs.
Definition: ASTVector.cs:29
Expr[] ToExprArray()
Translates an ASTVector into an Expr[]
Definition: ASTVector.cs:115
The main interaction with Z3 happens via the Context.
Definition: Context.cs:34
IDecRefQueue Model_DRQ
Model DRQ
Definition: Context.cs:4928
Expressions are terms.
Definition: Expr.cs:31
FuncDecl FuncDecl
The function declaration of the function that is applied in this expression.
Definition: Expr.cs:50
Function declarations.
Definition: FuncDecl.cs:31
uint Arity
The arity of the function declaration
Definition: FuncDecl.cs:92
A function interpretation is represented as a finite map and an 'else' value. Each entry in the finit...
Definition: FuncInterp.cs:30
A ModelEvaluationFailedException is thrown when an expression cannot be evaluated by the model.
Definition: Model.cs:199
ModelEvaluationFailedException()
An exception that is thrown when model evaluation fails.
Definition: Model.cs:203
A Model contains interpretations (assignments) of constants and functions.
Definition: Model.cs:30
FuncDecl[] Decls
All symbols that have an interpretation in the model.
Definition: Model.cs:179
Expr ConstInterp(Expr a)
Retrieves the interpretation (the assignment) of a in the model.
Definition: Model.cs:36
Expr Evaluate(Expr t, bool completion=false)
Alias for Eval.
Definition: Model.cs:234
FuncDecl[] FuncDecls
The function declarations of the function interpretations in the model.
Definition: Model.cs:163
double Double(Expr t)
Evaluate expression to a double, assuming it is a numeral already.
Definition: Model.cs:244
FuncInterp FuncInterp(FuncDecl f)
Retrieves the interpretation (the assignment) of a non-constant f in the model.
Definition: Model.cs:69
Expr[] SortUniverse(Sort s)
The finite set of distinct values that represent the interpretation for sort s .
Definition: Model.cs:283
Sort[] Sorts
The uninterpreted sorts that the model has an interpretation for.
Definition: Model.cs:265
IEnumerable< KeyValuePair< FuncDecl, Expr > > Consts
Enumerate constants in model.
Definition: Model.cs:137
FuncDecl[] ConstDecls
The function declarations of the constants in the model.
Definition: Model.cs:121
override string ToString()
Conversion of models to strings.
Definition: Model.cs:295
uint NumFuncs
The number of function interpretations in the model.
Definition: Model.cs:155
Expr ConstInterp(FuncDecl f)
Retrieves the interpretation (the assignment) of f in the model.
Definition: Model.cs:49
Expr Eval(Expr t, bool completion=false)
Evaluates the expression t in the current model.
Definition: Model.cs:220
uint NumConsts
The number of constants that have an interpretation in the model.
Definition: Model.cs:113
uint NumSorts
The number of uninterpreted sorts that the model has an interpretation for.
Definition: Model.cs:252
The Sort class implements type information for ASTs.
Definition: Sort.cs:29
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_sort_kind
The different kinds of Z3 types (See Z3_get_sort_kind).
Definition: z3_api.h:108