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
 
def evaluate
 
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 update_value (self, x, value)
 
def translate (self, target)
 
def __copy__ (self)
 
def __deepcopy__
 
- 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 6299 of file z3py.py.

Constructor & Destructor Documentation

def __init__ (   self,
  m,
  ctx 
)

Definition at line 6302 of file z3py.py.

6302  def __init__(self, m, ctx):
6303  assert ctx is not None
6304  self.model = m
6305  self.ctx = ctx
6306  Z3_model_inc_ref(self.ctx.ref(), self.model)
6307 
def __init__(self, m, ctx)
Definition: z3py.py:6302
void Z3_API Z3_model_inc_ref(Z3_context c, Z3_model m)
Increment the reference counter of the given model.
def __del__ (   self)

Definition at line 6308 of file z3py.py.

6308  def __del__(self):
6309  if self.ctx.ref() is not None:
6310  Z3_model_dec_ref(self.ctx.ref(), self.model)
6311 
def __del__(self)
Definition: z3py.py:6308
void Z3_API Z3_model_dec_ref(Z3_context c, Z3_model m)
Decrement the reference counter of the given model.

Member Function Documentation

def __copy__ (   self)

Definition at line 6583 of file z3py.py.

6583  def __copy__(self):
6584  return self.translate(self.ctx)
6585 
def __copy__(self)
Definition: z3py.py:6583
def translate(self, target)
Definition: z3py.py:6575
def __deepcopy__ (   self,
  memo = {} 
)

Definition at line 6586 of file z3py.py.

6586  def __deepcopy__(self, memo={}):
6587  return self.translate(self.ctx)
6588 
6589 
def __deepcopy__
Definition: z3py.py:6586
def translate(self, target)
Definition: z3py.py:6575
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 6502 of file z3py.py.

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

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

Definition at line 6312 of file z3py.py.

6312  def __repr__(self):
6313  return obj_to_string(self)
6314 
def __repr__(self)
Definition: z3py.py:6312
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 6547 of file z3py.py.

6547  def decls(self):
6548  """Return a list with all symbols that have an interpretation in the model `self`.
6549  >>> f = Function('f', IntSort(), IntSort())
6550  >>> x = Int('x')
6551  >>> s = Solver()
6552  >>> s.add(x > 0, x < 2, f(x) == 0)
6553  >>> s.check()
6554  sat
6555  >>> m = s.model()
6556  >>> m.decls()
6557  [x, f]
6558  """
6559  r = []
6560  for i in range(Z3_model_get_num_consts(self.ctx.ref(), self.model)):
6561  r.append(FuncDeclRef(Z3_model_get_const_decl(self.ctx.ref(), self.model, i), self.ctx))
6562  for i in range(Z3_model_get_num_funcs(self.ctx.ref(), self.model)):
6563  r.append(FuncDeclRef(Z3_model_get_func_decl(self.ctx.ref(), self.model, i), self.ctx))
6564  return r
6565 
Function Declarations.
Definition: z3py.py:717
expr range(expr const &lo, expr const &hi)
Definition: z3++.h:3794
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.
def decls(self)
Definition: z3py.py:6547
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 6319 of file z3py.py.

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

6350  def evaluate(self, t, model_completion=False):
6351  """Alias for `eval`.
6352 
6353  >>> x = Int('x')
6354  >>> s = Solver()
6355  >>> s.add(x > 0, x < 2)
6356  >>> s.check()
6357  sat
6358  >>> m = s.model()
6359  >>> m.evaluate(x + 1)
6360  2
6361  >>> m.evaluate(x == 1)
6362  True
6363  >>> y = Int('y')
6364  >>> m.evaluate(y + x)
6365  1 + y
6366  >>> m.evaluate(y)
6367  y
6368  >>> m.evaluate(y, model_completion=True)
6369  0
6370  >>> # Now, m contains an interpretation for y
6371  >>> m.evaluate(y + x)
6372  1
6373  """
6374  return self.eval(t, model_completion)
6375 
def evaluate
Definition: z3py.py:6350
def eval
Definition: z3py.py:6319
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 6393 of file z3py.py.

