Z3
Data Structures | Public Member Functions | Properties
NativeModel Class Reference

A Model contains interpretations (assignments) of constants and functions. More...

+ Inheritance diagram for NativeModel:

Data Structures

class  ArrayValue
 An array value obtained by untangling a model assignment. More...
 
class  ModelEvaluationFailedException
 A ModelEvaluationFailedException is thrown when an expression cannot be evaluated by the model. More...
 

Public Member Functions

Z3_ast ConstInterp (Z3_ast a)
 Retrieves the interpretation (the assignment) of a in the model. More...
 
Z3_ast ConstFuncInterp (Z3_func_decl f)
 Retrieves the interpretation (the assignment) of f in the model. More...
 
NativeFuncInterp FuncInterp (Z3_func_decl f)
 Retrieves the interpretation (the assignment) of a non-constant f in the model. More...
 
Z3_ast Eval (Z3_ast t, bool completion=false)
 Evaluates the expression t in the current model. More...
 
Z3_ast Evaluate (Z3_ast t, bool completion=false)
 Alias for Eval. More...
 
double Double (Z3_ast t)
 Evaluate expression to a double, assuming it is a numeral already. More...
 
bool TryGetArrayValue (Z3_ast t, out ArrayValue result)
 Convert the interpretation of t into a sequence of array updates More...
 
override string ToString ()
 Conversion of models to strings. More...
 
void Dispose ()
 Disposes of the underlying native Z3 object. More...
 

Properties

uint NumConsts [get]
 The number of constants that have an interpretation in the model. More...
 
Z3_func_decl[] ConstDecls [get]
 The function declarations of the constants in the model. More...
 
IEnumerable< KeyValuePair< Z3_func_decl, Z3_ast > > Consts [get]
 Enumerate constants in model. More...
 
uint NumFuncs [get]
 The number of function interpretations in the model. More...
 
Z3_func_decl[] FuncDecls [get]
 The function declarations of the function interpretations in the model. More...
 
Z3_func_decl[] Decls [get]
 All symbols that have an interpretation in the model. More...
 
uint NumSorts [get]
 The number of uninterpreted sorts that the model has an interpretation for. More...
 
Z3_sort[] Sorts [get]
 The uninterpreted sorts that the model has an interpretation for. More...
 

Detailed Description

A Model contains interpretations (assignments) of constants and functions.

Definition at line 36 of file NativeModel.cs.

Member Function Documentation

◆ ConstFuncInterp()

Z3_ast ConstFuncInterp ( Z3_func_decl  f)
inline

Retrieves the interpretation (the assignment) of f in the model.

Parameters
fA function declaration of zero arity
Returns
An expression if the function has an interpretation in the model, null otherwise.


Definition at line 51 of file NativeModel.cs.

52  {
53  if (Native.Z3_get_arity(ntvContext.nCtx, f) != 0)
54  throw new Z3Exception("Non-zero arity functions have FunctionInterpretations as a model. Use FuncInterp.");
55 
56  return Native.Z3_model_get_const_interp(ntvContext.nCtx, NativeObject, f);
57  }

◆ ConstInterp()

Z3_ast ConstInterp ( Z3_ast  a)

Retrieves the interpretation (the assignment) of a in the model.

Parameters
aA Constant
Returns
An expression if the constant has an interpretation in the model, null otherwise.

◆ Dispose()

void Dispose ( )
inline

Disposes of the underlying native Z3 object.

Definition at line 368 of file NativeModel.cs.

369  {
370  if (NativeObject != IntPtr.Zero)
371  {
372  Native.Z3_model_dec_ref(ntvContext.nCtx, NativeObject);
373  NativeObject = IntPtr.Zero;
374  }
375  GC.SuppressFinalize(this);
376  }

◆ Double()

double Double ( Z3_ast  t)
inline

Evaluate expression to a double, assuming it is a numeral already.

Definition at line 232 of file NativeModel.cs.

233  {
234  var r = Eval(t, true);
235  return Native.Z3_get_numeral_double(ntvContext.nCtx, r);
236  }
Z3_ast Eval(Z3_ast t, bool completion=false)
Evaluates the expression t in the current model.
Definition: NativeModel.cs:214

◆ Eval()

Z3_ast Eval ( Z3_ast  t,
bool  completion = false 
)
inline

Evaluates the expression t in the current model.

This function may fail if t contains quantifiers, is partial (MODEL_PARTIAL enabled), or if t is not well-sorted. In this case a ModelEvaluationFailedException is thrown.

Parameters
tAn expression
completionWhen this flag is enabled, a model value will be assigned to any constant or function that does not have an interpretation in the model.
Returns
The evaluation of t in the model.


