Z3
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Public Member Functions | Data Fields
ModelRef Class Reference
+ Inheritance diagram for ModelRef:

Public Member Functions

def __init__
 
def __del__
 
def __repr__
 
def sexpr
 
def eval
 
def evaluate
 
def __len__
 
def get_interp
 
def num_sorts
 
def get_sort
 
def sorts
 
def get_universe
 
def __getitem__
 
def decls
 
def translate
 
def __copy__
 
def __deepcopy__
 
- Public Member Functions inherited from Z3PPObject
def use_pp
 

Data Fields

 model
 
 ctx
 

Detailed Description

Model/Solution of a satisfiability problem (aka system of constraints).

Definition at line 5940 of file z3py.py.

Constructor & Destructor Documentation

def __init__ (   self,
  m,
  ctx 
)

Definition at line 5943 of file z3py.py.

5944  def __init__(self, m, ctx):
5945  assert ctx is not None
5946  self.model = m
5947  self.ctx = ctx
5948  Z3_model_inc_ref(self.ctx.ref(), self.model)
def __init__
Definition: z3py.py:5943
void Z3_API Z3_model_inc_ref(Z3_context c, Z3_model m)
Increment the reference counter of the given model.
def __del__ (   self)

Definition at line 5949 of file z3py.py.

5950  def __del__(self):
5951  if self.ctx.ref() is not None:
5952  Z3_model_dec_ref(self.ctx.ref(), self.model)
void Z3_API Z3_model_dec_ref(Z3_context c, Z3_model m)
Decrement the reference counter of the given model.
def __del__
Definition: z3py.py:5949

Member Function Documentation

def __copy__ (   self)

Definition at line 6210 of file z3py.py.

6211  def __copy__(self):
6212  return self.translate(self.ctx)
def __copy__
Definition: z3py.py:6210
def translate
Definition: z3py.py:6202
def __deepcopy__ (   self,
  memo = {} 
)

Definition at line 6213 of file z3py.py.

6214  def __deepcopy__(self, memo={}):
6215  return self.translate(self.ctx)
def translate
Definition: z3py.py:6202
def __deepcopy__
Definition: z3py.py:6213
def __getitem__ (   self,
  idx 
)
If `idx` is an integer, then the declaration at position `idx` in the model `self` is returned. If `idx` is a declaration, then the actual interpretation is returned.

The elements can be retrieved using position or the actual declaration.

>>> f = Function('f', IntSort(), IntSort())
>>> x = Int('x')
>>> s = Solver()
>>> s.add(x > 0, x < 2, f(x) == 0)
>>> s.check()
sat
>>> m = s.model()
>>> len(m)
2
>>> m[0]
x
>>> m[1]
f
>>> m[x]
1
>>> m[f]
[else -> 0]
>>> for d in m: print("%s -> %s" % (d, m[d]))
x -> 1
f -> [else -> 0]

Definition at line 6139 of file z3py.py.