Referenced by ModelRef.__getitem__().

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

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

Referenced by ModelRef.__getitem__().

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

Referenced by ModelRef.get_sort().

6427  def num_sorts(self):
6428  """Return the number of uninterpreted sorts that contain an interpretation in the model `self`.
6429 
6430  >>> A = DeclareSort('A')
6431  >>> a, b = Consts('a b', A)
6432  >>> s = Solver()
6433  >>> s.add(a != b)
6434  >>> s.check()
6435  sat
6436  >>> m = s.model()
6437  >>> m.num_sorts()
6438  1
6439  """
6440  return int(Z3_model_get_num_sorts(self.ctx.ref(), self.model))
6441 
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.
def num_sorts(self)
Definition: z3py.py:6427
def sexpr (   self)
Return a textual representation of the s-expression representing the model.

Definition at line 6315 of file z3py.py.

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

6315  def sexpr(self):
6316  """Return a textual representation of the s-expression representing the model."""
6317  return Z3_model_to_string(self.ctx.ref(), self.model)
6318 
def sexpr(self)
Definition: z3py.py:6315
Z3_string Z3_API Z3_model_to_string(Z3_context c, Z3_model m)
Convert the given model into a string.
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 6465 of file z3py.py.

6465  def sorts(self):
6466  """Return all uninterpreted sorts that have an interpretation in the model `self`.
6467 
6468  >>> A = DeclareSort('A')
6469  >>> B = DeclareSort('B')
6470  >>> a1, a2 = Consts('a1 a2', A)
6471  >>> b1, b2 = Consts('b1 b2', B)
6472  >>> s = Solver()
6473  >>> s.add(a1 != a2, b1 != b2)
6474  >>> s.check()
6475  sat
6476  >>> m = s.model()
6477  >>> m.sorts()
6478  [A, B]
6479  """
6480  return [self.get_sort(i) for i in range(self.num_sorts())]
6481 
expr range(expr const &lo, expr const &hi)
Definition: z3++.h:3794
def sorts(self)
Definition: z3py.py:6465
def num_sorts(self)
Definition: z3py.py:6427
def get_sort(self, idx)
Definition: z3py.py:6442
def translate (   self,
  target 
)
Translate `self` to the context `target`. That is, return a copy of `self` in the context `target`.

Definition at line 6575 of file z3py.py.

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

6575  def translate(self, target):
6576  """Translate `self` to the context `target`. That is, return a copy of `self` in the context `target`.
6577  """
6578  if z3_debug():
6579  _z3_assert(isinstance(target, Context), "argument must be a Z3 context")
6580  model = Z3_model_translate(self.ctx.ref(), self.model, target.ref())
6581  return ModelRef(model, target)
6582 
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:64
def translate(self, target)
Definition: z3py.py:6575
def update_value (   self,
  x,
  value 
)
Update the interpretation of a constant

Definition at line 6566 of file z3py.py.

6566  def update_value(self, x, value):
6567  """Update the interpretation of a constant"""
6568  if is_expr(x):
6569  x = x.decl()
6570  if not is_func_decl(x) or x.arity() != 0:
6571  raise Z3Exception("Expecting 0-ary function or constant expression")
6572  value = _py2expr(value)
6573  Z3_add_const_interp(x.ctx_ref(), self.model, x.ast, value.ast)
6574 
void Z3_API Z3_add_const_interp(Z3_context c, Z3_model m, Z3_func_decl f, Z3_ast a)
Add a constant interpretation.
def update_value(self, x, value)
Definition: z3py.py:6566
def is_expr(a)
Definition: z3py.py:1212
def is_func_decl(a)
Definition: z3py.py:849

Field Documentation

ctx
model