Definition at line 214 of file NativeModel.cs.

215  {
216 
217  IntPtr v = IntPtr.Zero;
218  if (Native.Z3_model_eval(ntvContext.nCtx, NativeObject, t, (byte)(completion ? 1 : 0), ref v) == (byte)0)
219  throw new ModelEvaluationFailedException();
220  else
221  return v;
222  }

Referenced by NativeModel.Double(), and NativeModel.TryGetArrayValue().

◆ Evaluate()

Z3_ast Evaluate ( Z3_ast  t,
bool  completion = false 
)

Alias for Eval.


◆ FuncInterp()

Retrieves the interpretation (the assignment) of a non-constant f in the model.

Parameters
fA function declaration of non-zero arity
Returns
A FunctionInterpretation if the function has an interpretation in the model, null otherwise.


Definition at line 64 of file NativeModel.cs.

65  {
66  Z3_sort_kind sk = (Z3_sort_kind)Native.Z3_get_sort_kind(ntvContext.nCtx, Native.Z3_get_range(ntvContext.nCtx, f));
67 
68  if (Native.Z3_get_arity(ntvContext.nCtx, f) == 0)
69  {
70  IntPtr n = Native.Z3_model_get_const_interp(ntvContext.nCtx, NativeObject, f);
71 
72  if (sk == Z3_sort_kind.Z3_ARRAY_SORT)
73  {
74  if (n == IntPtr.Zero)
75  return null;
76  else
77  {
78  if (Native.Z3_is_as_array(ntvContext.nCtx, n) == 0)
79  throw new Z3Exception("Argument was not an array constant");
80  var fd = Native.Z3_get_as_array_func_decl(ntvContext.nCtx, n);
81  return new NativeFuncInterp(ntvContext, this, f, fd);
82  }
83  }
84  else
85  {
86  throw new Z3Exception("Constant functions do not have a function interpretation; use ConstInterp");
87  }
88  }
89  else
90  {
91  IntPtr n = Native.Z3_model_get_func_interp(ntvContext.nCtx, NativeObject, f);
92  if (n == IntPtr.Zero)
93  return null;
94  else
95  return new NativeFuncInterp(ntvContext, this, f, n);
96  }
97  }
Z3_sort_kind
The different kinds of Z3 types (See Z3_get_sort_kind).
Definition: z3_api.h:108

◆ ToString()

override string ToString ( )
inline

Conversion of models to strings.

Returns
A string representation of the model.

Definition at line 339 of file NativeModel.cs.

340  {
341  return Native.Z3_model_to_string(ntvContext.nCtx, NativeObject);
342  }

◆ TryGetArrayValue()

bool TryGetArrayValue ( Z3_ast  t,
out ArrayValue  result 
)
inline

Convert the interpretation of t into a sequence of array updates

Parameters
t
result
Returns
null if the argument does evaluate to a sequence of stores to an array

Definition at line 272 of file NativeModel.cs.

273  {
274  var r = Eval(t, true);
275  // check that r is a sequence of store over a constant default array.
276  var updates = new Dictionary<Z3_ast, Z3_ast>();
277  result = null;
278  while (true)
279  {
280  if (ntvContext.GetAstKind(r) != Z3_ast_kind.Z3_APP_AST)
281  return false;
282  Z3_func_decl f = ntvContext.GetAppDecl(r);
283  var kind = ntvContext.GetDeclKind(f);
284  if (kind == Z3_decl_kind.Z3_OP_CONST_ARRAY)
285  {
286  result = new ArrayValue();
287  result.Else = ntvContext.GetAppArg(r, 0);
288  result.Updates = updates.ToArray();
289  result.Domain = updates.Keys.ToArray();
290  result.Range = updates.Values.ToArray();
291  return true;
292  }
293  else if (kind == Z3_decl_kind.Z3_OP_STORE)
294  {
295  Debug.Assert(ntvContext.GetNumArgs(r) == 3);
296  updates[ntvContext.GetAppArg(r, 1)] = ntvContext.GetAppArg(r, 2);
297  r = ntvContext.GetAppArg(r, 0);
298  }
299  else
300  {
301  return false;
302  }
303  }
304  }
Z3_ast_kind GetAstKind(Z3_ast ast)
Get the AST kind from IntPtr
uint GetNumArgs(Z3_app app)
Return number of arguments for app
Z3_decl_kind GetDeclKind(Z3_func_decl decl)
Get the Decl kind from IntPtr
Z3_func_decl GetAppDecl(Z3_ast ast)
Get App Decl from IntPtr
Z3_ast_kind
The different kinds of Z3 AST (abstract syntax trees). That is, terms, formulas and types.
Definition: z3_api.h:139
Z3_decl_kind
The different kinds of interpreted function kinds.
Definition: z3_api.h:961
System.IntPtr Z3_func_decl

