Z3
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Public Member Functions | Data Fields
ModelRef Class Reference
+ Inheritance diagram for ModelRef:

Public Member Functions

def __init__
 
def __del__
 
def __repr__
 
def sexpr
 
def eval
 
def evaluate
 
def __len__
 
def get_interp
 
def num_sorts
 
def get_sort
 
def sorts
 
def get_universe
 
def __getitem__
 
def decls
 
- Public Member Functions inherited from Z3PPObject
def use_pp
 

Data Fields

 model
 
 ctx
 

Detailed Description

Model/Solution of a satisfiability problem (aka system of constraints).

Definition at line 5180 of file z3py.py.

Constructor & Destructor Documentation

def __init__ (   self,
  m,
  ctx 
)

Definition at line 5183 of file z3py.py.

5184  def __init__(self, m, ctx):
5185  assert ctx != None
5186  self.model = m
5187  self.ctx = ctx
5188  Z3_model_inc_ref(self.ctx.ref(), self.model)
def __del__ (   self)

Definition at line 5189 of file z3py.py.

5190  def __del__(self):
5191  Z3_model_dec_ref(self.ctx.ref(), self.model)

Member Function Documentation

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

5376  def __getitem__(self, idx):
5377  """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 interpreation is returned.
5378 
5379  The elements can be retrieved using position or the actual declaration.
5380 
5381  >>> f = Function('f', IntSort(), IntSort())
5382  >>> x = Int('x')
5383  >>> s = Solver()
5384  >>> s.add(x > 0, x < 2, f(x) == 0)
5385  >>> s.check()
5386  sat
5387  >>> m = s.model()
5388  >>> len(m)
5389  2
5390  >>> m[0]
5391  x
5392  >>> m[1]
5393  f
5394  >>> m[x]
5395  1
5396  >>> m[f]
5397  [1 -> 0, else -> 0]
5398  >>> for d in m: print "%s -> %s" % (d, m[d])
5399  x -> 1
5400  f -> [1 -> 0, else -> 0]
5401  """
5402  if isinstance(idx, int):
5403  if idx >= len(self):
5404  raise IndexError
5405  num_consts = Z3_model_get_num_consts(self.ctx.ref(), self.model)
5406  if (idx < num_consts):
5407  return FuncDeclRef(Z3_model_get_const_decl(self.ctx.ref(), self.model, idx), self.ctx)
5408  else:
5409  return FuncDeclRef(Z3_model_get_func_decl(self.ctx.ref(), self.model, idx - num_consts), self.ctx)
5410  if isinstance(idx, FuncDeclRef):
5411  return self.get_interp(idx)
5412  if is_const(idx):
5413  return self.get_interp(idx.decl())
5414  if isinstance(idx, SortRef):
5415  return self.get_universe(idx)
5416  if __debug__:
5417  _z3_assert(False, "Integer, Z3 declaration, or Z3 constant expected")
5418  return None
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 5254 of file z3py.py.

5255  def __len__(self):
5256  """Return the number of constant and function declarations in the model `self`.
5257 
5258  >>> f = Function('f', IntSort(), IntSort())
5259  >>> x = Int('x')
5260  >>> s = Solver()
5261  >>> s.add(x > 0, f(x) != x)
5262  >>> s.check()
5263  sat
5264  >>> m = s.model()
5265  >>> len(m)
5266  2
5267  """
5268  return int(Z3_model_get_num_consts(self.ctx.ref(), self.model)) + int(Z3_model_get_num_funcs(self.ctx.ref(), self.model))
def __repr__ (   self)

Definition at line 5192 of file z3py.py.

5193  def __repr__(self):
5194  return obj_to_string(self)
def decls (   self)
Return a list with all symbols that have an interpreation 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 5419 of file z3py.py.

