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, memo={})
 
- 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 6297 of file z3py.py.

Constructor & Destructor Documentation

◆ __init__()

def __init__ (   self,
  m,
  ctx 
)

Definition at line 6300 of file z3py.py.

6300  def __init__(self, m, ctx):
6301  assert ctx is not None
6302  self.model = m
6303  self.ctx = ctx
6304  Z3_model_inc_ref(self.ctx.ref(), self.model)
6305 

◆ __del__()

def __del__ (   self)

Definition at line 6306 of file z3py.py.

6306  def __del__(self):
6307  if self.ctx.ref() is not None:
6308  Z3_model_dec_ref(self.ctx.ref(), self.model)
6309 

Member Function Documentation

◆ __copy__()

def __copy__ (   self)

Definition at line 6572 of file z3py.py.

6572  def __copy__(self):
6573  return self.translate(self.ctx)
6574 

◆ __deepcopy__()

def __deepcopy__ (   self,
  memo = {} 
)

Definition at line 6575 of file z3py.py.

6575  def __deepcopy__(self, memo={}):
6576  return self.translate(self.ctx)
6577 
6578 

◆ __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 6500 of file z3py.py.

6500  def __getitem__(self, idx):
6501  """If `idx` is an integer, then the declaration at position `idx` in the model `self` is returned.
6502  If `idx` is a declaration, then the actual interpretation is returned.
6503 
6504  The elements can be retrieved using position or the actual declaration.
6505 
6506  >>> f = Function('f', IntSort(), IntSort())
6507  >>> x = Int('x')
6508  >>> s = Solver()
6509  >>> s.add(x > 0, x < 2, f(x) == 0)
6510  >>> s.check()
6511  sat
6512  >>> m = s.model()
6513  >>> len(m)
6514  2
6515  >>> m[0]
6516  x
6517  >>> m[1]
6518  f
6519  >>> m[x]
6520  1
6521  >>> m[f]
6522  [else -> 0]
6523  >>> for d in m: print("%s -> %s" % (d, m[d]))
6524  x -> 1
6525  f -> [else -> 0]
6526  """
6527  if _is_int(idx):
6528  if idx >= len(self):
6529  raise IndexError
6530  num_consts = Z3_model_get_num_consts(self.ctx.ref(), self.model)
6531  if (idx < num_consts):
6532  return FuncDeclRef(Z3_model_get_const_decl(self.ctx.ref(), self.model, idx), self.ctx)
6533  else:
6534  return FuncDeclRef(Z3_model_get_func_decl(self.ctx.ref(), self.model, idx - num_consts), self.ctx)
6535  if isinstance(idx, FuncDeclRef):
6536  return self.get_interp(idx)
6537  if is_const(idx):
6538  return self.get_interp(idx.decl())
6539  if isinstance(idx, SortRef):
6540  return self.get_universe(idx)
6541  if z3_debug():
6542  _z3_assert(False, "Integer, Z3 declaration, or Z3 constant expected")
6543  return None
6544 

◆ __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 6374 of file z3py.py.

6374  def __len__(self):
6375  """Return the number of constant and function declarations in the model `self`.
6376 
6377  >>> f = Function('f', IntSort(), IntSort())
6378  >>> x = Int('x')
6379  >>> s = Solver()
6380  >>> s.add(x > 0, f(x) != x)
6381  >>> s.check()
6382  sat
6383  >>> m = s.model()
6384  >>> len(m)
6385  2
6386  """
6387  num_consts = int(Z3_model_get_num_consts(self.ctx.ref(), self.model))
6388  num_funcs = int(Z3_model_get_num_funcs(self.ctx.ref(), self.model))
6389  return num_consts + num_funcs
6390 

◆ __repr__()

def __repr__ (   self)

Definition at line 6310 of file z3py.py.

6310  def __repr__(self):
6311  return obj_to_string(self)
6312 

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

6545  def decls(self):
6546  """Return a list with all symbols that have an interpretation in the model `self`.
6547  >>> f = Function('f', IntSort(), IntSort())
6548  >>> x = Int('x')
6549  >>> s = Solver()
6550  >>> s.add(x > 0, x < 2, f(x) == 0)
6551  >>> s.check()
6552  sat
6553  >>> m = s.model()
6554  >>> m.decls()
6555  [x, f]
6556  """
6557  r = []
6558  for i in range(Z3_model_get_num_consts(self.ctx.ref(), self.model)):
6559  r.append(FuncDeclRef(Z3_model_get_const_decl(self.ctx.ref(), self.model, i), self.ctx))
6560  for i in range(Z3_model_get_num_funcs(self.ctx.ref(), self.model)):
6561  r.append(FuncDeclRef(Z3_model_get_func_decl(self.ctx.ref(), self.model, i), self.ctx))
6562  return r
6563 

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

