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

Constructor & Destructor Documentation

◆ __init__()

def __init__ (   self,
  m,
  ctx 
)

Definition at line 5894 of file z3py.py.

5894  def __init__(self, m, ctx):
5895  assert ctx is not None
5896  self.model = m
5897  self.ctx = ctx
5898  Z3_model_inc_ref(self.ctx.ref(), self.model)
5899 
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 5900 of file z3py.py.

5900  def __del__(self):
5901  if self.ctx.ref() is not None:
5902  Z3_model_dec_ref(self.ctx.ref(), self.model)
5903 
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 6161 of file z3py.py.

6161  def __copy__(self):
6162  return self.translate(self.ctx)
6163 

◆ __deepcopy__()

def __deepcopy__ (   self)

Definition at line 6164 of file z3py.py.

6164  def __deepcopy__(self):
6165  return self.translate(self.ctx)
6166 

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

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

5966  def __len__(self):
5967  """Return the number of constant and function declarations in the model `self`.
5968 
5969  >>> f = Function('f', IntSort(), IntSort())
5970  >>> x = Int('x')
5971  >>> s = Solver()
5972  >>> s.add(x > 0, f(x) != x)
5973  >>> s.check()
5974  sat
5975  >>> m = s.model()
5976  >>> len(m)
5977  2
5978  """
5979  return int(Z3_model_get_num_consts(self.ctx.ref(), self.model)) + int(Z3_model_get_num_funcs(self.ctx.ref(), self.model))
5980 
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 5904 of file z3py.py.

5904  def __repr__(self):
5905  return obj_to_string(self)
5906 

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

6134  def decls(self):
6135  """Return a list with all symbols that have an interpretation in the model `self`.
6136  >>> f = Function('f', IntSort(), IntSort())
6137  >>> x = Int('x')
6138  >>> s = Solver()
6139  >>> s.add(x > 0, x < 2, f(x) == 0)
6140  >>> s.check()
6141  sat
6142  >>> m = s.model()
6143  >>> m.decls()
6144  [x, f]
6145  """
6146  r = []
6147  for i in range(Z3_model_get_num_consts(self.ctx.ref(), self.model)):
6148  r.append(FuncDeclRef(Z3_model_get_const_decl(self.ctx.ref(), self.model, i), self.ctx))
6149  for i in range(Z3_model_get_num_funcs(self.ctx.ref(), self.model)):
6150  r.append(FuncDeclRef(Z3_model_get_func_decl(self.ctx.ref(), self.model, i), self.ctx))
6151  return r
6152 
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 5911 of file z3py.py.

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

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

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

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

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

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

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

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

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

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

Referenced by ModelRef.get_sort().

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

◆ sexpr()

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

Definition at line 5907 of file z3py.py.

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

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

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

6053  def sorts(self):
6054  """Return all uninterpreted sorts that have an interpretation in the model `self`.
6055 
6056  >>> A = DeclareSort('A')
6057  >>> B = DeclareSort('B')
6058  >>> a1, a2 = Consts('a1 a2', A)
6059  >>> b1, b2 = Consts('b1 b2', B)
6060  >>> s = Solver()
6061  >>> s.add(a1 != a2, b1 != b2)
6062  >>> s.check()
6063  sat
6064  >>> m = s.model()
6065  >>> m.sorts()
6066  [A, B]
6067  """
6068  return [ self.get_sort(i) for i in range(self.num_sorts()) ]
6069 
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 6153 of file z3py.py.

6153  def translate(self, target):
6154  """Translate `self` to the context `target`. That is, return a copy of `self` in the context `target`.
6155  """
6156  if __debug__:
6157  _z3_assert(isinstance(target, Context), "argument must be a Z3 context")
6158  model = Z3_model_translate(self.ctx.ref(), self.model, target.ref())
6159  return Model(model, target)
6160 
def Model(ctx=None)
Definition: z3py.py:6167
Z3_model Z3_API Z3_model_translate(Z3_context c, Z3_model m, Z3_context dst)
translate model from context c to context dst.

Field Documentation

◆ ctx

ctx

Definition at line 5897 of file z3py.py.

Referenced by Probe.__call__(), Fixedpoint.__deepcopy__(), Optimize.__deepcopy__(), ApplyResult.__deepcopy__(), Tactic.__deepcopy__(), Probe.__deepcopy__(), Fixedpoint.__del__(), Optimize.__del__(), ApplyResult.__del__(), Tactic.__del__(), Probe.__del__(), Probe.__eq__(), Probe.__ge__(), ApplyResult.__getitem__(), Probe.__gt__(), Probe.__le__(), ApplyResult.__len__(), Probe.__lt__(), Probe.__ne__(), Fixedpoint.add_cover(), Fixedpoint.add_rule(), Optimize.add_soft(), Tactic.apply(), ApplyResult.as_expr(), Fixedpoint.assert_exprs(), Optimize.assert_exprs(), Optimize.assertions(), Optimize.check(), Optimize.from_file(), Optimize.from_string(), Fixedpoint.get_answer(), Fixedpoint.get_assertions(), Fixedpoint.get_cover_delta(), Fixedpoint.get_ground_sat_answer(), Fixedpoint.get_num_levels(), Fixedpoint.get_rule_names_along_trace(), Fixedpoint.get_rules(), Fixedpoint.get_rules_along_trace(), Fixedpoint.help(), Optimize.help(), Tactic.help(), Optimize.maximize(), Optimize.minimize(), Optimize.model(), Optimize.objectives(), Fixedpoint.param_descrs(), Optimize.param_descrs(), Tactic.param_descrs(), Fixedpoint.parse_file(), Fixedpoint.parse_string(), Fixedpoint.pop(), Optimize.pop(), Fixedpoint.push(), Optimize.push(), Fixedpoint.query(), Fixedpoint.query_from_lvl(), Fixedpoint.reason_unknown(), Optimize.reason_unknown(), Fixedpoint.register_relation(), Fixedpoint.set(), Optimize.set(), Fixedpoint.set_predicate_representation(), Fixedpoint.sexpr(), Optimize.sexpr(), ApplyResult.sexpr(), Tactic.solver(), Fixedpoint.statistics(), Optimize.statistics(), Solver.to_smt2(), Fixedpoint.to_string(), Optimize.unsat_core(), and Fixedpoint.update_rule().

◆ model

model

Definition at line 5896 of file z3py.py.