6140  def __getitem__(self, idx):
6141  """If `idx` is an integer, then the declaration at position `idx` in the model `self` is returned. If `idx` is a declaration, then the actual interpretation is returned.
6142 
6143  The elements can be retrieved using position or the actual declaration.
6144 
6145  >>> f = Function('f', IntSort(), IntSort())
6146  >>> x = Int('x')
6147  >>> s = Solver()
6148  >>> s.add(x > 0, x < 2, f(x) == 0)
6149  >>> s.check()
6150  sat
6151  >>> m = s.model()
6152  >>> len(m)
6153  2
6154  >>> m[0]
6155  x
6156  >>> m[1]
6157  f
6158  >>> m[x]
6159  1
6160  >>> m[f]
6161  [else -> 0]
6162  >>> for d in m: print("%s -> %s" % (d, m[d]))
6163  x -> 1
6164  f -> [else -> 0]
6165  """
6166  if _is_int(idx):
6167  if idx >= len(self):
6168  raise IndexError
6169  num_consts = Z3_model_get_num_consts(self.ctx.ref(), self.model)
6170  if (idx < num_consts):
6171  return FuncDeclRef(Z3_model_get_const_decl(self.ctx.ref(), self.model, idx), self.ctx)
6172  else:
6173  return FuncDeclRef(Z3_model_get_func_decl(self.ctx.ref(), self.model, idx - num_consts), self.ctx)
6174  if isinstance(idx, FuncDeclRef):
6175  return self.get_interp(idx)
6176  if is_const(idx):
6177  return self.get_interp(idx.decl())
6178  if isinstance(idx, SortRef):
6179  return self.get_universe(idx)
6180  if z3_debug():
6181  _z3_assert(False, "Integer, Z3 declaration, or Z3 constant expected")
6182  return None
Function Declarations.
Definition: z3py.py:651
Z3_func_decl Z3_API Z3_model_get_func_decl(Z3_context c, Z3_model m, unsigned i)
Return the declaration of the i-th function in the given model.
def get_universe
Definition: z3py.py:6119
Z3_func_decl Z3_API Z3_model_get_const_decl(Z3_context c, Z3_model m, unsigned i)
Return the i-th constant in the given model.
unsigned Z3_API Z3_model_get_num_consts(Z3_context c, Z3_model m)
Return the number of constants assigned by the given model.
def is_const
Definition: z3py.py:1152
def __getitem__
Definition: z3py.py:6139
def z3_debug
Definition: z3py.py:58
def get_interp
Definition: z3py.py:6030
def __len__ (   self)
Return the number of constant and function declarations in the model `self`.

>>> f = Function('f', IntSort(), IntSort())
>>> x = Int('x')
>>> s = Solver()
>>> s.add(x > 0, f(x) != x)
>>> s.check()
sat
>>> m = s.model()
>>> len(m)
2

Definition at line 6015 of file z3py.py.

6016  def __len__(self):
6017  """Return the number of constant and function declarations in the model `self`.
6018 
6019  >>> f = Function('f', IntSort(), IntSort())
6020  >>> x = Int('x')
6021  >>> s = Solver()
6022  >>> s.add(x > 0, f(x) != x)
6023  >>> s.check()
6024  sat
6025  >>> m = s.model()
6026  >>> len(m)
6027  2
6028  """
6029  return int(Z3_model_get_num_consts(self.ctx.ref(), self.model)) + int(Z3_model_get_num_funcs(self.ctx.ref(), self.model))
def __len__
Definition: z3py.py:6015
unsigned Z3_API Z3_model_get_num_consts(Z3_context c, Z3_model m)
Return the number of constants assigned by the given model.
unsigned Z3_API Z3_model_get_num_funcs(Z3_context c, Z3_model m)
Return the number of function interpretations in the given model.
def __repr__ (   self)

Definition at line 5953 of file z3py.py.

5954  def __repr__(self):
5955  return obj_to_string(self)
def __repr__
Definition: z3py.py:5953
def decls (   self)
Return a list with all symbols that have an interpretation in the model `self`.
>>> f = Function('f', IntSort(), IntSort())
>>> x = Int('x')
>>> s = Solver()
>>> s.add(x > 0, x < 2, f(x) == 0)
>>> s.check()
sat
>>> m = s.model()
>>> m.decls()
[x, f]

Definition at line 6183 of file z3py.py.

