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

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

+ Inheritance diagram for Model:

Data Structures

class  ModelEvaluationFailedException
 A ModelEvaluationFailedException is thrown when an expression cannot be evaluated by the model. More...
 

Public Member Functions

Expr ConstInterp (Expr a)
 Retrieves the interpretation (the assignment) of a in the model. More...
 
Expr ConstInterp (FuncDecl f)
 Retrieves the interpretation (the assignment) of f in the model. More...
 
FuncInterp FuncInterp (FuncDecl f)
 Retrieves the interpretation (the assignment) of a non-constant f in the model. More...
 
Expr Eval (Expr t, bool completion=false)
 Evaluates the expression t in the current model. More...
 
Expr Evaluate (Expr t, bool completion=false)
 Alias for Eval. More...
 
double Double (Expr t)
 Evaluate expression to a double, assuming it is a numeral already. More...
 
Expr[] SortUniverse (Sort s)
 The finite set of distinct values that represent the interpretation for sort s . More...
 
override string ToString ()
 Conversion of models to strings. More...
 
- Public Member Functions inherited from Z3Object
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...
 
FuncDecl[] ConstDecls [get]
 The function declarations of the constants in the model. More...
 
IEnumerable< KeyValuePair< FuncDecl, Expr > > Consts [get]
 Enumerate constants in model. More...
 
uint NumFuncs [get]
 The number of function interpretations in the model. More...
 
FuncDecl[] FuncDecls [get]
 The function declarations of the function interpretations in the model. More...
 
FuncDecl[] 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...
 
Sort[] Sorts [get]
 The uninterpreted sorts that the model has an interpretation for. More...
 
- Properties inherited from Z3Object
Context Context [get]
 Access Context object More...
 

Detailed Description

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

Definition at line 29 of file Model.cs.

Member Function Documentation

◆ ConstInterp() [1/2]

Expr ConstInterp ( Expr  a)
inline

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.

Definition at line 36 of file Model.cs.

37  {
38  Debug.Assert(a != null);
39 
40  Context.CheckContextMatch(a);
41  return ConstInterp(a.FuncDecl);
42  }
Expr ConstInterp(Expr a)
Retrieves the interpretation (the assignment) of a in the model.
Definition: Model.cs:36
Context Context
Access Context object
Definition: Z3Object.cs:111

◆ ConstInterp() [2/2]

Expr ConstInterp ( FuncDecl  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 49 of file Model.cs.

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  }

◆ Double()

double Double ( Expr  t)
inline

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

Definition at line 244 of file Model.cs.

244  {
245  using var r = Eval(t, true);
246  return Native.Z3_get_numeral_double(Context.nCtx, r.NativeObject);
247  }
Expr Eval(Expr t, bool completion=false)
Evaluates the expression t in the current model.
Definition: Model.cs:220

◆ Eval()

Expr Eval ( Expr  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 220 of file Model.cs.

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)
226  throw new ModelEvaluationFailedException();
227  else
228  return Expr.Create(Context, v);
229  }

Referenced by Model.Double(), and Model.Evaluate().

◆ Evaluate()

Expr Evaluate ( Expr  t,
bool  completion = false 
)
inline

Alias for Eval.


Definition at line 234 of file Model.cs.

235  {
236  Debug.Assert(t != null);
237 
238  return Eval(t, completion);
239  }

◆ 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 69 of file Model.cs.

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  }
FuncInterp FuncInterp(FuncDecl f)
Retrieves the interpretation (the assignment) of a non-constant f in the model.
Definition: Model.cs:69
Z3_sort_kind
The different kinds of Z3 types (See Z3_get_sort_kind).
Definition: z3_api.h:108

◆ SortUniverse()

Expr [] SortUniverse ( Sort  s)
inline

The finite set of distinct values that represent the interpretation for sort s .

See also
Sorts
Parameters
sAn uninterpreted sort
Returns
An array of expressions, where each is an element of the universe of s

Definition at line 283 of file Model.cs.

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  }

◆ ToString()

override string ToString ( )
inline

Conversion of models to strings.

Returns
A string representation of the model.

Definition at line 295 of file Model.cs.

296  {
297  return Native.Z3_model_to_string(Context.nCtx, NativeObject);
298  }

Property Documentation

◆ ConstDecls

FuncDecl [] ConstDecls
get

The function declarations of the constants in the model.

Definition at line 120 of file Model.cs.

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  }
uint NumConsts
The number of constants that have an interpretation in the model.
Definition: Model.cs:113

◆ Consts

IEnumerable<KeyValuePair<FuncDecl, Expr> > Consts
get

Enumerate constants in model.

Definition at line 136 of file Model.cs.

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  }

◆ Decls

FuncDecl [] Decls
get

All symbols that have an interpretation in the model.

Definition at line 178 of file Model.cs.

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  }
uint NumFuncs
The number of function interpretations in the model.
Definition: Model.cs:155

◆ FuncDecls

FuncDecl [] FuncDecls
get

The function declarations of the function interpretations in the model.

Definition at line 162 of file Model.cs.

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  }

◆ NumConsts

uint NumConsts
get

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

Definition at line 112 of file Model.cs.

113  {
114  get { return Native.Z3_model_get_num_consts(Context.nCtx, NativeObject); }
115  }

◆ NumFuncs

uint NumFuncs
get

The number of function interpretations in the model.

Definition at line 154 of file Model.cs.

155  {
156  get { return Native.Z3_model_get_num_funcs(Context.nCtx, NativeObject); }
157  }

◆ NumSorts

uint NumSorts
get

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


Definition at line 252 of file Model.cs.

252 { get { return Native.Z3_model_get_num_sorts(Context.nCtx, NativeObject); } }

◆ Sorts

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, SortUniverse

Definition at line 264 of file Model.cs.

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  }
uint NumSorts
The number of uninterpreted sorts that the model has an interpretation for.
Definition: Model.cs:252