6317  def eval(self, t, model_completion=False):
6318  """Evaluate the expression `t` in the model `self`.
6319  If `model_completion` is enabled, then a default interpretation is automatically added
6320  for symbols that do not have an interpretation in the model `self`.
6321 
6322  >>> x = Int('x')
6323  >>> s = Solver()
6324  >>> s.add(x > 0, x < 2)
6325  >>> s.check()
6326  sat
6327  >>> m = s.model()
6328  >>> m.eval(x + 1)
6329  2
6330  >>> m.eval(x == 1)
6331  True
6332  >>> y = Int('y')
6333  >>> m.eval(y + x)
6334  1 + y
6335  >>> m.eval(y)
6336  y
6337  >>> m.eval(y, model_completion=True)
6338  0
6339  >>> # Now, m contains an interpretation for y
6340  >>> m.eval(y + x)
6341  1
6342  """
6343  r = (Ast * 1)()
6344  if Z3_model_eval(self.ctx.ref(), self.model, t.as_ast(), model_completion, r):
6345  return _to_expr_ref(r[0], self.ctx)
6346  raise Z3Exception("failed to evaluate expression in the model")
6347 

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

6348  def evaluate(self, t, model_completion=False):
6349  """Alias for `eval`.
6350 
6351  >>> x = Int('x')
6352  >>> s = Solver()
6353  >>> s.add(x > 0, x < 2)
6354  >>> s.check()
6355  sat
6356  >>> m = s.model()
6357  >>> m.evaluate(x + 1)
6358  2
6359  >>> m.evaluate(x == 1)
6360  True
6361  >>> y = Int('y')
6362  >>> m.evaluate(y + x)
6363  1 + y
6364  >>> m.evaluate(y)
6365  y
6366  >>> m.evaluate(y, model_completion=True)
6367  0
6368  >>> # Now, m contains an interpretation for y
6369  >>> m.evaluate(y + x)
6370  1
6371  """
6372  return self.eval(t, model_completion)
6373 

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

6391  def get_interp(self, decl):
6392  """Return the interpretation for a given declaration or constant.
6393 
6394  >>> f = Function('f', IntSort(), IntSort())
6395  >>> x = Int('x')
6396  >>> s = Solver()
6397  >>> s.add(x > 0, x < 2, f(x) == 0)
6398  >>> s.check()
6399  sat
6400  >>> m = s.model()
6401  >>> m[x]
6402  1
6403  >>> m[f]
6404  [else -> 0]
6405  """
6406  if z3_debug():
6407  _z3_assert(isinstance(decl, FuncDeclRef) or is_const(decl), "Z3 declaration expected")
6408  if is_const(decl):
6409  decl = decl.decl()
6410  try:
6411  if decl.arity() == 0:
6412  _r = Z3_model_get_const_interp(self.ctx.ref(), self.model, decl.ast)
6413  if _r.value is None:
6414  return None
6415  r = _to_expr_ref(_r, self.ctx)
6416  if is_as_array(r):
6417  return self.get_interp(get_as_array_func(r))
6418  else:
6419  return r
6420  else:
6421  return FuncInterp(Z3_model_get_func_interp(self.ctx.ref(), self.model, decl.ast), self.ctx)
6422  except Z3Exception:
6423  return None
6424 

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

6440  def get_sort(self, idx):
6441  """Return the uninterpreted sort at position `idx` < self.num_sorts().
6442 
6443  >>> A = DeclareSort('A')
6444  >>> B = DeclareSort('B')
6445  >>> a1, a2 = Consts('a1 a2', A)
6446  >>> b1, b2 = Consts('b1 b2', B)
6447  >>> s = Solver()
6448  >>> s.add(a1 != a2, b1 != b2)
6449  >>> s.check()
6450  sat
6451  >>> m = s.model()
6452  >>> m.num_sorts()
6453  2
6454  >>> m.get_sort(0)
6455  A
6456  >>> m.get_sort(1)
6457  B
6458  """
6459  if idx >= self.num_sorts():
6460  raise IndexError
6461  return _to_sort_ref(Z3_model_get_sort(self.ctx.ref(), self.model, idx), self.ctx)
6462 

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!1, A!val!0]

Definition at line 6480 of file z3py.py.

6480  def get_universe(self, s):
6481  """Return the interpretation for the uninterpreted sort `s` in the model `self`.
6482 
6483  >>> A = DeclareSort('A')
6484  >>> a, b = Consts('a b', A)
6485  >>> s = Solver()
6486  >>> s.add(a != b)
6487  >>> s.check()
6488  sat
6489  >>> m = s.model()
6490  >>> m.get_universe(A)
6491  [A!val!1, A!val!0]
6492  """
6493  if z3_debug():
6494  _z3_assert(isinstance(s, SortRef), "Z3 sort expected")
6495  try:
6496  return AstVector(Z3_model_get_sort_universe(self.ctx.ref(), self.model, s.ast), self.ctx)
6497  except Z3Exception:
6498  return None
6499 

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