6184  def decls(self):
6185  """Return a list with all symbols that have an interpretation in the model `self`.
6186  >>> f = Function('f', IntSort(), IntSort())
6187  >>> x = Int('x')
6188  >>> s = Solver()
6189  >>> s.add(x > 0, x < 2, f(x) == 0)
6190  >>> s.check()
6191  sat
6192  >>> m = s.model()
6193  >>> m.decls()
6194  [x, f]
6195  """
6196  r = []
6197  for i in range(Z3_model_get_num_consts(self.ctx.ref(), self.model)):
6198  r.append(FuncDeclRef(Z3_model_get_const_decl(self.ctx.ref(), self.model, i), self.ctx))
6199  for i in range(Z3_model_get_num_funcs(self.ctx.ref(), self.model)):
6200  r.append(FuncDeclRef(Z3_model_get_func_decl(self.ctx.ref(), self.model, i), self.ctx))
6201  return r
Function Declarations.
Definition: z3py.py:651
expr range(expr const &lo, expr const &hi)
Definition: z3++.h:3358
Z3_func_decl Z3_API Z3_model_get_func_decl(Z3_context c, Z3_model m, unsigned i)
Return the declaration of the i-th function in the given model.
Z3_func_decl Z3_API Z3_model_get_const_decl(Z3_context c, Z3_model m, unsigned i)
Return the i-th constant in the given model.
unsigned Z3_API Z3_model_get_num_consts(Z3_context c, Z3_model m)
Return the number of constants assigned by the given model.
unsigned Z3_API Z3_model_get_num_funcs(Z3_context c, Z3_model m)
Return the number of function interpretations in the given model.
def decls
Definition: z3py.py:6183
def eval (   self,
  t,
  model_completion = False 
)
Evaluate the expression `t` in the model `self`. If `model_completion` is enabled, then a default interpretation is automatically added for symbols that do not have an interpretation in the model `self`.

>>> x = Int('x')
>>> s = Solver()
>>> s.add(x > 0, x < 2)
>>> s.check()
sat
>>> m = s.model()
>>> m.eval(x + 1)
2
>>> m.eval(x == 1)
True
>>> y = Int('y')
>>> m.eval(y + x)
1 + y
>>> m.eval(y)
y
>>> m.eval(y, model_completion=True)
0
>>> # Now, m contains an interpretation for y
>>> m.eval(y + x)
1

Definition at line 5960 of file z3py.py.

5961  def eval(self, t, model_completion=False):
5962  """Evaluate the expression `t` in the model `self`. If `model_completion` is enabled, then a default interpretation is automatically added for symbols that do not have an interpretation in the model `self`.
5963 
5964  >>> x = Int('x')
5965  >>> s = Solver()
5966  >>> s.add(x > 0, x < 2)
5967  >>> s.check()
5968  sat
5969  >>> m = s.model()
5970  >>> m.eval(x + 1)
5971  2
5972  >>> m.eval(x == 1)
5973  True
5974  >>> y = Int('y')
5975  >>> m.eval(y + x)
5976  1 + y
5977  >>> m.eval(y)
5978  y
5979  >>> m.eval(y, model_completion=True)
5980  0
5981  >>> # Now, m contains an interpretation for y
5982  >>> m.eval(y + x)
5983  1
5984  """
5985  r = (Ast * 1)()
5986  if Z3_model_eval(self.ctx.ref(), self.model, t.as_ast(), model_completion, r):
5987  return _to_expr_ref(r[0], self.ctx)
5988  raise Z3Exception("failed to evaluate expression in the model")
Z3_bool Z3_API Z3_model_eval(Z3_context c, Z3_model m, Z3_ast t, bool model_completion, Z3_ast *v)
Evaluate the AST node t in the given model. Return true if succeeded, and store the result in v...
def eval
Definition: z3py.py:5960
def evaluate (   self,
  t,
  model_completion = False 
)
Alias for `eval`.

>>> x = Int('x')
>>> s = Solver()
>>> s.add(x > 0, x < 2)
>>> s.check()
sat
>>> m = s.model()
>>> m.evaluate(x + 1)
2
>>> m.evaluate(x == 1)
True
>>> y = Int('y')
>>> m.evaluate(y + x)
1 + y
>>> m.evaluate(y)
y
>>> m.evaluate(y, model_completion=True)
0
>>> # Now, m contains an interpretation for y
>>> m.evaluate(y + x)
1

Definition at line 5989 of file z3py.py.

