Z3
Loading...
Searching...
No Matches
ModelRef Class Reference
Inheritance diagram for ModelRef:

Public Member Functions

 __init__ (self, m, ctx)
 __del__ (self)
 __repr__ (self)
 sexpr (self)
 eval (self, t, model_completion=False)
 evaluate (self, t, model_completion=False)
 __len__ (self)
 get_interp (self, decl)
 num_sorts (self)
 get_sort (self, idx)
 sorts (self)
 get_universe (self, s)
 __getitem__ (self, idx)
 decls (self)
 update_value (self, x, value)
 translate (self, target)
 project (self, vars, fml)
 project_with_witness (self, vars, fml)
 __copy__ (self)
 __deepcopy__ (self, memo={})
Public Member Functions inherited from Z3PPObject
 use_pp (self)

Data Fields

 model = m
 ctx = ctx

Additional Inherited Members

Protected Member Functions inherited from Z3PPObject
 _repr_html_ (self)

Detailed Description

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

Definition at line 7122 of file z3py.py.

Constructor & Destructor Documentation

◆ __init__()

__init__ ( self,
m,
ctx )

Definition at line 7125 of file z3py.py.

7125 def __init__(self, m, ctx):
7126 assert ctx is not None
7127 self.model = m
7128 self.ctx = ctx
7129 Z3_model_inc_ref(self.ctx.ref(), self.model)
7130
void Z3_API Z3_model_inc_ref(Z3_context c, Z3_model m)
Increment the reference counter of the given model.

◆ __del__()

__del__ ( self)

Definition at line 7131 of file z3py.py.

7131 def __del__(self):
7132 if self.ctx.ref() is not None and Z3_model_dec_ref is not None:
7133 Z3_model_dec_ref(self.ctx.ref(), self.model)
7134
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__()

__copy__ ( self)

Definition at line 7463 of file z3py.py.

7463 def __copy__(self):
7464 return self.translate(self.ctx)
7465

◆ __deepcopy__()

__deepcopy__ ( self,
memo = {} )

Definition at line 7466 of file z3py.py.

7466 def __deepcopy__(self, memo={}):
7467 return self.translate(self.ctx)
7468
7469

◆ __getitem__()

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

7343 def __getitem__(self, idx):
7344 """If `idx` is an integer, then the declaration at position `idx` in the model `self` is returned.
7345 If `idx` is a declaration, then the actual interpretation is returned.
7346
7347 The elements can be retrieved using position or the actual declaration.
7348
7349 >>> f = Function('f', IntSort(), IntSort())
7350 >>> x = Int('x')
7351 >>> s = Solver()
7352 >>> s.add(x > 0, x < 2, f(x) == 0)
7353 >>> s.check()
7354 sat
7355 >>> m = s.model()
7356 >>> len(m)
7357 2
7358 >>> m[0]
7359 x
7360 >>> m[1]
7361 f
7362 >>> m[x]
7363 1
7364 >>> m[f]
7365 [else -> 0]
7366 >>> for d in m: print("%s -> %s" % (d, m[d]))
7367 x -> 1
7368 f -> [else -> 0]
7369 """
7370 if _is_int(idx):
7371 if idx < 0:
7372 idx += len(self)
7373 if idx < 0 or idx >= len(self):
7374 raise IndexError
7375 num_consts = Z3_model_get_num_consts(self.ctx.ref(), self.model)
7376 if (idx < num_consts):
7377 return FuncDeclRef(Z3_model_get_const_decl(self.ctx.ref(), self.model, idx), self.ctx)
7378 else:
7379 return FuncDeclRef(Z3_model_get_func_decl(self.ctx.ref(), self.model, idx - num_consts), self.ctx)
7380 if isinstance(idx, FuncDeclRef):
7381 return self.get_interp(idx)
7382 if is_const(idx):
7383 return self.get_interp(idx.decl())
7384 if isinstance(idx, SortRef):
7385 return self.get_universe(idx)
7386 if z3_debug():
7387 _z3_assert(False, "Integer, Z3 declaration, or Z3 constant expected. Use model.eval instead for complicated expressions")
7388 return None
7389
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.
unsigned Z3_API Z3_model_get_num_consts(Z3_context c, Z3_model m)
Return the number of constants assigned by 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.

◆ __len__()

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