Property Documentation

◆ ConstDecls

Z3_func_decl [] ConstDecls
get

The function declarations of the constants in the model.

Definition at line 113 of file NativeModel.cs.

114  {
115  get
116  {
117 
118  uint n = NumConsts;
119  Z3_func_decl[] res = new Z3_func_decl[n];
120  for (uint i = 0; i < n; i++)
121  res[i] = Native.Z3_model_get_const_decl(ntvContext.nCtx, NativeObject, i);
122  return res;
123  }
124  }
uint NumConsts
The number of constants that have an interpretation in the model.
Definition: NativeModel.cs:105

◆ Consts

IEnumerable<KeyValuePair<Z3_func_decl, Z3_ast> > Consts
get

Enumerate constants in model.

Definition at line 130 of file NativeModel.cs.

131  {
132  get
133  {
134  uint nc = NumConsts;
135  for (uint i = 0; i < nc; ++i)
136  {
137  var f = Native.Z3_model_get_const_decl(ntvContext.nCtx, NativeObject, i);
138  IntPtr n = Native.Z3_model_get_const_interp(ntvContext.nCtx, NativeObject, f);
139  if (n == IntPtr.Zero) continue;
140  yield return new KeyValuePair<Z3_func_decl, Z3_ast>(f, n);
141  }
142  }
143  }

◆ Decls

Z3_func_decl [] Decls
get

All symbols that have an interpretation in the model.

Definition at line 172 of file NativeModel.cs.

173  {
174  get
175  {
176 
177  uint nFuncs = NumFuncs;
178  uint nConsts = NumConsts;
179  uint n = nFuncs + nConsts;
180  Z3_func_decl[] res = new Z3_func_decl[n];
181  for (uint i = 0; i < nConsts; i++)
182  res[i] = Native.Z3_model_get_const_decl(ntvContext.nCtx, NativeObject, i);
183  for (uint i = 0; i < nFuncs; i++)
184  res[nConsts + i] = Native.Z3_model_get_func_decl(ntvContext.nCtx, NativeObject, i);
185  return res;
186  }
187  }
uint NumFuncs
The number of function interpretations in the model.
Definition: NativeModel.cs:149

◆ FuncDecls

Z3_func_decl [] FuncDecls
get

The function declarations of the function interpretations in the model.

Definition at line 156 of file NativeModel.cs.

157  {
158  get
159  {
160 
161  uint n = NumFuncs;
162  Z3_func_decl[] res = new Z3_func_decl[n];
163  for (uint i = 0; i < n; i++)
164  res[i] = Native.Z3_model_get_func_decl(ntvContext.nCtx, NativeObject, i);
165  return res;
166  }
167  }

◆ NumConsts

uint NumConsts
get

The number of constants that have an interpretation in the model.

Definition at line 104 of file NativeModel.cs.

105  {
106  get { return Native.Z3_model_get_num_consts(ntvContext.nCtx, NativeObject); }
107  }

◆ NumFuncs

uint NumFuncs
get

The number of function interpretations in the model.

Definition at line 148 of file NativeModel.cs.

149  {
150  get { return Native.Z3_model_get_num_funcs(ntvContext.nCtx, NativeObject); }
151  }

◆ NumSorts

uint NumSorts
get

The number of uninterpreted sorts that the model has an interpretation for.


Definition at line 309 of file NativeModel.cs.

309 { get { return Native.Z3_model_get_num_sorts(ntvContext.nCtx, NativeObject); } }

◆ Sorts

Z3_sort [] Sorts
get

The uninterpreted sorts that the model has an interpretation for.

Z3 also provides an interpretation for uninterpreted sorts used in a formula. The interpretation for a sort is a finite set of distinct values. We say this finite set is the "universe" of the sort.

See also
NumSorts

Definition at line 321 of file NativeModel.cs.

322  {
323  get
324  {
325 
326  uint n = NumSorts;
327  Z3_sort[] res = new Z3_sort[n];
328  for (uint i = 0; i < n; i++)
329  res[i] = Native.Z3_model_get_sort(ntvContext.nCtx, NativeObject, i);
330  return res;
331  }
332  }
uint NumSorts
The number of uninterpreted sorts that the model has an interpretation for.
Definition: NativeModel.cs:309
System.IntPtr Z3_sort