5990  def evaluate(self, t, model_completion=False):
5991  """Alias for `eval`.
5992 
5993  >>> x = Int('x')
5994  >>> s = Solver()
5995  >>> s.add(x > 0, x < 2)
5996  >>> s.check()
5997  sat
5998  >>> m = s.model()
5999  >>> m.evaluate(x + 1)
6000  2
6001  >>> m.evaluate(x == 1)
6002  True
6003  >>> y = Int('y')
6004  >>> m.evaluate(y + x)
6005  1 + y
6006  >>> m.evaluate(y)
6007  y
6008  >>> m.evaluate(y, model_completion=True)
6009  0
6010  >>> # Now, m contains an interpretation for y
6011  >>> m.evaluate(y + x)
6012  1
6013  """
6014  return self.eval(t, model_completion)
def evaluate
Definition: z3py.py:5989
def eval
Definition: z3py.py:5960
def get_interp (   self,
  decl 
)
Return the interpretation for a given declaration or constant.

>>> f = Function('f', IntSort(), IntSort())
>>> x = Int('x')
>>> s = Solver()
>>> s.add(x > 0, x < 2, f(x) == 0)
>>> s.check()
sat
>>> m = s.model()
>>> m[x]
1
>>> m[f]
[else -> 0]

Definition at line 6030 of file z3py.py.

Referenced by ModelRef.__getitem__().

6031  def get_interp(self, decl):
6032  """Return the interpretation for a given declaration or constant.
6033 
6034  >>> f = Function('f', IntSort(), IntSort())
6035  >>> x = Int('x')
6036  >>> s = Solver()
6037  >>> s.add(x > 0, x < 2, f(x) == 0)
6038  >>> s.check()
6039  sat
6040  >>> m = s.model()
6041  >>> m[x]
6042  1
6043  >>> m[f]
6044  [else -> 0]
6045  """
6046  if z3_debug():
6047  _z3_assert(isinstance(decl, FuncDeclRef) or is_const(decl), "Z3 declaration expected")
6048  if is_const(decl):
6049  decl = decl.decl()
6050  try:
6051  if decl.arity() == 0:
6052  _r = Z3_model_get_const_interp(self.ctx.ref(), self.model, decl.ast)
6053  if _r.value is None:
6054  return None
6055  r = _to_expr_ref(_r, self.ctx)
6056  if is_as_array(r):
6057  return self.get_interp(get_as_array_func(r))
6058  else:
6059  return r
6060  else:
6061  return FuncInterp(Z3_model_get_func_interp(self.ctx.ref(), self.model, decl.ast), self.ctx)
6062  except Z3Exception:
6063  return None
def get_as_array_func
Definition: z3py.py:6224
Z3_func_interp Z3_API Z3_model_get_func_interp(Z3_context c, Z3_model m, Z3_func_decl f)
Return the interpretation of the function f in the model m. Return NULL, if the model does not assign...
Z3_ast Z3_API Z3_model_get_const_interp(Z3_context c, Z3_model m, Z3_func_decl a)
Return the interpretation (i.e., assignment) of constant a in the model m. Return NULL...
def is_const
Definition: z3py.py:1152
def z3_debug
Definition: z3py.py:58
def is_as_array
Definition: z3py.py:6220
def get_interp
Definition: z3py.py:6030
def get_sort (   self,
  idx 
)
Return the uninterpreted sort at position `idx` < self.num_sorts().

>>> A = DeclareSort('A')
>>> B = DeclareSort('B')
>>> a1, a2 = Consts('a1 a2', A)
>>> b1, b2 = Consts('b1 b2', B)
>>> s = Solver()
>>> s.add(a1 != a2, b1 != b2)
>>> s.check()
sat
>>> m = s.model()
>>> m.num_sorts()
2
>>> m.get_sort(0)
A
>>> m.get_sort(1)
B

Definition at line 6079 of file z3py.py.

