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

Constructor & Destructor Documentation

◆ __init__()

def __init__ (   self,
  m,
  ctx 
)

Definition at line 5630 of file z3py.py.

5630  def __init__(self, m, ctx):
5631  assert ctx is not None
5632  self.model = m
5633  self.ctx = ctx
5634  Z3_model_inc_ref(self.ctx.ref(), self.model)
5635 
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 5636 of file z3py.py.

5636  def __del__(self):
5637  if self.ctx.ref() is not None:
5638  Z3_model_dec_ref(self.ctx.ref(), self.model)
5639 
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 5897 of file z3py.py.

5897  def __copy__(self):
5898  return self.translate(self.ctx)
5899 

◆ __deepcopy__()

def __deepcopy__ (   self)

Definition at line 5900 of file z3py.py.

5900  def __deepcopy__(self):
5901  return self.translate(self.ctx)
5902 

◆ __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]
[1 -> 0, else -> 0]
>>> for d in m: print("%s -> %s" % (d, m[d]))
x -> 1
f -> [1 -> 0, else -> 0]

Definition at line 5826 of file z3py.py.

5826  def __getitem__(self, idx):
5827  """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.
5828 
5829  The elements can be retrieved using position or the actual declaration.
5830 
5831  >>> f = Function('f', IntSort(), IntSort())
5832  >>> x = Int('x')
5833  >>> s = Solver()
5834  >>> s.add(x > 0, x < 2, f(x) == 0)
5835  >>> s.check()
5836  sat
5837  >>> m = s.model()
5838  >>> len(m)
5839  2
5840  >>> m[0]
5841  x
5842  >>> m[1]
5843  f
5844  >>> m[x]
5845  1
5846  >>> m[f]
5847  [1 -> 0, else -> 0]
5848  >>> for d in m: print("%s -> %s" % (d, m[d]))
5849  x -> 1
5850  f -> [1 -> 0, else -> 0]
5851  """
5852  if _is_int(idx):
5853  if idx >= len(self):
5854  raise IndexError
5855  num_consts = Z3_model_get_num_consts(self.ctx.ref(), self.model)
5856  if (idx < num_consts):
5857  return FuncDeclRef(Z3_model_get_const_decl(self.ctx.ref(), self.model, idx), self.ctx)
5858  else:
5859  return FuncDeclRef(Z3_model_get_func_decl(self.ctx.ref(), self.model, idx - num_consts), self.ctx)
5860  if isinstance(idx, FuncDeclRef):
5861  return self.get_interp(idx)
5862  if is_const(idx):
5863  return self.get_interp(idx.decl())
5864  if isinstance(idx, SortRef):
5865  return self.get_universe(idx)
5866  if __debug__:
5867  _z3_assert(False, "Integer, Z3 declaration, or Z3 constant expected")
5868  return None
5869 
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:1094
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 5702 of file z3py.py.

5702  def __len__(self):
5703  """Return the number of constant and function declarations in the model `self`.
5704 
5705  >>> f = Function('f', IntSort(), IntSort())
5706  >>> x = Int('x')
5707  >>> s = Solver()
5708  >>> s.add(x > 0, f(x) != x)
5709  >>> s.check()
5710  sat
5711  >>> m = s.model()
5712  >>> len(m)
5713  2
5714  """
5715  return int(Z3_model_get_num_consts(self.ctx.ref(), self.model)) + int(Z3_model_get_num_funcs(self.ctx.ref(), self.model))
5716 
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 5640 of file z3py.py.

5640  def __repr__(self):
5641  return obj_to_string(self)
5642 

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

