Z3
Public Member Functions | Data Fields
ModelRef Class Reference
+ Inheritance diagram for ModelRef:

Public Member Functions

def __init__ (self, m, ctx)
 
def __del__ (self)
 
def __repr__ (self)
 
def sexpr (self)
 
def eval (self, t, model_completion=False)
 
def evaluate (self, t, model_completion=False)
 
def __len__ (self)
 
def get_interp (self, decl)
 
def num_sorts (self)
 
def get_sort (self, idx)
 
def sorts (self)
 
def get_universe (self, s)
 
def __getitem__ (self, idx)
 
def decls (self)
 
def translate (self, target)
 
def __copy__ (self)
 
def __deepcopy__ (self)
 
- Public Member Functions inherited from Z3PPObject
def use_pp (self)
 

Data Fields

 model
 
 ctx
 

Detailed Description

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

Definition at line 5897 of file z3py.py.

Constructor & Destructor Documentation

◆ __init__()

def __init__ (   self,
  m,
  ctx 
)

Definition at line 5900 of file z3py.py.

5900  def __init__(self, m, ctx):
5901  assert ctx is not None
5902  self.model = m
5903  self.ctx = ctx
5904  Z3_model_inc_ref(self.ctx.ref(), self.model)
5905 
void Z3_API Z3_model_inc_ref(Z3_context c, Z3_model m)
Increment the reference counter of the given model.

◆ __del__()

def __del__ (   self)

Definition at line 5906 of file z3py.py.

5906  def __del__(self):
5907  if self.ctx.ref() is not None:
5908  Z3_model_dec_ref(self.ctx.ref(), self.model)
5909 
void Z3_API Z3_model_dec_ref(Z3_context c, Z3_model m)
Decrement the reference counter of the given model.

Member Function Documentation

◆ __copy__()

def __copy__ (   self)

Definition at line 6167 of file z3py.py.

6167  def __copy__(self):
6168  return self.translate(self.ctx)
6169 

◆ __deepcopy__()

def __deepcopy__ (   self)

Definition at line 6170 of file z3py.py.

6170  def __deepcopy__(self):
6171  return self.translate(self.ctx)
6172 

◆ __getitem__()

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 6096 of file z3py.py.

6096  def __getitem__(self, idx):
6097  """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.
6098 
6099  The elements can be retrieved using position or the actual declaration.
6100 
6101  >>> f = Function('f', IntSort(), IntSort())
6102  >>> x = Int('x')
6103  >>> s = Solver()
6104  >>> s.add(x > 0, x < 2, f(x) == 0)
6105  >>> s.check()
6106  sat
6107  >>> m = s.model()
6108  >>> len(m)
6109  2
6110  >>> m[0]
6111  x
6112  >>> m[1]
6113  f
6114  >>> m[x]
6115  1
6116  >>> m[f]
6117  [else -> 0]
6118  >>> for d in m: print("%s -> %s" % (d, m[d]))
6119  x -> 1
6120  f -> [else -> 0]
6121  """
6122  if _is_int(idx):
6123  if idx >= len(self):
6124  raise IndexError
6125  num_consts = Z3_model_get_num_consts(self.ctx.ref(), self.model)
6126  if (idx < num_consts):
6127  return FuncDeclRef(Z3_model_get_const_decl(self.ctx.ref(), self.model, idx), self.ctx)
6128  else:
6129  return FuncDeclRef(Z3_model_get_func_decl(self.ctx.ref(), self.model, idx - num_consts), self.ctx)
6130  if isinstance(idx, FuncDeclRef):
6131  return self.get_interp(idx)
6132  if is_const(idx):
6133  return self.get_interp(idx.decl())
6134  if isinstance(idx, SortRef):
6135  return self.get_universe(idx)
6136  if __debug__:
6137  _z3_assert(False, "Integer, Z3 declaration, or Z3 constant expected")
6138  return None
6139 
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.
def is_const(a)
Definition: z3py.py:1141
unsigned Z3_API Z3_model_get_num_consts(Z3_context c, Z3_model m)
Return the number of constants assigned by the given model.

◆ __len__()

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 5972 of file z3py.py.

