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

Constructor & Destructor Documentation

◆ __init__()

def __init__ (   self,
  m,
  ctx 
)

Definition at line 5943 of file z3py.py.

5943  def __init__(self, m, ctx):
5944  assert ctx is not None
5945  self.model = m
5946  self.ctx = ctx
5947  Z3_model_inc_ref(self.ctx.ref(), self.model)
5948 
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 5949 of file z3py.py.

5949  def __del__(self):
5950  if self.ctx.ref() is not None:
5951  Z3_model_dec_ref(self.ctx.ref(), self.model)
5952 
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 6210 of file z3py.py.

6210  def __copy__(self):
6211  return self.translate(self.ctx)
6212 

◆ __deepcopy__()

def __deepcopy__ (   self,
  memo = {} 
)

Definition at line 6213 of file z3py.py.

6213  def __deepcopy__(self, memo={}):
6214  return self.translate(self.ctx)
6215 

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

6139  def __getitem__(self, idx):
6140  """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.
6141 
6142  The elements can be retrieved using position or the actual declaration.
6143 
6144  >>> f = Function('f', IntSort(), IntSort())
6145  >>> x = Int('x')
6146  >>> s = Solver()
6147  >>> s.add(x > 0, x < 2, f(x) == 0)
6148  >>> s.check()
6149  sat
6150  >>> m = s.model()
6151  >>> len(m)
6152  2
6153  >>> m[0]
6154  x
6155  >>> m[1]
6156  f
6157  >>> m[x]
6158  1
6159  >>> m[f]
6160  [else -> 0]
6161  >>> for d in m: print("%s -> %s" % (d, m[d]))
6162  x -> 1
6163  f -> [else -> 0]
6164  """
6165  if _is_int(idx):
6166  if idx >= len(self):
6167  raise IndexError
6168  num_consts = Z3_model_get_num_consts(self.ctx.ref(), self.model)
6169  if (idx < num_consts):
6170  return FuncDeclRef(Z3_model_get_const_decl(self.ctx.ref(), self.model, idx), self.ctx)
6171  else:
6172  return FuncDeclRef(Z3_model_get_func_decl(self.ctx.ref(), self.model, idx - num_consts), self.ctx)
6173  if isinstance(idx, FuncDeclRef):
6174  return self.get_interp(idx)
6175  if is_const(idx):
6176  return self.get_interp(idx.decl())
6177  if isinstance(idx, SortRef):
6178  return self.get_universe(idx)
6179  if z3_debug():
6180  _z3_assert(False, "Integer, Z3 declaration, or Z3 constant expected")
6181  return None
6182 
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:1152
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 z3_debug()
Definition: z3py.py:58

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

6015  def __len__(self):
6016  """Return the number of constant and function declarations in the model `self`.
6017 
6018  >>> f = Function('f', IntSort(), IntSort())
6019  >>> x = Int('x')
6020  >>> s = Solver()
6021  >>> s.add(x > 0, f(x) != x)
6022  >>> s.check()
6023  sat
6024  >>> m = s.model()
6025  >>> len(m)
6026  2
6027  """
6028  return int(Z3_model_get_num_consts(self.ctx.ref(), self.model)) + int(Z3_model_get_num_funcs(self.ctx.ref(), self.model))
6029 
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 5953 of file z3py.py.

5953  def __repr__(self):
5954  return obj_to_string(self)
5955 

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

6183  def decls(self):
6184  """Return a list with all symbols that have an interpretation in the model `self`.
6185  >>> f = Function('f', IntSort(), IntSort())
6186  >>> x = Int('x')
6187  >>> s = Solver()
6188  >>> s.add(x > 0, x < 2, f(x) == 0)
6189  >>> s.check()
6190  sat
6191  >>> m = s.model()
6192  >>> m.decls()
6193  [x, f]
6194  """
6195  r = []
6196  for i in range(Z3_model_get_num_consts(self.ctx.ref(), self.model)):
6197  r.append(FuncDeclRef(Z3_model_get_const_decl(self.ctx.ref(), self.model, i), self.ctx))
6198  for i in range(Z3_model_get_num_funcs(self.ctx.ref(), self.model)):
6199  r.append(FuncDeclRef(Z3_model_get_func_decl(self.ctx.ref(), self.model, i), self.ctx))
6200  return r
6201 
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.

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

5960  def eval(self, t, model_completion=False):
5961  """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`.
5962 
5963  >>> x = Int('x')
5964  >>> s = Solver()
5965  >>> s.add(x > 0, x < 2)
5966  >>> s.check()
5967  sat
5968  >>> m = s.model()
5969  >>> m.eval(x + 1)
5970  2
5971  >>> m.eval(x == 1)
5972  True
5973  >>> y = Int('y')
5974  >>> m.eval(y + x)
5975  1 + y
5976  >>> m.eval(y)
5977  y
5978  >>> m.eval(y, model_completion=True)
5979  0
5980  >>> # Now, m contains an interpretation for y
5981  >>> m.eval(y + x)
5982  1
5983  """
5984  r = (Ast * 1)()
5985  if Z3_model_eval(self.ctx.ref(), self.model, t.as_ast(), model_completion, r):
5986  return _to_expr_ref(r[0], self.ctx)
5987  raise Z3Exception("failed to evaluate expression in the model")
5988 
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...

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

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

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