7199 def __len__(self):
7200 """Return the number of constant and function declarations in the model `self`.
7201
7202 >>> f = Function('f', IntSort(), IntSort())
7203 >>> x = Int('x')
7204 >>> s = Solver()
7205 >>> s.add(x > 0, f(x) != x)
7206 >>> s.check()
7207 sat
7208 >>> m = s.model()
7209 >>> len(m)
7210 2
7211 """
7212 num_consts = int(Z3_model_get_num_consts(self.ctx.ref(), self.model))
7213 num_funcs = int(Z3_model_get_num_funcs(self.ctx.ref(), self.model))
7214 return num_consts + num_funcs
7215
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__()

__repr__ ( self)

Definition at line 7135 of file z3py.py.

7135 def __repr__(self):
7136 return obj_to_string(self)
7137

◆ decls()

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

7390 def decls(self):
7391 """Return a list with all symbols that have an interpretation in the model `self`.
7392 >>> f = Function('f', IntSort(), IntSort())
7393 >>> x = Int('x')
7394 >>> s = Solver()
7395 >>> s.add(x > 0, x < 2, f(x) == 0)
7396 >>> s.check()
7397 sat
7398 >>> m = s.model()
7399 >>> m.decls()
7400 [x, f]
7401 """
7402 r = []
7403 for i in range(Z3_model_get_num_consts(self.ctx.ref(), self.model)):
7404 r.append(FuncDeclRef(Z3_model_get_const_decl(self.ctx.ref(), self.model, i), self.ctx))
7405 for i in range(Z3_model_get_num_funcs(self.ctx.ref(), self.model)):
7406 r.append(FuncDeclRef(Z3_model_get_func_decl(self.ctx.ref(), self.model, i), self.ctx))
7407 return r
7408

◆ eval()

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

7142 def eval(self, t, model_completion=False):
7143 """Evaluate the expression `t` in the model `self`.
7144 If `model_completion` is enabled, then a default interpretation is automatically added
7145 for symbols that do not have an interpretation in the model `self`.
7146
7147 >>> x = Int('x')
7148 >>> s = Solver()
7149 >>> s.add(x > 0, x < 2)
7150 >>> s.check()
7151 sat
7152 >>> m = s.model()
7153 >>> m.eval(x + 1)
7154 2
7155 >>> m.eval(x == 1)
7156 True
7157 >>> y = Int('y')
7158 >>> m.eval(y + x)
7159 1 + y
7160 >>> m.eval(y)
7161 y
7162 >>> m.eval(y, model_completion=True)
7163 0
7164 >>> # Now, m contains an interpretation for y
7165 >>> m.eval(y + x)
7166 1
7167 """
7168 r = (Ast * 1)()
7169 if Z3_model_eval(self.ctx.ref(), self.model, t.as_ast(), model_completion, r):
7170 return _to_expr_ref(r[0], self.ctx)
7171 raise Z3Exception("failed to evaluate expression in the model")
7172
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.

Referenced by evaluate().

◆ evaluate()

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

7173 def evaluate(self, t, model_completion=False):
7174 """Alias for `eval`.
7175
7176 >>> x = Int('x')
7177 >>> s = Solver()
7178 >>> s.add(x > 0, x < 2)
7179 >>> s.check()
7180 sat
7181 >>> m = s.model()
7182 >>> m.evaluate(x + 1)
7183 2
7184 >>> m.evaluate(x == 1)
7185 True
7186 >>> y = Int('y')
7187 >>> m.evaluate(y + x)
7188 1 + y
7189 >>> m.evaluate(y)
7190 y
7191 >>> m.evaluate(y, model_completion=True)
7192 0
7193 >>> # Now, m contains an interpretation for y
7194 >>> m.evaluate(y + x)
7195 1
7196 """
7197 return self.eval(t, model_completion)
7198

◆ get_interp()

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