5972  def __len__(self):
5973  """Return the number of constant and function declarations in the model `self`.
5974 
5975  >>> f = Function('f', IntSort(), IntSort())
5976  >>> x = Int('x')
5977  >>> s = Solver()
5978  >>> s.add(x > 0, f(x) != x)
5979  >>> s.check()
5980  sat
5981  >>> m = s.model()
5982  >>> len(m)
5983  2
5984  """
5985  return int(Z3_model_get_num_consts(self.ctx.ref(), self.model)) + int(Z3_model_get_num_funcs(self.ctx.ref(), self.model))
5986 
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.

◆ __repr__()

def __repr__ (   self)

Definition at line 5910 of file z3py.py.

5910  def __repr__(self):
5911  return obj_to_string(self)
5912 

◆ decls()

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 6140 of file z3py.py.

6140  def decls(self):
6141  """Return a list with all symbols that have an interpretation in the model `self`.
6142  >>> f = Function('f', IntSort(), IntSort())
6143  >>> x = Int('x')
6144  >>> s = Solver()
6145  >>> s.add(x > 0, x < 2, f(x) == 0)
6146  >>> s.check()
6147  sat
6148  >>> m = s.model()
6149  >>> m.decls()
6150  [x, f]
6151  """
6152  r = []
6153  for i in range(Z3_model_get_num_consts(self.ctx.ref(), self.model)):
6154  r.append(FuncDeclRef(Z3_model_get_const_decl(self.ctx.ref(), self.model, i), self.ctx))
6155  for i in range(Z3_model_get_num_funcs(self.ctx.ref(), self.model)):
6156  r.append(FuncDeclRef(Z3_model_get_func_decl(self.ctx.ref(), self.model, i), self.ctx))
6157  return r
6158 
expr range(expr const &lo, expr const &hi)
Definition: z3++.h:3244
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.

◆ eval()

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 5917 of file z3py.py.

5917  def eval(self, t, model_completion=False):
5918  """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`.
5919 
5920  >>> x = Int('x')
5921  >>> s = Solver()
5922  >>> s.add(x > 0, x < 2)
5923  >>> s.check()
5924  sat
5925  >>> m = s.model()
5926  >>> m.eval(x + 1)
5927  2
5928  >>> m.eval(x == 1)
5929  True
5930  >>> y = Int('y')
5931  >>> m.eval(y + x)
5932  1 + y
5933  >>> m.eval(y)
5934  y
5935  >>> m.eval(y, model_completion=True)
5936  0
5937  >>> # Now, m contains an interpretation for y
5938  >>> m.eval(y + x)
5939  1
5940  """
5941  r = (Ast * 1)()
5942  if Z3_model_eval(self.ctx.ref(), self.model, t.as_ast(), model_completion, r):
5943  return _to_expr_ref(r[0], self.ctx)
5944  raise Z3Exception("failed to evaluate expression in the model")
5945 
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.

Referenced by ModelRef.evaluate().

◆ evaluate()

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 5946 of file z3py.py.

5946  def evaluate(self, t, model_completion=False):
5947  """Alias for `eval`.
5948 
5949  >>> x = Int('x')
5950  >>> s = Solver()
5951  >>> s.add(x > 0, x < 2)
5952  >>> s.check()
5953  sat
5954  >>> m = s.model()
5955  >>> m.evaluate(x + 1)
5956  2
5957  >>> m.evaluate(x == 1)
5958  True
5959  >>> y = Int('y')
5960  >>> m.evaluate(y + x)
5961  1 + y
5962  >>> m.evaluate(y)
5963  y
5964  >>> m.evaluate(y, model_completion=True)
5965  0
5966  >>> # Now, m contains an interpretation for y
5967  >>> m.evaluate(y + x)
5968  1
5969  """
5970  return self.eval(t, model_completion)
5971 

◆ get_interp()

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 5987 of file z3py.py.

5987  def get_interp(self, decl):
5988  """Return the interpretation for a given declaration or constant.
5989 
5990  >>> f = Function('f', IntSort(), IntSort())
5991  >>> x = Int('x')
5992  >>> s = Solver()
5993  >>> s.add(x > 0, x < 2, f(x) == 0)
5994  >>> s.check()
5995  sat
5996  >>> m = s.model()
5997  >>> m[x]
5998  1
5999  >>> m[f]
6000  [else -> 0]
6001  """
6002  if __debug__:
6003  _z3_assert(isinstance(decl, FuncDeclRef) or is_const(decl), "Z3 declaration expected")
6004  if is_const(decl):
6005  decl = decl.decl()
6006  try:
6007  if decl.arity() == 0:
6008  _r = Z3_model_get_const_interp(self.ctx.ref(), self.model, decl.ast)
6009  if _r.value is None:
6010  return None
6011  r = _to_expr_ref(_r, self.ctx)
6012  if is_as_array(r):
6013  return self.get_interp(get_as_array_func(r))
6014  else:
6015  return r
6016  else:
6017  return FuncInterp(Z3_model_get_func_interp(self.ctx.ref(), self.model, decl.ast), self.ctx)
6018  except Z3Exception:
6019  return None
6020 
def is_const(a)
Definition: z3py.py:1141
def get_as_array_func(n)
Definition: z3py.py:6181
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_as_array(n)
Definition: z3py.py:6177