6080  def get_sort(self, idx):
6081  """Return the uninterpreted sort at position `idx` < self.num_sorts().
6082 
6083  >>> A = DeclareSort('A')
6084  >>> B = DeclareSort('B')
6085  >>> a1, a2 = Consts('a1 a2', A)
6086  >>> b1, b2 = Consts('b1 b2', B)
6087  >>> s = Solver()
6088  >>> s.add(a1 != a2, b1 != b2)
6089  >>> s.check()
6090  sat
6091  >>> m = s.model()
6092  >>> m.num_sorts()
6093  2
6094  >>> m.get_sort(0)
6095  A
6096  >>> m.get_sort(1)
6097  B
6098  """
6099  if idx >= self.num_sorts():
6100  raise IndexError
6101  return _to_sort_ref(Z3_model_get_sort(self.ctx.ref(), self.model, idx), self.ctx)
Z3_sort Z3_API Z3_model_get_sort(Z3_context c, Z3_model m, unsigned i)
Return a uninterpreted sort that m assigns an interpretation.
def num_sorts
Definition: z3py.py:6064
def get_sort
Definition: z3py.py:6079
def get_universe (   self,
  s 
)
Return the interpretation for the uninterpreted sort `s` in the model `self`.

>>> A = DeclareSort('A')
>>> a, b = Consts('a b', A)
>>> s = Solver()
>>> s.add(a != b)
>>> s.check()
sat
>>> m = s.model()
>>> m.get_universe(A)
[A!val!0, A!val!1]

Definition at line 6119 of file z3py.py.

Referenced by ModelRef.__getitem__().

6120  def get_universe(self, s):
6121  """Return the interpretation for the uninterpreted sort `s` in the model `self`.
6122 
6123  >>> A = DeclareSort('A')
6124  >>> a, b = Consts('a b', A)
6125  >>> s = Solver()
6126  >>> s.add(a != b)
6127  >>> s.check()
6128  sat
6129  >>> m = s.model()
6130  >>> m.get_universe(A)
6131  [A!val!0, A!val!1]
6132  """
6133  if z3_debug():
6134  _z3_assert(isinstance(s, SortRef), "Z3 sort expected")
6135  try:
6136  return AstVector(Z3_model_get_sort_universe(self.ctx.ref(), self.model, s.ast), self.ctx)
6137  except Z3Exception:
6138  return None
Z3_ast_vector Z3_API Z3_model_get_sort_universe(Z3_context c, Z3_model m, Z3_sort s)
Return the finite set of distinct values that represent the interpretation for sort s...
def get_universe
Definition: z3py.py:6119
def z3_debug
Definition: z3py.py:58
def num_sorts (   self)
Return the number of uninterpreted sorts that contain an interpretation in the model `self`.

>>> A = DeclareSort('A')
>>> a, b = Consts('a b', A)
>>> s = Solver()
>>> s.add(a != b)
>>> s.check()
sat
>>> m = s.model()
>>> m.num_sorts()
1

Definition at line 6064 of file z3py.py.

Referenced by ModelRef.get_sort().

6065  def num_sorts(self):
6066  """Return the number of uninterpreted sorts that contain an interpretation in the model `self`.
6067 
6068  >>> A = DeclareSort('A')
6069  >>> a, b = Consts('a b', A)
6070  >>> s = Solver()
6071  >>> s.add(a != b)
6072  >>> s.check()
6073  sat
6074  >>> m = s.model()
6075  >>> m.num_sorts()
6076  1
6077  """
6078  return int(Z3_model_get_num_sorts(self.ctx.ref(), self.model))
unsigned Z3_API Z3_model_get_num_sorts(Z3_context c, Z3_model m)
Return the number of uninterpreted sorts that m assigns an interpretation to.
def num_sorts
Definition: z3py.py:6064
def sexpr (   self)
Return a textual representation of the s-expression representing the model.

Definition at line 5956 of file z3py.py.

Referenced by Fixedpoint.__repr__(), and Optimize.__repr__().

5957  def sexpr(self):
5958  """Return a textual representation of the s-expression representing the model."""
5959  return Z3_model_to_string(self.ctx.ref(), self.model)
def sexpr
Definition: z3py.py:5956
Z3_string Z3_API Z3_model_to_string(Z3_context c, Z3_model m)
Convert the given model into a string.
def sorts (   self)
Return all uninterpreted sorts that have an interpretation in the model `self`.

>>> A = DeclareSort('A')
>>> B = DeclareSort('B')
>>> a1, a2 = Consts('a1 a2', A)
>>> b1, b2 = Consts('b1 b2', B)
>>> s = Solver()
>>> s.add(a1 != a2, b1 != b2)
>>> s.check()
sat
>>> m = s.model()
>>> m.sorts()
[A, B]