5870  def decls(self):
5871  """Return a list with all symbols that have an interpretation in the model `self`.
5872  >>> f = Function('f', IntSort(), IntSort())
5873  >>> x = Int('x')
5874  >>> s = Solver()
5875  >>> s.add(x > 0, x < 2, f(x) == 0)
5876  >>> s.check()
5877  sat
5878  >>> m = s.model()
5879  >>> m.decls()
5880  [x, f]
5881  """
5882  r = []
5883  for i in range(Z3_model_get_num_consts(self.ctx.ref(), self.model)):
5884  r.append(FuncDeclRef(Z3_model_get_const_decl(self.ctx.ref(), self.model, i), self.ctx))
5885  for i in range(Z3_model_get_num_funcs(self.ctx.ref(), self.model)):
5886  r.append(FuncDeclRef(Z3_model_get_func_decl(self.ctx.ref(), self.model, i), self.ctx))
5887  return r
5888 
expr range(expr const &lo, expr const &hi)
Definition: z3++.h:2868
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 5647 of file z3py.py.

5647  def eval(self, t, model_completion=False):
5648  """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`.
5649 
5650  >>> x = Int('x')
5651  >>> s = Solver()
5652  >>> s.add(x > 0, x < 2)
5653  >>> s.check()
5654  sat
5655  >>> m = s.model()
5656  >>> m.eval(x + 1)
5657  2
5658  >>> m.eval(x == 1)
5659  True
5660  >>> y = Int('y')
5661  >>> m.eval(y + x)
5662  1 + y
5663  >>> m.eval(y)
5664  y
5665  >>> m.eval(y, model_completion=True)
5666  0
5667  >>> # Now, m contains an interpretation for y
5668  >>> m.eval(y + x)
5669  1
5670  """
5671  r = (Ast * 1)()
5672  if Z3_model_eval(self.ctx.ref(), self.model, t.as_ast(), model_completion, r):
5673  return _to_expr_ref(r[0], self.ctx)
5674  raise Z3Exception("failed to evaluate expression in the model")
5675 
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 Z3_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 5676 of file z3py.py.

5676  def evaluate(self, t, model_completion=False):
5677  """Alias for `eval`.
5678 
5679  >>> x = Int('x')
5680  >>> s = Solver()
5681  >>> s.add(x > 0, x < 2)
5682  >>> s.check()
5683  sat
5684  >>> m = s.model()
5685  >>> m.evaluate(x + 1)
5686  2
5687  >>> m.evaluate(x == 1)
5688  True
5689  >>> y = Int('y')
5690  >>> m.evaluate(y + x)
5691  1 + y
5692  >>> m.evaluate(y)
5693  y
5694  >>> m.evaluate(y, model_completion=True)
5695  0
5696  >>> # Now, m contains an interpretation for y
5697  >>> m.evaluate(y + x)
5698  1
5699  """
5700  return self.eval(t, model_completion)
5701 

◆ 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]
[1 -> 0, else -> 0]

Definition at line 5717 of file z3py.py.

5717  def get_interp(self, decl):
5718  """Return the interpretation for a given declaration or constant.
5719 
5720  >>> f = Function('f', IntSort(), IntSort())
5721  >>> x = Int('x')
5722  >>> s = Solver()
5723  >>> s.add(x > 0, x < 2, f(x) == 0)
5724  >>> s.check()
5725  sat
5726  >>> m = s.model()
5727  >>> m[x]
5728  1
5729  >>> m[f]
5730  [1 -> 0, else -> 0]
5731  """
5732  if __debug__:
5733  _z3_assert(isinstance(decl, FuncDeclRef) or is_const(decl), "Z3 declaration expected")
5734  if is_const(decl):
5735  decl = decl.decl()
5736  try:
5737  if decl.arity() == 0:
5738  _r = Z3_model_get_const_interp(self.ctx.ref(), self.model, decl.ast)
5739  if _r.value is None:
5740  return None
5741  r = _to_expr_ref(_r, self.ctx)
5742  if is_as_array(r):
5743  return self.get_interp(get_as_array_func(r))
5744  else:
5745  return r
5746  else:
5747  return FuncInterp(Z3_model_get_func_interp(self.ctx.ref(), self.model, decl.ast), self.ctx)
5748  except Z3Exception:
5749  return None
5750 
def is_const(a)
Definition: z3py.py:1094
def get_as_array_func(n)
Definition: z3py.py:5907
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:5903

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