5420  def decls(self):
5421  """Return a list with all symbols that have an interpreation in the model `self`.
5422  >>> f = Function('f', IntSort(), IntSort())
5423  >>> x = Int('x')
5424  >>> s = Solver()
5425  >>> s.add(x > 0, x < 2, f(x) == 0)
5426  >>> s.check()
5427  sat
5428  >>> m = s.model()
5429  >>> m.decls()
5430  [x, f]
5431  """
5432  r = []
5433  for i in range(Z3_model_get_num_consts(self.ctx.ref(), self.model)):
5434  r.append(FuncDeclRef(Z3_model_get_const_decl(self.ctx.ref(), self.model, i), self.ctx))
5435  for i in range(Z3_model_get_num_funcs(self.ctx.ref(), self.model)):
5436  r.append(FuncDeclRef(Z3_model_get_func_decl(self.ctx.ref(), self.model, i), self.ctx))
5437  return r
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 5199 of file z3py.py.

5200  def eval(self, t, model_completion=False):
5201  """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`.
5202 
5203  >>> x = Int('x')
5204  >>> s = Solver()
5205  >>> s.add(x > 0, x < 2)
5206  >>> s.check()
5207  sat
5208  >>> m = s.model()
5209  >>> m.eval(x + 1)
5210  2
5211  >>> m.eval(x == 1)
5212  True
5213  >>> y = Int('y')
5214  >>> m.eval(y + x)
5215  1 + y
5216  >>> m.eval(y)
5217  y
5218  >>> m.eval(y, model_completion=True)
5219  0
5220  >>> # Now, m contains an interpretation for y
5221  >>> m.eval(y + x)
5222  1
5223  """
5224  r = (Ast * 1)()
5225  if Z3_model_eval(self.ctx.ref(), self.model, t.as_ast(), model_completion, r):
5226  return _to_expr_ref(r[0], self.ctx)
5227  raise Z3Exception("failed to evaluate expression in the model")
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 5228 of file z3py.py.

5229  def evaluate(self, t, model_completion=False):
5230  """Alias for `eval`.
5231 
5232  >>> x = Int('x')
5233  >>> s = Solver()
5234  >>> s.add(x > 0, x < 2)
5235  >>> s.check()
5236  sat
5237  >>> m = s.model()
5238  >>> m.evaluate(x + 1)
5239  2
5240  >>> m.evaluate(x == 1)
5241  True
5242  >>> y = Int('y')
5243  >>> m.evaluate(y + x)
5244  1 + y
5245  >>> m.evaluate(y)
5246  y
5247  >>> m.evaluate(y, model_completion=True)
5248  0
5249  >>> # Now, m contains an interpretation for y
5250  >>> m.evaluate(y + x)
5251  1
5252  """
5253  return self.eval(t, model_completion)
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 5269 of file z3py.py.

Referenced by ModelRef.__getitem__().

5270  def get_interp(self, decl):
5271  """Return the interpretation for a given declaration or constant.
5272 
5273  >>> f = Function('f', IntSort(), IntSort())
5274  >>> x = Int('x')
5275  >>> s = Solver()
5276  >>> s.add(x > 0, x < 2, f(x) == 0)
5277  >>> s.check()
5278  sat
5279  >>> m = s.model()
5280  >>> m[x]
5281  1
5282  >>> m[f]
5283  [1 -> 0, else -> 0]
5284  """
5285  if __debug__:
5286  _z3_assert(isinstance(decl, FuncDeclRef) or is_const(decl), "Z3 declaration expected")
5287  if is_const(decl):
5288  decl = decl.decl()
5289  try:
5290  if decl.arity() == 0:
5291  r = _to_expr_ref(Z3_model_get_const_interp(self.ctx.ref(), self.model, decl.ast), self.ctx)
5292  if is_as_array(r):
5293  return self.get_interp(get_as_array_func(r))
5294  else:
5295  return r
5296  else:
5297  return FuncInterp(Z3_model_get_func_interp(self.ctx.ref(), self.model, decl.ast), self.ctx)
5298  except Z3Exception:
5299  return None
def get_sort (   self,
  idx 
)
Return the unintepreted 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 5315 of file z3py.py.