Definition at line 6102 of file z3py.py.

6103  def sorts(self):
6104  """Return all uninterpreted sorts that have an interpretation in the model `self`.
6105 
6106  >>> A = DeclareSort('A')
6107  >>> B = DeclareSort('B')
6108  >>> a1, a2 = Consts('a1 a2', A)
6109  >>> b1, b2 = Consts('b1 b2', B)
6110  >>> s = Solver()
6111  >>> s.add(a1 != a2, b1 != b2)
6112  >>> s.check()
6113  sat
6114  >>> m = s.model()
6115  >>> m.sorts()
6116  [A, B]
6117  """
6118  return [ self.get_sort(i) for i in range(self.num_sorts()) ]
expr range(expr const &lo, expr const &hi)
Definition: z3++.h:3358
def num_sorts
Definition: z3py.py:6064
def sorts
Definition: z3py.py:6102
def get_sort
Definition: z3py.py:6079
def translate (   self,
  target 
)
Translate `self` to the context `target`. That is, return a copy of `self` in the context `target`.

Definition at line 6202 of file z3py.py.

Referenced by ModelRef.__copy__(), and ModelRef.__deepcopy__().

6203  def translate(self, target):
6204  """Translate `self` to the context `target`. That is, return a copy of `self` in the context `target`.
6205  """
6206  if z3_debug():
6207  _z3_assert(isinstance(target, Context), "argument must be a Z3 context")
6208  model = Z3_model_translate(self.ctx.ref(), self.model, target.ref())
6209  return Model(model, target)
def translate
Definition: z3py.py:6202
Z3_model Z3_API Z3_model_translate(Z3_context c, Z3_model m, Z3_context dst)
translate model from context c to context dst.
def Model
Definition: z3py.py:6216
def z3_debug
Definition: z3py.py:58

Field Documentation

ctx

Definition at line 5946 of file z3py.py.

Referenced by ModelRef.__copy__(), ModelRef.__deepcopy__(), Fixedpoint.__deepcopy__(), Optimize.__deepcopy__(), ApplyResult.__deepcopy__(), Tactic.__deepcopy__(), Probe.__deepcopy__(), Probe.__eq__(), Probe.__ge__(), ModelRef.__getitem__(), ApplyResult.__getitem__(), Probe.__gt__(), Probe.__le__(), Probe.__lt__(), Probe.__ne__(), Fixedpoint.add_rule(), Optimize.add_soft(), Tactic.apply(), ApplyResult.as_expr(), Optimize.assert_and_track(), Fixedpoint.assert_exprs(), Optimize.assert_exprs(), Optimize.assertions(), ModelRef.decls(), ModelRef.eval(), Fixedpoint.get_answer(), Fixedpoint.get_assertions(), Fixedpoint.get_cover_delta(), Fixedpoint.get_ground_sat_answer(), ModelRef.get_interp(), Fixedpoint.get_rule_names_along_trace(), Fixedpoint.get_rules(), Fixedpoint.get_rules_along_trace(), ModelRef.get_sort(), ModelRef.get_universe(), Optimize.model(), Optimize.objectives(), Fixedpoint.param_descrs(), Optimize.param_descrs(), Tactic.param_descrs(), Fixedpoint.parse_file(), Fixedpoint.parse_string(), Fixedpoint.query(), Fixedpoint.set(), Optimize.set(), Tactic.solver(), Fixedpoint.statistics(), Optimize.statistics(), Solver.to_smt2(), Optimize.unsat_core(), and Fixedpoint.update_rule().

model

Definition at line 5945 of file z3py.py.

Referenced by ModelRef.__del__(), ModelRef.__getitem__(), ModelRef.__len__(), ModelRef.decls(), ModelRef.eval(), ModelRef.get_interp(), ModelRef.get_sort(), ModelRef.get_universe(), ModelRef.num_sorts(), ModelRef.sexpr(), FuncInterp.translate(), and ModelRef.translate().