7216 def get_interp(self, decl):
7217 """Return the interpretation for a given declaration or constant.
7218
7219 >>> f = Function('f', IntSort(), IntSort())
7220 >>> x = Int('x')
7221 >>> s = Solver()
7222 >>> s.add(x > 0, x < 2, f(x) == 0)
7223 >>> s.check()
7224 sat
7225 >>> m = s.model()
7226 >>> m[x]
7227 1
7228 >>> m[f]
7229 [else -> 0]
7230 """
7231 if z3_debug():
7232 _z3_assert(isinstance(decl, FuncDeclRef) or is_const(decl), "Z3 declaration expected")
7233 if is_const(decl):
7234 decl = decl.decl()
7235 try:
7236 if decl.arity() == 0:
7237 _r = Z3_model_get_const_interp(self.ctx.ref(), self.model, decl.ast)
7238 if _r.value is None:
7239 return None
7240 r = _to_expr_ref(_r, self.ctx)
7241 if is_as_array(r):
7242 fi = self.get_interp(get_as_array_func(r))
7243 if fi is None:
7244 return fi
7245 e = fi.else_value()
7246 if e is None:
7247 return fi
7248 if fi.arity() != 1:
7249 return fi
7250 srt = decl.range()
7251 dom = srt.domain()
7252 e = K(dom, e)
7253 i = 0
7254 sz = fi.num_entries()
7255 n = fi.arity()
7256 while i < sz:
7257 fe = fi.entry(i)
7258 e = Store(e, fe.arg_value(0), fe.value())
7259 i += 1
7260 return e
7261 else:
7262 return r
7263 else:
7264 return FuncInterp(Z3_model_get_func_interp(self.ctx.ref(), self.model, decl.ast), self.ctx)
7265 except Z3Exception:
7266 return None
7267
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,...
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...

Referenced by __getitem__(), and get_interp().

◆ get_sort()

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

7283 def get_sort(self, idx):
7284 """Return the uninterpreted sort at position `idx` < self.num_sorts().
7285
7286 >>> A = DeclareSort('A')
7287 >>> B = DeclareSort('B')
7288 >>> a1, a2 = Consts('a1 a2', A)
7289 >>> b1, b2 = Consts('b1 b2', B)
7290 >>> s = Solver()
7291 >>> s.add(a1 != a2, b1 != b2)
7292 >>> s.check()
7293 sat
7294 >>> m = s.model()
7295 >>> m.num_sorts()
7296 2
7297 >>> m.get_sort(0)
7298 A
7299 >>> m.get_sort(1)
7300 B
7301 """
7302 if idx >= self.num_sorts():
7303 raise IndexError
7304 return _to_sort_ref(Z3_model_get_sort(self.ctx.ref(), self.model, idx), self.ctx)
7305
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.

Referenced by sorts().

◆ get_universe()

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

7323 def get_universe(self, s):
7324 """Return the interpretation for the uninterpreted sort `s` in the model `self`.
7325
7326 >>> A = DeclareSort('A')
7327 >>> a, b = Consts('a b', A)
7328 >>> s = Solver()
7329 >>> s.add(a != b)
7330 >>> s.check()
7331 sat
7332 >>> m = s.model()
7333 >>> m.get_universe(A)
7334 [A!val!0, A!val!1]
7335 """
7336 if z3_debug():
7337 _z3_assert(isinstance(s, SortRef), "Z3 sort expected")
7338 try:
7339 return AstVector(Z3_model_get_sort_universe(self.ctx.ref(), self.model, s.ast), self.ctx)
7340 except Z3Exception:
7341 return None
7342
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.

Referenced by __getitem__().

◆ num_sorts()

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

7268 def num_sorts(self):
7269 """Return the number of uninterpreted sorts that contain an interpretation in the model `self`.
7270
7271 >>> A = DeclareSort('A')
7272 >>> a, b = Consts('a b', A)
7273 >>> s = Solver()
7274 >>> s.add(a != b)
7275 >>> s.check()
7276 sat
7277 >>> m = s.model()
7278 >>> m.num_sorts()
7279 1
7280 """
7281 return int(Z3_model_get_num_sorts(self.ctx.ref(), self.model))
7282
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.

Referenced by get_sort(), and sorts().

◆ project()

project ( self,
vars,
fml )
Perform model-based projection on fml with respect to vars.
Assume that the model satisfies fml. Then compute a projection fml_p, such
that vars do not occur free in fml_p, fml_p is true in the model and
fml_p => exists vars . fml

Definition at line 7439 of file z3py.py.

7439 def project(self, vars, fml):
7440 """Perform model-based projection on fml with respect to vars.
7441 Assume that the model satisfies fml. Then compute a projection fml_p, such
7442 that vars do not occur free in fml_p, fml_p is true in the model and
7443 fml_p => exists vars . fml
7444 """
7445 ctx = self.ctx.ref()
7446 _vars = (Ast * len(vars))()
7447 for i in range(len(vars)):
7448 _vars[i] = vars[i].as_ast()
7449 return _to_expr_ref(Z3_qe_model_project(ctx, self.model, len(vars), _vars, fml.ast), self.ctx)
7450

◆ project_with_witness()