5766  def get_sort(self, idx):
5767  """Return the uninterpreted sort at position `idx` < self.num_sorts().
5768 
5769  >>> A = DeclareSort('A')
5770  >>> B = DeclareSort('B')
5771  >>> a1, a2 = Consts('a1 a2', A)
5772  >>> b1, b2 = Consts('b1 b2', B)
5773  >>> s = Solver()
5774  >>> s.add(a1 != a2, b1 != b2)
5775  >>> s.check()
5776  sat
5777  >>> m = s.model()
5778  >>> m.num_sorts()
5779  2
5780  >>> m.get_sort(0)
5781  A
5782  >>> m.get_sort(1)
5783  B
5784  """
5785  if idx >= self.num_sorts():
5786  raise IndexError
5787  return _to_sort_ref(Z3_model_get_sort(self.ctx.ref(), self.model, idx), self.ctx)
5788 
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 5806 of file z3py.py.

5806  def get_universe(self, s):
5807  """Return the interpretation for the uninterpreted sort `s` in the model `self`.
5808 
5809  >>> A = DeclareSort('A')
5810  >>> a, b = Consts('a b', A)
5811  >>> s = Solver()
5812  >>> s.add(a != b)
5813  >>> s.check()
5814  sat
5815  >>> m = s.model()
5816  >>> m.get_universe(A)
5817  [A!val!0, A!val!1]
5818  """
5819  if __debug__:
5820  _z3_assert(isinstance(s, SortRef), "Z3 sort expected")
5821  try:
5822  return AstVector(Z3_model_get_sort_universe(self.ctx.ref(), self.model, s.ast), self.ctx)
5823  except Z3Exception:
5824  return None
5825 
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 5751 of file z3py.py.

Referenced by ModelRef.get_sort().

5751  def num_sorts(self):
5752  """Return the number of uninterpreted sorts that contain an interpretation in the model `self`.
5753 
5754  >>> A = DeclareSort('A')
5755  >>> a, b = Consts('a b', A)
5756  >>> s = Solver()
5757  >>> s.add(a != b)
5758  >>> s.check()
5759  sat
5760  >>> m = s.model()
5761  >>> m.num_sorts()
5762  1
5763  """
5764  return int(Z3_model_get_num_sorts(self.ctx.ref(), self.model))
5765 
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 5643 of file z3py.py.

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

5643  def sexpr(self):
5644  """Return a textual representation of the s-expression representing the model."""
5645  return Z3_model_to_string(self.ctx.ref(), self.model)
5646 
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 5789 of file z3py.py.

5789  def sorts(self):
5790  """Return all uninterpreted sorts that have an interpretation in the model `self`.
5791 
5792  >>> A = DeclareSort('A')
5793  >>> B = DeclareSort('B')
5794  >>> a1, a2 = Consts('a1 a2', A)
5795  >>> b1, b2 = Consts('b1 b2', B)
5796  >>> s = Solver()
5797  >>> s.add(a1 != a2, b1 != b2)
5798  >>> s.check()
5799  sat
5800  >>> m = s.model()
5801  >>> m.sorts()
5802  [A, B]
5803  """
5804  return [ self.get_sort(i) for i in range(self.num_sorts()) ]
5805 
expr range(expr const &lo, expr const &hi)
Definition: z3++.h:2868

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

5889  def translate(self, target):
5890  """Translate `self` to the context `target`. That is, return a copy of `self` in the context `target`.
5891  """
5892  if __debug__:
5893  _z3_assert(isinstance(target, Context), "argument must be a Z3 context")
5894  model = Z3_model_translate(self.ctx.ref(), self.model, target.ref())
5895  return Model(model, target)
5896 
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 5633 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(), ApplyResult.convert_model(), 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(), and Fixedpoint.update_rule().

◆ model

model

Definition at line 5632 of file z3py.py.