Referenced by ModelRef.__getitem__(), and ModelRef.get_interp().

◆ get_sort()

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 6036 of file z3py.py.

6036  def get_sort(self, idx):
6037  """Return the uninterpreted sort at position `idx` < self.num_sorts().
6038 
6039  >>> A = DeclareSort('A')
6040  >>> B = DeclareSort('B')
6041  >>> a1, a2 = Consts('a1 a2', A)
6042  >>> b1, b2 = Consts('b1 b2', B)
6043  >>> s = Solver()
6044  >>> s.add(a1 != a2, b1 != b2)
6045  >>> s.check()
6046  sat
6047  >>> m = s.model()
6048  >>> m.num_sorts()
6049  2
6050  >>> m.get_sort(0)
6051  A
6052  >>> m.get_sort(1)
6053  B
6054  """
6055  if idx >= self.num_sorts():
6056  raise IndexError
6057  return _to_sort_ref(Z3_model_get_sort(self.ctx.ref(), self.model, idx), self.ctx)
6058 
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.

Referenced by ModelRef.sorts().

◆ get_universe()

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 6076 of file z3py.py.

6076  def get_universe(self, s):
6077  """Return the interpretation for the uninterpreted sort `s` in the model `self`.
6078 
6079  >>> A = DeclareSort('A')
6080  >>> a, b = Consts('a b', A)
6081  >>> s = Solver()
6082  >>> s.add(a != b)
6083  >>> s.check()
6084  sat
6085  >>> m = s.model()
6086  >>> m.get_universe(A)
6087  [A!val!0, A!val!1]
6088  """
6089  if __debug__:
6090  _z3_assert(isinstance(s, SortRef), "Z3 sort expected")
6091  try:
6092  return AstVector(Z3_model_get_sort_universe(self.ctx.ref(), self.model, s.ast), self.ctx)
6093  except Z3Exception:
6094  return None
6095 
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.

Referenced by ModelRef.__getitem__().

◆ num_sorts()

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 6021 of file z3py.py.

6021  def num_sorts(self):
6022  """Return the number of uninterpreted sorts that contain an interpretation in the model `self`.
6023 
6024  >>> A = DeclareSort('A')
6025  >>> a, b = Consts('a b', A)
6026  >>> s = Solver()
6027  >>> s.add(a != b)
6028  >>> s.check()
6029  sat
6030  >>> m = s.model()
6031  >>> m.num_sorts()
6032  1
6033  """
6034  return int(Z3_model_get_num_sorts(self.ctx.ref(), self.model))
6035 
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.

Referenced by ModelRef.get_sort(), and ModelRef.sorts().

◆ sexpr()

def sexpr (   self)
Return a textual representation of the s-expression representing the model.

Definition at line 5913 of file z3py.py.

5913  def sexpr(self):
5914  """Return a textual representation of the s-expression representing the model."""
5915  return Z3_model_to_string(self.ctx.ref(), self.model)
5916 
Z3_string Z3_API Z3_model_to_string(Z3_context c, Z3_model m)
Convert the given model into a string.

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

◆ sorts()

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 6059 of file z3py.py.

6059  def sorts(self):
6060  """Return all uninterpreted sorts that have an interpretation in the model `self`.
6061 
6062  >>> A = DeclareSort('A')
6063  >>> B = DeclareSort('B')
6064  >>> a1, a2 = Consts('a1 a2', A)
6065  >>> b1, b2 = Consts('b1 b2', B)
6066  >>> s = Solver()
6067  >>> s.add(a1 != a2, b1 != b2)
6068  >>> s.check()
6069  sat
6070  >>> m = s.model()
6071  >>> m.sorts()
6072  [A, B]
6073  """
6074  return [ self.get_sort(i) for i in range(self.num_sorts()) ]
6075 
expr range(expr const &lo, expr const &hi)
Definition: z3++.h:3244