6030  def get_interp(self, decl):
6031  """Return the interpretation for a given declaration or constant.
6032 
6033  >>> f = Function('f', IntSort(), IntSort())
6034  >>> x = Int('x')
6035  >>> s = Solver()
6036  >>> s.add(x > 0, x < 2, f(x) == 0)
6037  >>> s.check()
6038  sat
6039  >>> m = s.model()
6040  >>> m[x]
6041  1
6042  >>> m[f]
6043  [else -> 0]
6044  """
6045  if z3_debug():
6046  _z3_assert(isinstance(decl, FuncDeclRef) or is_const(decl), "Z3 declaration expected")
6047  if is_const(decl):
6048  decl = decl.decl()
6049  try:
6050  if decl.arity() == 0:
6051  _r = Z3_model_get_const_interp(self.ctx.ref(), self.model, decl.ast)
6052  if _r.value is None:
6053  return None
6054  r = _to_expr_ref(_r, self.ctx)
6055  if is_as_array(r):
6056  return self.get_interp(get_as_array_func(r))
6057  else:
6058  return r
6059  else:
6060  return FuncInterp(Z3_model_get_func_interp(self.ctx.ref(), self.model, decl.ast), self.ctx)
6061  except Z3Exception:
6062  return None
6063 
def is_const(a)
Definition: z3py.py:1152
def get_as_array_func(n)
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 z3_debug()
Definition: z3py.py:58
def is_as_array(n)
Definition: z3py.py:6220

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

6079  def get_sort(self, idx):
6080  """Return the uninterpreted sort at position `idx` < self.num_sorts().
6081 
6082  >>> A = DeclareSort('A')
6083  >>> B = DeclareSort('B')
6084  >>> a1, a2 = Consts('a1 a2', A)
6085  >>> b1, b2 = Consts('b1 b2', B)
6086  >>> s = Solver()
6087  >>> s.add(a1 != a2, b1 != b2)
6088  >>> s.check()
6089  sat
6090  >>> m = s.model()
6091  >>> m.num_sorts()
6092  2
6093  >>> m.get_sort(0)
6094  A
6095  >>> m.get_sort(1)
6096  B
6097  """
6098  if idx >= self.num_sorts():
6099  raise IndexError
6100  return _to_sort_ref(Z3_model_get_sort(self.ctx.ref(), self.model, idx), self.ctx)
6101 
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 6119 of file z3py.py.

6119  def get_universe(self, s):
6120  """Return the interpretation for the uninterpreted sort `s` in the model `self`.
6121 
6122  >>> A = DeclareSort('A')
6123  >>> a, b = Consts('a b', A)
6124  >>> s = Solver()
6125  >>> s.add(a != b)
6126  >>> s.check()
6127  sat
6128  >>> m = s.model()
6129  >>> m.get_universe(A)
6130  [A!val!0, A!val!1]
6131  """
6132  if z3_debug():
6133  _z3_assert(isinstance(s, SortRef), "Z3 sort expected")
6134  try:
6135  return AstVector(Z3_model_get_sort_universe(self.ctx.ref(), self.model, s.ast), self.ctx)
6136  except Z3Exception:
6137  return None
6138 
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 z3_debug()
Definition: z3py.py:58

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

Referenced by ModelRef.get_sort().

6064  def num_sorts(self):
6065  """Return the number of uninterpreted sorts that contain an interpretation in the model `self`.
6066 
6067  >>> A = DeclareSort('A')
6068  >>> a, b = Consts('a b', A)
6069  >>> s = Solver()
6070  >>> s.add(a != b)
6071  >>> s.check()
6072  sat
6073  >>> m = s.model()
6074  >>> m.num_sorts()
6075  1
6076  """
6077  return int(Z3_model_get_num_sorts(self.ctx.ref(), self.model))
6078 
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 5956 of file z3py.py.

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

5956  def sexpr(self):
5957  """Return a textual representation of the s-expression representing the model."""
5958  return Z3_model_to_string(self.ctx.ref(), self.model)
5959 
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 6102 of file z3py.py.

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

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

6202  def translate(self, target):
6203  """Translate `self` to the context `target`. That is, return a copy of `self` in the context `target`.
6204  """
6205  if z3_debug():
6206  _z3_assert(isinstance(target, Context), "argument must be a Z3 context")
6207  model = Z3_model_translate(self.ctx.ref(), self.model, target.ref())
6208  return Model(model, target)
6209 
def Model(ctx=None)
Definition: z3py.py:6216
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 z3_debug()
Definition: z3py.py:58

Field Documentation

◆ ctx

ctx

Definition at line 5946 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(), Optimize.assert_and_track(), 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(), Optimize.pop(), 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 5945 of file z3py.py.