6425  def num_sorts(self):
6426  """Return the number of uninterpreted sorts that contain an interpretation in the model `self`.
6427 
6428  >>> A = DeclareSort('A')
6429  >>> a, b = Consts('a b', A)
6430  >>> s = Solver()
6431  >>> s.add(a != b)
6432  >>> s.check()
6433  sat
6434  >>> m = s.model()
6435  >>> m.num_sorts()
6436  1
6437  """
6438  return int(Z3_model_get_num_sorts(self.ctx.ref(), self.model))
6439 

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

6313  def sexpr(self):
6314  """Return a textual representation of the s-expression representing the model."""
6315  return Z3_model_to_string(self.ctx.ref(), self.model)
6316 

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

6463  def sorts(self):
6464  """Return all uninterpreted sorts that have an interpretation in the model `self`.
6465 
6466  >>> A = DeclareSort('A')
6467  >>> B = DeclareSort('B')
6468  >>> a1, a2 = Consts('a1 a2', A)
6469  >>> b1, b2 = Consts('b1 b2', B)
6470  >>> s = Solver()
6471  >>> s.add(a1 != a2, b1 != b2)
6472  >>> s.check()
6473  sat
6474  >>> m = s.model()
6475  >>> m.sorts()
6476  [A, B]
6477  """
6478  return [self.get_sort(i) for i in range(self.num_sorts())]
6479 

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

6564  def translate(self, target):
6565  """Translate `self` to the context `target`. That is, return a copy of `self` in the context `target`.
6566  """
6567  if z3_debug():
6568  _z3_assert(isinstance(target, Context), "argument must be a Z3 context")
6569  model = Z3_model_translate(self.ctx.ref(), self.model, target.ref())
6570  return ModelRef(model, target)
6571 

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

Field Documentation

◆ ctx

ctx

Definition at line 6303 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(), Optimize.assert_and_track(), Solver.assert_exprs(), Fixedpoint.assert_exprs(), Optimize.assert_exprs(), Solver.assertions(), Optimize.assertions(), Solver.check(), Optimize.check(), UserPropagateBase.conflict(), Solver.consequences(), UserPropagateBase.ctx_ref(), ModelRef.decls(), Solver.dimacs(), 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(), Solver.import_model_converter(), 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(), Optimize.pop(), Solver.pop(), Solver.proof(), Solver.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(), Optimize.set_on_model(), 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(), Solver.trail(), Solver.trail_levels(), ModelRef.translate(), Solver.translate(), Solver.units(), Solver.unsat_core(), Optimize.unsat_core(), and Fixedpoint.update_rule().

◆ model

model
Z3_model_get_func_interp
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_model_eval
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.
Z3_model_dec_ref
void Z3_API Z3_model_dec_ref(Z3_context c, Z3_model m)
Decrement the reference counter of the given model.
Z3_model_translate
Z3_model Z3_API Z3_model_translate(Z3_context c, Z3_model m, Z3_context dst)
translate model from context c to context dst.
z3py.UserPropagateBase.__init__
def __init__(self, s, ctx=None)
Definition: z3py.py:11177
Z3_model_get_sort
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.
z3::range
expr range(expr const &lo, expr const &hi)
Definition: z3++.h:3725
Z3_model_get_const_decl
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.
z3py.UserPropagateBase.__del__
def __del__(self)
Definition: z3py.py:11202
z3py.get_as_array_func
def get_as_array_func(n)
Definition: z3py.py:6589
z3py.is_const
def is_const(a)
Definition: z3py.py:1259
Z3_model_get_const_interp
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,...
z3py.z3_debug
def z3_debug()
Definition: z3py.py:64
Z3_model_get_num_funcs
unsigned Z3_API Z3_model_get_num_funcs(Z3_context c, Z3_model m)
Return the number of function interpretations in the given model.
z3py.is_as_array
def is_as_array(n)
Definition: z3py.py:6584
Z3_model_get_sort_universe
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.
Z3_model_inc_ref
void Z3_API Z3_model_inc_ref(Z3_context c, Z3_model m)
Increment the reference counter of the given model.
Z3_model_to_string
Z3_string Z3_API Z3_model_to_string(Z3_context c, Z3_model m)
Convert the given model into a string.
Z3_model_get_num_sorts
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.
Z3_model_get_func_decl
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_model_get_num_consts
unsigned Z3_API Z3_model_get_num_consts(Z3_context c, Z3_model m)
Return the number of constants assigned by the given model.