◆ translate()

def translate (   self,
  target 
)
Translate `self` to the context `target`. That is, return a copy of `self` in the context `target`.

Definition at line 6159 of file z3py.py.

6159  def translate(self, target):
6160  """Translate `self` to the context `target`. That is, return a copy of `self` in the context `target`.
6161  """
6162  if __debug__:
6163  _z3_assert(isinstance(target, Context), "argument must be a Z3 context")
6164  model = Z3_model_translate(self.ctx.ref(), self.model, target.ref())
6165  return Model(model, target)
6166 
def Model(ctx=None)
Definition: z3py.py:6173
Z3_model Z3_API Z3_model_translate(Z3_context c, Z3_model m, Z3_context dst)
translate model from context c to context dst.

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

Field Documentation

◆ ctx

ctx

Definition at line 5903 of file z3py.py.

Referenced by Probe.__call__(), ModelRef.__copy__(), Solver.__copy__(), ModelRef.__deepcopy__(), Statistics.__deepcopy__(), Solver.__deepcopy__(), Fixedpoint.__deepcopy__(), Optimize.__deepcopy__(), ApplyResult.__deepcopy__(), Tactic.__deepcopy__(), Probe.__deepcopy__(), ModelRef.__del__(), Statistics.__del__(), Solver.__del__(), Fixedpoint.__del__(), Optimize.__del__(), ApplyResult.__del__(), Tactic.__del__(), Probe.__del__(), Probe.__eq__(), Probe.__ge__(), ModelRef.__getitem__(), Statistics.__getitem__(), ApplyResult.__getitem__(), Probe.__gt__(), Probe.__le__(), ModelRef.__len__(), Statistics.__len__(), ApplyResult.__len__(), Probe.__lt__(), Probe.__ne__(), Statistics.__repr__(), Fixedpoint.add_cover(), Fixedpoint.add_rule(), Optimize.add_soft(), Tactic.apply(), ApplyResult.as_expr(), Solver.assert_and_track(), Solver.assert_exprs(), Fixedpoint.assert_exprs(), Optimize.assert_exprs(), Solver.assertions(), Optimize.assertions(), Solver.check(), Optimize.check(), Solver.consequences(), ModelRef.decls(), ModelRef.eval(), Solver.from_file(), Optimize.from_file(), Solver.from_string(), Optimize.from_string(), Fixedpoint.get_answer(), Fixedpoint.get_assertions(), Fixedpoint.get_cover_delta(), Fixedpoint.get_ground_sat_answer(), ModelRef.get_interp(), Statistics.get_key_value(), Fixedpoint.get_num_levels(), Fixedpoint.get_rule_names_along_trace(), Fixedpoint.get_rules(), Fixedpoint.get_rules_along_trace(), ModelRef.get_sort(), ModelRef.get_universe(), Solver.help(), Fixedpoint.help(), Optimize.help(), Tactic.help(), Statistics.keys(), Optimize.maximize(), Optimize.minimize(), Solver.model(), Optimize.model(), Solver.non_units(), Solver.num_scopes(), ModelRef.num_sorts(), Optimize.objectives(), Solver.param_descrs(), Fixedpoint.param_descrs(), Optimize.param_descrs(), Tactic.param_descrs(), Fixedpoint.parse_file(), Fixedpoint.parse_string(), Solver.pop(), Fixedpoint.pop(), Optimize.pop(), Solver.proof(), Solver.push(), Fixedpoint.push(), Optimize.push(), Fixedpoint.query(), Fixedpoint.query_from_lvl(), Solver.reason_unknown(), Fixedpoint.reason_unknown(), Optimize.reason_unknown(), Fixedpoint.register_relation(), Solver.reset(), Solver.set(), Fixedpoint.set(), Optimize.set(), Fixedpoint.set_predicate_representation(), ModelRef.sexpr(), Solver.sexpr(), Fixedpoint.sexpr(), Optimize.sexpr(), ApplyResult.sexpr(), Tactic.solver(), Solver.statistics(), Fixedpoint.statistics(), Optimize.statistics(), Solver.to_smt2(), Fixedpoint.to_string(), ModelRef.translate(), Solver.translate(), Solver.units(), Solver.unsat_core(), Optimize.unsat_core(), and Fixedpoint.update_rule().

◆ model

model