5316  def get_sort(self, idx):
5317  """Return the unintepreted sort at position `idx` < self.num_sorts().
5318 
5319  >>> A = DeclareSort('A')
5320  >>> B = DeclareSort('B')
5321  >>> a1, a2 = Consts('a1 a2', A)
5322  >>> b1, b2 = Consts('b1 b2', B)
5323  >>> s = Solver()
5324  >>> s.add(a1 != a2, b1 != b2)
5325  >>> s.check()
5326  sat
5327  >>> m = s.model()
5328  >>> m.num_sorts()
5329  2
5330  >>> m.get_sort(0)
5331  A
5332  >>> m.get_sort(1)
5333  B
5334  """
5335  if idx >= self.num_sorts():
5336  raise IndexError
5337  return _to_sort_ref(Z3_model_get_sort(self.ctx.ref(), self.model, idx), self.ctx)
def get_universe (   self,
  s 
)
Return the intepretation 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 5355 of file z3py.py.

Referenced by ModelRef.__getitem__().

5356  def get_universe(self, s):
5357  """Return the intepretation for the uninterpreted sort `s` in the model `self`.
5358 
5359  >>> A = DeclareSort('A')
5360  >>> a, b = Consts('a b', A)
5361  >>> s = Solver()
5362  >>> s.add(a != b)
5363  >>> s.check()
5364  sat
5365  >>> m = s.model()
5366  >>> m.get_universe(A)
5367  [A!val!0, A!val!1]
5368  """
5369  if __debug__:
5370  _z3_assert(isinstance(s, SortRef), "Z3 sort expected")
5371  try:
5372  return AstVector(Z3_model_get_sort_universe(self.ctx.ref(), self.model, s.ast), self.ctx)
5373  except Z3Exception:
5374  return None
def num_sorts (   self)
Return the number of unintepreted 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 5300 of file z3py.py.

Referenced by ModelRef.get_sort().

5301  def num_sorts(self):
5302  """Return the number of unintepreted sorts that contain an interpretation in the model `self`.
5303 
5304  >>> A = DeclareSort('A')
5305  >>> a, b = Consts('a b', A)
5306  >>> s = Solver()
5307  >>> s.add(a != b)
5308  >>> s.check()
5309  sat
5310  >>> m = s.model()
5311  >>> m.num_sorts()
5312  1
5313  """
5314  return int(Z3_model_get_num_sorts(self.ctx.ref(), self.model))
def sexpr (   self)
Return a textual representation of the s-expression representing the model.

Definition at line 5195 of file z3py.py.

5196  def sexpr(self):
5197  """Return a textual representation of the s-expression representing the model."""
5198  return Z3_model_to_string(self.ctx.ref(), self.model)
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 5338 of file z3py.py.

5339  def sorts(self):
5340  """Return all uninterpreted sorts that have an interpretation in the model `self`.
5341 
5342  >>> A = DeclareSort('A')
5343  >>> B = DeclareSort('B')
5344  >>> a1, a2 = Consts('a1 a2', A)
5345  >>> b1, b2 = Consts('b1 b2', B)
5346  >>> s = Solver()
5347  >>> s.add(a1 != a2, b1 != b2)
5348  >>> s.check()
5349  sat
5350  >>> m = s.model()
5351  >>> m.sorts()
5352  [A, B]
5353  """
5354  return [ self.get_sort(i) for i in range(self.num_sorts()) ]

Field Documentation

ctx

Definition at line 5186 of file z3py.py.

Referenced by Probe.__eq__(), Probe.__ge__(), ModelRef.__getitem__(), ApplyResult.__getitem__(), Probe.__gt__(), Probe.__le__(), Probe.__lt__(), Probe.__ne__(), Tactic.apply(), ApplyResult.as_expr(), ApplyResult.convert_model(), ModelRef.decls(), ModelRef.eval(), Fixedpoint.get_assertions(), Fixedpoint.get_cover_delta(), ModelRef.get_interp(), Fixedpoint.get_rules(), ModelRef.get_sort(), ModelRef.get_universe(), Tactic.param_descrs(), Fixedpoint.parse_file(), Fixedpoint.parse_string(), Tactic.solver(), and Fixedpoint.statistics().

model

Definition at line 5185 of file z3py.py.

Referenced by ModelRef.__del__(), ModelRef.__getitem__(), ModelRef.__len__(), ModelRef.decls(), ModelRef.eval(), ModelRef.get_interp(), ModelRef.get_sort(), ModelRef.get_universe(), ModelRef.num_sorts(), and ModelRef.sexpr().