project_with_witness ( self,
vars,
fml )
Perform model-based projection, but also include realizer terms for the projected variables

Definition at line 7451 of file z3py.py.

7451 def project_with_witness(self, vars, fml):
7452 """Perform model-based projection, but also include realizer terms for the projected variables"""
7453 ctx = self.ctx.ref()
7454 _vars = (Ast * len(vars))()
7455 for i in range(len(vars)):
7456 _vars[i] = vars[i].as_ast()
7457 defs = AstMap()
7458 result = Z3_qe_model_project_with_witness(ctx, self.model, len(vars), _vars, fml.ast, defs.map)
7459 result = _to_expr_ref(result, self.ctx)
7460 return result, defs
7461
7462

◆ sexpr()

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

Definition at line 7138 of file z3py.py.

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

◆ sorts()

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

7306 def sorts(self):
7307 """Return all uninterpreted sorts that have an interpretation in the model `self`.
7308
7309 >>> A = DeclareSort('A')
7310 >>> B = DeclareSort('B')
7311 >>> a1, a2 = Consts('a1 a2', A)
7312 >>> b1, b2 = Consts('b1 b2', B)
7313 >>> s = Solver()
7314 >>> s.add(a1 != a2, b1 != b2)
7315 >>> s.check()
7316 sat
7317 >>> m = s.model()
7318 >>> m.sorts()
7319 [A, B]
7320 """
7321 return [self.get_sort(i) for i in range(self.num_sorts())]
7322

◆ translate()

translate ( self,
target )
Translate `self` to the context `target`. That is, return a copy of `self` in the context `target`.

Definition at line 7431 of file z3py.py.

7431 def translate(self, target):
7432 """Translate `self` to the context `target`. That is, return a copy of `self` in the context `target`.
7433 """
7434 if z3_debug():
7435 _z3_assert(isinstance(target, Context), "argument must be a Z3 context")
7436 model = Z3_model_translate(self.ctx.ref(), self.model, target.ref())
7437 return ModelRef(model, target)
7438
Z3_model Z3_API Z3_model_translate(Z3_context c, Z3_model m, Z3_context dst)
translate model from context c to context dst.

Referenced by __copy__(), and __deepcopy__().

◆ update_value()

update_value ( self,
x,
value )
Update the interpretation of a constant

Definition at line 7409 of file z3py.py.

7409 def update_value(self, x, value):
7410 """Update the interpretation of a constant"""
7411 if is_expr(x):
7412 x = x.decl()
7413 if is_func_decl(x) and x.arity() != 0 and isinstance(value, FuncInterp):
7414 fi1 = value.f
7415 fi2 = Z3_add_func_interp(x.ctx_ref(), self.model, x.ast, value.else_value().ast);
7416 fi2 = FuncInterp(fi2, x.ctx)
7417 for i in range(value.num_entries()):
7418 e = value.entry(i)
7419 n = Z3_func_entry_get_num_args(x.ctx_ref(), e.entry)
7420 v = AstVector()
7421 for j in range(n):
7422 v.push(e.arg_value(j))
7423 val = Z3_func_entry_get_value(x.ctx_ref(), e.entry)
7424 Z3_func_interp_add_entry(x.ctx_ref(), fi2.f, v.vector, val)
7425 return
7426 if not is_func_decl(x) or x.arity() != 0:
7427 raise Z3Exception("Expecting 0-ary function or constant expression")
7428 value = _py2expr(value)
7429 Z3_add_const_interp(x.ctx_ref(), self.model, x.ast, value.ast)
7430
Z3_func_interp Z3_API Z3_add_func_interp(Z3_context c, Z3_model m, Z3_func_decl f, Z3_ast default_value)
Create a fresh func_interp object, add it to a model for a specified function. It has reference count...
unsigned Z3_API Z3_func_entry_get_num_args(Z3_context c, Z3_func_entry e)
Return the number of arguments in a Z3_func_entry object.
Z3_ast Z3_API Z3_func_entry_get_value(Z3_context c, Z3_func_entry e)
Return the value of this point.
void Z3_API Z3_add_const_interp(Z3_context c, Z3_model m, Z3_func_decl f, Z3_ast a)
Add a constant interpretation.
void Z3_API Z3_func_interp_add_entry(Z3_context c, Z3_func_interp fi, Z3_ast_vector args, Z3_ast value)
add a function entry to a function interpretation.

Field Documentation

◆ ctx

◆ model