9 """Z3 is a high performance theorem prover developed at Microsoft Research. Z3 is used in many applications such as: software/hardware verification and testing, constraint solving, analysis of hybrid systems, security, biology (in silico analysis), and geometrical problems.
11 Several online tutorials for Z3Py are available at:
12 http://rise4fun.com/Z3Py/tutorial/guide
14 Please send feedback, comments and/or corrections to leonardo@microsoft.com. Your comments are very valuable.
37 ... # the expression x + y is type incorrect
39 ... except Z3Exception as ex:
40 ... print("failed: %s" % ex)
45 from z3consts
import *
46 from z3printer
import *
47 from fractions
import Fraction
53 return isinstance(v, int)
or isinstance(v, long)
56 return isinstance(v, int)
65 major = ctypes.c_uint(0)
66 minor = ctypes.c_uint(0)
67 build = ctypes.c_uint(0)
68 rev = ctypes.c_uint(0)
70 return "%s.%s.%s" % (major.value, minor.value, build.value)
73 major = ctypes.c_uint(0)
74 minor = ctypes.c_uint(0)
75 build = ctypes.c_uint(0)
76 rev = ctypes.c_uint(0)
78 return (major.value, minor.value, build.value, rev.value)
82 def _z3_assert(cond, msg):
84 raise Z3Exception(msg)
87 """Log interaction to a file. This function must be invoked immediately after init(). """
91 """Append user-defined string to interaction log. """
95 """Convert an integer or string into a Z3 symbol."""
96 if isinstance(s, int):
101 def _symbol2py(ctx, s):
102 """Convert a Z3 symbol back into a Python object. """
108 _error_handler_fptr = ctypes.CFUNCTYPE(
None, ctypes.c_void_p, ctypes.c_uint)
114 if len(args) == 1
and (isinstance(args[0], tuple)
or isinstance(args[0], list)):
121 def _Z3python_error_handler_core(c, e):
128 def _to_param_value(val):
129 if isinstance(val, bool):
138 """A Context manages all other Z3 objects, global configuration options, etc.
140 Z3Py uses a default global context. For most applications this is sufficient.
141 An application may use multiple Z3 contexts. Objects created in one context
142 cannot be used in another one. However, several objects may be "translated" from
143 one context to another. It is not safe to access Z3 objects from multiple threads.
144 The only exception is the method `interrupt()` that can be used to interrupt() a long
146 The initialization method receives global configuration options for the new context.
150 _z3_assert(len(args) % 2 == 0,
"Argument list must have an even number of elements.")
165 lib().Z3_set_error_handler.restype =
None
166 lib().Z3_set_error_handler.argtypes = [ContextObj, _error_handler_fptr]
171 self.lib.Z3_del_context(self.
ctx)
174 """Return a reference to the actual C pointer to the Z3 context."""
178 """Interrupt a solver performing a satisfiability test, a tactic processing a goal, or simplify functions.
180 This method can be invoked from a thread different from the one executing the
181 interruptable procedure.
189 """Return a reference to the global Z3 context.
192 >>> x.ctx == main_ctx()
197 >>> x2 = Real('x', c)
204 if _main_ctx ==
None:
215 """Set Z3 global (or module) parameters.
217 >>> set_param(precision=10)
220 _z3_assert(len(args) % 2 == 0,
"Argument list must have an even number of elements.")
224 if not set_pp_option(k, v):
238 """Reset all global (or module) parameters.
243 """Alias for 'set_param' for backward compatibility.
248 """Return the value of a Z3 global (or module) parameter
250 >>> get_param('nlsat.reorder')
253 ptr = (ctypes.c_char_p * 1)()
255 r = z3core._to_pystr(ptr[0])
257 raise Z3Exception(
"failed to retrieve value for '%s'" % name)
267 """Superclass for all Z3 objects that have support for pretty printing."""
272 """AST are Direct Acyclic Graphs (DAGs) used to represent sorts, declarations and expressions."""
282 return obj_to_string(self)
285 return obj_to_string(self)
288 """Return an string representing the AST node in s-expression notation.
291 >>> ((x + 1)*x).sexpr()
297 """Return a pointer to the corresponding C Z3_ast object."""
301 """Return unique identifier for object. It can be used for hash-tables and maps."""
306 """Return a reference to the C context where this AST node is stored."""
307 return self.ctx.ref()
310 """Return `True` if `self` and `other` are structurally identical.
317 >>> n1 = simplify(n1)
318 >>> n2 = simplify(n2)
323 _z3_assert(
is_ast(other),
"Z3 AST expected")
327 """Translate `self` to the context `target`. That is, return a copy of `self` in the context `target`.
333 >>> # Nodes in different contexts can't be mixed.
334 >>> # However, we can translate nodes from one context to another.
335 >>> x.translate(c2) + y
339 _z3_assert(isinstance(target, Context),
"argument must be a Z3 context")
343 """Return a hashcode for the `self`.
345 >>> n1 = simplify(Int('x') + 1)
346 >>> n2 = simplify(2 + Int('x') - 1)
347 >>> n1.hash() == n2.hash()
353 """Return `True` if `a` is an AST node.
357 >>> is_ast(IntVal(10))
361 >>> is_ast(BoolSort())
363 >>> is_ast(Function('f', IntSort(), IntSort()))
370 return isinstance(a, AstRef)
373 """Return `True` if `a` and `b` are structurally identical AST nodes.
383 >>> eq(simplify(x + 1), simplify(1 + x))
390 def _ast_kind(ctx, a):
395 def _ctx_from_ast_arg_list(args, default_ctx=None):
403 _z3_assert(ctx == a.ctx,
"Context mismatch")
408 def _ctx_from_ast_args(*args):
409 return _ctx_from_ast_arg_list(args)
411 def _to_func_decl_array(args):
413 _args = (FuncDecl * sz)()
415 _args[i] = args[i].as_func_decl()
418 def _to_ast_array(args):
422 _args[i] = args[i].as_ast()
425 def _to_ref_array(ref, args):
429 _args[i] = args[i].as_ast()
432 def _to_ast_ref(a, ctx):
433 k = _ast_kind(ctx, a)
435 return _to_sort_ref(a, ctx)
436 elif k == Z3_FUNC_DECL_AST:
437 return _to_func_decl_ref(a, ctx)
439 return _to_expr_ref(a, ctx)
447 def _sort_kind(ctx, s):
451 """A Sort is essentially a type. Every Z3 expression has a sort. A sort is an AST node."""
460 """Return the Z3 internal kind of a sort. This method can be used to test if `self` is one of the Z3 builtin sorts.
463 >>> b.kind() == Z3_BOOL_SORT
465 >>> b.kind() == Z3_INT_SORT
467 >>> A = ArraySort(IntSort(), IntSort())
468 >>> A.kind() == Z3_ARRAY_SORT
470 >>> A.kind() == Z3_INT_SORT
473 return _sort_kind(self.
ctx, self.
ast)
476 """Return `True` if `self` is a subsort of `other`.
478 >>> IntSort().subsort(RealSort())
484 """Try to cast `val` as an element of sort `self`.
486 This method is used in Z3Py to convert Python objects such as integers,
487 floats, longs and strings into Z3 expressions.
490 >>> RealSort().cast(x)
494 _z3_assert(
is_expr(val),
"Z3 expression expected")
495 _z3_assert(self.
eq(val.sort()),
"Sort mismatch")
499 """Return the name (string) of sort `self`.
501 >>> BoolSort().name()
503 >>> ArraySort(IntSort(), IntSort()).name()
509 """Return `True` if `self` and `other` are the same Z3 sort.
512 >>> p.sort() == BoolSort()
514 >>> p.sort() == IntSort()
522 """Return `True` if `self` and `other` are not the same Z3 sort.
525 >>> p.sort() != BoolSort()
527 >>> p.sort() != IntSort()
533 """Return `True` if `s` is a Z3 sort.
535 >>> is_sort(IntSort())
537 >>> is_sort(Int('x'))
539 >>> is_expr(Int('x'))
542 return isinstance(s, SortRef)
544 def _to_sort_ref(s, ctx):
546 _z3_assert(isinstance(s, Sort),
"Z3 Sort expected")
547 k = _sort_kind(ctx, s)
548 if k == Z3_BOOL_SORT:
550 elif k == Z3_INT_SORT
or k == Z3_REAL_SORT:
552 elif k == Z3_BV_SORT:
554 elif k == Z3_ARRAY_SORT:
556 elif k == Z3_DATATYPE_SORT:
561 return _to_sort_ref(
Z3_get_sort(ctx.ref(), a), ctx)
564 """Create a new uninterpred sort named `name`.
566 If `ctx=None`, then the new sort is declared in the global Z3Py context.
568 >>> A = DeclareSort('A')
569 >>> a = Const('a', A)
570 >>> b = Const('b', A)
588 """Function declaration. Every constant and function have an associated declaration.
590 The declaration assigns a name, a sort (i.e., type), and for function
591 the sort (i.e., type) of each of its arguments. Note that, in Z3,
592 a constant is a function with 0 arguments.
604 """Return the name of the function declaration `self`.
606 >>> f = Function('f', IntSort(), IntSort())
609 >>> isinstance(f.name(), str)
615 """Return the number of arguments of a function declaration. If `self` is a constant, then `self.arity()` is 0.
617 >>> f = Function('f', IntSort(), RealSort(), BoolSort())
624 """Return the sort of the argument `i` of a function declaration. This method assumes that `0 <= i < self.arity()`.
626 >>> f = Function('f', IntSort(), RealSort(), BoolSort())
633 _z3_assert(i < self.
arity(),
"Index out of bounds")
637 """Return the sort of the range of a function declaration. For constants, this is the sort of the constant.
639 >>> f = Function('f', IntSort(), RealSort(), BoolSort())
646 """Return the internal kind of a function declaration. It can be used to identify Z3 built-in functions such as addition, multiplication, etc.
649 >>> d = (x + 1).decl()
650 >>> d.kind() == Z3_OP_ADD
652 >>> d.kind() == Z3_OP_MUL
658 """Create a Z3 application expression using the function `self`, and the given arguments.
660 The arguments must be Z3 expressions. This method assumes that
661 the sorts of the elements in `args` match the sorts of the
662 domain. Limited coersion is supported. For example, if
663 args[0] is a Python integer, and the function expects a Z3
664 integer, then the argument is automatically converted into a
667 >>> f = Function('f', IntSort(), RealSort(), BoolSort())
675 args = _get_args(args)
678 _z3_assert(num == self.
arity(),
"Incorrect number of arguments to %s" % self)
679 _args = (Ast * num)()
684 tmp = self.
domain(i).cast(args[i])
686 _args[i] = tmp.as_ast()
690 """Return `True` if `a` is a Z3 function declaration.
692 >>> f = Function('f', IntSort(), IntSort())
699 return isinstance(a, FuncDeclRef)
702 """Create a new Z3 uninterpreted function with the given sorts.
704 >>> f = Function('f', IntSort(), IntSort())
710 _z3_assert(len(sig) > 0,
"At least two arguments expected")
714 _z3_assert(
is_sort(rng),
"Z3 sort expected")
715 dom = (Sort * arity)()
716 for i
in range(arity):
718 _z3_assert(
is_sort(sig[i]),
"Z3 sort expected")
723 def _to_func_decl_ref(a, ctx):
733 """Constraints, formulas and terms are expressions in Z3.
735 Expressions are ASTs. Every expression has a sort.
736 There are three main kinds of expressions:
737 function applications, quantifiers and bounded variables.
738 A constant is a function application with 0 arguments.
739 For quantifier free problems, all expressions are
740 function applications.
749 """Return the sort of expression `self`.
761 """Shorthand for `self.sort().kind()`.
763 >>> a = Array('a', IntSort(), IntSort())
764 >>> a.sort_kind() == Z3_ARRAY_SORT
766 >>> a.sort_kind() == Z3_INT_SORT
769 return self.
sort().kind()
772 """Return a Z3 expression that represents the constraint `self == other`.
774 If `other` is `None`, then this method simply returns `False`.
785 a, b = _coerce_exprs(self, other)
789 """Return a Z3 expression that represents the constraint `self != other`.
791 If `other` is `None`, then this method simply returns `True`.
802 a, b = _coerce_exprs(self, other)
803 _args, sz = _to_ast_array((a, b))
807 """Return the Z3 function declaration associated with a Z3 application.
809 >>> f = Function('f', IntSort(), IntSort())
818 _z3_assert(
is_app(self),
"Z3 application expected")
822 """Return the number of arguments of a Z3 application.
826 >>> (a + b).num_args()
828 >>> f = Function('f', IntSort(), IntSort(), IntSort(), IntSort())
834 _z3_assert(
is_app(self),
"Z3 application expected")
838 """Return argument `idx` of the application `self`.
840 This method assumes that `self` is a function application with at least `idx+1` arguments.
844 >>> f = Function('f', IntSort(), IntSort(), IntSort(), IntSort())
854 _z3_assert(
is_app(self),
"Z3 application expected")
855 _z3_assert(idx < self.
num_args(),
"Invalid argument index")
859 """Return a list containing the children of the given expression
863 >>> f = Function('f', IntSort(), IntSort(), IntSort(), IntSort())
869 return [self.
arg(i)
for i
in range(self.
num_args())]
873 def _to_expr_ref(a, ctx):
874 if isinstance(a, Pattern):
878 if k == Z3_QUANTIFIER_AST:
881 if sk == Z3_BOOL_SORT:
883 if sk == Z3_INT_SORT:
884 if k == Z3_NUMERAL_AST:
887 if sk == Z3_REAL_SORT:
888 if k == Z3_NUMERAL_AST:
890 if _is_algebraic(ctx, a):
894 if k == Z3_NUMERAL_AST:
898 if sk == Z3_ARRAY_SORT:
900 if sk == Z3_DATATYPE_SORT:
904 def _coerce_expr_merge(s, a):
917 _z3_assert(s1.ctx == s.ctx,
"context mismatch")
918 _z3_assert(
False,
"sort mismatch")
922 def _coerce_exprs(a, b, ctx=None):
927 s = _coerce_expr_merge(s, a)
928 s = _coerce_expr_merge(s, b)
933 def _reduce(f, l, a):
939 def _coerce_expr_list(alist, ctx=None):
946 alist = [ _py2expr(a, ctx)
for a
in alist ]
947 s = _reduce(_coerce_expr_merge, alist,
None)
948 return [ s.cast(a)
for a
in alist ]
951 """Return `True` if `a` is a Z3 expression.
958 >>> is_expr(IntSort())
962 >>> is_expr(IntVal(1))
965 >>> is_expr(ForAll(x, x >= 0))
968 return isinstance(a, ExprRef)
971 """Return `True` if `a` is a Z3 function application.
973 Note that, constants are function applications with 0 arguments.
980 >>> is_app(IntSort())
984 >>> is_app(IntVal(1))
987 >>> is_app(ForAll(x, x >= 0))
990 if not isinstance(a, ExprRef):
992 k = _ast_kind(a.ctx, a)
993 return k == Z3_NUMERAL_AST
or k == Z3_APP_AST
996 """Return `True` if `a` is Z3 constant/variable expression.
1005 >>> is_const(IntVal(1))
1008 >>> is_const(ForAll(x, x >= 0))
1011 return is_app(a)
and a.num_args() == 0
1014 """Return `True` if `a` is variable.
1016 Z3 uses de-Bruijn indices for representing bound variables in
1024 >>> f = Function('f', IntSort(), IntSort())
1025 >>> # Z3 replaces x with bound variables when ForAll is executed.
1026 >>> q = ForAll(x, f(x) == x)
1032 >>> is_var(b.arg(1))
1035 return is_expr(a)
and _ast_kind(a.ctx, a) == Z3_VAR_AST
1038 """Return the de-Bruijn index of the Z3 bounded variable `a`.
1046 >>> f = Function('f', IntSort(), IntSort(), IntSort())
1047 >>> # Z3 replaces x and y with bound variables when ForAll is executed.
1048 >>> q = ForAll([x, y], f(x, y) == x + y)
1050 f(Var(1), Var(0)) == Var(1) + Var(0)
1054 >>> v1 = b.arg(0).arg(0)
1055 >>> v2 = b.arg(0).arg(1)
1060 >>> get_var_index(v1)
1062 >>> get_var_index(v2)
1066 _z3_assert(
is_var(a),
"Z3 bound variable expected")
1070 """Return `True` if `a` is an application of the given kind `k`.
1074 >>> is_app_of(n, Z3_OP_ADD)
1076 >>> is_app_of(n, Z3_OP_MUL)
1079 return is_app(a)
and a.decl().kind() == k
1081 def If(a, b, c, ctx=None):
1082 """Create a Z3 if-then-else expression.
1086 >>> max = If(x > y, x, y)
1092 if isinstance(a, Probe)
or isinstance(b, Tactic)
or isinstance(c, Tactic):
1093 return Cond(a, b, c, ctx)
1095 ctx = _get_ctx(_ctx_from_ast_arg_list([a, b, c], ctx))
1098 b, c = _coerce_exprs(b, c, ctx)
1100 _z3_assert(a.ctx == b.ctx,
"Context mismatch")
1101 return _to_expr_ref(
Z3_mk_ite(ctx.ref(), a.as_ast(), b.as_ast(), c.as_ast()), ctx)
1104 """Create a Z3 distinct expression.
1111 >>> Distinct(x, y, z)
1113 >>> simplify(Distinct(x, y, z))
1115 >>> simplify(Distinct(x, y, z), blast_distinct=True)
1116 And(Not(x == y), Not(x == z), Not(y == z))
1118 args = _get_args(args)
1119 ctx = _ctx_from_ast_arg_list(args)
1121 _z3_assert(ctx !=
None,
"At least one of the arguments must be a Z3 expression")
1122 args = _coerce_expr_list(args, ctx)
1123 _args, sz = _to_ast_array(args)
1126 def _mk_bin(f, a, b):
1129 _z3_assert(a.ctx == b.ctx,
"Context mismatch")
1130 args[0] = a.as_ast()
1131 args[1] = b.as_ast()
1132 return f(a.ctx.ref(), 2, args)
1135 """Create a constant of the given sort.
1137 >>> Const('x', IntSort())
1141 _z3_assert(isinstance(sort, SortRef),
"Z3 sort expected")
1146 """Create a several constants of the given sort.
1148 `names` is a string containing the names of all constants to be created.
1149 Blank spaces separate the names of different constants.
1151 >>> x, y, z = Consts('x y z', IntSort())
1155 if isinstance(names, str):
1156 names = names.split(
" ")
1157 return [
Const(name, sort)
for name
in names]
1160 """Create a Z3 free variable. Free variables are used to create quantified formulas.
1162 >>> Var(0, IntSort())
1164 >>> eq(Var(0, IntSort()), Var(0, BoolSort()))
1168 _z3_assert(
is_sort(s),
"Z3 sort expected")
1169 return _to_expr_ref(
Z3_mk_bound(s.ctx_ref(), idx, s.ast), s.ctx)
1173 Create a real free variable. Free variables are used to create quantified formulas.
1174 They are also used to create polynomials.
1183 Create a list of Real free variables.
1184 The variables have ids: 0, 1, ..., n-1
1186 >>> x0, x1, x2, x3 = RealVarVector(4)
1190 return [
RealVar(i, ctx)
for i
in range(n) ]
1201 """Try to cast `val` as a Boolean.
1203 >>> x = BoolSort().cast(True)
1213 if isinstance(val, bool):
1216 _z3_assert(
is_expr(val),
"True, False or Z3 Boolean expression expected")
1217 _z3_assert(self.
eq(val.sort()),
"Value cannot be converted into a Z3 Boolean value")
1221 """All Boolean expressions are instances of this class."""
1226 """Return `True` if `a` is a Z3 Boolean expression.
1232 >>> is_bool(And(p, q))
1240 return isinstance(a, BoolRef)
1243 """Return `True` if `a` is the Z3 true expression.
1248 >>> is_true(simplify(p == p))
1253 >>> # True is a Python Boolean expression
1260 """Return `True` if `a` is the Z3 false expression.
1267 >>> is_false(BoolVal(False))
1273 """Return `True` if `a` is a Z3 and expression.
1275 >>> p, q = Bools('p q')
1276 >>> is_and(And(p, q))
1278 >>> is_and(Or(p, q))
1284 """Return `True` if `a` is a Z3 or expression.
1286 >>> p, q = Bools('p q')
1289 >>> is_or(And(p, q))
1295 """Return `True` if `a` is a Z3 not expression.
1306 """Return `True` if `a` is a Z3 equality expression.
1308 >>> x, y = Ints('x y')
1315 """Return `True` if `a` is a Z3 distinct expression.
1317 >>> x, y, z = Ints('x y z')
1318 >>> is_distinct(x == y)
1320 >>> is_distinct(Distinct(x, y, z))
1326 """Return the Boolean Z3 sort. If `ctx=None`, then the global context is used.
1330 >>> p = Const('p', BoolSort())
1333 >>> r = Function('r', IntSort(), IntSort(), BoolSort())
1340 return BoolSortRef(Z3_mk_bool_sort(ctx.ref()), ctx)
1342 def BoolVal(val, ctx=None):
1343 """Return the Boolean value `
True`
or `
False`. If `ctx=
None`, then the
global context
is used.
1356 return BoolRef(Z3_mk_false(ctx.ref()), ctx)
1358 return BoolRef(Z3_mk_true(ctx.ref()), ctx)
1360 def Bool(name, ctx=None):
1361 """Return a Boolean constant named `name`. If `ctx=
None`, then the
global context
is used.
1369 return BoolRef(Z3_mk_const(ctx.ref(), to_symbol(name, ctx), BoolSort(ctx).ast), ctx)
1371 def Bools(names, ctx=None):
1372 """Return a tuple of Boolean constants.
1374 `names`
is a single string containing all names separated by blank spaces.
1375 If `ctx=
None`, then the
global context
is used.
1377 >>> p, q, r =
Bools(
'p q r')
1378 >>>
And(p,
Or(q, r))
1382 if isinstance(names, str):
1383 names = names.split(" ")
1384 return [Bool(name, ctx) for name in names]
1386 def BoolVector(prefix, sz, ctx=None):
1387 """Return a list of Boolean constants of size `sz`.
1389 The constants are named using the given prefix.
1390 If `ctx=
None`, then the
global context
is used.
1396 And(p__0, p__1, p__2)
1398 return [ Bool('%s__%s' % (prefix, i)) for i in range(sz) ]
1400 def FreshBool(prefix='b', ctx=None):
1401 """Return a fresh Boolean constant
in the given context using the given prefix.
1403 If `ctx=
None`, then the
global context
is used.
1411 return BoolRef(Z3_mk_fresh_const(ctx.ref(), prefix, BoolSort(ctx).ast), ctx)
1413 def Implies(a, b, ctx=None):
1414 """Create a Z3 implies expression.
1416 >>> p, q =
Bools(
'p q')
1422 ctx = _get_ctx(_ctx_from_ast_arg_list([a, b], ctx))
1426 return BoolRef(Z3_mk_implies(ctx.ref(), a.as_ast(), b.as_ast()), ctx)
1428 def Xor(a, b, ctx=None):
1429 """Create a Z3 Xor expression.
1431 >>> p, q =
Bools(
'p q')
1437 ctx = _get_ctx(_ctx_from_ast_arg_list([a, b], ctx))
1441 return BoolRef(Z3_mk_xor(ctx.ref(), a.as_ast(), b.as_ast()), ctx)
1443 def Not(a, ctx=None):
1444 """Create a Z3
not expression
or probe.
1452 ctx = _get_ctx(_ctx_from_ast_arg_list([a], ctx))
1454 # Not is also used to build probes
1455 return Probe(Z3_probe_not(ctx.ref(), a.probe), ctx)
1459 return BoolRef(Z3_mk_not(ctx.ref(), a.as_ast()), ctx)
1461 def _has_probe(args):
1462 """Return `
True`
if one of the elements of the given collection
is a Z3 probe.
"""
1469 """Create a Z3
and-expression
or and-probe.
1471 >>> p, q, r =
Bools(
'p q r')
1476 And(p__0, p__1, p__2, p__3, p__4)
1480 last_arg = args[len(args)-1]
1481 if isinstance(last_arg, Context):
1482 ctx = args[len(args)-1]
1483 args = args[:len(args)-1]
1486 args = _get_args(args)
1487 ctx_args = _ctx_from_ast_arg_list(args, ctx)
1489 _z3_assert(ctx_args == None or ctx_args == ctx, "context mismatch")
1490 _z3_assert(ctx != None, "At least one of the arguments must be a Z3 expression or probe")
1491 if _has_probe(args):
1492 return _probe_and(args, ctx)
1494 args = _coerce_expr_list(args, ctx)
1495 _args, sz = _to_ast_array(args)
1496 return BoolRef(Z3_mk_and(ctx.ref(), sz, _args), ctx)
1499 """Create a Z3
or-expression
or or-probe.
1501 >>> p, q, r =
Bools(
'p q r')
1506 Or(p__0, p__1, p__2, p__3, p__4)
1510 last_arg = args[len(args)-1]
1511 if isinstance(last_arg, Context):
1512 ctx = args[len(args)-1]
1513 args = args[:len(args)-1]
1516 args = _get_args(args)
1517 ctx_args = _ctx_from_ast_arg_list(args, ctx)
1519 _z3_assert(ctx_args == None or ctx_args == ctx, "context mismatch")
1520 _z3_assert(ctx != None, "At least one of the arguments must be a Z3 expression or probe")
1521 if _has_probe(args):
1522 return _probe_or(args, ctx)
1524 args = _coerce_expr_list(args, ctx)
1525 _args, sz = _to_ast_array(args)
1526 return BoolRef(Z3_mk_or(ctx.ref(), sz, _args), ctx)
1528 #########################################
1532 #########################################
1534 class PatternRef(ExprRef):
1535 """Patterns are hints
for quantifier instantiation.
1537 See http://rise4fun.com/Z3Py/tutorial/advanced
for more details.
1540 return Z3_pattern_to_ast(self.ctx_ref(), self.ast)
1543 return Z3_get_ast_id(self.ctx_ref(), self.as_ast())
1546 """Return `
True`
if `a`
is a Z3 pattern (hint
for quantifier instantiation.
1548 See http://rise4fun.com/Z3Py/tutorial/advanced
for more details.
1552 >>> q =
ForAll(x, f(x) == 0, patterns = [ f(x) ])
1555 >>> q.num_patterns()
1562 return isinstance(a, PatternRef)
1564 def MultiPattern(*args):
1565 """Create a Z3 multi-pattern using the given expressions `*args`
1567 See http://rise4fun.com/Z3Py/tutorial/advanced
for more details.
1575 >>> q.num_patterns()
1583 _z3_assert(len(args) > 0, "At least one argument expected")
1584 _z3_assert(all([ is_expr(a) for a in args ]), "Z3 expressions expected")
1586 args, sz = _to_ast_array(args)
1587 return PatternRef(Z3_mk_pattern(ctx.ref(), sz, args), ctx)
1589 def _to_pattern(arg):
1593 return MultiPattern(arg)
1595 #########################################
1599 #########################################
1601 class QuantifierRef(BoolRef):
1602 """Universally
and Existentially quantified formulas.
"""
1608 return Z3_get_ast_id(self.ctx_ref(), self.as_ast())
1611 """Return the Boolean sort.
"""
1612 return BoolSort(self.ctx)
1614 def is_forall(self):
1615 """Return `
True`
if `self`
is a universal quantifier.
1619 >>> q =
ForAll(x, f(x) == 0)
1622 >>> q =
Exists(x, f(x) != 0)
1626 return Z3_is_quantifier_forall(self.ctx_ref(), self.ast)
1629 """Return the weight annotation of `self`.
1633 >>> q =
ForAll(x, f(x) == 0)
1636 >>> q =
ForAll(x, f(x) == 0, weight=10)
1640 return int(Z3_get_quantifier_weight(self.ctx_ref(), self.ast))
1642 def num_patterns(self):
1643 """Return the number of patterns (i.e., quantifier instantiation hints)
in `self`.
1648 >>> q =
ForAll(x, f(x) != g(x), patterns = [ f(x), g(x) ])
1649 >>> q.num_patterns()
1652 return int(Z3_get_quantifier_num_patterns(self.ctx_ref(), self.ast))
1654 def pattern(self, idx):
1655 """Return a pattern (i.e., quantifier instantiation hints)
in `self`.
1660 >>> q =
ForAll(x, f(x) != g(x), patterns = [ f(x), g(x) ])
1661 >>> q.num_patterns()
1669 _z3_assert(idx < self.num_patterns(), "Invalid pattern idx")
1670 return PatternRef(Z3_get_quantifier_pattern_ast(self.ctx_ref(), self.ast, idx), self.ctx)
1672 def num_no_patterns(self):
1673 """Return the number of no-patterns.
"""
1674 return Z3_get_quantifier_num_no_patterns(self.ctx_ref(), self.ast)
1676 def no_pattern(self, idx):
1677 """Return a no-pattern.
"""
1679 _z3_assert(idx < self.num_no_patterns(), "Invalid no-pattern idx")
1680 return _to_expr_ref(Z3_get_quantifier_no_pattern_ast(self.ctx_ref(), self.ast, idx), self.ctx)
1683 """Return the expression being quantified.
1687 >>> q =
ForAll(x, f(x) == 0)
1691 return _to_expr_ref(Z3_get_quantifier_body(self.ctx_ref(), self.ast), self.ctx)
1694 """Return the number of variables bounded by this quantifier.
1699 >>> q =
ForAll([x, y], f(x, y) >= x)
1703 return int(Z3_get_quantifier_num_bound(self.ctx_ref(), self.ast))
1705 def var_name(self, idx):
1706 """Return a string representing a name used when displaying the quantifier.
1711 >>> q =
ForAll([x, y], f(x, y) >= x)
1718 _z3_assert(idx < self.num_vars(), "Invalid variable idx")
1719 return _symbol2py(self.ctx, Z3_get_quantifier_bound_name(self.ctx_ref(), self.ast, idx))
1721 def var_sort(self, idx):
1722 """Return the sort of a bound variable.
1727 >>> q =
ForAll([x, y], f(x, y) >= x)
1734 _z3_assert(idx < self.num_vars(), "Invalid variable idx")
1735 return SortRef(Z3_get_quantifier_bound_sort(self.ctx_ref(), self.ast, idx), self.ctx)
1738 """Return a list containing a single element self.
body()
1742 >>> q =
ForAll(x, f(x) == 0)
1746 return [ self.body() ]
1748 def is_quantifier(a):
1749 """Return `
True`
if `a`
is a Z3 quantifier.
1753 >>> q =
ForAll(x, f(x) == 0)
1759 return isinstance(a, QuantifierRef)
1761 def _mk_quantifier(is_forall, vs, body, weight=1, qid="", skid="", patterns=[], no_patterns=[]):
1763 _z3_assert(is_bool(body), "Z3 expression expected")
1764 _z3_assert(is_const(vs) or (len(vs) > 0 and all([ is_const(v) for v in vs])), "Invalid bounded variable(s)")
1765 _z3_assert(all([is_pattern(a) or is_expr(a) for a in patterns]), "Z3 patterns expected")
1766 _z3_assert(all([is_expr(p) for p in no_patterns]), "no patterns are Z3 expressions")
1771 _vs = (Ast * num_vars)()
1772 for i in range(num_vars):
1773 ## TODO: Check if is constant
1774 _vs[i] = vs[i].as_ast()
1775 patterns = [ _to_pattern(p) for p in patterns ]
1776 num_pats = len(patterns)
1777 _pats = (Pattern * num_pats)()
1778 for i in range(num_pats):
1779 _pats[i] = patterns[i].ast
1780 _no_pats, num_no_pats = _to_ast_array(no_patterns)
1781 qid = to_symbol(qid, ctx)
1782 skid = to_symbol(skid, ctx)
1783 return QuantifierRef(Z3_mk_quantifier_const_ex(ctx.ref(), is_forall, weight, qid, skid,
1786 num_no_pats, _no_pats,
1787 body.as_ast()), ctx)
1789 def ForAll(vs, body, weight=1, qid="", skid="", patterns=[], no_patterns=[]):
1790 """Create a Z3 forall formula.
1792 The parameters `weight`, `qif`, `skid`, `patterns`
and `no_patterns` are optional annotations.
1794 See http://rise4fun.com/Z3Py/tutorial/advanced
for more details.
1799 >>>
ForAll([x, y], f(x, y) >= x)
1800 ForAll([x, y], f(x, y) >= x)
1801 >>>
ForAll([x, y], f(x, y) >= x, patterns=[ f(x, y) ])
1802 ForAll([x, y], f(x, y) >= x)
1803 >>>
ForAll([x, y], f(x, y) >= x, weight=10)
1804 ForAll([x, y], f(x, y) >= x)
1806 return _mk_quantifier(True, vs, body, weight, qid, skid, patterns, no_patterns)
1808 def Exists(vs, body, weight=1, qid="", skid="", patterns=[], no_patterns=[]):
1809 """Create a Z3 exists formula.
1811 The parameters `weight`, `qif`, `skid`, `patterns`
and `no_patterns` are optional annotations.
1813 See http://rise4fun.com/Z3Py/tutorial/advanced
for more details.
1818 >>> q =
Exists([x, y], f(x, y) >= x, skid=
"foo")
1820 Exists([x, y], f(x, y) >= x)
1823 >>> r =
Tactic(
'nnf')(q).as_expr()
1827 return _mk_quantifier(False, vs, body, weight, qid, skid, patterns, no_patterns)
1829 #########################################
1833 #########################################
1835 class ArithSortRef(SortRef):
1836 """Real
and Integer sorts.
"""
1839 """Return `
True`
if `self`
is of the sort Real.
1850 return self.kind() == Z3_REAL_SORT
1853 """Return `
True`
if `self`
is of the sort Integer.
1864 return self.kind() == Z3_INT_SORT
1866 def subsort(self, other):
1867 """Return `
True`
if `self`
is a subsort of `other`.
"""
1868 return self.is_int() and is_arith_sort(other) and other.is_real()
1870 def cast(self, val):
1871 """Try to cast `val`
as an Integer
or Real.
1886 _z3_assert(self.ctx == val.ctx, "Context mismatch")
1890 if val_s.is_int() and self.is_real():
1893 _z3_assert(False, "Z3 Integer/Real expression expected" )
1896 return IntVal(val, self.ctx)
1898 return RealVal(val, self.ctx)
1900 _z3_assert(False, "int, long, float, string (numeral), or Z3 Integer/Real expression expected")
1902 def is_arith_sort(s):
1903 """Return `
True`
if s
is an arithmetical sort (type).
1911 >>> n =
Int(
'x') + 1
1915 return isinstance(s, ArithSortRef)
1917 class ArithRef(ExprRef):
1918 """Integer
and Real expressions.
"""
1921 """Return the sort (type) of the arithmetical expression `self`.
1928 return ArithSortRef(Z3_get_sort(self.ctx_ref(), self.as_ast()), self.ctx)
1931 """Return `
True`
if `self`
is an integer expression.
1942 return self.sort().is_int()
1945 """Return `
True`
if `self`
is an real expression.
1953 return self.sort().is_real()
1955 def __add__(self, other):
1956 """Create the Z3 expression `self + other`.
1965 a, b = _coerce_exprs(self, other)
1966 return ArithRef(_mk_bin(Z3_mk_add, a, b), self.ctx)
1968 def __radd__(self, other):
1969 """Create the Z3 expression `other + self`.
1975 a, b = _coerce_exprs(self, other)
1976 return ArithRef(_mk_bin(Z3_mk_add, b, a), self.ctx)
1978 def __mul__(self, other):
1979 """Create the Z3 expression `self * other`.
1988 a, b = _coerce_exprs(self, other)
1989 return ArithRef(_mk_bin(Z3_mk_mul, a, b), self.ctx)
1991 def __rmul__(self, other):
1992 """Create the Z3 expression `other * self`.
1998 a, b = _coerce_exprs(self, other)
1999 return ArithRef(_mk_bin(Z3_mk_mul, b, a), self.ctx)
2001 def __sub__(self, other):
2002 """Create the Z3 expression `self - other`.
2011 a, b = _coerce_exprs(self, other)
2012 return ArithRef(_mk_bin(Z3_mk_sub, a, b), self.ctx)
2014 def __rsub__(self, other):
2015 """Create the Z3 expression `other - self`.
2021 a, b = _coerce_exprs(self, other)
2022 return ArithRef(_mk_bin(Z3_mk_sub, b, a), self.ctx)
2024 def __pow__(self, other):
2025 """Create the Z3 expression `self**other` (**
is the power operator).
2035 a, b = _coerce_exprs(self, other)
2036 return ArithRef(Z3_mk_power(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
2038 def __rpow__(self, other):
2039 """Create the Z3 expression `other**self` (**
is the power operator).
2049 a, b = _coerce_exprs(self, other)
2050 return ArithRef(Z3_mk_power(self.ctx_ref(), b.as_ast(), a.as_ast()), self.ctx)
2052 def __div__(self, other):
2053 """Create the Z3 expression `other/self`.
2072 a, b = _coerce_exprs(self, other)
2073 return ArithRef(Z3_mk_div(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
2075 def __truediv__(self, other):
2076 """Create the Z3 expression `other/self`.
"""
2077 return self.__div__(other)
2079 def __rdiv__(self, other):
2080 """Create the Z3 expression `other/self`.
2093 a, b = _coerce_exprs(self, other)
2094 return ArithRef(Z3_mk_div(self.ctx_ref(), b.as_ast(), a.as_ast()), self.ctx)
2096 def __rtruediv__(self, other):
2097 """Create the Z3 expression `other/self`.
"""
2098 return self.__rdiv__(other)
2100 def __mod__(self, other):
2101 """Create the Z3 expression `other%self`.
2110 a, b = _coerce_exprs(self, other)
2112 _z3_assert(a.is_int(), "Z3 integer expression expected")
2113 return ArithRef(Z3_mk_mod(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
2115 def __rmod__(self, other):
2116 """Create the Z3 expression `other%self`.
2122 a, b = _coerce_exprs(self, other)
2124 _z3_assert(a.is_int(), "Z3 integer expression expected")
2125 return ArithRef(Z3_mk_mod(self.ctx_ref(), b.as_ast(), a.as_ast()), self.ctx)
2128 """Return an expression representing `-self`.
2136 return ArithRef(Z3_mk_unary_minus(self.ctx_ref(), self.as_ast()), self.ctx)
2147 def __le__(self, other):
2148 """Create the Z3 expression `other <= self`.
2150 >>> x, y =
Ints(
'x y')
2157 a, b = _coerce_exprs(self, other)
2158 return BoolRef(Z3_mk_le(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
2160 def __lt__(self, other):
2161 """Create the Z3 expression `other < self`.
2163 >>> x, y =
Ints(
'x y')
2170 a, b = _coerce_exprs(self, other)
2171 return BoolRef(Z3_mk_lt(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
2173 def __gt__(self, other):
2174 """Create the Z3 expression `other > self`.
2176 >>> x, y =
Ints(
'x y')
2183 a, b = _coerce_exprs(self, other)
2184 return BoolRef(Z3_mk_gt(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
2186 def __ge__(self, other):
2187 """Create the Z3 expression `other >= self`.
2189 >>> x, y =
Ints(
'x y')
2196 a, b = _coerce_exprs(self, other)
2197 return BoolRef(Z3_mk_ge(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
2200 """Return `
True`
if `a`
is an arithmetical expression.
2217 return isinstance(a, ArithRef)
2220 """Return `
True`
if `a`
is an integer expression.
2235 return is_arith(a) and a.is_int()
2238 """Return `
True`
if `a`
is a real expression.
2253 return is_arith(a) and a.is_real()
2255 def _is_numeral(ctx, a):
2256 return Z3_is_numeral_ast(ctx.ref(), a)
2258 def _is_algebraic(ctx, a):
2259 return Z3_is_algebraic_number(ctx.ref(), a)
2261 def is_int_value(a):
2262 """Return `
True`
if `a`
is an integer value of sort Int.
2270 >>> n =
Int(
'x') + 1
2282 return is_arith(a) and a.is_int() and _is_numeral(a.ctx, a.as_ast())
2284 def is_rational_value(a):
2285 """Return `
True`
if `a`
is rational value of sort Real.
2295 >>> n =
Real(
'x') + 1
2303 return is_arith(a) and a.is_real() and _is_numeral(a.ctx, a.as_ast())
2305 def is_algebraic_value(a):
2306 """Return `
True`
if `a`
is an algerbraic value of sort Real.
2316 return is_arith(a) and a.is_real() and _is_algebraic(a.ctx, a.as_ast())
2319 """Return `
True`
if `a`
is an expression of the form b + c.
2321 >>> x, y =
Ints(
'x y')
2327 return is_app_of(a, Z3_OP_ADD)
2330 """Return `
True`
if `a`
is an expression of the form b * c.
2332 >>> x, y =
Ints(
'x y')
2338 return is_app_of(a, Z3_OP_MUL)
2341 """Return `
True`
if `a`
is an expression of the form b - c.
2343 >>> x, y =
Ints(
'x y')
2349 return is_app_of(a, Z3_OP_SUB)
2352 """Return `
True`
if `a`
is an expression of the form b / c.
2354 >>> x, y =
Reals(
'x y')
2359 >>> x, y =
Ints(
'x y')
2365 return is_app_of(a, Z3_OP_DIV)
2368 """Return `
True`
if `a`
is an expression of the form b div c.
2370 >>> x, y =
Ints(
'x y')
2376 return is_app_of(a, Z3_OP_IDIV)
2379 """Return `
True`
if `a`
is an expression of the form b % c.
2381 >>> x, y =
Ints(
'x y')
2387 return is_app_of(a, Z3_OP_MOD)
2390 """Return `
True`
if `a`
is an expression of the form b <= c.
2392 >>> x, y =
Ints(
'x y')
2398 return is_app_of(a, Z3_OP_LE)
2401 """Return `
True`
if `a`
is an expression of the form b < c.
2403 >>> x, y =
Ints(
'x y')
2409 return is_app_of(a, Z3_OP_LT)
2412 """Return `
True`
if `a`
is an expression of the form b >= c.
2414 >>> x, y =
Ints(
'x y')
2420 return is_app_of(a, Z3_OP_GE)
2423 """Return `
True`
if `a`
is an expression of the form b > c.
2425 >>> x, y =
Ints(
'x y')
2431 return is_app_of(a, Z3_OP_GT)
2434 """Return `
True`
if `a`
is an expression of the form
IsInt(b).
2442 return is_app_of(a, Z3_OP_IS_INT)
2445 """Return `
True`
if `a`
is an expression of the form
ToReal(b).
2456 return is_app_of(a, Z3_OP_TO_REAL)
2459 """Return `
True`
if `a`
is an expression of the form
ToInt(b).
2470 return is_app_of(a, Z3_OP_TO_INT)
2472 class IntNumRef(ArithRef):
2473 """Integer values.
"""
2476 """Return a Z3 integer numeral
as a Python long (bignum) numeral.
2485 _z3_assert(self.is_int(), "Integer value expected")
2486 return int(self.as_string())
2488 def as_string(self):
2489 """Return a Z3 integer numeral
as a Python string.
2494 return Z3_get_numeral_string(self.ctx_ref(), self.as_ast())
2496 class RatNumRef(ArithRef):
2497 """Rational values.
"""
2499 def numerator(self):
2500 """ Return the numerator of a Z3 rational numeral.
2512 return IntNumRef(Z3_get_numerator(self.ctx_ref(), self.as_ast()), self.ctx)
2514 def denominator(self):
2515 """ Return the denominator of a Z3 rational numeral.
2523 return IntNumRef(Z3_get_denominator(self.ctx_ref(), self.as_ast()), self.ctx)
2525 def numerator_as_long(self):
2526 """ Return the numerator
as a Python long.
2533 >>> v.numerator_as_long() + 1 == 10000000001
2536 return self.numerator().as_long()
2538 def denominator_as_long(self):
2539 """ Return the denominator
as a Python long.
2544 >>> v.denominator_as_long()
2547 return self.denominator().as_long()
2549 def as_decimal(self, prec):
2550 """ Return a Z3 rational value
as a string
in decimal notation using at most `prec` decimal places.
2559 return Z3_get_numeral_decimal_string(self.ctx_ref(), self.as_ast(), prec)
2561 def as_string(self):
2562 """Return a Z3 rational numeral
as a Python string.
2568 return Z3_get_numeral_string(self.ctx_ref(), self.as_ast())
2570 def as_fraction(self):
2571 """Return a Z3 rational
as a Python Fraction object.
2577 return Fraction(self.numerator_as_long(), self.denominator_as_long())
2579 class AlgebraicNumRef(ArithRef):
2580 """Algebraic irrational values.
"""
2582 def approx(self, precision=10):
2583 """Return a Z3 rational number that approximates the algebraic number `self`.
2584 The result `r`
is such that |r - self| <= 1/10^precision
2588 6838717160008073720548335/4835703278458516698824704
2592 return RatNumRef(Z3_get_algebraic_number_upper(self.ctx_ref(), self.as_ast(), precision), self.ctx)
2593 def as_decimal(self, prec):
2594 """Return a string representation of the algebraic number `self`
in decimal notation using `prec` decimal places
2597 >>> x.as_decimal(10)
2599 >>> x.as_decimal(20)
2600 '1.41421356237309504880?'
2602 return Z3_get_numeral_decimal_string(self.ctx_ref(), self.as_ast(), prec)
2604 def _py2expr(a, ctx=None):
2605 if isinstance(a, bool):
2606 return BoolVal(a, ctx)
2608 return IntVal(a, ctx)
2609 if isinstance(a, float):
2610 return RealVal(a, ctx)
2612 _z3_assert(False, "Python bool, int, long or float expected")
2614 def IntSort(ctx=None):
2615 """Return the interger sort
in the given context. If `ctx=
None`, then the
global context
is used.
2628 return ArithSortRef(Z3_mk_int_sort(ctx.ref()), ctx)
2630 def RealSort(ctx=None):
2631 """Return the real sort
in the given context. If `ctx=
None`, then the
global context
is used.
2644 return ArithSortRef(Z3_mk_real_sort(ctx.ref()), ctx)
2646 def _to_int_str(val):
2647 if isinstance(val, float):
2648 return str(int(val))
2649 elif isinstance(val, bool):
2656 elif isinstance(val, str):
2659 _z3_assert(False, "Python value cannot be used as a Z3 integer")
2661 def IntVal(val, ctx=None):
2662 """Return a Z3 integer value. If `ctx=
None`, then the
global context
is used.
2670 return IntNumRef(Z3_mk_numeral(ctx.ref(), _to_int_str(val), IntSort(ctx).ast), ctx)
2672 def RealVal(val, ctx=None):
2673 """Return a Z3 real value.
2675 `val` may be a Python int, long, float
or string representing a number
in decimal
or rational notation.
2676 If `ctx=
None`, then the
global context
is used.
2688 return RatNumRef(Z3_mk_numeral(ctx.ref(), str(val), RealSort(ctx).ast), ctx)
2690 def RatVal(a, b, ctx=None):
2691 """Return a Z3 rational a/b.
2693 If `ctx=
None`, then the
global context
is used.
2701 _z3_assert(_is_int(a) or isinstance(a, str), "First argument cannot be converted into an integer")
2702 _z3_assert(_is_int(b) or isinstance(b, str), "Second argument cannot be converted into an integer")
2703 return simplify(RealVal(a, ctx)/RealVal(b, ctx))
2705 def Q(a, b, ctx=None):
2706 """Return a Z3 rational a/b.
2708 If `ctx=
None`, then the
global context
is used.
2715 return simplify(RatVal(a, b))
2717 def Int(name, ctx=None):
2718 """Return an integer constant named `name`. If `ctx=
None`, then the
global context
is used.
2727 return ArithRef(Z3_mk_const(ctx.ref(), to_symbol(name, ctx), IntSort(ctx).ast), ctx)
2729 def Ints(names, ctx=None):
2730 """Return a tuple of Integer constants.
2732 >>> x, y, z =
Ints(
'x y z')
2737 if isinstance(names, str):
2738 names = names.split(" ")
2739 return [Int(name, ctx) for name in names]
2741 def IntVector(prefix, sz, ctx=None):
2742 """Return a list of integer constants of size `sz`.
2750 return [ Int('%s__%s' % (prefix, i)) for i in range(sz) ]
2752 def FreshInt(prefix='x', ctx=None):
2753 """Return a fresh integer constant
in the given context using the given prefix.
2763 return ArithRef(Z3_mk_fresh_const(ctx.ref(), prefix, IntSort(ctx).ast), ctx)
2765 def Real(name, ctx=None):
2766 """Return a real constant named `name`. If `ctx=
None`, then the
global context
is used.
2775 return ArithRef(Z3_mk_const(ctx.ref(), to_symbol(name, ctx), RealSort(ctx).ast), ctx)
2777 def Reals(names, ctx=None):
2778 """Return a tuple of real constants.
2780 >>> x, y, z =
Reals(
'x y z')
2783 >>>
Sum(x, y, z).sort()
2787 if isinstance(names, str):
2788 names = names.split(" ")
2789 return [Real(name, ctx) for name in names]
2791 def RealVector(prefix, sz, ctx=None):
2792 """Return a list of real constants of size `sz`.
2802 return [ Real('%s__%s' % (prefix, i)) for i in range(sz) ]
2804 def FreshReal(prefix='b', ctx=None):
2805 """Return a fresh real constant
in the given context using the given prefix.
2815 return ArithRef(Z3_mk_fresh_const(ctx.ref(), prefix, RealSort(ctx).ast), ctx)
2818 """ Return the Z3 expression
ToReal(a).
2830 _z3_assert(a.is_int(), "Z3 integer expression expected.")
2832 return ArithRef(Z3_mk_int2real(ctx.ref(), a.as_ast()), ctx)
2835 """ Return the Z3 expression
ToInt(a).
2847 _z3_assert(a.is_real(), "Z3 real expression expected.")
2849 return ArithRef(Z3_mk_real2int(ctx.ref(), a.as_ast()), ctx)
2852 """ Return the Z3 predicate
IsInt(a).
2855 >>>
IsInt(x +
"1/2")
2859 >>>
solve(
IsInt(x +
"1/2"), x > 0, x < 1, x !=
"1/2")
2863 _z3_assert(a.is_real(), "Z3 real expression expected.")
2865 return BoolRef(Z3_mk_is_int(ctx.ref(), a.as_ast()), ctx)
2867 def Sqrt(a, ctx=None):
2868 """ Return a Z3 expression which represents the square root of a.
2879 def Cbrt(a, ctx=None):
2880 """ Return a Z3 expression which represents the cubic root of a.
2891 #########################################
2895 #########################################
2897 class BitVecSortRef(SortRef):
2898 """Bit-vector sort.
"""
2901 """Return the size (number of bits) of the bit-vector sort `self`.
2907 return int(Z3_get_bv_sort_size(self.ctx_ref(), self.ast))
2909 def subsort(self, other):
2910 return is_bv_sort(other) and self.size() < other.size()
2912 def cast(self, val):
2913 """Try to cast `val`
as a Bit-Vector.
2918 >>> b.cast(10).
sexpr()
2923 _z3_assert(self.ctx == val.ctx, "Context mismatch")
2924 # Idea: use sign_extend if sort of val is a bitvector of smaller size
2927 return BitVecVal(val, self)
2930 """Return
True if `s`
is a Z3 bit-vector sort.
2937 return isinstance(s, BitVecSortRef)
2939 class BitVecRef(ExprRef):
2940 """Bit-vector expressions.
"""
2943 """Return the sort of the bit-vector expression `self`.
2951 return BitVecSortRef(Z3_get_sort(self.ctx_ref(), self.as_ast()), self.ctx)
2954 """Return the number of bits of the bit-vector expression `self`.
2962 return self.sort().size()
2964 def __add__(self, other):
2965 """Create the Z3 expression `self + other`.
2974 a, b = _coerce_exprs(self, other)
2975 return BitVecRef(Z3_mk_bvadd(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
2977 def __radd__(self, other):
2978 """Create the Z3 expression `other + self`.
2984 a, b = _coerce_exprs(self, other)
2985 return BitVecRef(Z3_mk_bvadd(self.ctx_ref(), b.as_ast(), a.as_ast()), self.ctx)
2987 def __mul__(self, other):
2988 """Create the Z3 expression `self * other`.
2997 a, b = _coerce_exprs(self, other)
2998 return BitVecRef(Z3_mk_bvmul(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
3000 def __rmul__(self, other):
3001 """Create the Z3 expression `other * self`.
3007 a, b = _coerce_exprs(self, other)
3008 return BitVecRef(Z3_mk_bvmul(self.ctx_ref(), b.as_ast(), a.as_ast()), self.ctx)
3010 def __sub__(self, other):
3011 """Create the Z3 expression `self - other`.
3020 a, b = _coerce_exprs(self, other)
3021 return BitVecRef(Z3_mk_bvsub(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
3023 def __rsub__(self, other):
3024 """Create the Z3 expression `other - self`.
3030 a, b = _coerce_exprs(self, other)
3031 return BitVecRef(Z3_mk_bvsub(self.ctx_ref(), b.as_ast(), a.as_ast()), self.ctx)
3033 def __or__(self, other):
3034 """Create the Z3 expression bitwise-
or `self | other`.
3043 a, b = _coerce_exprs(self, other)
3044 return BitVecRef(Z3_mk_bvor(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
3046 def __ror__(self, other):
3047 """Create the Z3 expression bitwise-
or `other | self`.
3053 a, b = _coerce_exprs(self, other)
3054 return BitVecRef(Z3_mk_bvor(self.ctx_ref(), b.as_ast(), a.as_ast()), self.ctx)
3056 def __and__(self, other):
3057 """Create the Z3 expression bitwise-
and `self & other`.
3066 a, b = _coerce_exprs(self, other)
3067 return BitVecRef(Z3_mk_bvand(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
3069 def __rand__(self, other):
3070 """Create the Z3 expression bitwise-
or `other & self`.
3076 a, b = _coerce_exprs(self, other)
3077 return BitVecRef(Z3_mk_bvand(self.ctx_ref(), b.as_ast(), a.as_ast()), self.ctx)
3079 def __xor__(self, other):
3080 """Create the Z3 expression bitwise-xor `self ^ other`.
3089 a, b = _coerce_exprs(self, other)
3090 return BitVecRef(Z3_mk_bvxor(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
3092 def __rxor__(self, other):
3093 """Create the Z3 expression bitwise-xor `other ^ self`.
3099 a, b = _coerce_exprs(self, other)
3100 return BitVecRef(Z3_mk_bvxor(self.ctx_ref(), b.as_ast(), a.as_ast()), self.ctx)
3112 """Return an expression representing `-self`.
3120 return BitVecRef(Z3_mk_bvneg(self.ctx_ref(), self.as_ast()), self.ctx)
3122 def __invert__(self):
3123 """Create the Z3 expression bitwise-
not `~self`.
3131 return BitVecRef(Z3_mk_bvnot(self.ctx_ref(), self.as_ast()), self.ctx)
3133 def __div__(self, other):
3134 """Create the Z3 expression (signed) division `self / other`.
3136 Use the function
UDiv()
for unsigned division.
3149 a, b = _coerce_exprs(self, other)
3150 return BitVecRef(Z3_mk_bvsdiv(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
3152 def __truediv__(self, other):
3153 """Create the Z3 expression (signed) division `self / other`.
"""
3154 return self.__div__(other)
3156 def __rdiv__(self, other):
3157 """Create the Z3 expression (signed) division `other / self`.
3159 Use the function
UDiv()
for unsigned division.
3164 >>> (10 / x).
sexpr()
3165 '(bvsdiv #x0000000a x)'
3167 '(bvudiv #x0000000a x)'
3169 a, b = _coerce_exprs(self, other)
3170 return BitVecRef(Z3_mk_bvsdiv(self.ctx_ref(), b.as_ast(), a.as_ast()), self.ctx)
3172 def __rtruediv__(self, other):
3173 """Create the Z3 expression (signed) division `other / self`.
"""
3174 return self.__rdiv__(other)
3176 def __mod__(self, other):
3177 """Create the Z3 expression (signed) mod `self % other`.
3179 Use the function
URem()
for unsigned remainder,
and SRem()
for signed remainder.
3194 a, b = _coerce_exprs(self, other)
3195 return BitVecRef(Z3_mk_bvsmod(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
3197 def __rmod__(self, other):
3198 """Create the Z3 expression (signed) mod `other % self`.
3200 Use the function
URem()
for unsigned remainder,
and SRem()
for signed remainder.
3205 >>> (10 % x).
sexpr()
3206 '(bvsmod #x0000000a x)'
3208 '(bvurem #x0000000a x)'
3210 '(bvsrem #x0000000a x)'
3212 a, b = _coerce_exprs(self, other)
3213 return BitVecRef(Z3_mk_bvsmod(self.ctx_ref(), b.as_ast(), a.as_ast()), self.ctx)
3215 def __le__(self, other):
3216 """Create the Z3 expression (signed) `other <= self`.
3218 Use the function
ULE()
for unsigned less than
or equal to.
3223 >>> (x <= y).
sexpr()
3228 a, b = _coerce_exprs(self, other)
3229 return BoolRef(Z3_mk_bvsle(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
3231 def __lt__(self, other):
3232 """Create the Z3 expression (signed) `other < self`.
3234 Use the function
ULT()
for unsigned less than.
3244 a, b = _coerce_exprs(self, other)
3245 return BoolRef(Z3_mk_bvslt(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
3247 def __gt__(self, other):
3248 """Create the Z3 expression (signed) `other > self`.
3250 Use the function
UGT()
for unsigned greater than.
3260 a, b = _coerce_exprs(self, other)
3261 return BoolRef(Z3_mk_bvsgt(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
3263 def __ge__(self, other):
3264 """Create the Z3 expression (signed) `other >= self`.
3266 Use the function
UGE()
for unsigned greater than
or equal to.
3271 >>> (x >= y).
sexpr()
3276 a, b = _coerce_exprs(self, other)
3277 return BoolRef(Z3_mk_bvsge(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
3279 def __rshift__(self, other):
3280 """Create the Z3 expression (arithmetical) right shift `self >> other`
3282 Use the function
LShR()
for the right logical shift
3287 >>> (x >> y).
sexpr()
3306 a, b = _coerce_exprs(self, other)
3307 return BitVecRef(Z3_mk_bvashr(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
3309 def __lshift__(self, other):
3310 """Create the Z3 expression left shift `self << other`
3315 >>> (x << y).
sexpr()
3320 a, b = _coerce_exprs(self, other)
3321 return BitVecRef(Z3_mk_bvshl(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
3323 def __rrshift__(self, other):
3324 """Create the Z3 expression (arithmetical) right shift `other` >> `self`.
3326 Use the function
LShR()
for the right logical shift
3331 >>> (10 >> x).
sexpr()
3332 '(bvashr #x0000000a x)'
3334 a, b = _coerce_exprs(self, other)
3335 return BitVecRef(Z3_mk_bvashr(self.ctx_ref(), b.as_ast(), a.as_ast()), self.ctx)
3337 def __rlshift__(self, other):
3338 """Create the Z3 expression left shift `other << self`.
3340 Use the function
LShR()
for the right logical shift
3345 >>> (10 << x).
sexpr()
3346 '(bvshl #x0000000a x)'
3348 a, b = _coerce_exprs(self, other)
3349 return BitVecRef(Z3_mk_bvshl(self.ctx_ref(), b.as_ast(), a.as_ast()), self.ctx)
3351 class BitVecNumRef(BitVecRef):
3352 """Bit-vector values.
"""
3355 """Return a Z3 bit-vector numeral
as a Python long (bignum) numeral.
3360 >>> print(
"0x%.8x" % v.as_long())
3363 return int(self.as_string())
3365 def as_signed_long(self):
3366 """Return a Z3 bit-vector numeral
as a Python long (bignum) numeral. The most significant bit
is assumed to be the sign.
3380 val = self.as_long()
3381 if val >= 2**(sz - 1):
3383 if val < -2**(sz - 1):
3387 def as_string(self):
3388 return Z3_get_numeral_string(self.ctx_ref(), self.as_ast())
3391 """Return `
True`
if `a`
is a Z3 bit-vector expression.
3401 return isinstance(a, BitVecRef)
3404 """Return `
True`
if `a`
is a Z3 bit-vector numeral value.
3415 return is_bv(a) and _is_numeral(a.ctx, a.as_ast())
3418 """Return the Z3 expression
BV2Int(a).
3430 _z3_assert(is_bv(a), "Z3 bit-vector expression expected")
3432 ## investigate problem with bv2int
3433 return ArithRef(Z3_mk_bv2int(ctx.ref(), a.as_ast(), 0), ctx)
3435 def BitVecSort(sz, ctx=None):
3436 """Return a Z3 bit-vector sort of the given size. If `ctx=
None`, then the
global context
is used.
3442 >>> x =
Const(
'x', Byte)
3447 return BitVecSortRef(Z3_mk_bv_sort(ctx.ref(), sz), ctx)
3449 def BitVecVal(val, bv, ctx=None):
3450 """Return a bit-vector value with the given number of bits. If `ctx=
None`, then the
global context
is used.
3455 >>> print(
"0x%.8x" % v.as_long())
3460 return BitVecNumRef(Z3_mk_numeral(ctx.ref(), _to_int_str(val), bv.ast), ctx)
3463 return BitVecNumRef(Z3_mk_numeral(ctx.ref(), _to_int_str(val), BitVecSort(bv, ctx).ast), ctx)
3465 def BitVec(name, bv, ctx=None):
3466 """Return a bit-vector constant named `name`. `bv` may be the number of bits of a bit-vector sort.
3467 If `ctx=
None`, then the
global context
is used.
3477 >>> x2 =
BitVec(
'x', word)
3481 if isinstance(bv, BitVecSortRef):
3485 bv = BitVecSort(bv, ctx)
3486 return BitVecRef(Z3_mk_const(ctx.ref(), to_symbol(name, ctx), bv.ast), ctx)
3488 def BitVecs(names, bv, ctx=None):
3489 """Return a tuple of bit-vector constants of size bv.
3491 >>> x, y, z =
BitVecs(
'x y z', 16)
3504 if isinstance(names, str):
3505 names = names.split(" ")
3506 return [BitVec(name, bv, ctx) for name in names]
3509 """Create a Z3 bit-vector concatenation expression.
3519 args = _get_args(args)
3521 _z3_assert(all([is_bv(a) for a in args]), "All arguments must be Z3 bit-vector expressions.")
3522 _z3_assert(len(args) >= 2, "At least two arguments expected.")
3525 for i in range(len(args) - 1):
3526 r = BitVecRef(Z3_mk_concat(ctx.ref(), r.as_ast(), args[i+1].as_ast()), ctx)
3529 def Extract(high, low, a):
3530 """Create a Z3 bit-vector extraction expression.
3539 _z3_assert(low <= high, "First argument must be greater than or equal to second argument")
3540 _z3_assert(isinstance(high, int) and high >= 0 and isinstance(low, int) and low >= 0, "First and second arguments must be non negative integers")
3541 _z3_assert(is_bv(a), "Third argument must be a Z3 Bitvector expression")
3542 return BitVecRef(Z3_mk_extract(a.ctx_ref(), high, low, a.as_ast()), a.ctx)
3544 def _check_bv_args(a, b):
3546 _z3_assert(is_bv(a) or is_bv(b), "At least one of the arguments must be a Z3 bit-vector expression")
3549 """Create the Z3 expression (unsigned) `other <= self`.
3551 Use the operator <=
for signed less than
or equal to.
3556 >>> (x <= y).sexpr()
3558 >>>
ULE(x, y).sexpr()
3561 _check_bv_args(a, b)
3562 a, b = _coerce_exprs(a, b)
3563 return BoolRef(Z3_mk_bvule(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)
3566 """Create the Z3 expression (unsigned) `other < self`.
3568 Use the operator <
for signed less than.
3575 >>>
ULT(x, y).sexpr()
3578 _check_bv_args(a, b)
3579 a, b = _coerce_exprs(a, b)
3580 return BoolRef(Z3_mk_bvult(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)
3583 """Create the Z3 expression (unsigned) `other >= self`.
3585 Use the operator >=
for signed greater than
or equal to.
3590 >>> (x >= y).sexpr()
3592 >>>
UGE(x, y).sexpr()
3595 _check_bv_args(a, b)
3596 a, b = _coerce_exprs(a, b)
3597 return BoolRef(Z3_mk_bvuge(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)
3600 """Create the Z3 expression (unsigned) `other > self`.
3602 Use the operator >
for signed greater than.
3609 >>>
UGT(x, y).sexpr()
3612 _check_bv_args(a, b)
3613 a, b = _coerce_exprs(a, b)
3614 return BoolRef(Z3_mk_bvugt(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)
3617 """Create the Z3 expression (unsigned) division `self / other`.
3619 Use the operator /
for signed division.
3625 >>>
UDiv(x, y).sort()
3629 >>>
UDiv(x, y).sexpr()
3632 _check_bv_args(a, b)
3633 a, b = _coerce_exprs(a, b)
3634 return BitVecRef(Z3_mk_bvudiv(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)
3637 """Create the Z3 expression (unsigned) remainder `self % other`.
3639 Use the operator %
for signed modulus,
and SRem()
for signed remainder.
3645 >>>
URem(x, y).sort()
3649 >>>
URem(x, y).sexpr()
3652 _check_bv_args(a, b)
3653 a, b = _coerce_exprs(a, b)
3654 return BitVecRef(Z3_mk_bvurem(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)
3657 """Create the Z3 expression signed remainder.
3659 Use the operator %
for signed modulus,
and URem()
for unsigned remainder.
3665 >>>
SRem(x, y).sort()
3669 >>>
SRem(x, y).sexpr()
3672 _check_bv_args(a, b)
3673 a, b = _coerce_exprs(a, b)
3674 return BitVecRef(Z3_mk_bvsrem(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)
3677 """Create the Z3 expression logical right shift.
3679 Use the operator >>
for the arithmetical right shift.
3684 >>> (x >> y).sexpr()
3686 >>>
LShR(x, y).sexpr()
3703 _check_bv_args(a, b)
3704 a, b = _coerce_exprs(a, b)
3705 return BitVecRef(Z3_mk_bvlshr(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)
3707 def RotateLeft(a, b):
3708 """Return an expression representing `a` rotated to the left `b` times.
3718 _check_bv_args(a, b)
3719 a, b = _coerce_exprs(a, b)
3720 return BitVecRef(Z3_mk_ext_rotate_left(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)
3722 def RotateRight(a, b):
3723 """Return an expression representing `a` rotated to the right `b` times.
3733 _check_bv_args(a, b)
3734 a, b = _coerce_exprs(a, b)
3735 return BitVecRef(Z3_mk_ext_rotate_right(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)
3738 """Return a bit-vector expression with `n` extra sign-bits.
3758 >>> print(
"%.x" % v.as_long())
3762 _z3_assert(isinstance(n, int), "First argument must be an integer")
3763 _z3_assert(is_bv(a), "Second argument must be a Z3 Bitvector expression")
3764 return BitVecRef(Z3_mk_sign_ext(a.ctx_ref(), n, a.as_ast()), a.ctx)
3767 """Return a bit-vector expression with `n` extra zero-bits.
3789 _z3_assert(isinstance(n, int), "First argument must be an integer")
3790 _z3_assert(is_bv(a), "Second argument must be a Z3 Bitvector expression")
3791 return BitVecRef(Z3_mk_zero_ext(a.ctx_ref(), n, a.as_ast()), a.ctx)
3793 def RepeatBitVec(n, a):
3794 """Return an expression representing `n` copies of `a`.
3803 >>> print(
"%.x" % v0.as_long())
3808 >>> print(
"%.x" % v.as_long())
3812 _z3_assert(isinstance(n, int), "First argument must be an integer")
3813 _z3_assert(is_bv(a), "Second argument must be a Z3 Bitvector expression")
3814 return BitVecRef(Z3_mk_repeat(a.ctx_ref(), n, a.as_ast()), a.ctx)
3816 #########################################
3820 #########################################
3822 class ArraySortRef(SortRef):
3826 """Return the domain of the array sort `self`.
3832 return _to_sort_ref(Z3_get_array_sort_domain(self.ctx_ref(), self.ast), self.ctx)
3835 """Return the range of the array sort `self`.
3841 return _to_sort_ref(Z3_get_array_sort_range(self.ctx_ref(), self.ast), self.ctx)
3843 class ArrayRef(ExprRef):
3844 """Array expressions.
"""
3847 """Return the array sort of the array expression `self`.
3853 return ArraySortRef(Z3_get_sort(self.ctx_ref(), self.as_ast()), self.ctx)
3862 return self.sort().domain()
3871 return self.sort().range()
3873 def __getitem__(self, arg):
3874 """Return the Z3 expression `self[arg]`.
3883 arg = self.domain().cast(arg)
3884 return _to_expr_ref(Z3_mk_select(self.ctx_ref(), self.as_ast(), arg.as_ast()), self.ctx)
3887 """Return `
True`
if `a`
is a Z3 array expression.
3897 return isinstance(a, ArrayRef)
3899 def is_const_array(a):
3900 """Return `
True`
if `a`
is a Z3 constant array.
3909 return is_app_of(a, Z3_OP_CONST_ARRAY)
3912 """Return `
True`
if `a`
is a Z3 constant array.
3921 return is_app_of(a, Z3_OP_CONST_ARRAY)
3924 """Return `
True`
if `a`
is a Z3 map array expression.
3936 return is_app_of(a, Z3_OP_ARRAY_MAP)
3938 def get_map_func(a):
3939 """Return the function declaration associated with a Z3 map array expression.
3952 _z3_assert(is_map(a), "Z3 array map expression expected.")
3953 return FuncDeclRef(Z3_to_func_decl(a.ctx_ref(), Z3_get_decl_ast_parameter(a.ctx_ref(), a.decl().ast, 0)), a.ctx)
3955 def ArraySort(d, r):
3956 """Return the Z3 array sort with the given domain
and range sorts.
3970 _z3_assert(is_sort(d), "Z3 sort expected")
3971 _z3_assert(is_sort(r), "Z3 sort expected")
3972 _z3_assert(d.ctx == r.ctx, "Context mismatch")
3974 return ArraySortRef(Z3_mk_array_sort(ctx.ref(), d.ast, r.ast), ctx)
3976 def Array(name, dom, rng):
3977 """Return an array constant named `name` with the given domain
and range sorts.
3985 s = ArraySort(dom, rng)
3987 return ArrayRef(Z3_mk_const(ctx.ref(), to_symbol(name, ctx), s.ast), ctx)
3989 def Update(a, i, v):
3990 """Return a Z3 store array expression.
3993 >>> i, v =
Ints(
'i v')
3997 >>>
prove(s[i] == v)
4004 _z3_assert(is_array(a), "First argument must be a Z3 array expression")
4005 i = a.domain().cast(i)
4006 v = a.range().cast(v)
4008 return _to_expr_ref(Z3_mk_store(ctx.ref(), a.as_ast(), i.as_ast(), v.as_ast()), ctx)
4011 """Return a Z3 store array expression.
4014 >>> i, v =
Ints(
'i v')
4015 >>> s =
Store(a, i, v)
4018 >>>
prove(s[i] == v)
4024 return Update(a, i, v)
4027 """Return a Z3 select array expression.
4037 _z3_assert(is_array(a), "First argument must be a Z3 array expression")
4041 """Return a Z3 map array expression.
4046 >>> b =
Map(f, a1, a2)
4049 >>>
prove(b[0] == f(a1[0], a2[0]))
4052 args = _get_args(args)
4054 _z3_assert(len(args) > 0, "At least one Z3 array expression expected")
4055 _z3_assert(is_func_decl(f), "First argument must be a Z3 function declaration")
4056 _z3_assert(all([is_array(a) for a in args]), "Z3 array expected expected")
4057 _z3_assert(len(args) == f.arity(), "Number of arguments mismatch")
4058 _args, sz = _to_ast_array(args)
4060 return ArrayRef(Z3_mk_map(ctx.ref(), f.ast, sz, _args), ctx)
4063 """Return a Z3 constant array expression.
4077 _z3_assert(is_sort(dom), "Z3 sort expected")
4080 v = _py2expr(v, ctx)
4081 return ArrayRef(Z3_mk_const_array(ctx.ref(), dom.ast, v.as_ast()), ctx)
4084 """Return `
True`
if `a`
is a Z3 array select application.
4093 return is_app_of(a, Z3_OP_SELECT)
4096 """Return `
True`
if `a`
is a Z3 array store application.
4104 return is_app_of(a, Z3_OP_STORE)
4106 #########################################
4110 #########################################
4112 def _valid_accessor(acc):
4113 """Return `
True`
if acc
is pair of the form (String, Datatype
or Sort).
"""
4114 return isinstance(acc, tuple) and len(acc) == 2 and isinstance(acc[0], str) and (isinstance(acc[1], Datatype) or is_sort(acc[1]))
4117 """Helper
class for declaring Z3 datatypes.
4120 >>> List.declare(
'cons', (
'car',
IntSort()), (
'cdr', List))
4121 >>> List.declare(
'nil')
4122 >>> List = List.create()
4126 >>> List.cons(10, List.nil)
4128 >>> List.cons(10, List.nil).sort()
4130 >>> cons = List.cons
4134 >>> n = cons(1, cons(0, nil))
4136 cons(1, cons(0, nil))
4142 def __init__(self, name, ctx=None):
4143 self.ctx = _get_ctx(ctx)
4145 self.constructors = []
4147 def declare_core(self, name, rec_name, *args):
4149 _z3_assert(isinstance(name, str), "String expected")
4150 _z3_assert(isinstance(rec_name, str), "String expected")
4151 _z3_assert(all([_valid_accessor(a) for a in args]), "Valid list of accessors expected. An accessor is a pair of the form (String, Datatype|Sort)")
4152 self.constructors.append((name, rec_name, args))
4154 def declare(self, name, *args):
4155 """Declare constructor named `name` with the given accessors `args`.
4156 Each accessor
is a pair `(name, sort)`, where `name`
is a string
and `sort` a Z3 sort
or a reference to the datatypes being declared.
4158 In the followin example `List.declare(
'cons', (
'car',
IntSort()), (
'cdr', List))`
4159 declares the constructor named `cons` that builds a new List using an integer
and a List.
4160 It also declares the accessors `car`
and `cdr`. The accessor `car` extracts the integer of a `cons` cell,
4161 and `cdr` the list of a `cons` cell. After all constructors were declared, we use the method
create() to create
4162 the actual datatype
in Z3.
4165 >>> List.declare(
'cons', (
'car',
IntSort()), (
'cdr', List))
4166 >>> List.declare(
'nil')
4167 >>> List = List.create()
4170 _z3_assert(isinstance(name, str), "String expected")
4171 _z3_assert(name != "", "Constructor name cannot be empty")
4172 return self.declare_core(name, "is_" + name, *args)
4175 return "Datatype(%s, %s)" % (self.name, self.constructors)
4178 """Create a Z3 datatype based on the constructors declared using the mehtod `
declare()`.
4180 The function `
CreateDatatypes()` must be used to define mutually recursive datatypes.
4183 >>> List.declare(
'cons', (
'car',
IntSort()), (
'cdr', List))
4184 >>> List.declare(
'nil')
4185 >>> List = List.create()
4188 >>> List.cons(10, List.nil)
4191 return CreateDatatypes([self])[0]
4193 class ScopedConstructor:
4194 """Auxiliary object used to create Z3 datatypes.
"""
4195 def __init__(self, c, ctx):
4199 Z3_del_constructor(self.ctx.ref(), self.c)
4201 class ScopedConstructorList:
4202 """Auxiliary object used to create Z3 datatypes.
"""
4203 def __init__(self, c, ctx):
4207 Z3_del_constructor_list(self.ctx.ref(), self.c)
4209 def CreateDatatypes(*ds):
4210 """Create mutually recursive Z3 datatypes using 1
or more Datatype helper objects.
4212 In the following example we define a Tree-List using two mutually recursive datatypes.
4214 >>> TreeList =
Datatype(
'TreeList')
4217 >>> Tree.declare(
'leaf', (
'val',
IntSort()))
4219 >>> Tree.declare(
'node', (
'children', TreeList))
4220 >>> TreeList.declare(
'nil')
4221 >>> TreeList.declare(
'cons', (
'car', Tree), (
'cdr', TreeList))
4223 >>> Tree.val(Tree.leaf(10))
4225 >>>
simplify(Tree.val(Tree.leaf(10)))
4227 >>> n1 = Tree.node(TreeList.cons(Tree.leaf(10), TreeList.cons(Tree.leaf(20), TreeList.nil)))
4229 node(cons(leaf(10), cons(leaf(20), nil)))
4230 >>> n2 = Tree.node(TreeList.cons(n1, TreeList.nil))
4233 >>>
simplify(TreeList.car(Tree.children(n2)) == n1)
4238 _z3_assert(len(ds) > 0, "At least one Datatype must be specified")
4239 _z3_assert(all([isinstance(d, Datatype) for d in ds]), "Arguments must be Datatypes")
4240 _z3_assert(all([d.ctx == ds[0].ctx for d in ds]), "Context mismatch")
4241 _z3_assert(all([d.constructors != [] for d in ds]), "Non-empty Datatypes expected")
4244 names = (Symbol * num)()
4245 out = (Sort * num)()
4246 clists = (ConstructorList * num)()
4248 for i in range(num):
4250 names[i] = to_symbol(d.name, ctx)
4251 num_cs = len(d.constructors)
4252 cs = (Constructor * num_cs)()
4253 for j in range(num_cs):
4254 c = d.constructors[j]
4255 cname = to_symbol(c[0], ctx)
4256 rname = to_symbol(c[1], ctx)
4259 fnames = (Symbol * num_fs)()
4260 sorts = (Sort * num_fs)()
4261 refs = (ctypes.c_uint * num_fs)()
4262 for k in range(num_fs):
4265 fnames[k] = to_symbol(fname, ctx)
4266 if isinstance(ftype, Datatype):
4268 _z3_assert(ds.count(ftype) == 1, "One and only one occurrence of each datatype is expected")
4270 refs[k] = ds.index(ftype)
4273 _z3_assert(is_sort(ftype), "Z3 sort expected")
4274 sorts[k] = ftype.ast
4276 cs[j] = Z3_mk_constructor(ctx.ref(), cname, rname, num_fs, fnames, sorts, refs)
4277 to_delete.append(ScopedConstructor(cs[j], ctx))
4278 clists[i] = Z3_mk_constructor_list(ctx.ref(), num_cs, cs)
4279 to_delete.append(ScopedConstructorList(clists[i], ctx))
4280 Z3_mk_datatypes(ctx.ref(), num, names, out, clists)
4282 ## Create a field for every constructor, recognizer and accessor
4283 for i in range(num):
4284 dref = DatatypeSortRef(out[i], ctx)
4285 num_cs = dref.num_constructors()
4286 for j in range(num_cs):
4287 cref = dref.constructor(j)
4288 cref_name = cref.name()
4289 cref_arity = cref.arity()
4290 if cref.arity() == 0:
4292 setattr(dref, cref_name, cref)
4293 rref = dref.recognizer(j)
4294 setattr(dref, rref.name(), rref)
4295 for k in range(cref_arity):
4296 aref = dref.accessor(j, k)
4297 setattr(dref, aref.name(), aref)
4299 return tuple(result)
4301 class DatatypeSortRef(SortRef):
4302 """Datatype sorts.
"""
4303 def num_constructors(self):
4304 """Return the number of constructors
in the given Z3 datatype.
4307 >>> List.declare(
'cons', (
'car',
IntSort()), (
'cdr', List))
4308 >>> List.declare(
'nil')
4309 >>> List = List.create()
4311 >>> List.num_constructors()
4314 return int(Z3_get_datatype_sort_num_constructors(self.ctx_ref(), self.ast))
4316 def constructor(self, idx):
4317 """Return a constructor of the datatype `self`.
4320 >>> List.declare(
'cons', (
'car',
IntSort()), (
'cdr', List))
4321 >>> List.declare(
'nil')
4322 >>> List = List.create()
4324 >>> List.num_constructors()
4326 >>> List.constructor(0)
4328 >>> List.constructor(1)
4332 _z3_assert(idx < self.num_constructors(), "Invalid constructor index")
4333 return FuncDeclRef(Z3_get_datatype_sort_constructor(self.ctx_ref(), self.ast, idx), self.ctx)
4335 def recognizer(self, idx):
4336 """In Z3, each constructor has an associated recognizer predicate.
4338 If the constructor
is named `name`, then the recognizer `is_name`.
4341 >>> List.declare(
'cons', (
'car',
IntSort()), (
'cdr', List))
4342 >>> List.declare(
'nil')
4343 >>> List = List.create()
4345 >>> List.num_constructors()
4347 >>> List.recognizer(0)
4349 >>> List.recognizer(1)
4351 >>>
simplify(List.is_nil(List.cons(10, List.nil)))
4353 >>>
simplify(List.is_cons(List.cons(10, List.nil)))
4355 >>> l =
Const(
'l', List)
4360 _z3_assert(idx < self.num_constructors(), "Invalid recognizer index")
4361 return FuncDeclRef(Z3_get_datatype_sort_recognizer(self.ctx_ref(), self.ast, idx), self.ctx)
4363 def accessor(self, i, j):
4364 """In Z3, each constructor has 0
or more accessor. The number of accessors
is equal to the arity of the constructor.
4367 >>> List.declare(
'cons', (
'car',
IntSort()), (
'cdr', List))
4368 >>> List.declare(
'nil')
4369 >>> List = List.create()
4370 >>> List.num_constructors()
4372 >>> List.constructor(0)
4374 >>> num_accs = List.constructor(0).arity()
4377 >>> List.accessor(0, 0)
4379 >>> List.accessor(0, 1)
4381 >>> List.constructor(1)
4383 >>> num_accs = List.constructor(1).arity()
4388 _z3_assert(i < self.num_constructors(), "Invalid constructor index")
4389 _z3_assert(j < self.constructor(i).arity(), "Invalid accessor index")
4390 return FuncDeclRef(Z3_get_datatype_sort_constructor_accessor(self.ctx_ref(), self.ast, i, j), self.ctx)
4392 class DatatypeRef(ExprRef):
4393 """Datatype expressions.
"""
4395 """Return the datatype sort of the datatype expression `self`.
"""
4396 return DatatypeSortRef(Z3_get_sort(self.ctx_ref(), self.as_ast()), self.ctx)
4398 def EnumSort(name, values, ctx=None):
4399 """Return a new enumeration sort named `name` containing the given values.
4401 The result
is a pair (sort, list of constants).
4403 >>> Color, (red, green, blue) =
EnumSort(
'Color', [
'red',
'green',
'blue'])
4406 _z3_assert(isinstance(name, str), "Name must be a string")
4407 _z3_assert(all([isinstance(v, str) for v in values]), "Eumeration sort values must be strings")
4408 _z3_assert(len(values) > 0, "At least one value expected")
4411 _val_names = (Symbol * num)()
4412 for i in range(num):
4413 _val_names[i] = to_symbol(values[i])
4414 _values = (FuncDecl * num)()
4415 _testers = (FuncDecl * num)()
4416 name = to_symbol(name)
4417 S = DatatypeSortRef(Z3_mk_enumeration_sort(ctx.ref(), name, num, _val_names, _values, _testers), ctx)
4419 for i in range(num):
4420 V.append(FuncDeclRef(_values[i], ctx))
4421 V = [a() for a in V]
4424 #########################################
4428 #########################################
4431 """Set of parameters used to configure Solvers, Tactics
and Simplifiers
in Z3.
4433 Consider using the function `args2params` to create instances of this object.
4435 def __init__(self, ctx=None):
4436 self.ctx = _get_ctx(ctx)
4437 self.params = Z3_mk_params(self.ctx.ref())
4438 Z3_params_inc_ref(self.ctx.ref(), self.params)
4441 Z3_params_dec_ref(self.ctx.ref(), self.params)
4443 def set(self, name, val):
4444 """Set parameter name with value val.
"""
4446 _z3_assert(isinstance(name, str), "parameter name must be a string")
4447 name_sym = to_symbol(name, self.ctx)
4448 if isinstance(val, bool):
4449 Z3_params_set_bool(self.ctx.ref(), self.params, name_sym, val)
4450 elif isinstance(val, int):
4451 Z3_params_set_uint(self.ctx.ref(), self.params, name_sym, val)
4452 elif isinstance(val, float):
4453 Z3_params_set_double(self.ctx.ref(), self.params, name_sym, val)
4454 elif isinstance(val, str):
4455 Z3_params_set_symbol(self.ctx.ref(), self.params, name_sym, to_symbol(val, self.ctx))
4458 _z3_assert(False, "invalid parameter value")
4461 return Z3_params_to_string(self.ctx.ref(), self.params)
4463 def validate(self, ds):
4464 _z3_assert(isinstance(ds, ParamDescrsRef), "parameter description set expected")
4465 Z3_params_validate(self.ctx.ref(), self.params, ds.descr)
4467 def args2params(arguments, keywords, ctx=None):
4468 """Convert python arguments into a Z3_params object.
4469 A
':' is added to the keywords,
and '_' is replaced with
'-'
4471 >>>
args2params([
'model',
True,
'relevancy', 2], {
'elim_and' :
True})
4472 (params model true relevancy 2 elim_and true)
4475 _z3_assert(len(arguments) % 2 == 0, "Argument list must have an even number of elements.")
4489 class ParamDescrsRef:
4490 """Set of parameter descriptions
for Solvers, Tactics
and Simplifiers
in Z3.
4492 def __init__(self, descr, ctx=None):
4493 _z3_assert(isinstance(descr, ParamDescrs), "parameter description object expected")
4494 self.ctx = _get_ctx(ctx)
4496 Z3_param_descrs_inc_ref(self.ctx.ref(), self.descr)
4499 Z3_param_descrs_dec_ref(self.ctx.ref(), self.descr)
4502 """Return the size of
in the parameter description `self`.
4504 return int(Z3_param_descrs_size(self.ctx.ref(), self.descr))
4507 """Return the size of
in the parameter description `self`.
4511 def get_name(self, i):
4512 """Return the i-th parameter name
in the parameter description `self`.
4514 return _symbol2py(self.ctx, Z3_param_descrs_get_name(self.ctx.ref(), self.descr, i))
4516 def get_kind(self, n):
4517 """Return the kind of the parameter named `n`.
4519 return Z3_param_descrs_get_kind(self.ctx.ref(), self.descr, to_symbol(n, self.ctx))
4521 def __getitem__(self, arg):
4523 return self.get_name(arg)
4525 return self.get_kind(arg)
4528 return Z3_param_descrs_to_string(self.ctx.ref(), self.descr)
4530 #########################################
4534 #########################################
4536 class Goal(Z3PPObject):
4537 """Goal
is a collection of constraints we want to find a solution
or show to be unsatisfiable (infeasible).
4539 Goals are processed using Tactics. A Tactic transforms a goal into a set of subgoals.
4540 A goal has a solution
if one of its subgoals has a solution.
4541 A goal
is unsatisfiable
if all subgoals are unsatisfiable.
4544 def __init__(self, models=True, unsat_cores=False, proofs=False, ctx=None, goal=None):
4546 _z3_assert(goal == None or ctx != None, "If goal is different from None, then ctx must be also different from None")
4547 self.ctx = _get_ctx(ctx)
4549 if self.goal == None:
4550 self.goal = Z3_mk_goal(self.ctx.ref(), models, unsat_cores, proofs)
4551 Z3_goal_inc_ref(self.ctx.ref(), self.goal)
4554 if self.goal != None:
4555 Z3_goal_dec_ref(self.ctx.ref(), self.goal)
4558 """Return the depth of the goal `self`. The depth corresponds to the number of tactics applied to `self`.
4560 >>> x, y =
Ints(
'x y')
4562 >>> g.add(x == 0, y >= x + 1)
4565 >>> r =
Then(
'simplify',
'solve-eqs')(g)
4572 return int(Z3_goal_depth(self.ctx.ref(), self.goal))
4574 def inconsistent(self):
4575 """Return `
True`
if `self` contains the `
False` constraints.
4577 >>> x, y =
Ints(
'x y')
4579 >>> g.inconsistent()
4581 >>> g.add(x == 0, x == 1)
4584 >>> g.inconsistent()
4586 >>> g2 =
Tactic(
'propagate-values')(g)[0]
4587 >>> g2.inconsistent()
4590 return Z3_goal_inconsistent(self.ctx.ref(), self.goal)
4593 """Return the precision (under-approximation, over-approximation,
or precise) of the goal `self`.
4596 >>> g.prec() == Z3_GOAL_PRECISE
4598 >>> x, y =
Ints(
'x y')
4599 >>> g.add(x == y + 1)
4600 >>> g.prec() == Z3_GOAL_PRECISE
4602 >>> t =
With(
Tactic(
'add-bounds'), add_bound_lower=0, add_bound_upper=10)
4605 [x == y + 1, x <= 10, x >= 0, y <= 10, y >= 0]
4606 >>> g2.prec() == Z3_GOAL_PRECISE
4608 >>> g2.prec() == Z3_GOAL_UNDER
4611 return Z3_goal_precision(self.ctx.ref(), self.goal)
4613 def precision(self):
4614 """Alias
for `
prec()`.
4617 >>> g.precision() == Z3_GOAL_PRECISE
4623 """Return the number of constraints
in the goal `self`.
4628 >>> x, y =
Ints(
'x y')
4629 >>> g.add(x == 0, y > x)
4633 return int(Z3_goal_size(self.ctx.ref(), self.goal))
4636 """Return the number of constraints
in the goal `self`.
4641 >>> x, y =
Ints(
'x y')
4642 >>> g.add(x == 0, y > x)
4649 """Return a constraint
in the goal `self`.
4652 >>> x, y =
Ints(
'x y')
4653 >>> g.add(x == 0, y > x)
4659 return _to_expr_ref(Z3_goal_formula(self.ctx.ref(), self.goal, i), self.ctx)
4661 def __getitem__(self, arg):
4662 """Return a constraint
in the goal `self`.
4665 >>> x, y =
Ints(
'x y')
4666 >>> g.add(x == 0, y > x)
4672 if arg >= len(self):
4674 return self.get(arg)
4676 def assert_exprs(self, *args):
4677 """Assert constraints into the goal.
4681 >>> g.assert_exprs(x > 0, x < 2)
4685 args = _get_args(args)
4686 s = BoolSort(self.ctx)
4689 Z3_goal_assert(self.ctx.ref(), self.goal, arg.as_ast())
4691 def append(self, *args):
4696 >>> g.append(x > 0, x < 2)
4700 self.assert_exprs(*args)
4702 def insert(self, *args):
4707 >>> g.insert(x > 0, x < 2)
4711 self.assert_exprs(*args)
4713 def add(self, *args):
4718 >>> g.add(x > 0, x < 2)
4722 self.assert_exprs(*args)
4725 return obj_to_string(self)
4728 """Return a textual representation of the s-expression representing the goal.
"""
4729 return Z3_goal_to_string(self.ctx.ref(), self.goal)
4731 def translate(self, target):
4732 """Copy goal `self` to context `target`.
4740 >>> g2 = g.translate(c2)
4751 _z3_assert(isinstance(target, Context), "target must be a context")
4752 return Goal(goal=Z3_goal_translate(self.ctx.ref(), self.goal, target.ref()), ctx=target)
4754 def simplify(self, *arguments, **keywords):
4755 """Return a new simplified goal.
4757 This method
is essentially invoking the simplify tactic.
4761 >>> g.add(x + 1 >= 2)
4764 >>> g2 = g.simplify()
4771 t = Tactic('simplify')
4772 return t.apply(self, *arguments, **keywords)[0]
4775 """Return goal `self`
as a single Z3 expression.
4790 return BoolVal(True, self.ctx)
4794 return And([ self.get(i) for i in range(len(self)) ])
4796 #########################################
4800 #########################################
4801 class AstVector(Z3PPObject):
4802 """A collection (vector) of ASTs.
"""
4804 def __init__(self, v=None, ctx=None):
4807 self.ctx = _get_ctx(ctx)
4808 self.vector = Z3_mk_ast_vector(self.ctx.ref())
4813 Z3_ast_vector_inc_ref(self.ctx.ref(), self.vector)
4816 if self.vector != None:
4817 Z3_ast_vector_dec_ref(self.ctx.ref(), self.vector)
4820 """Return the size of the vector `self`.
4825 >>> A.push(
Int(
'x'))
4826 >>> A.push(
Int(
'x'))
4830 return int(Z3_ast_vector_size(self.ctx.ref(), self.vector))
4832 def __getitem__(self, i):
4833 """Return the AST at position `i`.
4836 >>> A.push(
Int(
'x') + 1)
4837 >>> A.push(
Int(
'y'))
4843 if i >= self.__len__():
4845 return _to_ast_ref(Z3_ast_vector_get(self.ctx.ref(), self.vector, i), self.ctx)
4847 def __setitem__(self, i, v):
4848 """Update AST at position `i`.
4851 >>> A.push(
Int(
'x') + 1)
4852 >>> A.push(
Int(
'y'))
4859 if i >= self.__len__():
4861 Z3_ast_vector_set(self.ctx.ref(), self.vector, i, v.as_ast())
4864 """Add `v`
in the end of the vector.
4869 >>> A.push(
Int(
'x'))
4873 Z3_ast_vector_push(self.ctx.ref(), self.vector, v.as_ast())
4875 def resize(self, sz):
4876 """Resize the vector to `sz` elements.
4882 >>>
for i
in range(10): A[i] =
Int(
'x')
4886 Z3_ast_vector_resize(self.ctx.ref(), self.vector, sz)
4888 def __contains__(self, item):
4889 """Return `
True`
if the vector contains `item`.
4911 def translate(self, other_ctx):
4912 """Copy vector `self` to context `other_ctx`.
4918 >>> B = A.translate(c2)
4922 return AstVector(Z3_ast_vector_translate(self.ctx.ref(), self.vector, other_ctx.ref()), other_ctx)
4925 return obj_to_string(self)
4928 """Return a textual representation of the s-expression representing the vector.
"""
4929 return Z3_ast_vector_to_string(self.ctx.ref(), self.vector)
4931 #########################################
4935 #########################################
4937 """A mapping
from ASTs to ASTs.
"""
4939 def __init__(self, m=None, ctx=None):
4942 self.ctx = _get_ctx(ctx)
4943 self.map = Z3_mk_ast_map(self.ctx.ref())
4948 Z3_ast_map_inc_ref(self.ctx.ref(), self.map)
4951 if self.map != None:
4952 Z3_ast_map_dec_ref(self.ctx.ref(), self.map)
4955 """Return the size of the map.
4965 return int(Z3_ast_map_size(self.ctx.ref(), self.map))
4967 def __contains__(self, key):
4968 """Return `
True`
if the map contains key `key`.
4978 return Z3_ast_map_contains(self.ctx.ref(), self.map, key.as_ast())
4980 def __getitem__(self, key):
4981 """Retrieve the value associated with key `key`.
4989 return _to_ast_ref(Z3_ast_map_find(self.ctx.ref(), self.map, key.as_ast()), self.ctx)
4991 def __setitem__(self, k, v):
4992 """Add/Update key `k` with value `v`.
5005 Z3_ast_map_insert(self.ctx.ref(), self.map, k.as_ast(), v.as_ast())
5008 return Z3_ast_map_to_string(self.ctx.ref(), self.map)
5011 """Remove the entry associated with key `k`.
5022 Z3_ast_map_erase(self.ctx.ref(), self.map, k.as_ast())
5025 """Remove all entries
from the map.
5037 Z3_ast_map_reset(self.ctx.ref(), self.map)
5040 """Return an AstVector containing all keys
in the map.
5049 return AstVector(Z3_ast_map_keys(self.ctx.ref(), self.map), self.ctx)
5051 #########################################
5055 #########################################
5058 """Store the value of the interpretation of a function
in a particular point.
"""
5060 def __init__(self, entry, ctx):
5063 Z3_func_entry_inc_ref(self.ctx.ref(), self.entry)
5066 Z3_func_entry_dec_ref(self.ctx.ref(), self.entry)
5069 """Return the number of arguments
in the given entry.
5073 >>> s.add(f(0, 1) == 10, f(1, 2) == 20, f(1, 0) == 10)
5078 >>> f_i.num_entries()
5080 >>> e = f_i.entry(0)
5084 return int(Z3_func_entry_get_num_args(self.ctx.ref(), self.entry))
5086 def arg_value(self, idx):
5087 """Return the value of argument `idx`.
5091 >>> s.add(f(0, 1) == 10, f(1, 2) == 20, f(1, 0) == 10)
5096 >>> f_i.num_entries()
5098 >>> e = f_i.entry(0)
5109 ...
except IndexError:
5110 ... print(
"index error")
5113 if idx >= self.num_args():
5115 return _to_expr_ref(Z3_func_entry_get_arg(self.ctx.ref(), self.entry, idx), self.ctx)
5118 """Return the value of the function at point `self`.
5122 >>> s.add(f(0, 1) == 10, f(1, 2) == 20, f(1, 0) == 10)
5127 >>> f_i.num_entries()
5129 >>> e = f_i.entry(0)
5137 return _to_expr_ref(Z3_func_entry_get_value(self.ctx.ref(), self.entry), self.ctx)
5140 """Return entry `self`
as a Python list.
5143 >>> s.add(f(0, 1) == 10, f(1, 2) == 20, f(1, 0) == 10)
5148 >>> f_i.num_entries()
5150 >>> e = f_i.entry(0)
5154 args = [ self.arg_value(i) for i in range(self.num_args())]
5155 args.append(self.value())
5159 return repr(self.as_list())
5161 class FuncInterp(Z3PPObject):
5162 """Stores the interpretation of a function
in a Z3 model.
"""
5164 def __init__(self, f, ctx):
5168 Z3_func_interp_inc_ref(self.ctx.ref(), self.f)
5172 Z3_func_interp_dec_ref(self.ctx.ref(), self.f)
5174 def else_value(self):
5176 Return the `
else` value
for a function interpretation.
5177 Return
None if Z3 did
not specify the `
else` value
for
5182 >>> s.add(
f(0) == 1,
f(1) == 1,
f(2) == 0)
5187 [0 -> 1, 1 -> 1, 2 -> 0,
else -> 1]
5191 r = Z3_func_interp_get_else(self.ctx.ref(), self.f)
5193 return _to_expr_ref(r, self.ctx)
5197 def num_entries(self):
5198 """Return the number of entries/points
in the function interpretation `self`.
5202 >>> s.add(
f(0) == 1,
f(1) == 1,
f(2) == 0)
5207 [0 -> 1, 1 -> 1, 2 -> 0,
else -> 1]
5211 return int(Z3_func_interp_get_num_entries(self.ctx.ref(), self.f))
5214 """Return the number of arguments
for each entry
in the function interpretation `self`.
5218 >>> s.add(
f(0) == 1,
f(1) == 1,
f(2) == 0)
5225 return int(Z3_func_interp_get_arity(self.ctx.ref(), self.f))
5227 def entry(self, idx):
5228 """Return an entry at position `idx < self.
num_entries()`
in the function interpretation `self`.
5232 >>> s.add(
f(0) == 1,
f(1) == 1,
f(2) == 0)
5237 [0 -> 1, 1 -> 1, 2 -> 0,
else -> 1]
5247 if idx >= self.num_entries():
5249 return FuncEntry(Z3_func_interp_get_entry(self.ctx.ref(), self.f, idx), self.ctx)
5252 """Return the function interpretation
as a Python list.
5255 >>> s.add(
f(0) == 1,
f(1) == 1,
f(2) == 0)
5260 [0 -> 1, 1 -> 1, 2 -> 0,
else -> 1]
5262 [[0, 1], [1, 1], [2, 0], 1]
5264 r = [ self.entry(i).as_list() for i in range(self.num_entries())]
5265 r.append(self.else_value())
5269 return obj_to_string(self)
5271 class ModelRef(Z3PPObject):
5272 """Model/Solution of a satisfiability problem (aka system of constraints).
"""
5274 def __init__(self, m, ctx):
5278 Z3_model_inc_ref(self.ctx.ref(), self.model)
5281 Z3_model_dec_ref(self.ctx.ref(), self.model)
5284 return obj_to_string(self)
5287 """Return a textual representation of the s-expression representing the model.
"""
5288 return Z3_model_to_string(self.ctx.ref(), self.model)
5290 def eval(self, t, model_completion=False):
5291 """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`.
5295 >>> s.add(x > 0, x < 2)
5308 >>> m.eval(y, model_completion=
True)
5315 if Z3_model_eval(self.ctx.ref(), self.model, t.as_ast(), model_completion, r):
5316 return _to_expr_ref(r[0], self.ctx)
5317 raise Z3Exception("failed to evaluate expression in the model")
5319 def evaluate(self, t, model_completion=False):
5320 """Alias
for `eval`.
5324 >>> s.add(x > 0, x < 2)
5328 >>> m.evaluate(x + 1)
5330 >>> m.evaluate(x == 1)
5333 >>> m.evaluate(y + x)
5337 >>> m.evaluate(y, model_completion=
True)
5340 >>> m.evaluate(y + x)
5343 return self.eval(t, model_completion)
5346 """Return the number of constant
and function declarations
in the model `self`.
5351 >>> s.add(x > 0, f(x) != x)
5358 return int(Z3_model_get_num_consts(self.ctx.ref(), self.model)) + int(Z3_model_get_num_funcs(self.ctx.ref(), self.model))
5360 def get_interp(self, decl):
5361 """Return the interpretation
for a given declaration
or constant.
5366 >>> s.add(x > 0, x < 2, f(x) == 0)
5376 _z3_assert(isinstance(decl, FuncDeclRef) or is_const(decl), "Z3 declaration expected")
5380 if decl.arity() == 0:
5381 r = _to_expr_ref(Z3_model_get_const_interp(self.ctx.ref(), self.model, decl.ast), self.ctx)
5383 return self.get_interp(get_as_array_func(r))
5387 return FuncInterp(Z3_model_get_func_interp(self.ctx.ref(), self.model, decl.ast), self.ctx)
5391 def num_sorts(self):
5392 """Return the number of unintepreted sorts that contain an interpretation
in the model `self`.
5395 >>> a, b =
Consts(
'a b', A)
5404 return int(Z3_model_get_num_sorts(self.ctx.ref(), self.model))
5406 def get_sort(self, idx):
5407 """Return the unintepreted sort at position `idx` < self.
num_sorts().
5411 >>> a1, a2 =
Consts(
'a1 a2', A)
5412 >>> b1, b2 =
Consts(
'b1 b2', B)
5414 >>> s.add(a1 != a2, b1 != b2)
5425 if idx >= self.num_sorts():
5427 return _to_sort_ref(Z3_model_get_sort(self.ctx.ref(), self.model, idx), self.ctx)
5430 """Return all uninterpreted sorts that have an interpretation
in the model `self`.
5434 >>> a1, a2 =
Consts(
'a1 a2', A)
5435 >>> b1, b2 =
Consts(
'b1 b2', B)
5437 >>> s.add(a1 != a2, b1 != b2)
5444 return [ self.get_sort(i) for i in range(self.num_sorts()) ]
5446 def get_universe(self, s):
5447 """Return the intepretation
for the uninterpreted sort `s`
in the model `self`.
5450 >>> a, b =
Consts(
'a b', A)
5456 >>> m.get_universe(A)
5460 _z3_assert(isinstance(s, SortRef), "Z3 sort expected")
5462 return AstVector(Z3_model_get_sort_universe(self.ctx.ref(), self.model, s.ast), self.ctx)
5466 def __getitem__(self, idx):
5467 """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.
5469 The elements can be retrieved using position
or the actual declaration.
5474 >>> s.add(x > 0, x < 2, f(x) == 0)
5488 >>>
for d
in m: print(
"%s -> %s" % (d, m[d]))
5490 f -> [1 -> 0,
else -> 0]
5492 if isinstance(idx, int):
5493 if idx >= len(self):
5495 num_consts = Z3_model_get_num_consts(self.ctx.ref(), self.model)
5496 if (idx < num_consts):
5497 return FuncDeclRef(Z3_model_get_const_decl(self.ctx.ref(), self.model, idx), self.ctx)
5499 return FuncDeclRef(Z3_model_get_func_decl(self.ctx.ref(), self.model, idx - num_consts), self.ctx)
5500 if isinstance(idx, FuncDeclRef):
5501 return self.get_interp(idx)
5503 return self.get_interp(idx.decl())
5504 if isinstance(idx, SortRef):
5505 return self.get_universe(idx)
5507 _z3_assert(False, "Integer, Z3 declaration, or Z3 constant expected")
5511 """Return a list with all symbols that have an interpreation
in the model `self`.
5515 >>> s.add(x > 0, x < 2, f(x) == 0)
5523 for i in range(Z3_model_get_num_consts(self.ctx.ref(), self.model)):
5524 r.append(FuncDeclRef(Z3_model_get_const_decl(self.ctx.ref(), self.model, i), self.ctx))
5525 for i in range(Z3_model_get_num_funcs(self.ctx.ref(), self.model)):
5526 r.append(FuncDeclRef(Z3_model_get_func_decl(self.ctx.ref(), self.model, i), self.ctx))
5530 """Return true
if n
is a Z3 expression of the form (_
as-array f).
"""
5531 return isinstance(n, ExprRef) and Z3_is_as_array(n.ctx.ref(), n.as_ast())
5533 def get_as_array_func(n):
5534 """Return the function declaration f associated with a Z3 expression of the form (_
as-array f).
"""
5536 _z3_assert(is_as_array(n), "as-array Z3 expression expected.")
5537 return FuncDeclRef(Z3_get_as_array_func_decl(n.ctx.ref(), n.as_ast()), n.ctx)
5539 #########################################
5543 #########################################
5545 """Statistics
for `Solver.check()`.
"""
5547 def __init__(self, stats, ctx):
5550 Z3_stats_inc_ref(self.ctx.ref(), self.stats)
5553 Z3_stats_dec_ref(self.ctx.ref(), self.stats)
5559 out.write(u('<table border="1" cellpadding="2" cellspacing="0">'))
5562 out.write(u('<tr style="background-color:#CFCFCF">'))
5565 out.write(u('<tr>'))
5567 out.write(u('<td>%s</td><td>%s</td></tr>' % (k, v)))
5568 out.write(u('</table>'))
5569 return out.getvalue()
5571 return Z3_stats_to_string(self.ctx.ref(), self.stats)
5574 """Return the number of statistical counters.
5577 >>> s =
Then(
'simplify',
'nlsat').solver()
5581 >>> st = s.statistics()
5585 return int(Z3_stats_size(self.ctx.ref(), self.stats))
5587 def __getitem__(self, idx):
5588 """Return the value of statistical counter at position `idx`. The result
is a pair (key, value).
5591 >>> s =
Then(
'simplify',
'nlsat').solver()
5595 >>> st = s.statistics()
5599 (
'nlsat propagations', 2)
5603 if idx >= len(self):
5605 if Z3_stats_is_uint(self.ctx.ref(), self.stats, idx):
5606 val = int(Z3_stats_get_uint_value(self.ctx.ref(), self.stats, idx))
5608 val = Z3_stats_get_double_value(self.ctx.ref(), self.stats, idx)
5609 return (Z3_stats_get_key(self.ctx.ref(), self.stats, idx), val)
5612 """Return the list of statistical counters.
5615 >>> s =
Then(
'simplify',
'nlsat').solver()
5619 >>> st = s.statistics()
5621 [
'nlsat propagations',
'nlsat stages']
5623 return [Z3_stats_get_key(self.ctx.ref(), self.stats, idx) for idx in range(len(self))]
5625 def get_key_value(self, key):
5626 """Return the value of a particular statistical counter.
5629 >>> s =
Then(
'simplify',
'nlsat').solver()
5633 >>> st = s.statistics()
5634 >>> st.get_key_value(
'nlsat propagations')
5637 for idx in range(len(self)):
5638 if key == Z3_stats_get_key(self.ctx.ref(), self.stats, idx):
5639 if Z3_stats_is_uint(self.ctx.ref(), self.stats, idx):
5640 return int(Z3_stats_get_uint_value(self.ctx.ref(), self.stats, idx))
5642 return Z3_stats_get_double_value(self.ctx.ref(), self.stats, idx)
5643 raise Z3Exception("unknown key")
5645 def __getattr__(self, name):
5646 """Access the value of statistical using attributes.
5648 Remark: to access a counter containing blank spaces (e.g.,
'nlsat propagations'),
5649 we should use
'_' (e.g.,
'nlsat_propagations').
5652 >>> s =
Then(
'simplify',
'nlsat').solver()
5656 >>> st = s.statistics()
5658 [
'nlsat propagations',
'nlsat stages']
5659 >>> st.nlsat_propagations
5664 key = name.replace('_', ' ')
5666 return self.get_key_value(key)
5668 raise AttributeError
5670 #########################################
5674 #########################################
5675 class CheckSatResult:
5676 """Represents the result of a satisfiability check: sat, unsat, unknown.
5682 >>> isinstance(r, CheckSatResult)
5686 def __init__(self, r):
5689 def __eq__(self, other):
5690 return isinstance(other, CheckSatResult) and self.r == other.r
5692 def __ne__(self, other):
5693 return not self.__eq__(other)
5697 if self.r == Z3_L_TRUE:
5699 elif self.r == Z3_L_FALSE:
5700 return "<b>unsat</b>"
5702 return "<b>unknown</b>"
5704 if self.r == Z3_L_TRUE:
5706 elif self.r == Z3_L_FALSE:
5711 sat = CheckSatResult(Z3_L_TRUE)
5712 unsat = CheckSatResult(Z3_L_FALSE)
5713 unknown = CheckSatResult(Z3_L_UNDEF)
5715 class Solver(Z3PPObject):
5716 """Solver API provides methods
for implementing the main SMT 2.0 commands: push, pop, check, get-model, etc.
"""
5718 def __init__(self, solver=None, ctx=None):
5719 assert solver == None or ctx != None
5720 self.ctx = _get_ctx(ctx)
5723 self.solver = Z3_mk_solver(self.ctx.ref())
5725 self.solver = solver
5726 Z3_solver_inc_ref(self.ctx.ref(), self.solver)
5729 if self.solver != None:
5730 Z3_solver_dec_ref(self.ctx.ref(), self.solver)
5732 def set(self, *args, **keys):
5733 """Set a configuration option. The method `
help()`
return a string containing all available options.
5737 >>> s.set(mbqi=
True)
5738 >>> s.set(
'MBQI',
True)
5739 >>> s.set(
':mbqi',
True)
5741 p = args2params(args, keys, self.ctx)
5742 Z3_solver_set_params(self.ctx.ref(), self.solver, p.params)
5745 """Create a backtracking point.
5764 Z3_solver_push(self.ctx.ref(), self.solver)
5766 def pop(self, num=1):
5767 """Backtrack \c num backtracking points.
5786 Z3_solver_pop(self.ctx.ref(), self.solver, num)
5789 """Remove all asserted constraints
and backtracking points created using `
push()`.
5800 Z3_solver_reset(self.ctx.ref(), self.solver)
5802 def assert_exprs(self, *args):
5803 """Assert constraints into the solver.
5807 >>> s.assert_exprs(x > 0, x < 2)
5811 args = _get_args(args)
5812 s = BoolSort(self.ctx)
5814 if isinstance(arg, Goal) or isinstance(arg, AstVector):
5816 Z3_solver_assert(self.ctx.ref(), self.solver, f.as_ast())
5819 Z3_solver_assert(self.ctx.ref(), self.solver, arg.as_ast())
5821 def add(self, *args):
5822 """Assert constraints into the solver.
5826 >>> s.add(x > 0, x < 2)
5830 self.assert_exprs(*args)
5832 def append(self, *args):
5833 """Assert constraints into the solver.
5837 >>> s.append(x > 0, x < 2)
5841 self.assert_exprs(*args)
5843 def insert(self, *args):
5844 """Assert constraints into the solver.
5848 >>> s.insert(x > 0, x < 2)
5852 self.assert_exprs(*args)
5854 def assert_and_track(self, a, p):
5855 """Assert constraint `a`
and track it
in the unsat core using the Boolean constant `p`.
5857 If `p`
is a string, it will be automatically converted into a Boolean constant.
5862 >>> s.set(unsat_core=
True)
5863 >>> s.assert_and_track(x > 0,
'p1')
5864 >>> s.assert_and_track(x != 1,
'p2')
5865 >>> s.assert_and_track(x < 0, p3)
5866 >>> print(s.check())
5868 >>> c = s.unsat_core()
5878 if isinstance(p, str):
5879 p = Bool(p, self.ctx)
5880 _z3_assert(isinstance(a, BoolRef), "Boolean expression expected")
5881 _z3_assert(isinstance(p, BoolRef) and is_const(p), "Boolean expression expected")
5882 Z3_solver_assert_and_track(self.ctx.ref(), self.solver, a.as_ast(), p.as_ast())
5884 def check(self, *assumptions):
5885 """Check whether the assertions
in the given solver plus the optional assumptions are consistent
or not.
5891 >>> s.add(x > 0, x < 2)
5900 >>> s.add(2**x == 4)
5904 assumptions = _get_args(assumptions)
5905 num = len(assumptions)
5906 _assumptions = (Ast * num)()
5907 for i in range(num):
5908 _assumptions[i] = assumptions[i].as_ast()
5909 r = Z3_solver_check_assumptions(self.ctx.ref(), self.solver, num, _assumptions)
5910 return CheckSatResult(r)
5913 """Return a model
for the last `
check()`.
5915 This function raises an exception
if
5916 a model
is not available (e.g., last `
check()` returned unsat).
5920 >>> s.add(a + 2 == 0)
5927 return ModelRef(Z3_solver_get_model(self.ctx.ref(), self.solver), self.ctx)
5929 raise Z3Exception("model is not available")
5931 def unsat_core(self):
5932 """Return a subset (
as an AST vector) of the assumptions provided to the last
check().
5934 These are the assumptions Z3 used
in the unsatisfiability proof.
5935 Assumptions are available
in Z3. They are used to extract unsatisfiable cores.
5936 They may be also used to
"retract" assumptions. Note that, assumptions are
not really
5937 "soft constraints", but they can be used to implement them.
5939 >>> p1, p2, p3 =
Bools(
'p1 p2 p3')
5940 >>> x, y =
Ints(
'x y')
5945 >>> s.add(
Implies(p3, y > -3))
5946 >>> s.check(p1, p2, p3)
5948 >>> core = s.unsat_core()
5961 return AstVector(Z3_solver_get_unsat_core(self.ctx.ref(), self.solver), self.ctx)
5964 """Return a proof
for the last `
check()`. Proof construction must be enabled.
"""
5965 return _to_expr_ref(Z3_solver_get_proof(self.ctx.ref(), self.solver), self.ctx)
5967 def assertions(self):
5968 """Return an AST vector containing all added constraints.
5979 return AstVector(Z3_solver_get_assertions(self.ctx.ref(), self.solver), self.ctx)
5981 def statistics(self):
5982 """Return statistics
for the last `
check()`.
5989 >>> st = s.statistics()
5990 >>> st.get_key_value(
'final checks')
5997 return Statistics(Z3_solver_get_statistics(self.ctx.ref(), self.solver), self.ctx)
5999 def reason_unknown(self):
6000 """Return a string describing why the last `
check()` returned `unknown`.
6004 >>> s.add(2**x == 4)
6007 >>> s.reason_unknown()
6008 '(incomplete (theory arithmetic))'
6010 return Z3_solver_get_reason_unknown(self.ctx.ref(), self.solver)
6013 """Display a string describing all available options.
"""
6014 print(Z3_solver_get_help(self.ctx.ref(), self.solver))
6016 def param_descrs(self):
6017 """Return the parameter description set.
"""
6018 return ParamDescrsRef(Z3_solver_get_param_descrs(self.ctx.ref(), self.solver), self.ctx)
6021 """Return a formatted string with all added constraints.
"""
6022 return obj_to_string(self)
6025 """Return a formatted string (
in Lisp-like format) with all added constraints. We say the string
is in s-expression format.
6033 return Z3_solver_to_string(self.ctx.ref(), self.solver)
6036 """return SMTLIB2 formatted benchmark
for solver
's assertions"""
6043 for i
in range(sz1):
6044 v[i] = es[i].as_ast()
6046 e = es[sz1].as_ast()
6054 """Create a solver customized for the given logic.
6056 The parameter `logic` is a string. It should be contains
6057 the name of a SMT-LIB logic.
6058 See http://www.smtlib.org/ for the name of all available logics.
6060 >>> s = SolverFor("QF_LIA")
6074 """Return a simple general purpose solver with limited amount of preprocessing.
6076 >>> s = SimpleSolver()
6092 """Fixedpoint API provides methods for solving with recursive predicates"""
6095 assert fixedpoint ==
None or ctx !=
None
6098 if fixedpoint ==
None:
6110 """Set a configuration option. The method `help()` return a string containing all available options.
6116 """Display a string describing all available options."""
6120 """Return the parameter description set."""
6124 """Assert constraints as background axioms for the fixedpoint solver."""
6125 args = _get_args(args)
6128 if isinstance(arg, Goal)
or isinstance(arg, AstVector):
6138 """Assert constraints as background axioms for the fixedpoint solver. Alias for assert_expr."""
6142 """Assert constraints as background axioms for the fixedpoint solver. Alias for assert_expr."""
6146 """Assert constraints as background axioms for the fixedpoint solver. Alias for assert_expr."""
6150 """Assert rules defining recursive predicates to the fixedpoint solver.
6153 >>> s = Fixedpoint()
6154 >>> s.register_relation(a.decl())
6155 >>> s.register_relation(b.decl())
6168 body = _get_args(body)
6172 def rule(self, head, body = None, name = None):
6173 """Assert rules defining recursive predicates to the fixedpoint solver. Alias for add_rule."""
6177 """Assert facts defining recursive predicates to the fixedpoint solver. Alias for add_rule."""
6181 """Query the fixedpoint engine whether formula is derivable.
6182 You can also pass an tuple or list of recursive predicates.
6184 query = _get_args(query)
6186 if sz >= 1
and isinstance(query[0], FuncDecl):
6187 _decls = (FuncDecl * sz)()
6198 query = self.
abstract(query,
False)
6203 """create a backtracking point for added rules, facts and assertions"""
6207 """restore to previously created backtracking point"""
6215 body = _get_args(body)
6220 """Retrieve answer from last query call."""
6222 return _to_expr_ref(r, self.
ctx)
6225 """Retrieve number of levels used for predicate in PDR engine"""
6229 """Retrieve properties known about predicate for the level'th unfolding. -1 is treated as the limit (infinity)"""
6231 return _to_expr_ref(r, self.
ctx)
6234 """Add property to predicate for the level'th unfolding. -1 is treated as infinity (infinity)"""
6238 """Register relation as recursive"""
6239 relations = _get_args(relations)
6244 """Control how relation is represented"""
6245 representations = _get_args(representations)
6246 representations = [
to_symbol(s)
for s
in representations]
6247 sz = len(representations)
6248 args = (Symbol * sz)()
6250 args[i] = representations[i]
6254 """Parse rules and queries from a string"""
6258 """Parse rules and queries from a file"""
6262 """retrieve rules that have been added to fixedpoint context"""
6266 """retrieve assertions that have been added to fixedpoint context"""
6270 """Return a formatted string with all added rules and constraints."""
6274 """Return a formatted string (in Lisp-like format) with all added constraints. We say the string is in s-expression format.
6279 """Return a formatted string (in Lisp-like format) with all added constraints.
6280 We say the string is in s-expression format.
6281 Include also queries.
6283 args, len = _to_ast_array(queries)
6287 """Return statistics for the last `query()`.
6292 """Return a string describing why the last `query()` returned `unknown`.
6297 """Add variable or several variables.
6298 The added variable or variables will be bound in the rules
6301 vars = _get_args(vars)
6319 """An ApplyResult object contains the subgoals produced by a tactic when applied to a goal. It also contains model and proof converters."""
6330 """Return the number of subgoals in `self`.
6332 >>> a, b = Ints('a b')
6334 >>> g.add(Or(a == 0, a == 1), Or(b == 0, b == 1), a > b)
6335 >>> t = Tactic('split-clause')
6339 >>> t = Then(Tactic('split-clause'), Tactic('split-clause'))
6342 >>> t = Then(Tactic('split-clause'), Tactic('split-clause'), Tactic('propagate-values'))
6349 """Return one of the subgoals stored in ApplyResult object `self`.
6351 >>> a, b = Ints('a b')
6353 >>> g.add(Or(a == 0, a == 1), Or(b == 0, b == 1), a > b)
6354 >>> t = Tactic('split-clause')
6357 [a == 0, Or(b == 0, b == 1), a > b]
6359 [a == 1, Or(b == 0, b == 1), a > b]
6361 if idx >= len(self):
6366 return obj_to_string(self)
6369 """Return a textual representation of the s-expression representing the set of subgoals in `self`."""
6373 """Convert a model for a subgoal into a model for the original goal.
6375 >>> a, b = Ints('a b')
6377 >>> g.add(Or(a == 0, a == 1), Or(b == 0, b == 1), a > b)
6378 >>> t = Then(Tactic('split-clause'), Tactic('solve-eqs'))
6381 [Or(b == 0, b == 1), Not(0 <= b)]
6383 [Or(b == 0, b == 1), Not(1 <= b)]
6384 >>> # Remark: the subgoal r[0] is unsatisfiable
6385 >>> # Creating a solver for solving the second subgoal
6392 >>> # Model s.model() does not assign a value to `a`
6393 >>> # It is a model for subgoal `r[1]`, but not for goal `g`
6394 >>> # The method convert_model creates a model for `g` from a model for `r[1]`.
6395 >>> r.convert_model(s.model(), 1)
6399 _z3_assert(idx < len(self),
"index out of bounds")
6400 _z3_assert(isinstance(model, ModelRef),
"Z3 Model expected")
6404 """Return a Z3 expression consisting of all subgoals.
6409 >>> g.add(Or(x == 2, x == 3))
6410 >>> r = Tactic('simplify')(g)
6412 [[Not(x <= 1), Or(x == 2, x == 3)]]
6414 And(Not(x <= 1), Or(x == 2, x == 3))
6415 >>> r = Tactic('split-clause')(g)
6417 [[x > 1, x == 2], [x > 1, x == 3]]
6419 Or(And(x > 1, x == 2), And(x > 1, x == 3))
6427 return Or([ self[i].
as_expr()
for i
in range(len(self)) ])
6435 """Tactics transform, solver and/or simplify sets of constraints (Goal). A Tactic can be converted into a Solver using the method solver().
6437 Several combinators are available for creating new tactics using the built-in ones: Then(), OrElse(), FailIf(), Repeat(), When(), Cond().
6442 if isinstance(tactic, TacticObj):
6446 _z3_assert(isinstance(tactic, str),
"tactic name expected")
6450 raise Z3Exception(
"unknown tactic '%s'" % tactic)
6458 """Create a solver using the tactic `self`.
6460 The solver supports the methods `push()` and `pop()`, but it
6461 will always solve each `check()` from scratch.
6463 >>> t = Then('simplify', 'nlsat')
6466 >>> s.add(x**2 == 2, x > 0)
6474 def apply(self, goal, *arguments, **keywords):
6475 """Apply tactic `self` to the given goal or Z3 Boolean expression using the given options.
6477 >>> x, y = Ints('x y')
6478 >>> t = Tactic('solve-eqs')
6479 >>> t.apply(And(x == 0, y >= x + 1))
6483 _z3_assert(isinstance(goal, Goal)
or isinstance(goal, BoolRef),
"Z3 Goal or Boolean expressions expected")
6484 goal = _to_goal(goal)
6485 if len(arguments) > 0
or len(keywords) > 0:
6492 """Apply tactic `self` to the given goal or Z3 Boolean expression using the given options.
6494 >>> x, y = Ints('x y')
6495 >>> t = Tactic('solve-eqs')
6496 >>> t(And(x == 0, y >= x + 1))
6499 return self.
apply(goal, *arguments, **keywords)
6502 """Display a string containing a description of the available options for the `self` tactic."""
6506 """Return the parameter description set."""
6510 if isinstance(a, BoolRef):
6511 goal =
Goal(ctx = a.ctx)
6517 def _to_tactic(t, ctx=None):
6518 if isinstance(t, Tactic):
6523 def _and_then(t1, t2, ctx=None):
6524 t1 = _to_tactic(t1, ctx)
6525 t2 = _to_tactic(t2, ctx)
6527 _z3_assert(t1.ctx == t2.ctx,
"Context mismatch")
6530 def _or_else(t1, t2, ctx=None):
6531 t1 = _to_tactic(t1, ctx)
6532 t2 = _to_tactic(t2, ctx)
6534 _z3_assert(t1.ctx == t2.ctx,
"Context mismatch")
6538 """Return a tactic that applies the tactics in `*ts` in sequence.
6540 >>> x, y = Ints('x y')
6541 >>> t = AndThen(Tactic('simplify'), Tactic('solve-eqs'))
6542 >>> t(And(x == 0, y > x + 1))
6544 >>> t(And(x == 0, y > x + 1)).as_expr()
6548 _z3_assert(len(ts) >= 2,
"At least two arguments expected")
6549 ctx = ks.get(
'ctx',
None)
6552 for i
in range(num - 1):
6553 r = _and_then(r, ts[i+1], ctx)
6557 """Return a tactic that applies the tactics in `*ts` in sequence. Shorthand for AndThen(*ts, **ks).
6559 >>> x, y = Ints('x y')
6560 >>> t = Then(Tactic('simplify'), Tactic('solve-eqs'))
6561 >>> t(And(x == 0, y > x + 1))
6563 >>> t(And(x == 0, y > x + 1)).as_expr()
6569 """Return a tactic that applies the tactics in `*ts` until one of them succeeds (it doesn't fail).
6572 >>> t = OrElse(Tactic('split-clause'), Tactic('skip'))
6573 >>> # Tactic split-clause fails if there is no clause in the given goal.
6576 >>> t(Or(x == 0, x == 1))
6577 [[x == 0], [x == 1]]
6580 _z3_assert(len(ts) >= 2,
"At least two arguments expected")
6581 ctx = ks.get(
'ctx',
None)
6584 for i
in range(num - 1):
6585 r = _or_else(r, ts[i+1], ctx)
6589 """Return a tactic that applies the tactics in `*ts` in parallel until one of them succeeds (it doesn't fail).
6592 >>> t = ParOr(Tactic('simplify'), Tactic('fail'))
6597 _z3_assert(len(ts) >= 2,
"At least two arguments expected")
6598 ctx = _get_ctx(ks.get(
'ctx',
None))
6599 ts = [ _to_tactic(t, ctx)
for t
in ts ]
6601 _args = (TacticObj * sz)()
6603 _args[i] = ts[i].tactic
6607 """Return a tactic that applies t1 and then t2 to every subgoal produced by t1. The subgoals are processed in parallel.
6609 >>> x, y = Ints('x y')
6610 >>> t = ParThen(Tactic('split-clause'), Tactic('propagate-values'))
6611 >>> t(And(Or(x == 1, x == 2), y == x + 1))
6612 [[x == 1, y == 2], [x == 2, y == 3]]
6614 t1 = _to_tactic(t1, ctx)
6615 t2 = _to_tactic(t2, ctx)
6617 _z3_assert(t1.ctx == t2.ctx,
"Context mismatch")
6621 """Alias for ParThen(t1, t2, ctx)."""
6625 """Return a tactic that applies tactic `t` using the given configuration options.
6627 >>> x, y = Ints('x y')
6628 >>> t = With(Tactic('simplify'), som=True)
6629 >>> t((x + 1)*(y + 2) == 0)
6630 [[2*x + y + x*y == -2]]
6632 ctx = keys.get(
'ctx',
None)
6633 t = _to_tactic(t, ctx)
6638 """Return a tactic that keeps applying `t` until the goal is not modified anymore or the maximum number of iterations `max` is reached.
6640 >>> x, y = Ints('x y')
6641 >>> c = And(Or(x == 0, x == 1), Or(y == 0, y == 1), x > y)
6642 >>> t = Repeat(OrElse(Tactic('split-clause'), Tactic('skip')))
6644 >>> for subgoal in r: print(subgoal)
6645 [x == 0, y == 0, x > y]
6646 [x == 0, y == 1, x > y]
6647 [x == 1, y == 0, x > y]
6648 [x == 1, y == 1, x > y]
6649 >>> t = Then(t, Tactic('propagate-values'))
6653 t = _to_tactic(t, ctx)
6657 """Return a tactic that applies `t` to a given goal for `ms` milliseconds.
6659 If `t` does not terminate in `ms` milliseconds, then it fails.
6661 t = _to_tactic(t, ctx)
6665 """Return a list of all available tactics in Z3.
6668 >>> l.count('simplify') == 1
6675 """Return a short description for the tactic named `name`.
6677 >>> d = tactic_description('simplify')
6683 """Display a (tabular) description of all available tactics in Z3."""
6686 print(
'<table border="1" cellpadding="2" cellspacing="0">')
6689 print(
'<tr style="background-color:#CFCFCF">')
6694 print(
'<td>%s</td><td>%s</td></tr>' % (t, insert_line_breaks(
tactic_description(t), 40)))
6701 """Probes are used to inspect a goal (aka problem) and collect information that may be used to decide which solver and/or preprocessing step will be used."""
6705 if isinstance(probe, ProbeObj):
6707 elif isinstance(probe, float):
6709 elif _is_int(probe):
6711 elif isinstance(probe, bool):
6718 _z3_assert(isinstance(probe, str),
"probe name expected")
6722 raise Z3Exception(
"unknown probe '%s'" % probe)
6726 if self.
probe !=
None:
6730 """Return a probe that evaluates to "true" when the value returned by `self` is less than the value returned by `other`.
6732 >>> p = Probe('size') < 10
6743 """Return a probe that evaluates to "true" when the value returned by `self` is greater than the value returned by `other`.
6745 >>> p = Probe('size') > 10
6756 """Return a probe that evaluates to "true" when the value returned by `self` is less than or equal to the value returned by `other`.
6758 >>> p = Probe('size') <= 2
6769 """Return a probe that evaluates to "true" when the value returned by `self` is greater than or equal to the value returned by `other`.
6771 >>> p = Probe('size') >= 2
6782 """Return a probe that evaluates to "true" when the value returned by `self` is equal to the value returned by `other`.
6784 >>> p = Probe('size') == 2
6795 """Return a probe that evaluates to "true" when the value returned by `self` is not equal to the value returned by `other`.
6797 >>> p = Probe('size') != 2
6809 """Evaluate the probe `self` in the given goal.
6811 >>> p = Probe('size')
6821 >>> p = Probe('num-consts')
6824 >>> p = Probe('is-propositional')
6827 >>> p = Probe('is-qflia')
6832 _z3_assert(isinstance(goal, Goal)
or isinstance(goal, BoolRef),
"Z3 Goal or Boolean expression expected")
6833 goal = _to_goal(goal)
6837 """Return `True` if `p` is a Z3 probe.
6839 >>> is_probe(Int('x'))
6841 >>> is_probe(Probe('memory'))
6844 return isinstance(p, Probe)
6846 def _to_probe(p, ctx=None):
6850 return Probe(p, ctx)
6853 """Return a list of all available probes in Z3.
6856 >>> l.count('memory') == 1
6863 """Return a short description for the probe named `name`.
6865 >>> d = probe_description('memory')
6871 """Display a (tabular) description of all available probes in Z3."""
6874 print(
'<table border="1" cellpadding="2" cellspacing="0">')
6877 print(
'<tr style="background-color:#CFCFCF">')
6882 print(
'<td>%s</td><td>%s</td></tr>' % (p, insert_line_breaks(
probe_description(p), 40)))
6888 def _probe_nary(f, args, ctx):
6890 _z3_assert(len(args) > 0,
"At least one argument expected")
6892 r = _to_probe(args[0], ctx)
6893 for i
in range(num - 1):
6894 r =
Probe(f(ctx.ref(), r.probe, _to_probe(args[i+1], ctx).probe), ctx)
6897 def _probe_and(args, ctx):
6898 return _probe_nary(Z3_probe_and, args, ctx)
6900 def _probe_or(args, ctx):
6901 return _probe_nary(Z3_probe_or, args, ctx)
6904 """Return a tactic that fails if the probe `p` evaluates to true. Otherwise, it returns the input goal unmodified.
6906 In the following example, the tactic applies 'simplify' if and only if there are more than 2 constraints in the goal.
6908 >>> t = OrElse(FailIf(Probe('size') > 2), Tactic('simplify'))
6909 >>> x, y = Ints('x y')
6915 >>> g.add(x == y + 1)
6917 [[Not(x <= 0), Not(y <= 0), x == 1 + y]]
6919 p = _to_probe(p, ctx)
6923 """Return a tactic that applies tactic `t` only if probe `p` evaluates to true. Otherwise, it returns the input goal unmodified.
6925 >>> t = When(Probe('size') > 2, Tactic('simplify'))
6926 >>> x, y = Ints('x y')
6932 >>> g.add(x == y + 1)
6934 [[Not(x <= 0), Not(y <= 0), x == 1 + y]]
6936 p = _to_probe(p, ctx)
6937 t = _to_tactic(t, ctx)
6941 """Return a tactic that applies tactic `t1` to a goal if probe `p` evaluates to true, and `t2` otherwise.
6943 >>> t = Cond(Probe('is-qfnra'), Tactic('qfnra'), Tactic('smt'))
6945 p = _to_probe(p, ctx)
6946 t1 = _to_tactic(t1, ctx)
6947 t2 = _to_tactic(t2, ctx)
6957 """Simplify the expression `a` using the given options.
6959 This function has many options. Use `help_simplify` to obtain the complete list.
6963 >>> simplify(x + 1 + y + x + 1)
6965 >>> simplify((x + 1)*(y + 1), som=True)
6967 >>> simplify(Distinct(x, y, 1), blast_distinct=True)
6968 And(Not(x == y), Not(x == 1), Not(y == 1))
6969 >>> simplify(And(x == 0, y == 1), elim_and=True)
6970 Not(Or(Not(x == 0), Not(y == 1)))
6973 _z3_assert(
is_expr(a),
"Z3 expression expected")
6974 if len(arguments) > 0
or len(keywords) > 0:
6976 return _to_expr_ref(
Z3_simplify_ex(a.ctx_ref(), a.as_ast(), p.params), a.ctx)
6978 return _to_expr_ref(
Z3_simplify(a.ctx_ref(), a.as_ast()), a.ctx)
6981 """Return a string describing all options available for Z3 `simplify` procedure."""
6985 """Return the set of parameter descriptions for Z3 `simplify` procedure."""
6989 """Apply substitution m on t, m is a list of pairs of the form (from, to). Every occurrence in t of from is replaced with to.
6993 >>> substitute(x + 1, (x, y + 1))
6995 >>> f = Function('f', IntSort(), IntSort())
6996 >>> substitute(f(x) + f(y), (f(x), IntVal(1)), (f(y), IntVal(1)))
6999 if isinstance(m, tuple):
7001 if isinstance(m1, list):
7004 _z3_assert(
is_expr(t),
"Z3 expression expected")
7005 _z3_assert(all([isinstance(p, tuple)
and is_expr(p[0])
and is_expr(p[1])
and p[0].sort().
eq(p[1].sort())
for p
in m]),
"Z3 invalid substitution, expression pairs expected.")
7007 _from = (Ast * num)()
7009 for i
in range(num):
7010 _from[i] = m[i][0].as_ast()
7011 _to[i] = m[i][1].as_ast()
7012 return _to_expr_ref(
Z3_substitute(t.ctx.ref(), t.as_ast(), num, _from, _to), t.ctx)
7015 """Substitute the free variables in t with the expression in m.
7017 >>> v0 = Var(0, IntSort())
7018 >>> v1 = Var(1, IntSort())
7020 >>> f = Function('f', IntSort(), IntSort(), IntSort())
7021 >>> # replace v0 with x+1 and v1 with x
7022 >>> substitute_vars(f(v0, v1), x + 1, x)
7026 _z3_assert(
is_expr(t),
"Z3 expression expected")
7027 _z3_assert(all([
is_expr(n)
for n
in m]),
"Z3 invalid substitution, list of expressions expected.")
7030 for i
in range(num):
7031 _to[i] = m[i].as_ast()
7035 """Create the sum of the Z3 expressions.
7037 >>> a, b, c = Ints('a b c')
7042 >>> A = IntVector('a', 5)
7044 a__0 + a__1 + a__2 + a__3 + a__4
7046 args = _get_args(args)
7048 _z3_assert(len(args) > 0,
"Non empty list of arguments expected")
7049 ctx = _ctx_from_ast_arg_list(args)
7051 _z3_assert(ctx !=
None,
"At least one of the arguments must be a Z3 expression")
7052 args = _coerce_expr_list(args, ctx)
7054 return _reduce(
lambda a, b: a + b, args, 0)
7056 _args, sz = _to_ast_array(args)
7060 """Create the product of the Z3 expressions.
7062 >>> a, b, c = Ints('a b c')
7063 >>> Product(a, b, c)
7065 >>> Product([a, b, c])
7067 >>> A = IntVector('a', 5)
7069 a__0*a__1*a__2*a__3*a__4
7071 args = _get_args(args)
7073 _z3_assert(len(args) > 0,
"Non empty list of arguments expected")
7074 ctx = _ctx_from_ast_arg_list(args)
7076 _z3_assert(ctx !=
None,
"At least one of the arguments must be a Z3 expression")
7077 args = _coerce_expr_list(args, ctx)
7079 return _reduce(
lambda a, b: a * b, args, 1)
7081 _args, sz = _to_ast_array(args)
7085 """Solve the constraints `*args`.
7087 This is a simple function for creating demonstrations. It creates a solver,
7088 configure it using the options in `keywords`, adds the constraints
7089 in `args`, and invokes check.
7092 >>> solve(a > 0, a < 2)
7098 if keywords.get(
'show',
False):
7102 print(
"no solution")
7104 print(
"failed to solve")
7113 """Solve the constraints `*args` using solver `s`.
7115 This is a simple function for creating demonstrations. It is similar to `solve`,
7116 but it uses the given solver `s`.
7117 It configures solver `s` using the options in `keywords`, adds the constraints
7118 in `args`, and invokes check.
7121 _z3_assert(isinstance(s, Solver),
"Solver object expected")
7124 if keywords.get(
'show',
False):
7129 print(
"no solution")
7131 print(
"failed to solve")
7137 if keywords.get(
'show',
False):
7142 """Try to prove the given claim.
7144 This is a simple function for creating demonstrations. It tries to prove
7145 `claim` by showing the negation is unsatisfiable.
7147 >>> p, q = Bools('p q')
7148 >>> prove(Not(And(p, q)) == Or(Not(p), Not(q)))
7152 _z3_assert(
is_bool(claim),
"Z3 Boolean expression expected")
7156 if keywords.get(
'show',
False):
7162 print(
"failed to prove")
7165 print(
"counterexample")
7168 def _solve_html(*args, **keywords):
7169 """Version of funcion `solve` used in RiSE4Fun."""
7173 if keywords.get(
'show',
False):
7174 print(
"<b>Problem:</b>")
7178 print(
"<b>no solution</b>")
7180 print(
"<b>failed to solve</b>")
7186 if keywords.get(
'show',
False):
7187 print(
"<b>Solution:</b>")
7190 def _solve_using_html(s, *args, **keywords):
7191 """Version of funcion `solve_using` used in RiSE4Fun."""
7193 _z3_assert(isinstance(s, Solver),
"Solver object expected")
7196 if keywords.get(
'show',
False):
7197 print(
"<b>Problem:</b>")
7201 print(
"<b>no solution</b>")
7203 print(
"<b>failed to solve</b>")
7209 if keywords.get(
'show',
False):
7210 print(
"<b>Solution:</b>")
7213 def _prove_html(claim, **keywords):
7214 """Version of funcion `prove` used in RiSE4Fun."""
7216 _z3_assert(
is_bool(claim),
"Z3 Boolean expression expected")
7220 if keywords.get(
'show',
False):
7224 print(
"<b>proved</b>")
7226 print(
"<b>failed to prove</b>")
7229 print(
"<b>counterexample</b>")
7232 def _dict2sarray(sorts, ctx):
7234 _names = (Symbol * sz)()
7235 _sorts = (Sort * sz) ()
7240 _z3_assert(isinstance(k, str),
"String expected")
7241 _z3_assert(
is_sort(v),
"Z3 sort expected")
7245 return sz, _names, _sorts
7247 def _dict2darray(decls, ctx):
7249 _names = (Symbol * sz)()
7250 _decls = (FuncDecl * sz) ()
7255 _z3_assert(isinstance(k, str),
"String expected")
7259 _decls[i] = v.decl().ast
7263 return sz, _names, _decls
7266 """Parse a string in SMT 2.0 format using the given sorts and decls.
7268 The arguments sorts and decls are Python dictionaries used to initialize
7269 the symbol table used for the SMT 2.0 parser.
7271 >>> parse_smt2_string('(declare-const x Int) (assert (> x 0)) (assert (< x 10))')
7273 >>> x, y = Ints('x y')
7274 >>> f = Function('f', IntSort(), IntSort())
7275 >>> parse_smt2_string('(assert (> (+ foo (g bar)) 0))', decls={ 'foo' : x, 'bar' : y, 'g' : f})
7277 >>> parse_smt2_string('(declare-const a U) (assert (> a 0))', sorts={ 'U' : IntSort() })
7281 ssz, snames, ssorts = _dict2sarray(sorts, ctx)
7282 dsz, dnames, ddecls = _dict2darray(decls, ctx)
7283 return _to_expr_ref(Z3_parse_smtlib2_string(ctx.ref(), s, ssz, snames, ssorts, dsz, dnames, ddecls), ctx)
7285 def parse_smt2_file(f, sorts={}, decls={}, ctx=None):
7286 """Parse a file
in SMT 2.0 format using the given sorts
and decls.
7291 ssz, snames, ssorts = _dict2sarray(sorts, ctx)
7292 dsz, dnames, ddecls = _dict2darray(decls, ctx)
7293 return _to_expr_ref(Z3_parse_smtlib2_file(ctx.ref(), f, ssz, snames, ssorts, dsz, dnames, ddecls), ctx)
7295 def Interpolant(a,ctx=None):
7296 """Create an interpolation operator.
7298 The argument
is an interpolation pattern (see tree_interpolant).
7304 ctx = _get_ctx(_ctx_from_ast_arg_list([a], ctx))
7307 return BoolRef(Z3_mk_interpolant(ctx.ref(), a.as_ast()), ctx)
7309 def tree_interpolant(pat,p=None,ctx=None):
7310 """Compute interpolant
for a tree of formulas.
7312 The input
is an interpolation pattern over a set of formulas C.
7313 The pattern pat
is a formula combining the formulas
in C using
7314 logical conjunction
and the
"interp" operator (see Interp). This
7315 interp operator
is logically the identity operator. It marks the
7316 sub-formulas of the pattern
for which interpolants should be
7317 computed. The interpolant
is a map sigma
from marked subformulas
7318 to formulas, such that,
for each marked subformula phi of pat
7319 (where phi sigma
is phi with sigma(psi) substituted
for each
7320 subformula psi of phi such that psi
in dom(sigma)):
7322 1) phi sigma implies sigma(phi),
and
7324 2) sigma(phi)
is in the common uninterpreted vocabulary between
7325 the formulas of C occurring
in phi
and those
not occurring
in
7328 and moreover pat sigma implies false. In the simplest case
7329 an interpolant
for the pattern
"(and (interp A) B)" maps A
7330 to an interpolant
for A /\ B.
7332 The
return value
is a vector of formulas representing sigma. This
7333 vector contains sigma(phi)
for each marked subformula of pat,
in
7334 pre-order traversal. This means that subformulas of phi occur before phi
7335 in the vector. Also, subformulas that occur multiply
in pat will
7336 occur multiply
in the result vector.
7338 If pat
is satisfiable, raises an object of
class ModelRef
7339 that represents a model of pat.
7341 If parameters p are supplied, these are used
in creating the
7342 solver that determines satisfiability.
7347 [
Not(x >= 0),
Not(y <= 2)]
7352 ...
except ModelRef
as m:
7354 (define-fun x () Int
7358 ctx = _get_ctx(_ctx_from_ast_arg_list([f], ctx))
7359 ptr = (AstVectorObj * 1)()
7360 mptr = (Model * 1)()
7363 res = Z3_compute_interpolant(ctx.ref(),f.as_ast(),p.params,ptr,mptr)
7364 if res == Z3_L_FALSE:
7365 return AstVector(ptr[0],ctx)
7366 raise ModelRef(mptr[0], ctx)
7368 def binary_interpolant(a,b,p=None,ctx=None):
7369 """Compute an interpolant
for a binary conjunction.
7371 If a & b
is unsatisfiable, returns an interpolant
for a & b.
7372 This
is a formula phi such that
7375 2) b implies
not phi
7376 3) All the uninterpreted symbols of phi occur
in both a
and b.
7378 If a & b
is satisfiable, raises an object of
class ModelRef
7379 that represents a model of a &b.
7381 If parameters p are supplied, these are used
in creating the
7382 solver that determines satisfiability.
7388 f = And(Interpolant(a),b)
7389 return tree_interpolant(f,p,ctx)[0]
7391 def sequence_interpolant(v,p=None,ctx=None):
7392 """Compute interpolant
for a sequence of formulas.
7394 If len(v) == N,
and if the conjunction of the formulas
in v
is
7395 unsatisfiable, the interpolant
is a sequence of formulas w
7396 such that len(w) = N-1
and v[0] implies w[0]
and for i
in 0..N-1:
7398 1) w[i] & v[i+1] implies w[i+1] (
or false
if i+1 = N)
7399 2) All uninterpreted symbols
in w[i] occur
in both v[0]..v[i]
7402 Requires len(v) >= 1.
7404 If a & b
is satisfiable, raises an object of
class ModelRef
7405 that represents a model of a & b.
7407 If parameters p are supplied, these are used
in creating the
7408 solver that determines satisfiability.
7413 [
Not(x >= 0),
Not(y >= 0)]
7416 for i in range(1,len(v)):
7417 f = And(Interpolant(f),v[i])
7418 return tree_interpolant(f,p,ctx)
Z3_ast Z3_API Z3_mk_const(__in Z3_context c, __in Z3_symbol s, __in Z3_sort ty)
Declare and create a constant.
def simplify(a, arguments, keywords)
Utils.
Z3_tactic Z3_API Z3_tactic_par_and_then(__in Z3_context c, __in Z3_tactic t1, __in Z3_tactic t2)
Return a tactic that applies t1 to a given goal and then t2 to every subgoal produced by t1...
void Z3_API Z3_fixedpoint_assert(__in Z3_context c, __in Z3_fixedpoint d, __in Z3_ast axiom)
Assert a constraint to the fixedpoint context.
Z3_context Z3_API Z3_mk_context_rc(__in Z3_config c)
Create a context using the given configuration. This function is similar to Z3_mk_context. However, in the context returned by this function, the user is responsible for managing Z3_ast reference counters. Managing reference counters is a burden and error-prone, but allows the user to use the memory more efficiently. The user must invoke Z3_inc_ref for any Z3_ast returned by Z3, and Z3_dec_ref whenever the Z3_ast is not needed anymore. This idiom is similar to the one used in BDD (binary decision diagrams) packages such as CUDD.
Z3_ast Z3_API Z3_translate(__in Z3_context source, __in Z3_ast a, __in Z3_context target)
Translate/Copy the AST a from context source to context target. AST a must have been created using co...
Z3_ast Z3_API Z3_mk_mul(__in Z3_context c, __in unsigned num_args, __in_ecount(num_args) Z3_ast const args[])
Create an AST node representing args[0] * ... * args[num_args-1].The array args must have num_args el...
Z3_string Z3_API Z3_fixedpoint_get_help(__in Z3_context c, __in Z3_fixedpoint f)
Return a string describing all fixedpoint available parameters.
def update_rule(self, head, body, name)
void Z3_API Z3_inc_ref(__in Z3_context c, __in Z3_ast a)
Increment the reference counter of the given AST. The context c should have been created using Z3_mk_...
def get_cover_delta(self, level, predicate)
void Z3_API Z3_apply_result_inc_ref(__in Z3_context c, __in Z3_apply_result r)
Increment the reference counter of the given Z3_apply_result object.
def __getitem__(self, idx)
Z3_tactic Z3_API Z3_mk_tactic(__in Z3_context c, __in Z3_string name)
Return a tactic associated with the given name. The complete list of tactics may be obtained using th...
Z3_string Z3_API Z3_tactic_get_help(__in Z3_context c, __in Z3_tactic t)
Return a string containing a description of parameters accepted by the given tactic.
Z3_string Z3_API Z3_get_tactic_name(__in Z3_context c, unsigned i)
Return the name of the idx tactic.
Z3_ast Z3_API Z3_mk_bound(__in Z3_context c, __in unsigned index, __in Z3_sort ty)
Create a bound variable.
Z3_symbol Z3_API Z3_get_decl_name(__in Z3_context c, __in Z3_func_decl d)
Return the constant declaration name as a symbol.
Z3_probe Z3_API Z3_probe_eq(__in Z3_context x, __in Z3_probe p1, __in Z3_probe p2)
Return a probe that evaluates to "true" when the value returned by p1 is equal to the value returned ...
void Z3_API Z3_fixedpoint_update_rule(__in Z3_context c, __in Z3_fixedpoint d, __in Z3_ast a, __in Z3_symbol name)
Update a named rule. A rule with the same name must have been previously created. ...
Z3_ast Z3_API Z3_substitute_vars(__in Z3_context c, __in Z3_ast a, __in unsigned num_exprs, __in_ecount(num_exprs) Z3_ast const to[])
Substitute the free variables in a with the expressions in to. For every i smaller than num_exprs...
Z3_tactic Z3_API Z3_tactic_fail_if(__in Z3_context c, __in Z3_probe p)
Return a tactic that fails if the probe p evaluates to false.
unsigned Z3_API Z3_get_app_num_args(__in Z3_context c, __in Z3_app a)
Return the number of argument of an application. If t is an constant, then the number of arguments is...
def substitute_vars(t, m)
Z3_ast_vector Z3_API Z3_fixedpoint_get_assertions(__in Z3_context c, __in Z3_fixedpoint f)
Retrieve set of background assertions from fixedpoint context.
Z3_bool Z3_API Z3_global_param_get(__in Z3_string param_id, __out Z3_string_ptr param_value)
Get a global (or module) parameter.
def declare(self, name, args)
void Z3_API Z3_enable_trace(__in Z3_string tag)
Enable tracing messages tagged as tag when Z3 is compiled in debug mode. It is a NOOP otherwise...
def set_option(args, kws)
Z3_apply_result Z3_API Z3_tactic_apply(__in Z3_context c, __in Z3_tactic t, __in Z3_goal g)
Apply tactic t to the goal g.
Z3_model Z3_API Z3_apply_result_convert_model(__in Z3_context c, __in Z3_apply_result r, __in unsigned i, __in Z3_model m)
Convert a model for the subgoal Z3_apply_result_get_subgoal(c, r, i) into a model for the original go...
Z3_bool Z3_API Z3_open_log(__in Z3_string filename)
Log interaction to a file.
Z3_symbol Z3_API Z3_mk_string_symbol(__in Z3_context c, __in Z3_string s)
Create a Z3 symbol using a C string.
unsigned Z3_API Z3_get_num_tactics(__in Z3_context c)
Return the number of builtin tactics available in Z3.
Z3_ast_vector Z3_API Z3_fixedpoint_from_string(__in Z3_context c, __in Z3_fixedpoint f, __in Z3_string s)
Parse an SMT-LIB2 string with fixedpoint rules. Add the rules to the current fixedpoint context...
Z3_sort Z3_API Z3_get_range(__in Z3_context c, __in Z3_func_decl d)
Return the range of the given declaration.
def assert_exprs(self, args)
Z3_probe Z3_API Z3_probe_gt(__in Z3_context x, __in Z3_probe p1, __in Z3_probe p2)
Return a probe that evaluates to "true" when the value returned by p1 is greater than the value retur...
Z3_ast Z3_API Z3_substitute(__in Z3_context c, __in Z3_ast a, __in unsigned num_exprs, __in_ecount(num_exprs) Z3_ast const from[], __in_ecount(num_exprs) Z3_ast const to[])
Substitute every occurrence of from[i] in a with to[i], for i smaller than num_exprs. The result is the new AST. The arrays from and to must have size num_exprs. For every i smaller than num_exprs, we must have that sort of from[i] must be equal to sort of to[i].
Z3_bool Z3_API Z3_is_eq_ast(__in Z3_context c, __in Z3_ast t1, Z3_ast t2)
compare terms.
def register_relation(self, relations)
void Z3_API Z3_global_param_set(__in Z3_string param_id, __in Z3_string param_value)
Set a global (or module) parameter. This setting is shared by all Z3 contexts.
void Z3_API Z3_set_ast_print_mode(__in Z3_context c, __in Z3_ast_print_mode mode)
Select mode for the format used for pretty-printing AST nodes.
Z3_string Z3_API Z3_apply_result_to_string(__in Z3_context c, __in Z3_apply_result r)
Convert the Z3_apply_result object returned by Z3_tactic_apply into a string.
Z3_ast_vector Z3_API Z3_fixedpoint_get_rules(__in Z3_context c, __in Z3_fixedpoint f)
Retrieve set of rules from fixedpoint context.
void Z3_API Z3_apply_result_dec_ref(__in Z3_context c, __in Z3_apply_result r)
Decrement the reference counter of the given Z3_apply_result object.
Z3_sort Z3_API Z3_mk_uninterpreted_sort(__in Z3_context c, __in Z3_symbol s)
Create a free (uninterpreted) type using the given name (symbol).
Z3_tactic Z3_API Z3_tactic_repeat(__in Z3_context c, __in Z3_tactic t, unsigned max)
Return a tactic that keeps applying t until the goal is not modified anymore or the maximum number of...
Z3_ast_kind Z3_API Z3_get_ast_kind(__in Z3_context c, __in Z3_ast a)
Return the kind of the given AST.
void Z3_API Z3_fixedpoint_pop(Z3_context c, Z3_fixedpoint d)
Backtrack one backtracking point.
Z3_apply_result Z3_API Z3_tactic_apply_ex(Z3_context c, Z3_tactic t, Z3_goal g, Z3_params p)
Apply tactic t to the goal g using the parameter set p.
void Z3_API Z3_append_log(__in Z3_string string)
Append user-defined string to interaction log.
void Z3_API Z3_get_version(__out unsigned *major, __out unsigned *minor, __out unsigned *build_number, __out unsigned *revision_number)
Return Z3 version number information.
Z3_goal Z3_API Z3_apply_result_get_subgoal(__in Z3_context c, __in Z3_apply_result r, __in unsigned i)
Return one of the subgoals in the Z3_apply_result object returned by Z3_tactic_apply.
Z3_decl_kind Z3_API Z3_get_decl_kind(__in Z3_context c, __in Z3_func_decl d)
Return declaration kind corresponding to declaration.
Z3_tactic Z3_API Z3_tactic_try_for(__in Z3_context c, __in Z3_tactic t, __in unsigned ms)
Return a tactic that applies t to a given goal for ms milliseconds. If t does not terminate in ms mil...
def Array(name, dom, rng)
Z3_tactic Z3_API Z3_tactic_using_params(__in Z3_context c, __in Z3_tactic t, __in Z3_params p)
Return a tactic that applies t using the given set of parameters.
void Z3_API Z3_dec_ref(__in Z3_context c, __in Z3_ast a)
Decrement the reference counter of the given AST. The context c should have been created using Z3_mk_...
Z3_tactic Z3_API Z3_tactic_and_then(__in Z3_context c, __in Z3_tactic t1, __in Z3_tactic t2)
Return a tactic that applies t1 to a given goal and t2 to every subgoal produced by t1...
void Z3_API Z3_fixedpoint_push(Z3_context c, Z3_fixedpoint d)
Create a backtracking point.
Z3_ast Z3_API Z3_simplify(__in Z3_context c, __in Z3_ast a)
Interface to simplifier.
def simplify_param_descrs()
Z3_solver Z3_API Z3_mk_solver_for_logic(__in Z3_context c, __in Z3_symbol logic)
Create a new solver customized for the given logic. It behaves like Z3_mk_solver if the logic is unkn...
void Z3_API Z3_probe_inc_ref(__in Z3_context c, __in Z3_probe p)
Increment the reference counter of the given probe.
Z3_string Z3_API Z3_fixedpoint_to_string(__in Z3_context c, __in Z3_fixedpoint f, __in unsigned num_queries, __in_ecount(num_queries) Z3_ast queries[])
Print the current rules and background axioms as a string.
Z3_probe Z3_API Z3_probe_not(__in Z3_context x, __in Z3_probe p)
Return a probe that evaluates to "true" when p does not evaluate to true.
Z3_symbol_kind Z3_API Z3_get_symbol_kind(__in Z3_context c, __in Z3_symbol s)
Return Z3_INT_SYMBOL if the symbol was constructed using Z3_mk_int_symbol, and Z3_STRING_SYMBOL if th...
Z3_lbool Z3_API Z3_fixedpoint_query(__in Z3_context c, __in Z3_fixedpoint d, __in Z3_ast query)
Pose a query against the asserted rules.
Z3_lbool Z3_API Z3_fixedpoint_query_relations(__in Z3_context c, __in Z3_fixedpoint d, __in unsigned num_relations, __in_ecount(num_relations) Z3_func_decl const relations[])
Pose multiple queries against the asserted rules.
def __init__(self, args, kws)
Z3_ast Z3_API Z3_mk_ite(__in Z3_context c, __in Z3_ast t1, __in Z3_ast t2, __in Z3_ast t3)
Create an AST node representing an if-then-else: ite(t1, t2, t3).
Z3_tactic Z3_API Z3_tactic_cond(__in Z3_context c, __in Z3_probe p, __in Z3_tactic t1, __in Z3_tactic t2)
Return a tactic that applies t1 to a given goal if the probe p evaluates to true, and t2 if p evaluat...
Z3_string Z3_API Z3_probe_get_descr(__in Z3_context c, __in Z3_string name)
Return a string containing a description of the probe with the given name.
def Extract(high, low, a)
void Z3_API Z3_fixedpoint_inc_ref(__in Z3_context c, __in Z3_fixedpoint d)
Increment the reference counter of the given fixedpoint context.
def solve_using(s, args, keywords)
def set_predicate_representation(self, f, representations)
void Z3_API Z3_fixedpoint_add_rule(__in Z3_context c, __in Z3_fixedpoint d, __in Z3_ast rule, __in Z3_symbol name)
Add a universal Horn clause as a named rule. The horn_rule should be of the form: ...
unsigned Z3_API Z3_get_arity(__in Z3_context c, __in Z3_func_decl d)
Alias for Z3_get_domain_size.
int Z3_API Z3_get_symbol_int(__in Z3_context c, __in Z3_symbol s)
Return the symbol int value.
unsigned Z3_API Z3_get_ast_id(__in Z3_context c, Z3_ast t)
Return a unique identifier for t.
unsigned Z3_API Z3_get_ast_hash(__in Z3_context c, __in Z3_ast a)
Return a hash code for the given AST.
Z3_ast_vector Z3_API Z3_fixedpoint_from_file(__in Z3_context c, __in Z3_fixedpoint f, __in Z3_string s)
Parse an SMT-LIB2 file with fixedpoint rules. Add the rules to the current fixedpoint context...
def apply(self, goal, arguments, keywords)
def solve(args, keywords)
def check(self, assumptions)
Z3_string Z3_API Z3_ast_to_string(__in Z3_context c, __in Z3_ast a)
Convert the given AST node into a string.
def __call__(self, goal, arguments, keywords)
Z3_solver Z3_API Z3_mk_simple_solver(__in Z3_context c)
Create a new (incremental) solver.
Z3_probe Z3_API Z3_probe_const(__in Z3_context x, __in double val)
Return a probe that always evaluates to val.
def prove(claim, keywords)
Z3_string Z3_API Z3_tactic_get_descr(__in Z3_context c, __in Z3_string name)
Return a string containing a description of the tactic with the given name.
void Z3_API Z3_set_error_handler(__in Z3_context c, __in Z3_error_handler h)
Register a Z3 error handler.
Z3_stats Z3_API Z3_fixedpoint_get_statistics(__in Z3_context c, __in Z3_fixedpoint d)
Retrieve statistics information from the last call to Z3_fixedpoint_query.
Z3_string Z3_API Z3_simplify_get_help(__in Z3_context c)
Return a string describing all available parameters.
Z3_string Z3_API Z3_get_probe_name(__in Z3_context c, unsigned i)
Return the name of the i probe.
void Z3_API Z3_disable_trace(__in Z3_string tag)
Disable tracing messages tagged as tag when Z3 is compiled in debug mode. It is a NOOP otherwise...
Z3_string Z3_API Z3_benchmark_to_smtlib_string(__in Z3_context c, __in Z3_string name, __in Z3_string logic, __in Z3_string status, __in Z3_string attributes, __in unsigned num_assumptions, __in_ecount(num_assumptions) Z3_ast const assumptions[], __in Z3_ast formula)
Convert the given benchmark into SMT-LIB formatted string.
Z3_probe Z3_API Z3_probe_ge(__in Z3_context x, __in Z3_probe p1, __in Z3_probe p2)
Return a probe that evaluates to "true" when the value returned by p1 is greater than or equal to the...
void Z3_API Z3_del_config(__in Z3_config c)
Delete the given configuration object.
void Z3_API Z3_fixedpoint_dec_ref(__in Z3_context c, __in Z3_fixedpoint d)
Decrement the reference counter of the given fixedpoint context.
Z3_ast Z3_API Z3_sort_to_ast(__in Z3_context c, __in Z3_sort s)
Convert a Z3_sort into Z3_ast. This is just type casting.
Z3_sort Z3_API Z3_get_sort(__in Z3_context c, __in Z3_ast a)
Return the sort of an AST node.
Z3_param_descrs Z3_API Z3_tactic_get_param_descrs(__in Z3_context c, __in Z3_tactic t)
Return the parameter description set for the given tactic object.
def translate(self, target)
Z3_func_decl Z3_API Z3_get_app_decl(__in Z3_context c, __in Z3_app a)
Return the declaration of a constant or function application.
unsigned Z3_API Z3_fixedpoint_get_num_levels(Z3_context c, Z3_fixedpoint d, Z3_func_decl pred)
Query the PDR engine for the maximal levels properties are known about predicate. ...
void Z3_API Z3_fixedpoint_add_cover(Z3_context c, Z3_fixedpoint d, int level, Z3_func_decl pred, Z3_ast property)
Add property about the predicate pred. Add a property of predicate pred at level. It gets pushed forw...
unsigned Z3_API Z3_get_num_probes(__in Z3_context c)
Return the number of builtin probes available in Z3.
Z3_sort Z3_API Z3_get_domain(__in Z3_context c, __in Z3_func_decl d, __in unsigned i)
Return the sort of the i-th parameter of the given function declaration.
Z3_config Z3_API Z3_mk_config(void)
Create a configuration object for the Z3 context object.
void Z3_API Z3_tactic_dec_ref(__in Z3_context c, __in Z3_tactic g)
Decrement the reference counter of the given tactic.
def to_string(self, queries)
Z3_ast Z3_API Z3_mk_distinct(__in Z3_context c, __in unsigned num_args, __in_ecount(num_args) Z3_ast const args[])
Create an AST node representing distinct(args[0], ..., args[num_args-1]).The distinct construct is us...
void Z3_API Z3_interrupt(__in Z3_context c)
Interrupt the execution of a Z3 procedure. This procedure can be used to interrupt: solvers...
Z3_symbol Z3_API Z3_get_sort_name(__in Z3_context c, __in Z3_sort d)
Return the sort name as a symbol.
def parse_string(self, s)
Z3_tactic Z3_API Z3_tactic_when(__in Z3_context c, __in Z3_probe p, __in Z3_tactic t)
Return a tactic that applies t to a given goal is the probe p evaluates to true. If p evaluates to fa...
Z3_probe Z3_API Z3_probe_le(__in Z3_context x, __in Z3_probe p1, __in Z3_probe p2)
Return a probe that evaluates to "true" when the value returned by p1 is less than or equal to the va...
def declare_var(self, vars)
unsigned Z3_API Z3_get_index_value(__in Z3_context c, __in Z3_ast a)
Return index of de-Brujin bound variable.
Z3_solver Z3_API Z3_mk_solver_from_tactic(__in Z3_context c, __in Z3_tactic t)
Create a new solver that is implemented using the given tactic. The solver supports the commands Z3_s...
void Z3_API Z3_fixedpoint_register_relation(__in Z3_context c, __in Z3_fixedpoint d, __in Z3_func_decl f)
Register relation as Fixedpoint defined. Fixedpoint defined relations have least-fixedpoint semantics...
Z3_ast Z3_API Z3_func_decl_to_ast(__in Z3_context c, __in Z3_func_decl f)
Convert a Z3_func_decl into Z3_ast. This is just type casting.
Z3_probe Z3_API Z3_probe_lt(__in Z3_context x, __in Z3_probe p1, __in Z3_probe p2)
Return a probe that evaluates to "true" when the value returned by p1 is less than the value returned...
Z3_fixedpoint Z3_API Z3_mk_fixedpoint(__in Z3_context c)
Create a new fixedpoint context.
Z3_ast Z3_API Z3_fixedpoint_get_cover_delta(Z3_context c, Z3_fixedpoint d, int level, Z3_func_decl pred)
Z3_string Z3_API Z3_fixedpoint_get_reason_unknown(__in Z3_context c, __in Z3_fixedpoint d)
Retrieve a string that describes the last status returned by Z3_fixedpoint_query. ...
Z3_string Z3_API Z3_get_symbol_string(__in Z3_context c, __in Z3_symbol s)
Return the symbol name.
void Z3_API Z3_probe_dec_ref(__in Z3_context c, __in Z3_probe p)
Decrement the reference counter of the given probe.
Z3_ast Z3_API Z3_mk_eq(__in Z3_context c, __in Z3_ast l, __in Z3_ast r)
Create an AST node representing l = r.
Z3_tactic Z3_API Z3_tactic_par_or(__in Z3_context c, __in unsigned num, __in_ecount(num) Z3_tactic const ts[])
Return a tactic that applies the given tactics in parallel.
void Z3_API Z3_global_param_reset_all(void)
Restore the value of all global (and module) parameters. This command will not affect already created...
def get_num_levels(self, predicate)
void Z3_API Z3_fixedpoint_set_predicate_representation(__in Z3_context c, __in Z3_fixedpoint d, __in Z3_func_decl f, __in unsigned num_relations, __in_ecount(num_relations) Z3_symbol const relation_kinds[])
Configure the predicate representation.
Z3_sort_kind Z3_API Z3_get_sort_kind(__in Z3_context c, __in Z3_sort t)
Return the sort kind (e.g., array, tuple, int, bool, etc).
Z3_ast Z3_API Z3_mk_app(__in Z3_context c, __in Z3_func_decl d, __in unsigned num_args, __in_ecount(num_args) Z3_ast const args[])
Create a constant or function application.
Z3_func_decl Z3_API Z3_mk_func_decl(__in Z3_context c, __in Z3_symbol s, __in unsigned domain_size, __in_ecount(domain_size) Z3_sort const domain[], __in Z3_sort range)
Declare a constant or function.
def __init__(self, result, ctx)
def set(self, args, keys)
def add_cover(self, level, predicate, property)
Z3_tactic Z3_API Z3_tactic_or_else(__in Z3_context c, __in Z3_tactic t1, __in Z3_tactic t2)
Return a tactic that first applies t1 to a given goal, if it fails then returns the result of t2 appl...
void Z3_API Z3_fixedpoint_set_params(__in Z3_context c, __in Z3_fixedpoint f, __in Z3_params p)
Set parameters on fixedpoint context.
tuple _error_handler_fptr
Z3_symbol Z3_API Z3_mk_int_symbol(__in Z3_context c, __in int i)
Create a Z3 symbol using an integer.
Z3_ast Z3_API Z3_mk_add(__in Z3_context c, __in unsigned num_args, __in_ecount(num_args) Z3_ast const args[])
Create an AST node representing args[0] + ... + args[num_args-1].The array args must have num_args el...
Z3_ast Z3_API Z3_get_app_arg(__in Z3_context c, __in Z3_app a, __in unsigned i)
Return the i-th argument of the given application.
void Z3_API Z3_set_param_value(__in Z3_config c, __in Z3_string param_id, __in Z3_string param_value)
Set a configuration parameter.
Z3_probe Z3_API Z3_mk_probe(__in Z3_context c, __in Z3_string name)
Return a probe associated with the given name. The complete list of probes may be obtained using the ...
void Z3_API Z3_tactic_inc_ref(__in Z3_context c, __in Z3_tactic t)
Increment the reference counter of the given tactic.
Z3_param_descrs Z3_API Z3_fixedpoint_get_param_descrs(__in Z3_context c, __in Z3_fixedpoint f)
Return the parameter description set for the given fixedpoint object.
double Z3_API Z3_probe_apply(__in Z3_context c, __in Z3_probe p, __in Z3_goal g)
Execute the probe over the goal. The probe always produce a double value. "Boolean" probes return 0...
Z3_ast Z3_API Z3_simplify_ex(__in Z3_context c, __in Z3_ast a, __in Z3_params p)
Interface to simplifier.
Z3_param_descrs Z3_API Z3_simplify_get_param_descrs(__in Z3_context c)
Return the parameter description set for the simplify procedure.
unsigned Z3_API Z3_apply_result_get_num_subgoals(__in Z3_context c, __in Z3_apply_result r)
Return the number of subgoals in the Z3_apply_result object returned by Z3_tactic_apply.
def is_algebraic_value(a)
Z3_bool Z3_API Z3_is_eq_sort(__in Z3_context c, __in Z3_sort s1, __in Z3_sort s2)
compare sorts.
Z3_ast Z3_API Z3_fixedpoint_get_answer(__in Z3_context c, __in Z3_fixedpoint d)
Retrieve a formula that encodes satisfying answers to the query.