9 """Z3 is a high performance theorem prover developed at Microsoft Research.
11 Z3 is used in many applications such as: software/hardware verification and testing,
12 constraint solving, analysis of hybrid systems, security, biology (in silico analysis),
13 and geometrical problems.
15 Several online tutorials for Z3Py are available at:
16 http://rise4fun.com/Z3Py/tutorial/guide
18 Please send feedback, comments and/or corrections on the Issue tracker for
19 https://github.com/Z3prover/z3.git. Your comments are very valuable.
40 ... x = BitVec('x', 32)
42 ... # the expression x + y is type incorrect
44 ... except Z3Exception as ex:
45 ... print("failed: %s" % ex)
50 from .z3types
import *
51 from .z3consts
import *
52 from .z3printer
import *
53 from fractions
import Fraction
58 if sys.version_info.major >= 3:
59 from typing
import Iterable
69 if sys.version_info.major < 3:
71 return isinstance(v, (int, long))
74 return isinstance(v, int)
86 major = ctypes.c_uint(0)
87 minor = ctypes.c_uint(0)
88 build = ctypes.c_uint(0)
89 rev = ctypes.c_uint(0)
91 return "%s.%s.%s" % (major.value, minor.value, build.value)
95 major = ctypes.c_uint(0)
96 minor = ctypes.c_uint(0)
97 build = ctypes.c_uint(0)
98 rev = ctypes.c_uint(0)
100 return (major.value, minor.value, build.value, rev.value)
110 def _z3_assert(cond, msg):
112 raise Z3Exception(msg)
115 def _z3_check_cint_overflow(n, name):
116 _z3_assert(ctypes.c_int(n).value == n, name +
" is too large")
120 """Log interaction to a file. This function must be invoked immediately after init(). """
125 """Append user-defined string to interaction log. """
130 """Convert an integer or string into a Z3 symbol."""
137 def _symbol2py(ctx, s):
138 """Convert a Z3 symbol back into a Python object. """
151 if len(args) == 1
and (isinstance(args[0], tuple)
or isinstance(args[0], list)):
153 elif len(args) == 1
and (isinstance(args[0], set)
or isinstance(args[0], AstVector)):
154 return [arg
for arg
in args[0]]
163 def _get_args_ast_list(args):
165 if isinstance(args, (set, AstVector, tuple)):
166 return [arg
for arg
in args]
173 def _to_param_value(val):
174 if isinstance(val, bool):
175 return "true" if val
else "false"
186 """A Context manages all other Z3 objects, global configuration options, etc.
188 Z3Py uses a default global context. For most applications this is sufficient.
189 An application may use multiple Z3 contexts. Objects created in one context
190 cannot be used in another one. However, several objects may be "translated" from
191 one context to another. It is not safe to access Z3 objects from multiple threads.
192 The only exception is the method `interrupt()` that can be used to interrupt() a long
194 The initialization method receives global configuration options for the new context.
199 _z3_assert(len(args) % 2 == 0,
"Argument list must have an even number of elements.")
222 """Return a reference to the actual C pointer to the Z3 context."""
226 """Interrupt a solver performing a satisfiability test, a tactic processing a goal, or simplify functions.
228 This method can be invoked from a thread different from the one executing the
229 interruptible procedure.
239 """Return a reference to the global Z3 context.
242 >>> x.ctx == main_ctx()
247 >>> x2 = Real('x', c)
254 if _main_ctx
is None:
271 """Set Z3 global (or module) parameters.
273 >>> set_param(precision=10)
276 _z3_assert(len(args) % 2 == 0,
"Argument list must have an even number of elements.")
280 if not set_pp_option(k, v):
295 """Reset all global (or module) parameters.
301 """Alias for 'set_param' for backward compatibility.
307 """Return the value of a Z3 global (or module) parameter
309 >>> get_param('nlsat.reorder')
312 ptr = (ctypes.c_char_p * 1)()
314 r = z3core._to_pystr(ptr[0])
316 raise Z3Exception(
"failed to retrieve value for '%s'" % name)
328 """Superclass for all Z3 objects that have support for pretty printing."""
333 def _repr_html_(self):
334 in_html = in_html_mode()
337 set_html_mode(in_html)
342 """AST are Direct Acyclic Graphs (DAGs) used to represent sorts, declarations and expressions."""
350 if self.ctx.ref()
is not None and self.
ast is not None:
355 return _to_ast_ref(self.
ast, self.
ctx)
358 return obj_to_string(self)
361 return obj_to_string(self)
364 return self.
eq(other)
377 elif is_eq(self)
and self.num_args() == 2:
378 return self.arg(0).
eq(self.arg(1))
380 raise Z3Exception(
"Symbolic expressions cannot be cast to concrete Boolean values.")
383 """Return a string representing the AST node in s-expression notation.
386 >>> ((x + 1)*x).sexpr()
392 """Return a pointer to the corresponding C Z3_ast object."""
396 """Return unique identifier for object. It can be used for hash-tables and maps."""
400 """Return a reference to the C context where this AST node is stored."""
401 return self.ctx.ref()
404 """Return `True` if `self` and `other` are structurally identical.
411 >>> n1 = simplify(n1)
412 >>> n2 = simplify(n2)
417 _z3_assert(
is_ast(other),
"Z3 AST expected")
421 """Translate `self` to the context `target`. That is, return a copy of `self` in the context `target`.
427 >>> # Nodes in different contexts can't be mixed.
428 >>> # However, we can translate nodes from one context to another.
429 >>> x.translate(c2) + y
433 _z3_assert(isinstance(target, Context),
"argument must be a Z3 context")
440 """Return a hashcode for the `self`.
442 >>> n1 = simplify(Int('x') + 1)
443 >>> n2 = simplify(2 + Int('x') - 1)
444 >>> n1.hash() == n2.hash()
451 """Return `True` if `a` is an AST node.
455 >>> is_ast(IntVal(10))
459 >>> is_ast(BoolSort())
461 >>> is_ast(Function('f', IntSort(), IntSort()))
468 return isinstance(a, AstRef)
472 """Return `True` if `a` and `b` are structurally identical AST nodes.
482 >>> eq(simplify(x + 1), simplify(1 + x))
490 def _ast_kind(ctx, a):
496 def _ctx_from_ast_arg_list(args, default_ctx=None):
504 _z3_assert(ctx == a.ctx,
"Context mismatch")
510 def _ctx_from_ast_args(*args):
511 return _ctx_from_ast_arg_list(args)
514 def _to_func_decl_array(args):
516 _args = (FuncDecl * sz)()
518 _args[i] = args[i].as_func_decl()
522 def _to_ast_array(args):
526 _args[i] = args[i].as_ast()
530 def _to_ref_array(ref, args):
534 _args[i] = args[i].as_ast()
538 def _to_ast_ref(a, ctx):
539 k = _ast_kind(ctx, a)
541 return _to_sort_ref(a, ctx)
542 elif k == Z3_FUNC_DECL_AST:
543 return _to_func_decl_ref(a, ctx)
545 return _to_expr_ref(a, ctx)
554 def _sort_kind(ctx, s):
559 """A Sort is essentially a type. Every Z3 expression has a sort. A sort is an AST node."""
568 """Return the Z3 internal kind of a sort.
569 This method can be used to test if `self` is one of the Z3 builtin sorts.
572 >>> b.kind() == Z3_BOOL_SORT
574 >>> b.kind() == Z3_INT_SORT
576 >>> A = ArraySort(IntSort(), IntSort())
577 >>> A.kind() == Z3_ARRAY_SORT
579 >>> A.kind() == Z3_INT_SORT
582 return _sort_kind(self.
ctx, self.
ast)
585 """Return `True` if `self` is a subsort of `other`.
587 >>> IntSort().subsort(RealSort())
593 """Try to cast `val` as an element of sort `self`.
595 This method is used in Z3Py to convert Python objects such as integers,
596 floats, longs and strings into Z3 expressions.
599 >>> RealSort().cast(x)
603 _z3_assert(
is_expr(val),
"Z3 expression expected")
604 _z3_assert(self.
eq(val.sort()),
"Sort mismatch")
608 """Return the name (string) of sort `self`.
610 >>> BoolSort().name()
612 >>> ArraySort(IntSort(), IntSort()).name()
618 """Return `True` if `self` and `other` are the same Z3 sort.
621 >>> p.sort() == BoolSort()
623 >>> p.sort() == IntSort()
631 """Return `True` if `self` and `other` are not the same Z3 sort.
634 >>> p.sort() != BoolSort()
636 >>> p.sort() != IntSort()
643 return AstRef.__hash__(self)
647 """Return `True` if `s` is a Z3 sort.
649 >>> is_sort(IntSort())
651 >>> is_sort(Int('x'))
653 >>> is_expr(Int('x'))
656 return isinstance(s, SortRef)
659 def _to_sort_ref(s, ctx):
661 _z3_assert(isinstance(s, Sort),
"Z3 Sort expected")
662 k = _sort_kind(ctx, s)
663 if k == Z3_BOOL_SORT:
665 elif k == Z3_INT_SORT
or k == Z3_REAL_SORT:
667 elif k == Z3_BV_SORT:
669 elif k == Z3_ARRAY_SORT:
671 elif k == Z3_DATATYPE_SORT:
673 elif k == Z3_FINITE_DOMAIN_SORT:
675 elif k == Z3_FLOATING_POINT_SORT:
677 elif k == Z3_ROUNDING_MODE_SORT:
679 elif k == Z3_RE_SORT:
681 elif k == Z3_SEQ_SORT:
683 elif k == Z3_CHAR_SORT:
689 return _to_sort_ref(
Z3_get_sort(ctx.ref(), a), ctx)
693 """Create a new uninterpreted sort named `name`.
695 If `ctx=None`, then the new sort is declared in the global Z3Py context.
697 >>> A = DeclareSort('A')
698 >>> a = Const('a', A)
699 >>> b = Const('b', A)
718 """Function declaration. Every constant and function have an associated declaration.
720 The declaration assigns a name, a sort (i.e., type), and for function
721 the sort (i.e., type) of each of its arguments. Note that, in Z3,
722 a constant is a function with 0 arguments.
735 """Return the name of the function declaration `self`.
737 >>> f = Function('f', IntSort(), IntSort())
740 >>> isinstance(f.name(), str)
746 """Return the number of arguments of a function declaration.
747 If `self` is a constant, then `self.arity()` is 0.
749 >>> f = Function('f', IntSort(), RealSort(), BoolSort())
756 """Return the sort of the argument `i` of a function declaration.
757 This method assumes that `0 <= i < self.arity()`.
759 >>> f = Function('f', IntSort(), RealSort(), BoolSort())
766 _z3_assert(i < self.
arity(),
"Index out of bounds")
770 """Return the sort of the range of a function declaration.
771 For constants, this is the sort of the constant.
773 >>> f = Function('f', IntSort(), RealSort(), BoolSort())
780 """Return the internal kind of a function declaration.
781 It can be used to identify Z3 built-in functions such as addition, multiplication, etc.
784 >>> d = (x + 1).decl()
785 >>> d.kind() == Z3_OP_ADD
787 >>> d.kind() == Z3_OP_MUL
795 result = [
None for i
in range(n)]
798 if k == Z3_PARAMETER_INT:
800 elif k == Z3_PARAMETER_DOUBLE:
802 elif k == Z3_PARAMETER_RATIONAL:
804 elif k == Z3_PARAMETER_SYMBOL:
806 elif k == Z3_PARAMETER_SORT:
808 elif k == Z3_PARAMETER_AST:
810 elif k == Z3_PARAMETER_FUNC_DECL:
817 """Create a Z3 application expression using the function `self`, and the given arguments.
819 The arguments must be Z3 expressions. This method assumes that
820 the sorts of the elements in `args` match the sorts of the
821 domain. Limited coercion is supported. For example, if
822 args[0] is a Python integer, and the function expects a Z3
823 integer, then the argument is automatically converted into a
826 >>> f = Function('f', IntSort(), RealSort(), BoolSort())
834 args = _get_args(args)
837 _z3_assert(num == self.
arity(),
"Incorrect number of arguments to %s" % self)
838 _args = (Ast * num)()
843 tmp = self.
domain(i).cast(args[i])
845 _args[i] = tmp.as_ast()
850 """Return `True` if `a` is a Z3 function declaration.
852 >>> f = Function('f', IntSort(), IntSort())
859 return isinstance(a, FuncDeclRef)
863 """Create a new Z3 uninterpreted function with the given sorts.
865 >>> f = Function('f', IntSort(), IntSort())
871 _z3_assert(len(sig) > 0,
"At least two arguments expected")
875 _z3_assert(
is_sort(rng),
"Z3 sort expected")
876 dom = (Sort * arity)()
877 for i
in range(arity):
879 _z3_assert(
is_sort(sig[i]),
"Z3 sort expected")
886 """Create a new fresh Z3 uninterpreted function with the given sorts.
890 _z3_assert(len(sig) > 0,
"At least two arguments expected")
894 _z3_assert(
is_sort(rng),
"Z3 sort expected")
895 dom = (z3.Sort * arity)()
896 for i
in range(arity):
898 _z3_assert(
is_sort(sig[i]),
"Z3 sort expected")
904 def _to_func_decl_ref(a, ctx):
909 """Create a new Z3 recursive with the given sorts."""
912 _z3_assert(len(sig) > 0,
"At least two arguments expected")
916 _z3_assert(
is_sort(rng),
"Z3 sort expected")
917 dom = (Sort * arity)()
918 for i
in range(arity):
920 _z3_assert(
is_sort(sig[i]),
"Z3 sort expected")
927 """Set the body of a recursive function.
928 Recursive definitions can be simplified if they are applied to ground
931 >>> fac = RecFunction('fac', IntSort(ctx), IntSort(ctx))
932 >>> n = Int('n', ctx)
933 >>> RecAddDefinition(fac, n, If(n == 0, 1, n*fac(n-1)))
936 >>> s = Solver(ctx=ctx)
937 >>> s.add(fac(n) < 3)
940 >>> s.model().eval(fac(5))
946 args = _get_args(args)
950 _args[i] = args[i].ast
961 """Constraints, formulas and terms are expressions in Z3.
963 Expressions are ASTs. Every expression has a sort.
964 There are three main kinds of expressions:
965 function applications, quantifiers and bounded variables.
966 A constant is a function application with 0 arguments.
967 For quantifier free problems, all expressions are
968 function applications.
978 """Return the sort of expression `self`.
990 """Shorthand for `self.sort().kind()`.
992 >>> a = Array('a', IntSort(), IntSort())
993 >>> a.sort_kind() == Z3_ARRAY_SORT
995 >>> a.sort_kind() == Z3_INT_SORT
998 return self.
sort().kind()
1001 """Return a Z3 expression that represents the constraint `self == other`.
1003 If `other` is `None`, then this method simply returns `False`.
1014 a, b = _coerce_exprs(self, other)
1019 return AstRef.__hash__(self)
1022 """Return a Z3 expression that represents the constraint `self != other`.
1024 If `other` is `None`, then this method simply returns `True`.
1035 a, b = _coerce_exprs(self, other)
1036 _args, sz = _to_ast_array((a, b))
1043 """Return the Z3 function declaration associated with a Z3 application.
1045 >>> f = Function('f', IntSort(), IntSort())
1054 _z3_assert(
is_app(self),
"Z3 application expected")
1058 """Return the number of arguments of a Z3 application.
1062 >>> (a + b).num_args()
1064 >>> f = Function('f', IntSort(), IntSort(), IntSort(), IntSort())
1070 _z3_assert(
is_app(self),
"Z3 application expected")
1074 """Return argument `idx` of the application `self`.
1076 This method assumes that `self` is a function application with at least `idx+1` arguments.
1080 >>> f = Function('f', IntSort(), IntSort(), IntSort(), IntSort())
1090 _z3_assert(
is_app(self),
"Z3 application expected")
1091 _z3_assert(idx < self.
num_args(),
"Invalid argument index")
1095 """Return a list containing the children of the given expression
1099 >>> f = Function('f', IntSort(), IntSort(), IntSort(), IntSort())
1110 def _to_expr_ref(a, ctx):
1111 if isinstance(a, Pattern):
1115 if k == Z3_QUANTIFIER_AST:
1118 if sk == Z3_BOOL_SORT:
1120 if sk == Z3_INT_SORT:
1121 if k == Z3_NUMERAL_AST:
1124 if sk == Z3_REAL_SORT:
1125 if k == Z3_NUMERAL_AST:
1127 if _is_algebraic(ctx, a):
1130 if sk == Z3_BV_SORT:
1131 if k == Z3_NUMERAL_AST:
1135 if sk == Z3_ARRAY_SORT:
1137 if sk == Z3_DATATYPE_SORT:
1139 if sk == Z3_FLOATING_POINT_SORT:
1140 if k == Z3_APP_AST
and _is_numeral(ctx, a):
1143 return FPRef(a, ctx)
1144 if sk == Z3_FINITE_DOMAIN_SORT:
1145 if k == Z3_NUMERAL_AST:
1149 if sk == Z3_ROUNDING_MODE_SORT:
1151 if sk == Z3_SEQ_SORT:
1153 if sk == Z3_RE_SORT:
1154 return ReRef(a, ctx)
1158 def _coerce_expr_merge(s, a):
1171 _z3_assert(s1.ctx == s.ctx,
"context mismatch")
1172 _z3_assert(
False,
"sort mismatch")
1177 def _coerce_exprs(a, b, ctx=None):
1179 a = _py2expr(a, ctx)
1180 b = _py2expr(b, ctx)
1181 if isinstance(a, str)
and isinstance(b, SeqRef):
1183 if isinstance(b, str)
and isinstance(a, SeqRef):
1186 s = _coerce_expr_merge(s, a)
1187 s = _coerce_expr_merge(s, b)
1193 def _reduce(func, sequence, initial):
1195 for element
in sequence:
1196 result = func(result, element)
1200 def _coerce_expr_list(alist, ctx=None):
1207 alist = [_py2expr(a, ctx)
for a
in alist]
1208 s = _reduce(_coerce_expr_merge, alist,
None)
1209 return [s.cast(a)
for a
in alist]
1213 """Return `True` if `a` is a Z3 expression.
1220 >>> is_expr(IntSort())
1224 >>> is_expr(IntVal(1))
1227 >>> is_expr(ForAll(x, x >= 0))
1229 >>> is_expr(FPVal(1.0))
1232 return isinstance(a, ExprRef)
1236 """Return `True` if `a` is a Z3 function application.
1238 Note that, constants are function applications with 0 arguments.
1245 >>> is_app(IntSort())
1249 >>> is_app(IntVal(1))
1252 >>> is_app(ForAll(x, x >= 0))
1255 if not isinstance(a, ExprRef):
1257 k = _ast_kind(a.ctx, a)
1258 return k == Z3_NUMERAL_AST
or k == Z3_APP_AST
1262 """Return `True` if `a` is Z3 constant/variable expression.
1271 >>> is_const(IntVal(1))
1274 >>> is_const(ForAll(x, x >= 0))
1277 return is_app(a)
and a.num_args() == 0
1281 """Return `True` if `a` is variable.
1283 Z3 uses de-Bruijn indices for representing bound variables in
1291 >>> f = Function('f', IntSort(), IntSort())
1292 >>> # Z3 replaces x with bound variables when ForAll is executed.
1293 >>> q = ForAll(x, f(x) == x)
1299 >>> is_var(b.arg(1))
1302 return is_expr(a)
and _ast_kind(a.ctx, a) == Z3_VAR_AST
1306 """Return the de-Bruijn index of the Z3 bounded variable `a`.
1314 >>> f = Function('f', IntSort(), IntSort(), IntSort())
1315 >>> # Z3 replaces x and y with bound variables when ForAll is executed.
1316 >>> q = ForAll([x, y], f(x, y) == x + y)
1318 f(Var(1), Var(0)) == Var(1) + Var(0)
1322 >>> v1 = b.arg(0).arg(0)
1323 >>> v2 = b.arg(0).arg(1)
1328 >>> get_var_index(v1)
1330 >>> get_var_index(v2)
1334 _z3_assert(
is_var(a),
"Z3 bound variable expected")
1339 """Return `True` if `a` is an application of the given kind `k`.
1343 >>> is_app_of(n, Z3_OP_ADD)
1345 >>> is_app_of(n, Z3_OP_MUL)
1348 return is_app(a)
and a.decl().kind() == k
1351 def If(a, b, c, ctx=None):
1352 """Create a Z3 if-then-else expression.
1356 >>> max = If(x > y, x, y)
1362 if isinstance(a, Probe)
or isinstance(b, Tactic)
or isinstance(c, Tactic):
1363 return Cond(a, b, c, ctx)
1365 ctx = _get_ctx(_ctx_from_ast_arg_list([a, b, c], ctx))
1368 b, c = _coerce_exprs(b, c, ctx)
1370 _z3_assert(a.ctx == b.ctx,
"Context mismatch")
1371 return _to_expr_ref(
Z3_mk_ite(ctx.ref(), a.as_ast(), b.as_ast(), c.as_ast()), ctx)
1375 """Create a Z3 distinct expression.
1382 >>> Distinct(x, y, z)
1384 >>> simplify(Distinct(x, y, z))
1386 >>> simplify(Distinct(x, y, z), blast_distinct=True)
1387 And(Not(x == y), Not(x == z), Not(y == z))
1389 args = _get_args(args)
1390 ctx = _ctx_from_ast_arg_list(args)
1392 _z3_assert(ctx
is not None,
"At least one of the arguments must be a Z3 expression")
1393 args = _coerce_expr_list(args, ctx)
1394 _args, sz = _to_ast_array(args)
1398 def _mk_bin(f, a, b):
1401 _z3_assert(a.ctx == b.ctx,
"Context mismatch")
1402 args[0] = a.as_ast()
1403 args[1] = b.as_ast()
1404 return f(a.ctx.ref(), 2, args)
1408 """Create a constant of the given sort.
1410 >>> Const('x', IntSort())
1414 _z3_assert(isinstance(sort, SortRef),
"Z3 sort expected")
1420 """Create several constants of the given sort.
1422 `names` is a string containing the names of all constants to be created.
1423 Blank spaces separate the names of different constants.
1425 >>> x, y, z = Consts('x y z', IntSort())
1429 if isinstance(names, str):
1430 names = names.split(
" ")
1431 return [
Const(name, sort)
for name
in names]
1435 """Create a fresh constant of a specified sort"""
1436 ctx = _get_ctx(sort.ctx)
1441 """Create a Z3 free variable. Free variables are used to create quantified formulas.
1443 >>> Var(0, IntSort())
1445 >>> eq(Var(0, IntSort()), Var(0, BoolSort()))
1449 _z3_assert(
is_sort(s),
"Z3 sort expected")
1450 return _to_expr_ref(
Z3_mk_bound(s.ctx_ref(), idx, s.ast), s.ctx)
1455 Create a real free variable. Free variables are used to create quantified formulas.
1456 They are also used to create polynomials.
1466 Create a list of Real free variables.
1467 The variables have ids: 0, 1, ..., n-1
1469 >>> x0, x1, x2, x3 = RealVarVector(4)
1486 """Try to cast `val` as a Boolean.
1488 >>> x = BoolSort().cast(True)
1498 if isinstance(val, bool):
1502 msg =
"True, False or Z3 Boolean expression expected. Received %s of type %s"
1503 _z3_assert(
is_expr(val), msg % (val, type(val)))
1504 if not self.
eq(val.sort()):
1505 _z3_assert(self.
eq(val.sort()),
"Value cannot be converted into a Z3 Boolean value")
1509 return isinstance(other, ArithSortRef)
1519 """All Boolean expressions are instances of this class."""
1528 """Create the Z3 expression `self * other`.
1534 return If(self, other, 0)
1538 """Return `True` if `a` is a Z3 Boolean expression.
1544 >>> is_bool(And(p, q))
1552 return isinstance(a, BoolRef)
1556 """Return `True` if `a` is the Z3 true expression.
1561 >>> is_true(simplify(p == p))
1566 >>> # True is a Python Boolean expression
1574 """Return `True` if `a` is the Z3 false expression.
1581 >>> is_false(BoolVal(False))
1588 """Return `True` if `a` is a Z3 and expression.
1590 >>> p, q = Bools('p q')
1591 >>> is_and(And(p, q))
1593 >>> is_and(Or(p, q))
1600 """Return `True` if `a` is a Z3 or expression.
1602 >>> p, q = Bools('p q')
1605 >>> is_or(And(p, q))
1612 """Return `True` if `a` is a Z3 implication expression.
1614 >>> p, q = Bools('p q')
1615 >>> is_implies(Implies(p, q))
1617 >>> is_implies(And(p, q))
1624 """Return `True` if `a` is a Z3 not expression.
1636 """Return `True` if `a` is a Z3 equality expression.
1638 >>> x, y = Ints('x y')
1646 """Return `True` if `a` is a Z3 distinct expression.
1648 >>> x, y, z = Ints('x y z')
1649 >>> is_distinct(x == y)
1651 >>> is_distinct(Distinct(x, y, z))
1658 """Return the Boolean Z3 sort. If `ctx=None`, then the global context is used.
1662 >>> p = Const('p', BoolSort())
1665 >>> r = Function('r', IntSort(), IntSort(), BoolSort())
1672 return BoolSortRef(Z3_mk_bool_sort(ctx.ref()), ctx)
1675 def BoolVal(val, ctx=None):
1676 """Return the Boolean value `
True`
or `
False`. If `ctx=
None`, then the
global context
is used.
1689 return BoolRef(Z3_mk_true(ctx.ref()), ctx)
1691 return BoolRef(Z3_mk_false(ctx.ref()), ctx)
1694 def Bool(name, ctx=None):
1695 """Return a Boolean constant named `name`. If `ctx=
None`, then the
global context
is used.
1703 return BoolRef(Z3_mk_const(ctx.ref(), to_symbol(name, ctx), BoolSort(ctx).ast), ctx)
1706 def Bools(names, ctx=None):
1707 """Return a tuple of Boolean constants.
1709 `names`
is a single string containing all names separated by blank spaces.
1710 If `ctx=
None`, then the
global context
is used.
1712 >>> p, q, r =
Bools(
'p q r')
1713 >>>
And(p,
Or(q, r))
1717 if isinstance(names, str):
1718 names = names.split(" ")
1719 return [Bool(name, ctx) for name in names]
1722 def BoolVector(prefix, sz, ctx=None):
1723 """Return a list of Boolean constants of size `sz`.
1725 The constants are named using the given prefix.
1726 If `ctx=
None`, then the
global context
is used.
1732 And(p__0, p__1, p__2)
1734 return [Bool("%s__%s" % (prefix, i)) for i in range(sz)]
1737 def FreshBool(prefix="b", ctx=None):
1738 """Return a fresh Boolean constant
in the given context using the given prefix.
1740 If `ctx=
None`, then the
global context
is used.
1748 return BoolRef(Z3_mk_fresh_const(ctx.ref(), prefix, BoolSort(ctx).ast), ctx)
1751 def Implies(a, b, ctx=None):
1752 """Create a Z3 implies expression.
1754 >>> p, q =
Bools(
'p q')
1758 ctx = _get_ctx(_ctx_from_ast_arg_list([a, b], ctx))
1762 return BoolRef(Z3_mk_implies(ctx.ref(), a.as_ast(), b.as_ast()), ctx)
1765 def Xor(a, b, ctx=None):
1766 """Create a Z3 Xor expression.
1768 >>> p, q =
Bools(
'p q')
1774 ctx = _get_ctx(_ctx_from_ast_arg_list([a, b], ctx))
1778 return BoolRef(Z3_mk_xor(ctx.ref(), a.as_ast(), b.as_ast()), ctx)
1781 def Not(a, ctx=None):
1782 """Create a Z3
not expression
or probe.
1790 ctx = _get_ctx(_ctx_from_ast_arg_list([a], ctx))
1792 # Not is also used to build probes
1793 return Probe(Z3_probe_not(ctx.ref(), a.probe), ctx)
1797 return BoolRef(Z3_mk_not(ctx.ref(), a.as_ast()), ctx)
1807 def _has_probe(args):
1808 """Return `
True`
if one of the elements of the given collection
is a Z3 probe.
"""
1816 """Create a Z3
and-expression
or and-probe.
1818 >>> p, q, r =
Bools(
'p q r')
1823 And(p__0, p__1, p__2, p__3, p__4)
1827 last_arg = args[len(args) - 1]
1828 if isinstance(last_arg, Context):
1829 ctx = args[len(args) - 1]
1830 args = args[:len(args) - 1]
1831 elif len(args) == 1 and isinstance(args[0], AstVector):
1833 args = [a for a in args[0]]
1836 args = _get_args(args)
1837 ctx = _get_ctx(_ctx_from_ast_arg_list(args, ctx))
1839 _z3_assert(ctx is not None, "At least one of the arguments must be a Z3 expression or probe")
1840 if _has_probe(args):
1841 return _probe_and(args, ctx)
1843 args = _coerce_expr_list(args, ctx)
1844 _args, sz = _to_ast_array(args)
1845 return BoolRef(Z3_mk_and(ctx.ref(), sz, _args), ctx)
1849 """Create a Z3
or-expression
or or-probe.
1851 >>> p, q, r =
Bools(
'p q r')
1856 Or(p__0, p__1, p__2, p__3, p__4)
1860 last_arg = args[len(args) - 1]
1861 if isinstance(last_arg, Context):
1862 ctx = args[len(args) - 1]
1863 args = args[:len(args) - 1]
1864 elif len(args) == 1 and isinstance(args[0], AstVector):
1866 args = [a for a in args[0]]
1869 args = _get_args(args)
1870 ctx = _get_ctx(_ctx_from_ast_arg_list(args, ctx))
1872 _z3_assert(ctx is not None, "At least one of the arguments must be a Z3 expression or probe")
1873 if _has_probe(args):
1874 return _probe_or(args, ctx)
1876 args = _coerce_expr_list(args, ctx)
1877 _args, sz = _to_ast_array(args)
1878 return BoolRef(Z3_mk_or(ctx.ref(), sz, _args), ctx)
1880 #########################################
1884 #########################################
1887 class PatternRef(ExprRef):
1888 """Patterns are hints
for quantifier instantiation.
1893 return Z3_pattern_to_ast(self.ctx_ref(), self.ast)
1896 return Z3_get_ast_id(self.ctx_ref(), self.as_ast())
1900 """Return `
True`
if `a`
is a Z3 pattern (hint
for quantifier instantiation.
1904 >>> q =
ForAll(x, f(x) == 0, patterns = [ f(x) ])
1907 >>> q.num_patterns()
1914 return isinstance(a, PatternRef)
1917 def MultiPattern(*args):
1918 """Create a Z3 multi-pattern using the given expressions `*args`
1926 >>> q.num_patterns()
1934 _z3_assert(len(args) > 0, "At least one argument expected")
1935 _z3_assert(all([is_expr(a) for a in args]), "Z3 expressions expected")
1937 args, sz = _to_ast_array(args)
1938 return PatternRef(Z3_mk_pattern(ctx.ref(), sz, args), ctx)
1941 def _to_pattern(arg):
1945 return MultiPattern(arg)
1947 #########################################
1951 #########################################
1954 class QuantifierRef(BoolRef):
1955 """Universally
and Existentially quantified formulas.
"""
1961 return Z3_get_ast_id(self.ctx_ref(), self.as_ast())
1964 """Return the Boolean sort
or sort of Lambda.
"""
1965 if self.is_lambda():
1966 return _sort(self.ctx, self.as_ast())
1967 return BoolSort(self.ctx)
1969 def is_forall(self):
1970 """Return `
True`
if `self`
is a universal quantifier.
1974 >>> q =
ForAll(x, f(x) == 0)
1977 >>> q =
Exists(x, f(x) != 0)
1981 return Z3_is_quantifier_forall(self.ctx_ref(), self.ast)
1983 def is_exists(self):
1984 """Return `
True`
if `self`
is an existential quantifier.
1988 >>> q =
ForAll(x, f(x) == 0)
1991 >>> q =
Exists(x, f(x) != 0)
1995 return Z3_is_quantifier_exists(self.ctx_ref(), self.ast)
1997 def is_lambda(self):
1998 """Return `
True`
if `self`
is a
lambda expression.
2005 >>> q =
Exists(x, f(x) != 0)
2009 return Z3_is_lambda(self.ctx_ref(), self.ast)
2011 def __getitem__(self, arg):
2012 """Return the Z3 expression `self[arg]`.
2015 _z3_assert(self.is_lambda(), "quantifier should be a lambda expression")
2016 arg = self.sort().domain().cast(arg)
2017 return _to_expr_ref(Z3_mk_select(self.ctx_ref(), self.as_ast(), arg.as_ast()), self.ctx)
2020 """Return the weight annotation of `self`.
2024 >>> q =
ForAll(x, f(x) == 0)
2027 >>> q =
ForAll(x, f(x) == 0, weight=10)
2031 return int(Z3_get_quantifier_weight(self.ctx_ref(), self.ast))
2033 def num_patterns(self):
2034 """Return the number of patterns (i.e., quantifier instantiation hints)
in `self`.
2039 >>> q =
ForAll(x, f(x) != g(x), patterns = [ f(x), g(x) ])
2040 >>> q.num_patterns()
2043 return int(Z3_get_quantifier_num_patterns(self.ctx_ref(), self.ast))
2045 def pattern(self, idx):
2046 """Return a pattern (i.e., quantifier instantiation hints)
in `self`.
2051 >>> q =
ForAll(x, f(x) != g(x), patterns = [ f(x), g(x) ])
2052 >>> q.num_patterns()
2060 _z3_assert(idx < self.num_patterns(), "Invalid pattern idx")
2061 return PatternRef(Z3_get_quantifier_pattern_ast(self.ctx_ref(), self.ast, idx), self.ctx)
2063 def num_no_patterns(self):
2064 """Return the number of no-patterns.
"""
2065 return Z3_get_quantifier_num_no_patterns(self.ctx_ref(), self.ast)
2067 def no_pattern(self, idx):
2068 """Return a no-pattern.
"""
2070 _z3_assert(idx < self.num_no_patterns(), "Invalid no-pattern idx")
2071 return _to_expr_ref(Z3_get_quantifier_no_pattern_ast(self.ctx_ref(), self.ast, idx), self.ctx)
2074 """Return the expression being quantified.
2078 >>> q =
ForAll(x, f(x) == 0)
2082 return _to_expr_ref(Z3_get_quantifier_body(self.ctx_ref(), self.ast), self.ctx)
2085 """Return the number of variables bounded by this quantifier.
2090 >>> q =
ForAll([x, y], f(x, y) >= x)
2094 return int(Z3_get_quantifier_num_bound(self.ctx_ref(), self.ast))
2096 def var_name(self, idx):
2097 """Return a string representing a name used when displaying the quantifier.
2102 >>> q =
ForAll([x, y], f(x, y) >= x)
2109 _z3_assert(idx < self.num_vars(), "Invalid variable idx")
2110 return _symbol2py(self.ctx, Z3_get_quantifier_bound_name(self.ctx_ref(), self.ast, idx))
2112 def var_sort(self, idx):
2113 """Return the sort of a bound variable.
2118 >>> q =
ForAll([x, y], f(x, y) >= x)
2125 _z3_assert(idx < self.num_vars(), "Invalid variable idx")
2126 return _to_sort_ref(Z3_get_quantifier_bound_sort(self.ctx_ref(), self.ast, idx), self.ctx)
2129 """Return a list containing a single element self.
body()
2133 >>> q =
ForAll(x, f(x) == 0)
2137 return [self.body()]
2140 def is_quantifier(a):
2141 """Return `
True`
if `a`
is a Z3 quantifier.
2145 >>> q =
ForAll(x, f(x) == 0)
2151 return isinstance(a, QuantifierRef)
2154 def _mk_quantifier(is_forall, vs, body, weight=1, qid="", skid="", patterns=[], no_patterns=[]):
2156 _z3_assert(is_bool(body) or is_app(vs) or (len(vs) > 0 and is_app(vs[0])), "Z3 expression expected")
2157 _z3_assert(is_const(vs) or (len(vs) > 0 and all([is_const(v) for v in vs])), "Invalid bounded variable(s)")
2158 _z3_assert(all([is_pattern(a) or is_expr(a) for a in patterns]), "Z3 patterns expected")
2159 _z3_assert(all([is_expr(p) for p in no_patterns]), "no patterns are Z3 expressions")
2165 if not is_expr(body):
2166 body = BoolVal(body, ctx)
2170 _vs = (Ast * num_vars)()
2171 for i in range(num_vars):
2172 # TODO: Check if is constant
2173 _vs[i] = vs[i].as_ast()
2174 patterns = [_to_pattern(p) for p in patterns]
2175 num_pats = len(patterns)
2176 _pats = (Pattern * num_pats)()
2177 for i in range(num_pats):
2178 _pats[i] = patterns[i].ast
2179 _no_pats, num_no_pats = _to_ast_array(no_patterns)
2180 qid = to_symbol(qid, ctx)
2181 skid = to_symbol(skid, ctx)
2182 return QuantifierRef(Z3_mk_quantifier_const_ex(ctx.ref(), is_forall, weight, qid, skid,
2185 num_no_pats, _no_pats,
2186 body.as_ast()), ctx)
2189 def ForAll(vs, body, weight=1, qid="", skid="", patterns=[], no_patterns=[]):
2190 """Create a Z3 forall formula.
2192 The parameters `weight`, `qid`, `skid`, `patterns`
and `no_patterns` are optional annotations.
2197 >>>
ForAll([x, y], f(x, y) >= x)
2198 ForAll([x, y], f(x, y) >= x)
2199 >>>
ForAll([x, y], f(x, y) >= x, patterns=[ f(x, y) ])
2200 ForAll([x, y], f(x, y) >= x)
2201 >>>
ForAll([x, y], f(x, y) >= x, weight=10)
2202 ForAll([x, y], f(x, y) >= x)
2204 return _mk_quantifier(True, vs, body, weight, qid, skid, patterns, no_patterns)
2207 def Exists(vs, body, weight=1, qid="", skid="", patterns=[], no_patterns=[]):
2208 """Create a Z3 exists formula.
2210 The parameters `weight`, `qif`, `skid`, `patterns`
and `no_patterns` are optional annotations.
2216 >>> q =
Exists([x, y], f(x, y) >= x, skid=
"foo")
2218 Exists([x, y], f(x, y) >= x)
2221 >>> r =
Tactic(
'nnf')(q).as_expr()
2225 return _mk_quantifier(False, vs, body, weight, qid, skid, patterns, no_patterns)
2228 def Lambda(vs, body):
2229 """Create a Z3
lambda expression.
2233 >>> lo, hi, e, i =
Ints(
'lo hi e i')
2234 >>> mem1 =
Lambda([i],
If(
And(lo <= i, i <= hi), e, mem0[i]))
2242 _vs = (Ast * num_vars)()
2243 for i in range(num_vars):
2244 # TODO: Check if is constant
2245 _vs[i] = vs[i].as_ast()
2246 return QuantifierRef(Z3_mk_lambda_const(ctx.ref(), num_vars, _vs, body.as_ast()), ctx)
2248 #########################################
2252 #########################################
2255 class ArithSortRef(SortRef):
2256 """Real
and Integer sorts.
"""
2259 """Return `
True`
if `self`
is of the sort Real.
2270 return self.kind() == Z3_REAL_SORT
2273 """Return `
True`
if `self`
is of the sort Integer.
2284 return self.kind() == Z3_INT_SORT
2286 def subsort(self, other):
2287 """Return `
True`
if `self`
is a subsort of `other`.
"""
2288 return self.is_int() and is_arith_sort(other) and other.is_real()
2290 def cast(self, val):
2291 """Try to cast `val`
as an Integer
or Real.
2306 _z3_assert(self.ctx == val.ctx, "Context mismatch")
2310 if val_s.is_int() and self.is_real():
2312 if val_s.is_bool() and self.is_int():
2313 return If(val, 1, 0)
2314 if val_s.is_bool() and self.is_real():
2315 return ToReal(If(val, 1, 0))
2317 _z3_assert(False, "Z3 Integer/Real expression expected")
2320 return IntVal(val, self.ctx)
2322 return RealVal(val, self.ctx)
2324 msg = "int, long, float, string (numeral), or Z3 Integer/Real expression expected. Got %s"
2325 _z3_assert(False, msg % self)
2328 def is_arith_sort(s):
2329 """Return `
True`
if s
is an arithmetical sort (type).
2337 >>> n =
Int(
'x') + 1
2341 return isinstance(s, ArithSortRef)
2344 class ArithRef(ExprRef):
2345 """Integer
and Real expressions.
"""
2348 """Return the sort (type) of the arithmetical expression `self`.
2355 return ArithSortRef(Z3_get_sort(self.ctx_ref(), self.as_ast()), self.ctx)
2358 """Return `
True`
if `self`
is an integer expression.
2369 return self.sort().is_int()
2372 """Return `
True`
if `self`
is an real expression.
2380 return self.sort().is_real()
2382 def __add__(self, other):
2383 """Create the Z3 expression `self + other`.
2392 a, b = _coerce_exprs(self, other)
2393 return ArithRef(_mk_bin(Z3_mk_add, a, b), self.ctx)
2395 def __radd__(self, other):
2396 """Create the Z3 expression `other + self`.
2402 a, b = _coerce_exprs(self, other)
2403 return ArithRef(_mk_bin(Z3_mk_add, b, a), self.ctx)
2405 def __mul__(self, other):
2406 """Create the Z3 expression `self * other`.
2415 if isinstance(other, BoolRef):
2416 return If(other, self, 0)
2417 a, b = _coerce_exprs(self, other)
2418 return ArithRef(_mk_bin(Z3_mk_mul, a, b), self.ctx)
2420 def __rmul__(self, other):
2421 """Create the Z3 expression `other * self`.
2427 a, b = _coerce_exprs(self, other)
2428 return ArithRef(_mk_bin(Z3_mk_mul, b, a), self.ctx)
2430 def __sub__(self, other):
2431 """Create the Z3 expression `self - other`.
2440 a, b = _coerce_exprs(self, other)
2441 return ArithRef(_mk_bin(Z3_mk_sub, a, b), self.ctx)
2443 def __rsub__(self, other):
2444 """Create the Z3 expression `other - self`.
2450 a, b = _coerce_exprs(self, other)
2451 return ArithRef(_mk_bin(Z3_mk_sub, b, a), self.ctx)
2453 def __pow__(self, other):
2454 """Create the Z3 expression `self**other` (**
is the power operator).
2464 a, b = _coerce_exprs(self, other)
2465 return ArithRef(Z3_mk_power(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
2467 def __rpow__(self, other):
2468 """Create the Z3 expression `other**self` (**
is the power operator).
2478 a, b = _coerce_exprs(self, other)
2479 return ArithRef(Z3_mk_power(self.ctx_ref(), b.as_ast(), a.as_ast()), self.ctx)
2481 def __div__(self, other):
2482 """Create the Z3 expression `other/self`.
2501 a, b = _coerce_exprs(self, other)
2502 return ArithRef(Z3_mk_div(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
2504 def __truediv__(self, other):
2505 """Create the Z3 expression `other/self`.
"""
2506 return self.__div__(other)
2508 def __rdiv__(self, other):
2509 """Create the Z3 expression `other/self`.
2522 a, b = _coerce_exprs(self, other)
2523 return ArithRef(Z3_mk_div(self.ctx_ref(), b.as_ast(), a.as_ast()), self.ctx)
2525 def __rtruediv__(self, other):
2526 """Create the Z3 expression `other/self`.
"""
2527 return self.__rdiv__(other)
2529 def __mod__(self, other):
2530 """Create the Z3 expression `other%self`.
2539 a, b = _coerce_exprs(self, other)
2541 _z3_assert(a.is_int(), "Z3 integer expression expected")
2542 return ArithRef(Z3_mk_mod(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
2544 def __rmod__(self, other):
2545 """Create the Z3 expression `other%self`.
2551 a, b = _coerce_exprs(self, other)
2553 _z3_assert(a.is_int(), "Z3 integer expression expected")
2554 return ArithRef(Z3_mk_mod(self.ctx_ref(), b.as_ast(), a.as_ast()), self.ctx)
2557 """Return an expression representing `-self`.
2565 return ArithRef(Z3_mk_unary_minus(self.ctx_ref(), self.as_ast()), self.ctx)
2576 def __le__(self, other):
2577 """Create the Z3 expression `other <= self`.
2579 >>> x, y =
Ints(
'x y')
2586 a, b = _coerce_exprs(self, other)
2587 return BoolRef(Z3_mk_le(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
2589 def __lt__(self, other):
2590 """Create the Z3 expression `other < self`.
2592 >>> x, y =
Ints(
'x y')
2599 a, b = _coerce_exprs(self, other)
2600 return BoolRef(Z3_mk_lt(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
2602 def __gt__(self, other):
2603 """Create the Z3 expression `other > self`.
2605 >>> x, y =
Ints(
'x y')
2612 a, b = _coerce_exprs(self, other)
2613 return BoolRef(Z3_mk_gt(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
2615 def __ge__(self, other):
2616 """Create the Z3 expression `other >= self`.
2618 >>> x, y =
Ints(
'x y')
2625 a, b = _coerce_exprs(self, other)
2626 return BoolRef(Z3_mk_ge(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
2630 """Return `
True`
if `a`
is an arithmetical expression.
2647 return isinstance(a, ArithRef)
2651 """Return `
True`
if `a`
is an integer expression.
2666 return is_arith(a) and a.is_int()
2670 """Return `
True`
if `a`
is a real expression.
2685 return is_arith(a) and a.is_real()
2688 def _is_numeral(ctx, a):
2689 return Z3_is_numeral_ast(ctx.ref(), a)
2692 def _is_algebraic(ctx, a):
2693 return Z3_is_algebraic_number(ctx.ref(), a)
2696 def is_int_value(a):
2697 """Return `
True`
if `a`
is an integer value of sort Int.
2705 >>> n =
Int(
'x') + 1
2717 return is_arith(a) and a.is_int() and _is_numeral(a.ctx, a.as_ast())
2720 def is_rational_value(a):
2721 """Return `
True`
if `a`
is rational value of sort Real.
2731 >>> n =
Real(
'x') + 1
2739 return is_arith(a) and a.is_real() and _is_numeral(a.ctx, a.as_ast())
2742 def is_algebraic_value(a):
2743 """Return `
True`
if `a`
is an algebraic value of sort Real.
2753 return is_arith(a) and a.is_real() and _is_algebraic(a.ctx, a.as_ast())
2757 """Return `
True`
if `a`
is an expression of the form b + c.
2759 >>> x, y =
Ints(
'x y')
2765 return is_app_of(a, Z3_OP_ADD)
2769 """Return `
True`
if `a`
is an expression of the form b * c.
2771 >>> x, y =
Ints(
'x y')
2777 return is_app_of(a, Z3_OP_MUL)
2781 """Return `
True`
if `a`
is an expression of the form b - c.
2783 >>> x, y =
Ints(
'x y')
2789 return is_app_of(a, Z3_OP_SUB)
2793 """Return `
True`
if `a`
is an expression of the form b / c.
2795 >>> x, y =
Reals(
'x y')
2800 >>> x, y =
Ints(
'x y')
2806 return is_app_of(a, Z3_OP_DIV)
2810 """Return `
True`
if `a`
is an expression of the form b div c.
2812 >>> x, y =
Ints(
'x y')
2818 return is_app_of(a, Z3_OP_IDIV)
2822 """Return `
True`
if `a`
is an expression of the form b % c.
2824 >>> x, y =
Ints(
'x y')
2830 return is_app_of(a, Z3_OP_MOD)
2834 """Return `
True`
if `a`
is an expression of the form b <= c.
2836 >>> x, y =
Ints(
'x y')
2842 return is_app_of(a, Z3_OP_LE)
2846 """Return `
True`
if `a`
is an expression of the form b < c.
2848 >>> x, y =
Ints(
'x y')
2854 return is_app_of(a, Z3_OP_LT)
2858 """Return `
True`
if `a`
is an expression of the form b >= c.
2860 >>> x, y =
Ints(
'x y')
2866 return is_app_of(a, Z3_OP_GE)
2870 """Return `
True`
if `a`
is an expression of the form b > c.
2872 >>> x, y =
Ints(
'x y')
2878 return is_app_of(a, Z3_OP_GT)
2882 """Return `
True`
if `a`
is an expression of the form
IsInt(b).
2890 return is_app_of(a, Z3_OP_IS_INT)
2894 """Return `
True`
if `a`
is an expression of the form
ToReal(b).
2905 return is_app_of(a, Z3_OP_TO_REAL)
2909 """Return `
True`
if `a`
is an expression of the form
ToInt(b).
2920 return is_app_of(a, Z3_OP_TO_INT)
2923 class IntNumRef(ArithRef):
2924 """Integer values.
"""
2927 """Return a Z3 integer numeral
as a Python long (bignum) numeral.
2936 _z3_assert(self.is_int(), "Integer value expected")
2937 return int(self.as_string())
2939 def as_string(self):
2940 """Return a Z3 integer numeral
as a Python string.
2945 return Z3_get_numeral_string(self.ctx_ref(), self.as_ast())
2947 def as_binary_string(self):
2948 """Return a Z3 integer numeral
as a Python binary string.
2950 >>> v.as_binary_string()
2953 return Z3_get_numeral_binary_string(self.ctx_ref(), self.as_ast())
2956 class RatNumRef(ArithRef):
2957 """Rational values.
"""
2959 def numerator(self):
2960 """ Return the numerator of a Z3 rational numeral.
2972 return IntNumRef(Z3_get_numerator(self.ctx_ref(), self.as_ast()), self.ctx)
2974 def denominator(self):
2975 """ Return the denominator of a Z3 rational numeral.
2983 return IntNumRef(Z3_get_denominator(self.ctx_ref(), self.as_ast()), self.ctx)
2985 def numerator_as_long(self):
2986 """ Return the numerator
as a Python long.
2993 >>> v.numerator_as_long() + 1 == 10000000001
2996 return self.numerator().as_long()
2998 def denominator_as_long(self):
2999 """ Return the denominator
as a Python long.
3004 >>> v.denominator_as_long()
3007 return self.denominator().as_long()
3015 def is_int_value(self):
3016 return self.denominator().is_int() and self.denominator_as_long() == 1
3019 _z3_assert(self.is_int_value(), "Expected integer fraction")
3020 return self.numerator_as_long()
3022 def as_decimal(self, prec):
3023 """ Return a Z3 rational value
as a string
in decimal notation using at most `prec` decimal places.
3032 return Z3_get_numeral_decimal_string(self.ctx_ref(), self.as_ast(), prec)
3034 def as_string(self):
3035 """Return a Z3 rational numeral
as a Python string.
3041 return Z3_get_numeral_string(self.ctx_ref(), self.as_ast())
3043 def as_fraction(self):
3044 """Return a Z3 rational
as a Python Fraction object.
3050 return Fraction(self.numerator_as_long(), self.denominator_as_long())
3053 class AlgebraicNumRef(ArithRef):
3054 """Algebraic irrational values.
"""
3056 def approx(self, precision=10):
3057 """Return a Z3 rational number that approximates the algebraic number `self`.
3058 The result `r`
is such that |r - self| <= 1/10^precision
3062 6838717160008073720548335/4835703278458516698824704
3066 return RatNumRef(Z3_get_algebraic_number_upper(self.ctx_ref(), self.as_ast(), precision), self.ctx)
3068 def as_decimal(self, prec):
3069 """Return a string representation of the algebraic number `self`
in decimal notation
3070 using `prec` decimal places.
3073 >>> x.as_decimal(10)
3075 >>> x.as_decimal(20)
3076 '1.41421356237309504880?'
3078 return Z3_get_numeral_decimal_string(self.ctx_ref(), self.as_ast(), prec)
3081 return AstVector(Z3_algebraic_get_poly(self.ctx_ref(), self.as_ast()), self.ctx)
3084 return Z3_algebraic_get_i(self.ctx_ref(), self.as_ast())
3087 def _py2expr(a, ctx=None):
3088 if isinstance(a, bool):
3089 return BoolVal(a, ctx)
3091 return IntVal(a, ctx)
3092 if isinstance(a, float):
3093 return RealVal(a, ctx)
3094 if isinstance(a, str):
3095 return StringVal(a, ctx)
3099 _z3_assert(False, "Python bool, int, long or float expected")
3102 def IntSort(ctx=None):
3103 """Return the integer sort
in the given context. If `ctx=
None`, then the
global context
is used.
3116 return ArithSortRef(Z3_mk_int_sort(ctx.ref()), ctx)
3119 def RealSort(ctx=None):
3120 """Return the real sort
in the given context. If `ctx=
None`, then the
global context
is used.
3133 return ArithSortRef(Z3_mk_real_sort(ctx.ref()), ctx)
3136 def _to_int_str(val):
3137 if isinstance(val, float):
3138 return str(int(val))
3139 elif isinstance(val, bool):
3146 elif isinstance(val, str):
3149 _z3_assert(False, "Python value cannot be used as a Z3 integer")
3152 def IntVal(val, ctx=None):
3153 """Return a Z3 integer value. If `ctx=
None`, then the
global context
is used.
3161 return IntNumRef(Z3_mk_numeral(ctx.ref(), _to_int_str(val), IntSort(ctx).ast), ctx)
3164 def RealVal(val, ctx=None):
3165 """Return a Z3 real value.
3167 `val` may be a Python int, long, float
or string representing a number
in decimal
or rational notation.
3168 If `ctx=
None`, then the
global context
is used.
3180 return RatNumRef(Z3_mk_numeral(ctx.ref(), str(val), RealSort(ctx).ast), ctx)
3183 def RatVal(a, b, ctx=None):
3184 """Return a Z3 rational a/b.
3186 If `ctx=
None`, then the
global context
is used.
3194 _z3_assert(_is_int(a) or isinstance(a, str), "First argument cannot be converted into an integer")
3195 _z3_assert(_is_int(b) or isinstance(b, str), "Second argument cannot be converted into an integer")
3196 return simplify(RealVal(a, ctx) / RealVal(b, ctx))
3199 def Q(a, b, ctx=None):
3200 """Return a Z3 rational a/b.
3202 If `ctx=
None`, then the
global context
is used.
3209 return simplify(RatVal(a, b, ctx=ctx))
3212 def Int(name, ctx=None):
3213 """Return an integer constant named `name`. If `ctx=
None`, then the
global context
is used.
3222 return ArithRef(Z3_mk_const(ctx.ref(), to_symbol(name, ctx), IntSort(ctx).ast), ctx)
3225 def Ints(names, ctx=None):
3226 """Return a tuple of Integer constants.
3228 >>> x, y, z =
Ints(
'x y z')
3233 if isinstance(names, str):
3234 names = names.split(" ")
3235 return [Int(name, ctx) for name in names]
3238 def IntVector(prefix, sz, ctx=None):
3239 """Return a list of integer constants of size `sz`.
3248 return [Int("%s__%s" % (prefix, i), ctx) for i in range(sz)]
3251 def FreshInt(prefix="x", ctx=None):
3252 """Return a fresh integer constant
in the given context using the given prefix.
3262 return ArithRef(Z3_mk_fresh_const(ctx.ref(), prefix, IntSort(ctx).ast), ctx)
3265 def Real(name, ctx=None):
3266 """Return a real constant named `name`. If `ctx=
None`, then the
global context
is used.
3275 return ArithRef(Z3_mk_const(ctx.ref(), to_symbol(name, ctx), RealSort(ctx).ast), ctx)
3278 def Reals(names, ctx=None):
3279 """Return a tuple of real constants.
3281 >>> x, y, z =
Reals(
'x y z')
3284 >>>
Sum(x, y, z).sort()
3288 if isinstance(names, str):
3289 names = names.split(" ")
3290 return [Real(name, ctx) for name in names]
3293 def RealVector(prefix, sz, ctx=None):
3294 """Return a list of real constants of size `sz`.
3305 return [Real("%s__%s" % (prefix, i), ctx) for i in range(sz)]
3308 def FreshReal(prefix="b", ctx=None):
3309 """Return a fresh real constant
in the given context using the given prefix.
3319 return ArithRef(Z3_mk_fresh_const(ctx.ref(), prefix, RealSort(ctx).ast), ctx)
3323 """ Return the Z3 expression
ToReal(a).
3335 _z3_assert(a.is_int(), "Z3 integer expression expected.")
3337 return ArithRef(Z3_mk_int2real(ctx.ref(), a.as_ast()), ctx)
3341 """ Return the Z3 expression
ToInt(a).
3353 _z3_assert(a.is_real(), "Z3 real expression expected.")
3355 return ArithRef(Z3_mk_real2int(ctx.ref(), a.as_ast()), ctx)
3359 """ Return the Z3 predicate
IsInt(a).
3362 >>>
IsInt(x +
"1/2")
3366 >>>
solve(
IsInt(x +
"1/2"), x > 0, x < 1, x !=
"1/2")
3370 _z3_assert(a.is_real(), "Z3 real expression expected.")
3372 return BoolRef(Z3_mk_is_int(ctx.ref(), a.as_ast()), ctx)
3375 def Sqrt(a, ctx=None):
3376 """ Return a Z3 expression which represents the square root of a.
3388 def Cbrt(a, ctx=None):
3389 """ Return a Z3 expression which represents the cubic root of a.
3400 #########################################
3404 #########################################
3407 class BitVecSortRef(SortRef):
3408 """Bit-vector sort.
"""
3411 """Return the size (number of bits) of the bit-vector sort `self`.
3417 return int(Z3_get_bv_sort_size(self.ctx_ref(), self.ast))
3419 def subsort(self, other):
3420 return is_bv_sort(other) and self.size() < other.size()
3422 def cast(self, val):
3423 """Try to cast `val`
as a Bit-Vector.
3428 >>> b.cast(10).
sexpr()
3433 _z3_assert(self.ctx == val.ctx, "Context mismatch")
3434 # Idea: use sign_extend if sort of val is a bitvector of smaller size
3437 return BitVecVal(val, self)
3441 """Return
True if `s`
is a Z3 bit-vector sort.
3448 return isinstance(s, BitVecSortRef)
3451 class BitVecRef(ExprRef):
3452 """Bit-vector expressions.
"""
3455 """Return the sort of the bit-vector expression `self`.
3463 return BitVecSortRef(Z3_get_sort(self.ctx_ref(), self.as_ast()), self.ctx)
3466 """Return the number of bits of the bit-vector expression `self`.
3474 return self.sort().size()
3476 def __add__(self, other):
3477 """Create the Z3 expression `self + other`.
3486 a, b = _coerce_exprs(self, other)
3487 return BitVecRef(Z3_mk_bvadd(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
3489 def __radd__(self, other):
3490 """Create the Z3 expression `other + self`.
3496 a, b = _coerce_exprs(self, other)
3497 return BitVecRef(Z3_mk_bvadd(self.ctx_ref(), b.as_ast(), a.as_ast()), self.ctx)
3499 def __mul__(self, other):
3500 """Create the Z3 expression `self * other`.
3509 a, b = _coerce_exprs(self, other)
3510 return BitVecRef(Z3_mk_bvmul(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
3512 def __rmul__(self, other):
3513 """Create the Z3 expression `other * self`.
3519 a, b = _coerce_exprs(self, other)
3520 return BitVecRef(Z3_mk_bvmul(self.ctx_ref(), b.as_ast(), a.as_ast()), self.ctx)
3522 def __sub__(self, other):
3523 """Create the Z3 expression `self - other`.
3532 a, b = _coerce_exprs(self, other)
3533 return BitVecRef(Z3_mk_bvsub(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
3535 def __rsub__(self, other):
3536 """Create the Z3 expression `other - self`.
3542 a, b = _coerce_exprs(self, other)
3543 return BitVecRef(Z3_mk_bvsub(self.ctx_ref(), b.as_ast(), a.as_ast()), self.ctx)
3545 def __or__(self, other):
3546 """Create the Z3 expression bitwise-
or `self | other`.
3555 a, b = _coerce_exprs(self, other)
3556 return BitVecRef(Z3_mk_bvor(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
3558 def __ror__(self, other):
3559 """Create the Z3 expression bitwise-
or `other | self`.
3565 a, b = _coerce_exprs(self, other)
3566 return BitVecRef(Z3_mk_bvor(self.ctx_ref(), b.as_ast(), a.as_ast()), self.ctx)
3568 def __and__(self, other):
3569 """Create the Z3 expression bitwise-
and `self & other`.
3578 a, b = _coerce_exprs(self, other)
3579 return BitVecRef(Z3_mk_bvand(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
3581 def __rand__(self, other):
3582 """Create the Z3 expression bitwise-
or `other & self`.
3588 a, b = _coerce_exprs(self, other)
3589 return BitVecRef(Z3_mk_bvand(self.ctx_ref(), b.as_ast(), a.as_ast()), self.ctx)
3591 def __xor__(self, other):
3592 """Create the Z3 expression bitwise-xor `self ^ other`.
3601 a, b = _coerce_exprs(self, other)
3602 return BitVecRef(Z3_mk_bvxor(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
3604 def __rxor__(self, other):
3605 """Create the Z3 expression bitwise-xor `other ^ self`.
3611 a, b = _coerce_exprs(self, other)
3612 return BitVecRef(Z3_mk_bvxor(self.ctx_ref(), b.as_ast(), a.as_ast()), self.ctx)
3624 """Return an expression representing `-self`.
3632 return BitVecRef(Z3_mk_bvneg(self.ctx_ref(), self.as_ast()), self.ctx)
3634 def __invert__(self):
3635 """Create the Z3 expression bitwise-
not `~self`.
3643 return BitVecRef(Z3_mk_bvnot(self.ctx_ref(), self.as_ast()), self.ctx)
3645 def __div__(self, other):
3646 """Create the Z3 expression (signed) division `self / other`.
3648 Use the function
UDiv()
for unsigned division.
3661 a, b = _coerce_exprs(self, other)
3662 return BitVecRef(Z3_mk_bvsdiv(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
3664 def __truediv__(self, other):
3665 """Create the Z3 expression (signed) division `self / other`.
"""
3666 return self.__div__(other)
3668 def __rdiv__(self, other):
3669 """Create the Z3 expression (signed) division `other / self`.
3671 Use the function
UDiv()
for unsigned division.
3676 >>> (10 / x).
sexpr()
3677 '(bvsdiv #x0000000a x)'
3679 '(bvudiv #x0000000a x)'
3681 a, b = _coerce_exprs(self, other)
3682 return BitVecRef(Z3_mk_bvsdiv(self.ctx_ref(), b.as_ast(), a.as_ast()), self.ctx)
3684 def __rtruediv__(self, other):
3685 """Create the Z3 expression (signed) division `other / self`.
"""
3686 return self.__rdiv__(other)
3688 def __mod__(self, other):
3689 """Create the Z3 expression (signed) mod `self % other`.
3691 Use the function
URem()
for unsigned remainder,
and SRem()
for signed remainder.
3706 a, b = _coerce_exprs(self, other)
3707 return BitVecRef(Z3_mk_bvsmod(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
3709 def __rmod__(self, other):
3710 """Create the Z3 expression (signed) mod `other % self`.
3712 Use the function
URem()
for unsigned remainder,
and SRem()
for signed remainder.
3717 >>> (10 % x).
sexpr()
3718 '(bvsmod #x0000000a x)'
3720 '(bvurem #x0000000a x)'
3722 '(bvsrem #x0000000a x)'
3724 a, b = _coerce_exprs(self, other)
3725 return BitVecRef(Z3_mk_bvsmod(self.ctx_ref(), b.as_ast(), a.as_ast()), self.ctx)
3727 def __le__(self, other):
3728 """Create the Z3 expression (signed) `other <= self`.
3730 Use the function
ULE()
for unsigned less than
or equal to.
3735 >>> (x <= y).
sexpr()
3740 a, b = _coerce_exprs(self, other)
3741 return BoolRef(Z3_mk_bvsle(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
3743 def __lt__(self, other):
3744 """Create the Z3 expression (signed) `other < self`.
3746 Use the function
ULT()
for unsigned less than.
3756 a, b = _coerce_exprs(self, other)
3757 return BoolRef(Z3_mk_bvslt(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
3759 def __gt__(self, other):
3760 """Create the Z3 expression (signed) `other > self`.
3762 Use the function
UGT()
for unsigned greater than.
3772 a, b = _coerce_exprs(self, other)
3773 return BoolRef(Z3_mk_bvsgt(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
3775 def __ge__(self, other):
3776 """Create the Z3 expression (signed) `other >= self`.
3778 Use the function
UGE()
for unsigned greater than
or equal to.
3783 >>> (x >= y).
sexpr()
3788 a, b = _coerce_exprs(self, other)
3789 return BoolRef(Z3_mk_bvsge(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
3791 def __rshift__(self, other):
3792 """Create the Z3 expression (arithmetical) right shift `self >> other`
3794 Use the function
LShR()
for the right logical shift
3799 >>> (x >> y).
sexpr()
3818 a, b = _coerce_exprs(self, other)
3819 return BitVecRef(Z3_mk_bvashr(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
3821 def __lshift__(self, other):
3822 """Create the Z3 expression left shift `self << other`
3827 >>> (x << y).
sexpr()
3832 a, b = _coerce_exprs(self, other)
3833 return BitVecRef(Z3_mk_bvshl(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
3835 def __rrshift__(self, other):
3836 """Create the Z3 expression (arithmetical) right shift `other` >> `self`.
3838 Use the function
LShR()
for the right logical shift
3843 >>> (10 >> x).
sexpr()
3844 '(bvashr #x0000000a x)'
3846 a, b = _coerce_exprs(self, other)
3847 return BitVecRef(Z3_mk_bvashr(self.ctx_ref(), b.as_ast(), a.as_ast()), self.ctx)
3849 def __rlshift__(self, other):
3850 """Create the Z3 expression left shift `other << self`.
3852 Use the function
LShR()
for the right logical shift
3857 >>> (10 << x).
sexpr()
3858 '(bvshl #x0000000a x)'
3860 a, b = _coerce_exprs(self, other)
3861 return BitVecRef(Z3_mk_bvshl(self.ctx_ref(), b.as_ast(), a.as_ast()), self.ctx)
3864 class BitVecNumRef(BitVecRef):
3865 """Bit-vector values.
"""
3868 """Return a Z3 bit-vector numeral
as a Python long (bignum) numeral.
3873 >>> print(
"0x%.8x" % v.as_long())
3876 return int(self.as_string())
3878 def as_signed_long(self):
3879 """Return a Z3 bit-vector numeral
as a Python long (bignum) numeral.
3880 The most significant bit
is assumed to be the sign.
3894 val = self.as_long()
3895 if val >= 2**(sz - 1):
3897 if val < -2**(sz - 1):
3901 def as_string(self):
3902 return Z3_get_numeral_string(self.ctx_ref(), self.as_ast())
3904 def as_binary_string(self):
3905 return Z3_get_numeral_binary_string(self.ctx_ref(), self.as_ast())
3909 """Return `
True`
if `a`
is a Z3 bit-vector expression.
3919 return isinstance(a, BitVecRef)
3923 """Return `
True`
if `a`
is a Z3 bit-vector numeral value.
3934 return is_bv(a) and _is_numeral(a.ctx, a.as_ast())
3937 def BV2Int(a, is_signed=False):
3938 """Return the Z3 expression
BV2Int(a).
3946 >>> x >
BV2Int(b, is_signed=
False)
3948 >>> x >
BV2Int(b, is_signed=
True)
3954 _z3_assert(is_bv(a), "First argument must be a Z3 bit-vector expression")
3956 # investigate problem with bv2int
3957 return ArithRef(Z3_mk_bv2int(ctx.ref(), a.as_ast(), is_signed), ctx)
3960 def Int2BV(a, num_bits):
3961 """Return the z3 expression
Int2BV(a, num_bits).
3962 It
is a bit-vector of width num_bits
and represents the
3963 modulo of a by 2^num_bits
3966 return BitVecRef(Z3_mk_int2bv(ctx.ref(), num_bits, a.as_ast()), ctx)
3969 def BitVecSort(sz, ctx=None):
3970 """Return a Z3 bit-vector sort of the given size. If `ctx=
None`, then the
global context
is used.
3976 >>> x =
Const(
'x', Byte)
3981 return BitVecSortRef(Z3_mk_bv_sort(ctx.ref(), sz), ctx)
3984 def BitVecVal(val, bv, ctx=None):
3985 """Return a bit-vector value with the given number of bits. If `ctx=
None`, then the
global context
is used.
3990 >>> print(
"0x%.8x" % v.as_long())
3995 return BitVecNumRef(Z3_mk_numeral(ctx.ref(), _to_int_str(val), bv.ast), ctx)
3998 return BitVecNumRef(Z3_mk_numeral(ctx.ref(), _to_int_str(val), BitVecSort(bv, ctx).ast), ctx)
4001 def BitVec(name, bv, ctx=None):
4002 """Return a bit-vector constant named `name`. `bv` may be the number of bits of a bit-vector sort.
4003 If `ctx=
None`, then the
global context
is used.
4013 >>> x2 =
BitVec(
'x', word)
4017 if isinstance(bv, BitVecSortRef):
4021 bv = BitVecSort(bv, ctx)
4022 return BitVecRef(Z3_mk_const(ctx.ref(), to_symbol(name, ctx), bv.ast), ctx)
4025 def BitVecs(names, bv, ctx=None):
4026 """Return a tuple of bit-vector constants of size bv.
4028 >>> x, y, z =
BitVecs(
'x y z', 16)
4041 if isinstance(names, str):
4042 names = names.split(" ")
4043 return [BitVec(name, bv, ctx) for name in names]
4047 """Create a Z3 bit-vector concatenation expression.
4057 args = _get_args(args)
4060 _z3_assert(sz >= 2, "At least two arguments expected.")
4067 if is_seq(args[0]) or isinstance(args[0], str):
4068 args = [_coerce_seq(s, ctx) for s in args]
4070 _z3_assert(all([is_seq(a) for a in args]), "All arguments must be sequence expressions.")
4073 v[i] = args[i].as_ast()
4074 return SeqRef(Z3_mk_seq_concat(ctx.ref(), sz, v), ctx)
4078 _z3_assert(all([is_re(a) for a in args]), "All arguments must be regular expressions.")
4081 v[i] = args[i].as_ast()
4082 return ReRef(Z3_mk_re_concat(ctx.ref(), sz, v), ctx)
4085 _z3_assert(all([is_bv(a) for a in args]), "All arguments must be Z3 bit-vector expressions.")
4087 for i in range(sz - 1):
4088 r = BitVecRef(Z3_mk_concat(ctx.ref(), r.as_ast(), args[i + 1].as_ast()), ctx)
4092 def Extract(high, low, a):
4093 """Create a Z3 bit-vector extraction expression.
4094 Extract
is overloaded to also work on sequence extraction.
4095 The functions SubString
and SubSeq are redirected to Extract.
4096 For this case, the arguments are reinterpreted
as:
4097 high -
is a sequence (string)
4099 a -
is the length to be extracted
4109 if isinstance(high, str):
4110 high = StringVal(high)
4113 offset, length = _coerce_exprs(low, a, s.ctx)
4114 return SeqRef(Z3_mk_seq_extract(s.ctx_ref(), s.as_ast(), offset.as_ast(), length.as_ast()), s.ctx)
4116 _z3_assert(low <= high, "First argument must be greater than or equal to second argument")
4117 _z3_assert(_is_int(high) and high >= 0 and _is_int(low) and low >= 0,
4118 "First and second arguments must be non negative integers")
4119 _z3_assert(is_bv(a), "Third argument must be a Z3 bit-vector expression")
4120 return BitVecRef(Z3_mk_extract(a.ctx_ref(), high, low, a.as_ast()), a.ctx)
4123 def _check_bv_args(a, b):
4125 _z3_assert(is_bv(a) or is_bv(b), "First or second argument must be a Z3 bit-vector expression")
4129 """Create the Z3 expression (unsigned) `other <= self`.
4131 Use the operator <=
for signed less than
or equal to.
4136 >>> (x <= y).sexpr()
4138 >>>
ULE(x, y).sexpr()
4141 _check_bv_args(a, b)
4142 a, b = _coerce_exprs(a, b)
4143 return BoolRef(Z3_mk_bvule(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)
4147 """Create the Z3 expression (unsigned) `other < self`.
4149 Use the operator <
for signed less than.
4156 >>>
ULT(x, y).sexpr()
4159 _check_bv_args(a, b)
4160 a, b = _coerce_exprs(a, b)
4161 return BoolRef(Z3_mk_bvult(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)
4165 """Create the Z3 expression (unsigned) `other >= self`.
4167 Use the operator >=
for signed greater than
or equal to.
4172 >>> (x >= y).sexpr()
4174 >>>
UGE(x, y).sexpr()
4177 _check_bv_args(a, b)
4178 a, b = _coerce_exprs(a, b)
4179 return BoolRef(Z3_mk_bvuge(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)
4183 """Create the Z3 expression (unsigned) `other > self`.
4185 Use the operator >
for signed greater than.
4192 >>>
UGT(x, y).sexpr()
4195 _check_bv_args(a, b)
4196 a, b = _coerce_exprs(a, b)
4197 return BoolRef(Z3_mk_bvugt(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)
4201 """Create the Z3 expression (unsigned) division `self / other`.
4203 Use the operator /
for signed division.
4209 >>>
UDiv(x, y).sort()
4213 >>>
UDiv(x, y).sexpr()
4216 _check_bv_args(a, b)
4217 a, b = _coerce_exprs(a, b)
4218 return BitVecRef(Z3_mk_bvudiv(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)
4222 """Create the Z3 expression (unsigned) remainder `self % other`.
4224 Use the operator %
for signed modulus,
and SRem()
for signed remainder.
4230 >>>
URem(x, y).sort()
4234 >>>
URem(x, y).sexpr()
4237 _check_bv_args(a, b)
4238 a, b = _coerce_exprs(a, b)
4239 return BitVecRef(Z3_mk_bvurem(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)
4243 """Create the Z3 expression signed remainder.
4245 Use the operator %
for signed modulus,
and URem()
for unsigned remainder.
4251 >>>
SRem(x, y).sort()
4255 >>>
SRem(x, y).sexpr()
4258 _check_bv_args(a, b)
4259 a, b = _coerce_exprs(a, b)
4260 return BitVecRef(Z3_mk_bvsrem(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)
4264 """Create the Z3 expression logical right shift.
4266 Use the operator >>
for the arithmetical right shift.
4271 >>> (x >> y).sexpr()
4273 >>>
LShR(x, y).sexpr()
4290 _check_bv_args(a, b)
4291 a, b = _coerce_exprs(a, b)
4292 return BitVecRef(Z3_mk_bvlshr(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)
4295 def RotateLeft(a, b):
4296 """Return an expression representing `a` rotated to the left `b` times.
4306 _check_bv_args(a, b)
4307 a, b = _coerce_exprs(a, b)
4308 return BitVecRef(Z3_mk_ext_rotate_left(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)
4311 def RotateRight(a, b):
4312 """Return an expression representing `a` rotated to the right `b` times.
4322 _check_bv_args(a, b)
4323 a, b = _coerce_exprs(a, b)
4324 return BitVecRef(Z3_mk_ext_rotate_right(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)
4328 """Return a bit-vector expression with `n` extra sign-bits.
4348 >>> print(
"%.x" % v.as_long())
4352 _z3_assert(_is_int(n), "First argument must be an integer")
4353 _z3_assert(is_bv(a), "Second argument must be a Z3 bit-vector expression")
4354 return BitVecRef(Z3_mk_sign_ext(a.ctx_ref(), n, a.as_ast()), a.ctx)
4358 """Return a bit-vector expression with `n` extra zero-bits.
4380 _z3_assert(_is_int(n), "First argument must be an integer")
4381 _z3_assert(is_bv(a), "Second argument must be a Z3 bit-vector expression")
4382 return BitVecRef(Z3_mk_zero_ext(a.ctx_ref(), n, a.as_ast()), a.ctx)
4385 def RepeatBitVec(n, a):
4386 """Return an expression representing `n` copies of `a`.
4395 >>> print(
"%.x" % v0.as_long())
4400 >>> print(
"%.x" % v.as_long())
4404 _z3_assert(_is_int(n), "First argument must be an integer")
4405 _z3_assert(is_bv(a), "Second argument must be a Z3 bit-vector expression")
4406 return BitVecRef(Z3_mk_repeat(a.ctx_ref(), n, a.as_ast()), a.ctx)
4410 """Return the reduction-
and expression of `a`.
"""
4412 _z3_assert(is_bv(a), "First argument must be a Z3 bit-vector expression")
4413 return BitVecRef(Z3_mk_bvredand(a.ctx_ref(), a.as_ast()), a.ctx)
4417 """Return the reduction-
or expression of `a`.
"""
4419 _z3_assert(is_bv(a), "First argument must be a Z3 bit-vector expression")
4420 return BitVecRef(Z3_mk_bvredor(a.ctx_ref(), a.as_ast()), a.ctx)
4423 def BVAddNoOverflow(a, b, signed):
4424 """A predicate the determines that bit-vector addition does
not overflow
"""
4425 _check_bv_args(a, b)
4426 a, b = _coerce_exprs(a, b)
4427 return BoolRef(Z3_mk_bvadd_no_overflow(a.ctx_ref(), a.as_ast(), b.as_ast(), signed), a.ctx)
4430 def BVAddNoUnderflow(a, b):
4431 """A predicate the determines that signed bit-vector addition does
not underflow
"""
4432 _check_bv_args(a, b)
4433 a, b = _coerce_exprs(a, b)
4434 return BoolRef(Z3_mk_bvadd_no_underflow(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)
4437 def BVSubNoOverflow(a, b):
4438 """A predicate the determines that bit-vector subtraction does
not overflow
"""
4439 _check_bv_args(a, b)
4440 a, b = _coerce_exprs(a, b)
4441 return BoolRef(Z3_mk_bvsub_no_overflow(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)
4444 def BVSubNoUnderflow(a, b, signed):
4445 """A predicate the determines that bit-vector subtraction does
not underflow
"""
4446 _check_bv_args(a, b)
4447 a, b = _coerce_exprs(a, b)
4448 return BoolRef(Z3_mk_bvsub_no_underflow(a.ctx_ref(), a.as_ast(), b.as_ast(), signed), a.ctx)
4451 def BVSDivNoOverflow(a, b):
4452 """A predicate the determines that bit-vector signed division does
not overflow
"""
4453 _check_bv_args(a, b)
4454 a, b = _coerce_exprs(a, b)
4455 return BoolRef(Z3_mk_bvsdiv_no_overflow(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)
4458 def BVSNegNoOverflow(a):
4459 """A predicate the determines that bit-vector unary negation does
not overflow
"""
4461 _z3_assert(is_bv(a), "First argument must be a Z3 bit-vector expression")
4462 return BoolRef(Z3_mk_bvneg_no_overflow(a.ctx_ref(), a.as_ast()), a.ctx)
4465 def BVMulNoOverflow(a, b, signed):
4466 """A predicate the determines that bit-vector multiplication does
not overflow
"""
4467 _check_bv_args(a, b)
4468 a, b = _coerce_exprs(a, b)
4469 return BoolRef(Z3_mk_bvmul_no_overflow(a.ctx_ref(), a.as_ast(), b.as_ast(), signed), a.ctx)
4472 def BVMulNoUnderflow(a, b):
4473 """A predicate the determines that bit-vector signed multiplication does
not underflow
"""
4474 _check_bv_args(a, b)
4475 a, b = _coerce_exprs(a, b)
4476 return BoolRef(Z3_mk_bvmul_no_underflow(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)
4479 #########################################
4483 #########################################
4485 class ArraySortRef(SortRef):
4489 """Return the domain of the array sort `self`.
4495 return _to_sort_ref(Z3_get_array_sort_domain(self.ctx_ref(), self.ast), self.ctx)
4498 """Return the range of the array sort `self`.
4504 return _to_sort_ref(Z3_get_array_sort_range(self.ctx_ref(), self.ast), self.ctx)
4507 class ArrayRef(ExprRef):
4508 """Array expressions.
"""
4511 """Return the array sort of the array expression `self`.
4517 return ArraySortRef(Z3_get_sort(self.ctx_ref(), self.as_ast()), self.ctx)
4526 return self.sort().domain()
4535 return self.sort().range()
4537 def __getitem__(self, arg):
4538 """Return the Z3 expression `self[arg]`.
4547 arg = self.domain().cast(arg)
4548 return _to_expr_ref(Z3_mk_select(self.ctx_ref(), self.as_ast(), arg.as_ast()), self.ctx)
4551 return _to_expr_ref(Z3_mk_array_default(self.ctx_ref(), self.as_ast()), self.ctx)
4554 def is_array_sort(a):
4555 return Z3_get_sort_kind(a.ctx.ref(), Z3_get_sort(a.ctx.ref(), a.ast)) == Z3_ARRAY_SORT
4559 """Return `
True`
if `a`
is a Z3 array expression.
4569 return isinstance(a, ArrayRef)
4572 def is_const_array(a):
4573 """Return `
True`
if `a`
is a Z3 constant array.
4582 return is_app_of(a, Z3_OP_CONST_ARRAY)
4586 """Return `
True`
if `a`
is a Z3 constant array.
4595 return is_app_of(a, Z3_OP_CONST_ARRAY)
4599 """Return `
True`
if `a`
is a Z3 map array expression.
4611 return is_app_of(a, Z3_OP_ARRAY_MAP)
4615 """Return `
True`
if `a`
is a Z3 default array expression.
4620 return is_app_of(a, Z3_OP_ARRAY_DEFAULT)
4623 def get_map_func(a):
4624 """Return the function declaration associated with a Z3 map array expression.
4637 _z3_assert(is_map(a), "Z3 array map expression expected.")
4641 Z3_get_decl_ast_parameter(a.ctx_ref(), a.decl().ast, 0),
4647 def ArraySort(*sig):
4648 """Return the Z3 array sort with the given domain
and range sorts.
4661 sig = _get_args(sig)
4663 _z3_assert(len(sig) > 1, "At least two arguments expected")
4664 arity = len(sig) - 1
4669 _z3_assert(is_sort(s), "Z3 sort expected")
4670 _z3_assert(s.ctx == r.ctx, "Context mismatch")
4673 return ArraySortRef(Z3_mk_array_sort(ctx.ref(), d.ast, r.ast), ctx)
4674 dom = (Sort * arity)()
4675 for i in range(arity):
4677 return ArraySortRef(Z3_mk_array_sort_n(ctx.ref(), arity, dom, r.ast), ctx)
4680 def Array(name, dom, rng):
4681 """Return an array constant named `name` with the given domain
and range sorts.
4689 s = ArraySort(dom, rng)
4691 return ArrayRef(Z3_mk_const(ctx.ref(), to_symbol(name, ctx), s.ast), ctx)
4694 def Update(a, i, v):
4695 """Return a Z3 store array expression.
4698 >>> i, v =
Ints(
'i v')
4702 >>>
prove(s[i] == v)
4709 _z3_assert(is_array_sort(a), "First argument must be a Z3 array expression")
4710 i = a.sort().domain().cast(i)
4711 v = a.sort().range().cast(v)
4713 return _to_expr_ref(Z3_mk_store(ctx.ref(), a.as_ast(), i.as_ast(), v.as_ast()), ctx)
4717 """ Return a default value
for array expression.
4723 _z3_assert(is_array_sort(a), "First argument must be a Z3 array expression")
4728 """Return a Z3 store array expression.
4731 >>> i, v =
Ints(
'i v')
4732 >>> s =
Store(a, i, v)
4735 >>>
prove(s[i] == v)
4741 return Update(a, i, v)
4745 """Return a Z3 select array expression.
4755 _z3_assert(is_array_sort(a), "First argument must be a Z3 array expression")
4760 """Return a Z3 map array expression.
4765 >>> b =
Map(f, a1, a2)
4768 >>>
prove(b[0] == f(a1[0], a2[0]))
4771 args = _get_args(args)
4773 _z3_assert(len(args) > 0, "At least one Z3 array expression expected")
4774 _z3_assert(is_func_decl(f), "First argument must be a Z3 function declaration")
4775 _z3_assert(all([is_array(a) for a in args]), "Z3 array expected expected")
4776 _z3_assert(len(args) == f.arity(), "Number of arguments mismatch")
4777 _args, sz = _to_ast_array(args)
4779 return ArrayRef(Z3_mk_map(ctx.ref(), f.ast, sz, _args), ctx)
4783 """Return a Z3 constant array expression.
4797 _z3_assert(is_sort(dom), "Z3 sort expected")
4800 v = _py2expr(v, ctx)
4801 return ArrayRef(Z3_mk_const_array(ctx.ref(), dom.ast, v.as_ast()), ctx)
4805 """Return extensionality index
for one-dimensional arrays.
4812 _z3_assert(is_array_sort(a) and is_array(b), "arguments must be arrays")
4813 return _to_expr_ref(Z3_mk_array_ext(ctx.ref(), a.as_ast(), b.as_ast()), ctx)
4816 def SetHasSize(a, k):
4818 k = _py2expr(k, ctx)
4819 return _to_expr_ref(Z3_mk_set_has_size(ctx.ref(), a.as_ast(), k.as_ast()), ctx)
4823 """Return `
True`
if `a`
is a Z3 array select application.
4832 return is_app_of(a, Z3_OP_SELECT)
4836 """Return `
True`
if `a`
is a Z3 array store application.
4844 return is_app_of(a, Z3_OP_STORE)
4846 #########################################
4850 #########################################
4854 """ Create a set sort over element sort s
"""
4855 return ArraySort(s, BoolSort())
4859 """Create the empty set
4864 return ArrayRef(Z3_mk_empty_set(ctx.ref(), s.ast), ctx)
4868 """Create the full set
4873 return ArrayRef(Z3_mk_full_set(ctx.ref(), s.ast), ctx)
4876 def SetUnion(*args):
4877 """ Take the union of sets
4883 args = _get_args(args)
4884 ctx = _ctx_from_ast_arg_list(args)
4885 _args, sz = _to_ast_array(args)
4886 return ArrayRef(Z3_mk_set_union(ctx.ref(), sz, _args), ctx)
4889 def SetIntersect(*args):
4890 """ Take the union of sets
4896 args = _get_args(args)
4897 ctx = _ctx_from_ast_arg_list(args)
4898 _args, sz = _to_ast_array(args)
4899 return ArrayRef(Z3_mk_set_intersect(ctx.ref(), sz, _args), ctx)
4903 """ Add element e to set s
4908 ctx = _ctx_from_ast_arg_list([s, e])
4909 e = _py2expr(e, ctx)
4910 return ArrayRef(Z3_mk_set_add(ctx.ref(), s.as_ast(), e.as_ast()), ctx)
4914 """ Remove element e to set s
4919 ctx = _ctx_from_ast_arg_list([s, e])
4920 e = _py2expr(e, ctx)
4921 return ArrayRef(Z3_mk_set_del(ctx.ref(), s.as_ast(), e.as_ast()), ctx)
4924 def SetComplement(s):
4925 """ The complement of set s
4931 return ArrayRef(Z3_mk_set_complement(ctx.ref(), s.as_ast()), ctx)
4934 def SetDifference(a, b):
4935 """ The set difference of a
and b
4941 ctx = _ctx_from_ast_arg_list([a, b])
4942 return ArrayRef(Z3_mk_set_difference(ctx.ref(), a.as_ast(), b.as_ast()), ctx)
4946 """ Check
if e
is a member of set s
4951 ctx = _ctx_from_ast_arg_list([s, e])
4952 e = _py2expr(e, ctx)
4953 return BoolRef(Z3_mk_set_member(ctx.ref(), e.as_ast(), s.as_ast()), ctx)
4957 """ Check
if a
is a subset of b
4963 ctx = _ctx_from_ast_arg_list([a, b])
4964 return BoolRef(Z3_mk_set_subset(ctx.ref(), a.as_ast(), b.as_ast()), ctx)
4967 #########################################
4971 #########################################
4973 def _valid_accessor(acc):
4974 """Return `
True`
if acc
is pair of the form (String, Datatype
or Sort).
"""
4975 if not isinstance(acc, tuple):
4979 return isinstance(acc[0], str) and (isinstance(acc[1], Datatype) or is_sort(acc[1]))
4983 """Helper
class for declaring Z3 datatypes.
4986 >>> List.declare(
'cons', (
'car',
IntSort()), (
'cdr', List))
4987 >>> List.declare(
'nil')
4988 >>> List = List.create()
4992 >>> List.cons(10, List.nil)
4994 >>> List.cons(10, List.nil).sort()
4996 >>> cons = List.cons
5000 >>> n = cons(1, cons(0, nil))
5002 cons(1, cons(0, nil))
5009 def __init__(self, name, ctx=None):
5010 self.ctx = _get_ctx(ctx)
5012 self.constructors = []
5014 def __deepcopy__(self, memo={}):
5015 r = Datatype(self.name, self.ctx)
5016 r.constructors = copy.deepcopy(self.constructors)
5019 def declare_core(self, name, rec_name, *args):
5021 _z3_assert(isinstance(name, str), "String expected")
5022 _z3_assert(isinstance(rec_name, str), "String expected")
5024 all([_valid_accessor(a) for a in args]),
5025 "Valid list of accessors expected. An accessor is a pair of the form (String, Datatype|Sort)",
5027 self.constructors.append((name, rec_name, args))
5029 def declare(self, name, *args):
5030 """Declare constructor named `name` with the given accessors `args`.
5031 Each accessor
is a pair `(name, sort)`, where `name`
is a string
and `sort` a Z3 sort
5032 or a reference to the datatypes being declared.
5034 In the following example `List.declare(
'cons', (
'car',
IntSort()), (
'cdr', List))`
5035 declares the constructor named `cons` that builds a new List using an integer
and a List.
5036 It also declares the accessors `car`
and `cdr`. The accessor `car` extracts the integer
5037 of a `cons` cell,
and `cdr` the list of a `cons` cell. After all constructors were declared,
5038 we use the method
create() to create the actual datatype
in Z3.
5041 >>> List.declare(
'cons', (
'car',
IntSort()), (
'cdr', List))
5042 >>> List.declare(
'nil')
5043 >>> List = List.create()
5046 _z3_assert(isinstance(name, str), "String expected")
5047 _z3_assert(name != "", "Constructor name cannot be empty")
5048 return self.declare_core(name, "is-" + name, *args)
5051 return "Datatype(%s, %s)" % (self.name, self.constructors)
5054 """Create a Z3 datatype based on the constructors declared using the method `
declare()`.
5056 The function `
CreateDatatypes()` must be used to define mutually recursive datatypes.
5059 >>> List.declare(
'cons', (
'car',
IntSort()), (
'cdr', List))
5060 >>> List.declare(
'nil')
5061 >>> List = List.create()
5064 >>> List.cons(10, List.nil)
5067 return CreateDatatypes([self])[0]
5070 class ScopedConstructor:
5071 """Auxiliary object used to create Z3 datatypes.
"""
5073 def __init__(self, c, ctx):
5078 if self.ctx.ref() is not None:
5079 Z3_del_constructor(self.ctx.ref(), self.c)
5082 class ScopedConstructorList:
5083 """Auxiliary object used to create Z3 datatypes.
"""
5085 def __init__(self, c, ctx):
5090 if self.ctx.ref() is not None:
5091 Z3_del_constructor_list(self.ctx.ref(), self.c)
5094 def CreateDatatypes(*ds):
5095 """Create mutually recursive Z3 datatypes using 1
or more Datatype helper objects.
5097 In the following example we define a Tree-List using two mutually recursive datatypes.
5099 >>> TreeList =
Datatype(
'TreeList')
5102 >>> Tree.declare(
'leaf', (
'val',
IntSort()))
5104 >>> Tree.declare(
'node', (
'children', TreeList))
5105 >>> TreeList.declare(
'nil')
5106 >>> TreeList.declare(
'cons', (
'car', Tree), (
'cdr', TreeList))
5108 >>> Tree.val(Tree.leaf(10))
5110 >>>
simplify(Tree.val(Tree.leaf(10)))
5112 >>> n1 = Tree.node(TreeList.cons(Tree.leaf(10), TreeList.cons(Tree.leaf(20), TreeList.nil)))
5114 node(cons(leaf(10), cons(leaf(20), nil)))
5115 >>> n2 = Tree.node(TreeList.cons(n1, TreeList.nil))
5118 >>>
simplify(TreeList.car(Tree.children(n2)) == n1)
5123 _z3_assert(len(ds) > 0, "At least one Datatype must be specified")
5124 _z3_assert(all([isinstance(d, Datatype) for d in ds]), "Arguments must be Datatypes")
5125 _z3_assert(all([d.ctx == ds[0].ctx for d in ds]), "Context mismatch")
5126 _z3_assert(all([d.constructors != [] for d in ds]), "Non-empty Datatypes expected")
5129 names = (Symbol * num)()
5130 out = (Sort * num)()
5131 clists = (ConstructorList * num)()
5133 for i in range(num):
5135 names[i] = to_symbol(d.name, ctx)
5136 num_cs = len(d.constructors)
5137 cs = (Constructor * num_cs)()
5138 for j in range(num_cs):
5139 c = d.constructors[j]
5140 cname = to_symbol(c[0], ctx)
5141 rname = to_symbol(c[1], ctx)
5144 fnames = (Symbol * num_fs)()
5145 sorts = (Sort * num_fs)()
5146 refs = (ctypes.c_uint * num_fs)()
5147 for k in range(num_fs):
5150 fnames[k] = to_symbol(fname, ctx)
5151 if isinstance(ftype, Datatype):
5154 ds.count(ftype) == 1,
5155 "One and only one occurrence of each datatype is expected",
5158 refs[k] = ds.index(ftype)
5161 _z3_assert(is_sort(ftype), "Z3 sort expected")
5162 sorts[k] = ftype.ast
5164 cs[j] = Z3_mk_constructor(ctx.ref(), cname, rname, num_fs, fnames, sorts, refs)
5165 to_delete.append(ScopedConstructor(cs[j], ctx))
5166 clists[i] = Z3_mk_constructor_list(ctx.ref(), num_cs, cs)
5167 to_delete.append(ScopedConstructorList(clists[i], ctx))
5168 Z3_mk_datatypes(ctx.ref(), num, names, out, clists)
5170 # Create a field for every constructor, recognizer and accessor
5171 for i in range(num):
5172 dref = DatatypeSortRef(out[i], ctx)
5173 num_cs = dref.num_constructors()
5174 for j in range(num_cs):
5175 cref = dref.constructor(j)
5176 cref_name = cref.name()
5177 cref_arity = cref.arity()
5178 if cref.arity() == 0:
5180 setattr(dref, cref_name, cref)
5181 rref = dref.recognizer(j)
5182 setattr(dref, "is_" + cref_name, rref)
5183 for k in range(cref_arity):
5184 aref = dref.accessor(j, k)
5185 setattr(dref, aref.name(), aref)
5187 return tuple(result)
5190 class DatatypeSortRef(SortRef):
5191 """Datatype sorts.
"""
5193 def num_constructors(self):
5194 """Return the number of constructors
in the given Z3 datatype.
5197 >>> List.declare(
'cons', (
'car',
IntSort()), (
'cdr', List))
5198 >>> List.declare(
'nil')
5199 >>> List = List.create()
5201 >>> List.num_constructors()
5204 return int(Z3_get_datatype_sort_num_constructors(self.ctx_ref(), self.ast))
5206 def constructor(self, idx):
5207 """Return a constructor of the datatype `self`.
5210 >>> List.declare(
'cons', (
'car',
IntSort()), (
'cdr', List))
5211 >>> List.declare(
'nil')
5212 >>> List = List.create()
5214 >>> List.num_constructors()
5216 >>> List.constructor(0)
5218 >>> List.constructor(1)
5222 _z3_assert(idx < self.num_constructors(), "Invalid constructor index")
5223 return FuncDeclRef(Z3_get_datatype_sort_constructor(self.ctx_ref(), self.ast, idx), self.ctx)
5225 def recognizer(self, idx):
5226 """In Z3, each constructor has an associated recognizer predicate.
5228 If the constructor
is named `name`, then the recognizer `is_name`.
5231 >>> List.declare(
'cons', (
'car',
IntSort()), (
'cdr', List))
5232 >>> List.declare(
'nil')
5233 >>> List = List.create()
5235 >>> List.num_constructors()
5237 >>> List.recognizer(0)
5239 >>> List.recognizer(1)
5241 >>>
simplify(List.is_nil(List.cons(10, List.nil)))
5243 >>>
simplify(List.is_cons(List.cons(10, List.nil)))
5245 >>> l =
Const(
'l', List)
5250 _z3_assert(idx < self.num_constructors(), "Invalid recognizer index")
5251 return FuncDeclRef(Z3_get_datatype_sort_recognizer(self.ctx_ref(), self.ast, idx), self.ctx)
5253 def accessor(self, i, j):
5254 """In Z3, each constructor has 0
or more accessor.
5255 The number of accessors
is equal to the arity of the constructor.
5258 >>> List.declare(
'cons', (
'car',
IntSort()), (
'cdr', List))
5259 >>> List.declare(
'nil')
5260 >>> List = List.create()
5261 >>> List.num_constructors()
5263 >>> List.constructor(0)
5265 >>> num_accs = List.constructor(0).arity()
5268 >>> List.accessor(0, 0)
5270 >>> List.accessor(0, 1)
5272 >>> List.constructor(1)
5274 >>> num_accs = List.constructor(1).arity()
5279 _z3_assert(i < self.num_constructors(), "Invalid constructor index")
5280 _z3_assert(j < self.constructor(i).arity(), "Invalid accessor index")
5282 Z3_get_datatype_sort_constructor_accessor(self.ctx_ref(), self.ast, i, j),
5287 class DatatypeRef(ExprRef):
5288 """Datatype expressions.
"""
5291 """Return the datatype sort of the datatype expression `self`.
"""
5292 return DatatypeSortRef(Z3_get_sort(self.ctx_ref(), self.as_ast()), self.ctx)
5295 def TupleSort(name, sorts, ctx=None):
5296 """Create a named tuple sort base on a set of underlying sorts
5300 tuple = Datatype(name, ctx)
5301 projects = [("project%d" % i, sorts[i]) for i in range(len(sorts))]
5302 tuple.declare(name, *projects)
5303 tuple = tuple.create()
5304 return tuple, tuple.constructor(0), [tuple.accessor(0, i) for i in range(len(sorts))]
5307 def DisjointSum(name, sorts, ctx=None):
5308 """Create a named tagged union sort base on a set of underlying sorts
5312 sum = Datatype(name, ctx)
5313 for i in range(len(sorts)):
5314 sum.declare("inject%d" % i, ("project%d" % i, sorts[i]))
5316 return sum, [(sum.constructor(i), sum.accessor(i, 0)) for i in range(len(sorts))]
5319 def EnumSort(name, values, ctx=None):
5320 """Return a new enumeration sort named `name` containing the given values.
5322 The result
is a pair (sort, list of constants).
5324 >>> Color, (red, green, blue) =
EnumSort(
'Color', [
'red',
'green',
'blue'])
5327 _z3_assert(isinstance(name, str), "Name must be a string")
5328 _z3_assert(all([isinstance(v, str) for v in values]), "Eumeration sort values must be strings")
5329 _z3_assert(len(values) > 0, "At least one value expected")
5332 _val_names = (Symbol * num)()
5333 for i in range(num):
5334 _val_names[i] = to_symbol(values[i])
5335 _values = (FuncDecl * num)()
5336 _testers = (FuncDecl * num)()
5337 name = to_symbol(name)
5338 S = DatatypeSortRef(Z3_mk_enumeration_sort(ctx.ref(), name, num, _val_names, _values, _testers), ctx)
5340 for i in range(num):
5341 V.append(FuncDeclRef(_values[i], ctx))
5342 V = [a() for a in V]
5345 #########################################
5349 #########################################
5353 """Set of parameters used to configure Solvers, Tactics
and Simplifiers
in Z3.
5355 Consider using the function `args2params` to create instances of this object.
5358 def __init__(self, ctx=None, params=None):
5359 self.ctx = _get_ctx(ctx)
5361 self.params = Z3_mk_params(self.ctx.ref())
5363 self.params = params
5364 Z3_params_inc_ref(self.ctx.ref(), self.params)
5366 def __deepcopy__(self, memo={}):
5367 return ParamsRef(self.ctx, self.params)
5370 if self.ctx.ref() is not None:
5371 Z3_params_dec_ref(self.ctx.ref(), self.params)
5373 def set(self, name, val):
5374 """Set parameter name with value val.
"""
5376 _z3_assert(isinstance(name, str), "parameter name must be a string")
5377 name_sym = to_symbol(name, self.ctx)
5378 if isinstance(val, bool):
5379 Z3_params_set_bool(self.ctx.ref(), self.params, name_sym, val)
5381 Z3_params_set_uint(self.ctx.ref(), self.params, name_sym, val)
5382 elif isinstance(val, float):
5383 Z3_params_set_double(self.ctx.ref(), self.params, name_sym, val)
5384 elif isinstance(val, str):
5385 Z3_params_set_symbol(self.ctx.ref(), self.params, name_sym, to_symbol(val, self.ctx))
5388 _z3_assert(False, "invalid parameter value")
5391 return Z3_params_to_string(self.ctx.ref(), self.params)
5393 def validate(self, ds):
5394 _z3_assert(isinstance(ds, ParamDescrsRef), "parameter description set expected")
5395 Z3_params_validate(self.ctx.ref(), self.params, ds.descr)
5398 def args2params(arguments, keywords, ctx=None):
5399 """Convert python arguments into a Z3_params object.
5400 A
':' is added to the keywords,
and '_' is replaced with
'-'
5402 >>>
args2params([
'model',
True,
'relevancy', 2], {
'elim_and' :
True})
5403 (params model true relevancy 2 elim_and true)
5406 _z3_assert(len(arguments) % 2 == 0, "Argument list must have an even number of elements.")
5421 class ParamDescrsRef:
5422 """Set of parameter descriptions
for Solvers, Tactics
and Simplifiers
in Z3.
5425 def __init__(self, descr, ctx=None):
5426 _z3_assert(isinstance(descr, ParamDescrs), "parameter description object expected")
5427 self.ctx = _get_ctx(ctx)
5429 Z3_param_descrs_inc_ref(self.ctx.ref(), self.descr)
5431 def __deepcopy__(self, memo={}):
5432 return ParamsDescrsRef(self.descr, self.ctx)
5435 if self.ctx.ref() is not None:
5436 Z3_param_descrs_dec_ref(self.ctx.ref(), self.descr)
5439 """Return the size of
in the parameter description `self`.
5441 return int(Z3_param_descrs_size(self.ctx.ref(), self.descr))
5444 """Return the size of
in the parameter description `self`.
5448 def get_name(self, i):
5449 """Return the i-th parameter name
in the parameter description `self`.
5451 return _symbol2py(self.ctx, Z3_param_descrs_get_name(self.ctx.ref(), self.descr, i))
5453 def get_kind(self, n):
5454 """Return the kind of the parameter named `n`.
5456 return Z3_param_descrs_get_kind(self.ctx.ref(), self.descr, to_symbol(n, self.ctx))
5458 def get_documentation(self, n):
5459 """Return the documentation string of the parameter named `n`.
5461 return Z3_param_descrs_get_documentation(self.ctx.ref(), self.descr, to_symbol(n, self.ctx))
5463 def __getitem__(self, arg):
5465 return self.get_name(arg)
5467 return self.get_kind(arg)
5470 return Z3_param_descrs_to_string(self.ctx.ref(), self.descr)
5472 #########################################
5476 #########################################
5479 class Goal(Z3PPObject):
5480 """Goal
is a collection of constraints we want to find a solution
or show to be unsatisfiable (infeasible).
5482 Goals are processed using Tactics. A Tactic transforms a goal into a set of subgoals.
5483 A goal has a solution
if one of its subgoals has a solution.
5484 A goal
is unsatisfiable
if all subgoals are unsatisfiable.
5487 def __init__(self, models=True, unsat_cores=False, proofs=False, ctx=None, goal=None):
5489 _z3_assert(goal is None or ctx is not None,
5490 "If goal is different from None, then ctx must be also different from None")
5491 self.ctx = _get_ctx(ctx)
5493 if self.goal is None:
5494 self.goal = Z3_mk_goal(self.ctx.ref(), models, unsat_cores, proofs)
5495 Z3_goal_inc_ref(self.ctx.ref(), self.goal)
5498 if self.goal is not None and self.ctx.ref() is not None:
5499 Z3_goal_dec_ref(self.ctx.ref(), self.goal)
5502 """Return the depth of the goal `self`.
5503 The depth corresponds to the number of tactics applied to `self`.
5505 >>> x, y =
Ints(
'x y')
5507 >>> g.add(x == 0, y >= x + 1)
5510 >>> r =
Then(
'simplify',
'solve-eqs')(g)
5517 return int(Z3_goal_depth(self.ctx.ref(), self.goal))
5519 def inconsistent(self):
5520 """Return `
True`
if `self` contains the `
False` constraints.
5522 >>> x, y =
Ints(
'x y')
5524 >>> g.inconsistent()
5526 >>> g.add(x == 0, x == 1)
5529 >>> g.inconsistent()
5531 >>> g2 =
Tactic(
'propagate-values')(g)[0]
5532 >>> g2.inconsistent()
5535 return Z3_goal_inconsistent(self.ctx.ref(), self.goal)
5538 """Return the precision (under-approximation, over-approximation,
or precise) of the goal `self`.
5541 >>> g.prec() == Z3_GOAL_PRECISE
5543 >>> x, y =
Ints(
'x y')
5544 >>> g.add(x == y + 1)
5545 >>> g.prec() == Z3_GOAL_PRECISE
5547 >>> t =
With(
Tactic(
'add-bounds'), add_bound_lower=0, add_bound_upper=10)
5550 [x == y + 1, x <= 10, x >= 0, y <= 10, y >= 0]
5551 >>> g2.prec() == Z3_GOAL_PRECISE
5553 >>> g2.prec() == Z3_GOAL_UNDER
5556 return Z3_goal_precision(self.ctx.ref(), self.goal)
5558 def precision(self):
5559 """Alias
for `
prec()`.
5562 >>> g.precision() == Z3_GOAL_PRECISE
5568 """Return the number of constraints
in the goal `self`.
5573 >>> x, y =
Ints(
'x y')
5574 >>> g.add(x == 0, y > x)
5578 return int(Z3_goal_size(self.ctx.ref(), self.goal))
5581 """Return the number of constraints
in the goal `self`.
5586 >>> x, y =
Ints(
'x y')
5587 >>> g.add(x == 0, y > x)
5594 """Return a constraint
in the goal `self`.
5597 >>> x, y =
Ints(
'x y')
5598 >>> g.add(x == 0, y > x)
5604 return _to_expr_ref(Z3_goal_formula(self.ctx.ref(), self.goal, i), self.ctx)
5606 def __getitem__(self, arg):
5607 """Return a constraint
in the goal `self`.
5610 >>> x, y =
Ints(
'x y')
5611 >>> g.add(x == 0, y > x)
5617 if arg >= len(self):
5619 return self.get(arg)
5621 def assert_exprs(self, *args):
5622 """Assert constraints into the goal.
5626 >>> g.assert_exprs(x > 0, x < 2)
5630 args = _get_args(args)
5631 s = BoolSort(self.ctx)
5634 Z3_goal_assert(self.ctx.ref(), self.goal, arg.as_ast())
5636 def append(self, *args):
5641 >>> g.append(x > 0, x < 2)
5645 self.assert_exprs(*args)
5647 def insert(self, *args):
5652 >>> g.insert(x > 0, x < 2)
5656 self.assert_exprs(*args)
5658 def add(self, *args):
5663 >>> g.add(x > 0, x < 2)
5667 self.assert_exprs(*args)
5669 def convert_model(self, model):
5670 """Retrieve model
from a satisfiable goal
5671 >>> a, b =
Ints(
'a b')
5673 >>> g.add(
Or(a == 0, a == 1),
Or(b == 0, b == 1), a > b)
5677 [
Or(b == 0, b == 1),
Not(0 <= b)]
5679 [
Or(b == 0, b == 1),
Not(1 <= b)]
5695 _z3_assert(isinstance(model, ModelRef), "Z3 Model expected")
5696 return ModelRef(Z3_goal_convert_model(self.ctx.ref(), self.goal, model.model), self.ctx)
5699 return obj_to_string(self)
5702 """Return a textual representation of the s-expression representing the goal.
"""
5703 return Z3_goal_to_string(self.ctx.ref(), self.goal)
5705 def dimacs(self, include_names=True):
5706 """Return a textual representation of the goal
in DIMACS format.
"""
5707 return Z3_goal_to_dimacs_string(self.ctx.ref(), self.goal, include_names)
5709 def translate(self, target):
5710 """Copy goal `self` to context `target`.
5718 >>> g2 = g.translate(c2)
5729 _z3_assert(isinstance(target, Context), "target must be a context")
5730 return Goal(goal=Z3_goal_translate(self.ctx.ref(), self.goal, target.ref()), ctx=target)
5733 return self.translate(self.ctx)
5735 def __deepcopy__(self, memo={}):
5736 return self.translate(self.ctx)
5738 def simplify(self, *arguments, **keywords):
5739 """Return a new simplified goal.
5741 This method
is essentially invoking the simplify tactic.
5745 >>> g.add(x + 1 >= 2)
5748 >>> g2 = g.simplify()
5755 t = Tactic("simplify")
5756 return t.apply(self, *arguments, **keywords)[0]
5759 """Return goal `self`
as a single Z3 expression.
5774 return BoolVal(True, self.ctx)
5778 return And([self.get(i) for i in range(len(self))], self.ctx)
5780 #########################################
5784 #########################################
5787 class AstVector(Z3PPObject):
5788 """A collection (vector) of ASTs.
"""
5790 def __init__(self, v=None, ctx=None):
5793 self.ctx = _get_ctx(ctx)
5794 self.vector = Z3_mk_ast_vector(self.ctx.ref())
5797 assert ctx is not None
5799 Z3_ast_vector_inc_ref(self.ctx.ref(), self.vector)
5802 if self.vector is not None and self.ctx.ref() is not None:
5803 Z3_ast_vector_dec_ref(self.ctx.ref(), self.vector)
5806 """Return the size of the vector `self`.
5811 >>> A.push(
Int(
'x'))
5812 >>> A.push(
Int(
'x'))
5816 return int(Z3_ast_vector_size(self.ctx.ref(), self.vector))
5818 def __getitem__(self, i):
5819 """Return the AST at position `i`.
5822 >>> A.push(
Int(
'x') + 1)
5823 >>> A.push(
Int(
'y'))
5830 if isinstance(i, int):
5834 if i >= self.__len__():
5836 return _to_ast_ref(Z3_ast_vector_get(self.ctx.ref(), self.vector, i), self.ctx)
5838 elif isinstance(i, slice):
5840 for ii in range(*i.indices(self.__len__())):
5841 result.append(_to_ast_ref(
5842 Z3_ast_vector_get(self.ctx.ref(), self.vector, ii),
5847 def __setitem__(self, i, v):
5848 """Update AST at position `i`.
5851 >>> A.push(
Int(
'x') + 1)
5852 >>> A.push(
Int(
'y'))
5859 if i >= self.__len__():
5861 Z3_ast_vector_set(self.ctx.ref(), self.vector, i, v.as_ast())
5864 """Add `v`
in the end of the vector.
5869 >>> A.push(
Int(
'x'))
5873 Z3_ast_vector_push(self.ctx.ref(), self.vector, v.as_ast())
5875 def resize(self, sz):
5876 """Resize the vector to `sz` elements.
5882 >>>
for i
in range(10): A[i] =
Int(
'x')
5886 Z3_ast_vector_resize(self.ctx.ref(), self.vector, sz)
5888 def __contains__(self, item):
5889 """Return `
True`
if the vector contains `item`.
5911 def translate(self, other_ctx):
5912 """Copy vector `self` to context `other_ctx`.
5918 >>> B = A.translate(c2)
5923 Z3_ast_vector_translate(self.ctx.ref(), self.vector, other_ctx.ref()),
5928 return self.translate(self.ctx)
5930 def __deepcopy__(self, memo={}):
5931 return self.translate(self.ctx)
5934 return obj_to_string(self)
5937 """Return a textual representation of the s-expression representing the vector.
"""
5938 return Z3_ast_vector_to_string(self.ctx.ref(), self.vector)
5940 #########################################
5944 #########################################
5948 """A mapping
from ASTs to ASTs.
"""
5950 def __init__(self, m=None, ctx=None):
5953 self.ctx = _get_ctx(ctx)
5954 self.map = Z3_mk_ast_map(self.ctx.ref())
5957 assert ctx is not None
5959 Z3_ast_map_inc_ref(self.ctx.ref(), self.map)
5961 def __deepcopy__(self, memo={}):
5962 return AstMap(self.map, self.ctx)
5965 if self.map is not None and self.ctx.ref() is not None:
5966 Z3_ast_map_dec_ref(self.ctx.ref(), self.map)
5969 """Return the size of the map.
5979 return int(Z3_ast_map_size(self.ctx.ref(), self.map))
5981 def __contains__(self, key):
5982 """Return `
True`
if the map contains key `key`.
5992 return Z3_ast_map_contains(self.ctx.ref(), self.map, key.as_ast())
5994 def __getitem__(self, key):
5995 """Retrieve the value associated with key `key`.
6003 return _to_ast_ref(Z3_ast_map_find(self.ctx.ref(), self.map, key.as_ast()), self.ctx)
6005 def __setitem__(self, k, v):
6006 """Add/Update key `k` with value `v`.
6019 Z3_ast_map_insert(self.ctx.ref(), self.map, k.as_ast(), v.as_ast())
6022 return Z3_ast_map_to_string(self.ctx.ref(), self.map)
6025 """Remove the entry associated with key `k`.
6036 Z3_ast_map_erase(self.ctx.ref(), self.map, k.as_ast())
6039 """Remove all entries
from the map.
6051 Z3_ast_map_reset(self.ctx.ref(), self.map)
6054 """Return an AstVector containing all keys
in the map.
6063 return AstVector(Z3_ast_map_keys(self.ctx.ref(), self.map), self.ctx)
6065 #########################################
6069 #########################################
6073 """Store the value of the interpretation of a function
in a particular point.
"""
6075 def __init__(self, entry, ctx):
6078 Z3_func_entry_inc_ref(self.ctx.ref(), self.entry)
6080 def __deepcopy__(self, memo={}):
6081 return FuncEntry(self.entry, self.ctx)
6084 if self.ctx.ref() is not None:
6085 Z3_func_entry_dec_ref(self.ctx.ref(), self.entry)
6088 """Return the number of arguments
in the given entry.
6092 >>> s.add(f(0, 1) == 10, f(1, 2) == 20, f(1, 0) == 10)
6097 >>> f_i.num_entries()
6099 >>> e = f_i.entry(0)
6103 return int(Z3_func_entry_get_num_args(self.ctx.ref(), self.entry))
6105 def arg_value(self, idx):
6106 """Return the value of argument `idx`.
6110 >>> s.add(f(0, 1) == 10, f(1, 2) == 20, f(1, 0) == 10)
6115 >>> f_i.num_entries()
6117 >>> e = f_i.entry(0)
6128 ...
except IndexError:
6129 ... print(
"index error")
6132 if idx >= self.num_args():
6134 return _to_expr_ref(Z3_func_entry_get_arg(self.ctx.ref(), self.entry, idx), self.ctx)
6137 """Return the value of the function at point `self`.
6141 >>> s.add(f(0, 1) == 10, f(1, 2) == 20, f(1, 0) == 10)
6146 >>> f_i.num_entries()
6148 >>> e = f_i.entry(0)
6156 return _to_expr_ref(Z3_func_entry_get_value(self.ctx.ref(), self.entry), self.ctx)
6159 """Return entry `self`
as a Python list.
6162 >>> s.add(f(0, 1) == 10, f(1, 2) == 20, f(1, 0) == 10)
6167 >>> f_i.num_entries()
6169 >>> e = f_i.entry(0)
6173 args = [self.arg_value(i) for i in range(self.num_args())]
6174 args.append(self.value())
6178 return repr(self.as_list())
6181 class FuncInterp(Z3PPObject):
6182 """Stores the interpretation of a function
in a Z3 model.
"""
6184 def __init__(self, f, ctx):
6187 if self.f is not None:
6188 Z3_func_interp_inc_ref(self.ctx.ref(), self.f)
6191 if self.f is not None and self.ctx.ref() is not None:
6192 Z3_func_interp_dec_ref(self.ctx.ref(), self.f)
6194 def else_value(self):
6196 Return the `
else` value
for a function interpretation.
6197 Return
None if Z3 did
not specify the `
else` value
for
6202 >>> s.add(
f(0) == 1,
f(1) == 1,
f(2) == 0)
6211 r = Z3_func_interp_get_else(self.ctx.ref(), self.f)
6213 return _to_expr_ref(r, self.ctx)
6217 def num_entries(self):
6218 """Return the number of entries/points
in the function interpretation `self`.
6222 >>> s.add(
f(0) == 1,
f(1) == 1,
f(2) == 0)
6231 return int(Z3_func_interp_get_num_entries(self.ctx.ref(), self.f))
6234 """Return the number of arguments
for each entry
in the function interpretation `self`.
6238 >>> s.add(
f(0) == 1,
f(1) == 1,
f(2) == 0)
6245 return int(Z3_func_interp_get_arity(self.ctx.ref(), self.f))
6247 def entry(self, idx):
6248 """Return an entry at position `idx < self.
num_entries()`
in the function interpretation `self`.
6252 >>> s.add(
f(0) == 1,
f(1) == 1,
f(2) == 0)
6263 if idx >= self.num_entries():
6265 return FuncEntry(Z3_func_interp_get_entry(self.ctx.ref(), self.f, idx), self.ctx)
6267 def translate(self, other_ctx):
6268 """Copy model
'self' to context
'other_ctx'.
6270 return ModelRef(Z3_model_translate(self.ctx.ref(), self.model, other_ctx.ref()), other_ctx)
6273 return self.translate(self.ctx)
6275 def __deepcopy__(self, memo={}):
6276 return self.translate(self.ctx)
6279 """Return the function interpretation
as a Python list.
6282 >>> s.add(
f(0) == 1,
f(1) == 1,
f(2) == 0)
6291 r = [self.entry(i).as_list() for i in range(self.num_entries())]
6292 r.append(self.else_value())
6296 return obj_to_string(self)
6299 class ModelRef(Z3PPObject):
6300 """Model/Solution of a satisfiability problem (aka system of constraints).
"""
6302 def __init__(self, m, ctx):
6303 assert ctx is not None
6306 Z3_model_inc_ref(self.ctx.ref(), self.model)
6309 if self.ctx.ref() is not None:
6310 Z3_model_dec_ref(self.ctx.ref(), self.model)
6313 return obj_to_string(self)
6316 """Return a textual representation of the s-expression representing the model.
"""
6317 return Z3_model_to_string(self.ctx.ref(), self.model)
6319 def eval(self, t, model_completion=False):
6320 """Evaluate the expression `t`
in the model `self`.
6321 If `model_completion`
is enabled, then a default interpretation
is automatically added
6322 for symbols that do
not have an interpretation
in the model `self`.
6326 >>> s.add(x > 0, x < 2)
6339 >>> m.eval(y, model_completion=
True)
6346 if Z3_model_eval(self.ctx.ref(), self.model, t.as_ast(), model_completion, r):
6347 return _to_expr_ref(r[0], self.ctx)
6348 raise Z3Exception("failed to evaluate expression in the model")
6350 def evaluate(self, t, model_completion=False):
6351 """Alias
for `eval`.
6355 >>> s.add(x > 0, x < 2)
6359 >>> m.evaluate(x + 1)
6361 >>> m.evaluate(x == 1)
6364 >>> m.evaluate(y + x)
6368 >>> m.evaluate(y, model_completion=
True)
6371 >>> m.evaluate(y + x)
6374 return self.eval(t, model_completion)
6377 """Return the number of constant
and function declarations
in the model `self`.
6382 >>> s.add(x > 0, f(x) != x)
6389 num_consts = int(Z3_model_get_num_consts(self.ctx.ref(), self.model))
6390 num_funcs = int(Z3_model_get_num_funcs(self.ctx.ref(), self.model))
6391 return num_consts + num_funcs
6393 def get_interp(self, decl):
6394 """Return the interpretation
for a given declaration
or constant.
6399 >>> s.add(x > 0, x < 2, f(x) == 0)
6409 _z3_assert(isinstance(decl, FuncDeclRef) or is_const(decl), "Z3 declaration expected")
6413 if decl.arity() == 0:
6414 _r = Z3_model_get_const_interp(self.ctx.ref(), self.model, decl.ast)
6415 if _r.value is None:
6417 r = _to_expr_ref(_r, self.ctx)
6419 return self.get_interp(get_as_array_func(r))
6423 return FuncInterp(Z3_model_get_func_interp(self.ctx.ref(), self.model, decl.ast), self.ctx)
6427 def num_sorts(self):
6428 """Return the number of uninterpreted sorts that contain an interpretation
in the model `self`.
6431 >>> a, b =
Consts(
'a b', A)
6440 return int(Z3_model_get_num_sorts(self.ctx.ref(), self.model))
6442 def get_sort(self, idx):
6443 """Return the uninterpreted sort at position `idx` < self.
num_sorts().
6447 >>> a1, a2 =
Consts(
'a1 a2', A)
6448 >>> b1, b2 =
Consts(
'b1 b2', B)
6450 >>> s.add(a1 != a2, b1 != b2)
6461 if idx >= self.num_sorts():
6463 return _to_sort_ref(Z3_model_get_sort(self.ctx.ref(), self.model, idx), self.ctx)
6466 """Return all uninterpreted sorts that have an interpretation
in the model `self`.
6470 >>> a1, a2 =
Consts(
'a1 a2', A)
6471 >>> b1, b2 =
Consts(
'b1 b2', B)
6473 >>> s.add(a1 != a2, b1 != b2)
6480 return [self.get_sort(i) for i in range(self.num_sorts())]
6482 def get_universe(self, s):
6483 """Return the interpretation
for the uninterpreted sort `s`
in the model `self`.
6486 >>> a, b =
Consts(
'a b', A)
6492 >>> m.get_universe(A)
6496 _z3_assert(isinstance(s, SortRef), "Z3 sort expected")
6498 return AstVector(Z3_model_get_sort_universe(self.ctx.ref(), self.model, s.ast), self.ctx)
6502 def __getitem__(self, idx):
6503 """If `idx`
is an integer, then the declaration at position `idx`
in the model `self`
is returned.
6504 If `idx`
is a declaration, then the actual interpretation
is returned.
6506 The elements can be retrieved using position
or the actual declaration.
6511 >>> s.add(x > 0, x < 2, f(x) == 0)
6525 >>>
for d
in m: print(
"%s -> %s" % (d, m[d]))
6530 if idx >= len(self):
6532 num_consts = Z3_model_get_num_consts(self.ctx.ref(), self.model)
6533 if (idx < num_consts):
6534 return FuncDeclRef(Z3_model_get_const_decl(self.ctx.ref(), self.model, idx), self.ctx)
6536 return FuncDeclRef(Z3_model_get_func_decl(self.ctx.ref(), self.model, idx - num_consts), self.ctx)
6537 if isinstance(idx, FuncDeclRef):
6538 return self.get_interp(idx)
6540 return self.get_interp(idx.decl())
6541 if isinstance(idx, SortRef):
6542 return self.get_universe(idx)
6544 _z3_assert(False, "Integer, Z3 declaration, or Z3 constant expected")
6548 """Return a list with all symbols that have an interpretation
in the model `self`.
6552 >>> s.add(x > 0, x < 2, f(x) == 0)
6560 for i in range(Z3_model_get_num_consts(self.ctx.ref(), self.model)):
6561 r.append(FuncDeclRef(Z3_model_get_const_decl(self.ctx.ref(), self.model, i), self.ctx))
6562 for i in range(Z3_model_get_num_funcs(self.ctx.ref(), self.model)):
6563 r.append(FuncDeclRef(Z3_model_get_func_decl(self.ctx.ref(), self.model, i), self.ctx))
6566 def update_value(self, x, value):
6567 """Update the interpretation of a constant
"""
6570 if not is_func_decl(x) or x.arity() != 0:
6571 raise Z3Exception("Expecting 0-ary function or constant expression")
6572 value = _py2expr(value)
6573 Z3_add_const_interp(x.ctx_ref(), self.model, x.ast, value.ast)
6575 def translate(self, target):
6576 """Translate `self` to the context `target`. That
is,
return a copy of `self`
in the context `target`.
6579 _z3_assert(isinstance(target, Context), "argument must be a Z3 context")
6580 model = Z3_model_translate(self.ctx.ref(), self.model, target.ref())
6581 return ModelRef(model, target)
6584 return self.translate(self.ctx)
6586 def __deepcopy__(self, memo={}):
6587 return self.translate(self.ctx)
6590 def Model(ctx=None):
6592 return ModelRef(Z3_mk_model(ctx.ref()), ctx)
6596 """Return true
if n
is a Z3 expression of the form (_
as-array f).
"""
6597 return isinstance(n, ExprRef) and Z3_is_as_array(n.ctx.ref(), n.as_ast())
6600 def get_as_array_func(n):
6601 """Return the function declaration f associated with a Z3 expression of the form (_
as-array f).
"""
6603 _z3_assert(is_as_array(n), "as-array Z3 expression expected.")
6604 return FuncDeclRef(Z3_get_as_array_func_decl(n.ctx.ref(), n.as_ast()), n.ctx)
6606 #########################################
6610 #########################################
6614 """Statistics
for `Solver.check()`.
"""
6616 def __init__(self, stats, ctx):
6619 Z3_stats_inc_ref(self.ctx.ref(), self.stats)
6621 def __deepcopy__(self, memo={}):
6622 return Statistics(self.stats, self.ctx)
6625 if self.ctx.ref() is not None:
6626 Z3_stats_dec_ref(self.ctx.ref(), self.stats)
6632 out.write(u('<table border="1" cellpadding="2" cellspacing="0">'))
6635 out.write(u('<tr style="background-color:#CFCFCF">'))
6638 out.write(u("<tr>"))
6640 out.write(u("<td>%s</td><td>%s</td></tr>" % (k, v)))
6641 out.write(u("</table>"))
6642 return out.getvalue()
6644 return Z3_stats_to_string(self.ctx.ref(), self.stats)
6647 """Return the number of statistical counters.
6650 >>> s =
Then(
'simplify',
'nlsat').solver()
6654 >>> st = s.statistics()
6658 return int(Z3_stats_size(self.ctx.ref(), self.stats))
6660 def __getitem__(self, idx):
6661 """Return the value of statistical counter at position `idx`. The result
is a pair (key, value).
6664 >>> s =
Then(
'simplify',
'nlsat').solver()
6668 >>> st = s.statistics()
6672 (
'nlsat propagations', 2)
6676 if idx >= len(self):
6678 if Z3_stats_is_uint(self.ctx.ref(), self.stats, idx):
6679 val = int(Z3_stats_get_uint_value(self.ctx.ref(), self.stats, idx))
6681 val = Z3_stats_get_double_value(self.ctx.ref(), self.stats, idx)
6682 return (Z3_stats_get_key(self.ctx.ref(), self.stats, idx), val)
6685 """Return the list of statistical counters.
6688 >>> s =
Then(
'simplify',
'nlsat').solver()
6692 >>> st = s.statistics()
6694 return [Z3_stats_get_key(self.ctx.ref(), self.stats, idx) for idx in range(len(self))]
6696 def get_key_value(self, key):
6697 """Return the value of a particular statistical counter.
6700 >>> s =
Then(
'simplify',
'nlsat').solver()
6704 >>> st = s.statistics()
6705 >>> st.get_key_value(
'nlsat propagations')
6708 for idx in range(len(self)):
6709 if key == Z3_stats_get_key(self.ctx.ref(), self.stats, idx):
6710 if Z3_stats_is_uint(self.ctx.ref(), self.stats, idx):
6711 return int(Z3_stats_get_uint_value(self.ctx.ref(), self.stats, idx))
6713 return Z3_stats_get_double_value(self.ctx.ref(), self.stats, idx)
6714 raise Z3Exception("unknown key")
6716 def __getattr__(self, name):
6717 """Access the value of statistical using attributes.
6719 Remark: to access a counter containing blank spaces (e.g.,
'nlsat propagations'),
6720 we should use
'_' (e.g.,
'nlsat_propagations').
6723 >>> s =
Then(
'simplify',
'nlsat').solver()
6727 >>> st = s.statistics()
6728 >>> st.nlsat_propagations
6733 key = name.replace("_", " ")
6735 return self.get_key_value(key)
6737 raise AttributeError
6739 #########################################
6743 #########################################
6746 class CheckSatResult:
6747 """Represents the result of a satisfiability check: sat, unsat, unknown.
6753 >>> isinstance(r, CheckSatResult)
6757 def __init__(self, r):
6760 def __deepcopy__(self, memo={}):
6761 return CheckSatResult(self.r)
6763 def __eq__(self, other):
6764 return isinstance(other, CheckSatResult) and self.r == other.r
6766 def __ne__(self, other):
6767 return not self.__eq__(other)
6771 if self.r == Z3_L_TRUE:
6773 elif self.r == Z3_L_FALSE:
6774 return "<b>unsat</b>"
6776 return "<b>unknown</b>"
6778 if self.r == Z3_L_TRUE:
6780 elif self.r == Z3_L_FALSE:
6785 def _repr_html_(self):
6786 in_html = in_html_mode()
6789 set_html_mode(in_html)
6793 sat = CheckSatResult(Z3_L_TRUE)
6794 unsat = CheckSatResult(Z3_L_FALSE)
6795 unknown = CheckSatResult(Z3_L_UNDEF)
6798 class Solver(Z3PPObject):
6800 Solver API provides methods
for implementing the main SMT 2.0 commands:
6801 push, pop, check, get-model, etc.
6804 def __init__(self, solver=None, ctx=None, logFile=None):
6805 assert solver is None or ctx is not None
6806 self.ctx = _get_ctx(ctx)
6807 self.backtrack_level = 4000000000
6810 self.solver = Z3_mk_solver(self.ctx.ref())
6812 self.solver = solver
6813 Z3_solver_inc_ref(self.ctx.ref(), self.solver)
6814 if logFile is not None:
6815 self.set("smtlib2_log", logFile)
6818 if self.solver is not None and self.ctx.ref() is not None:
6819 Z3_solver_dec_ref(self.ctx.ref(), self.solver)
6821 def set(self, *args, **keys):
6822 """Set a configuration option.
6823 The method `
help()`
return a string containing all available options.
6827 >>> s.set(mbqi=
True)
6828 >>> s.set(
'MBQI',
True)
6829 >>> s.set(
':mbqi',
True)
6831 p = args2params(args, keys, self.ctx)
6832 Z3_solver_set_params(self.ctx.ref(), self.solver, p.params)
6835 """Create a backtracking point.
6854 Z3_solver_push(self.ctx.ref(), self.solver)
6856 def pop(self, num=1):
6857 """Backtrack \\c num backtracking points.
6876 Z3_solver_pop(self.ctx.ref(), self.solver, num)
6878 def num_scopes(self):
6879 """Return the current number of backtracking points.
6894 return Z3_solver_get_num_scopes(self.ctx.ref(), self.solver)
6897 """Remove all asserted constraints
and backtracking points created using `
push()`.
6908 Z3_solver_reset(self.ctx.ref(), self.solver)
6910 def assert_exprs(self, *args):
6911 """Assert constraints into the solver.
6915 >>> s.assert_exprs(x > 0, x < 2)
6919 args = _get_args(args)
6920 s = BoolSort(self.ctx)
6922 if isinstance(arg, Goal) or isinstance(arg, AstVector):
6924 Z3_solver_assert(self.ctx.ref(), self.solver, f.as_ast())
6927 Z3_solver_assert(self.ctx.ref(), self.solver, arg.as_ast())
6929 def add(self, *args):
6930 """Assert constraints into the solver.
6934 >>> s.add(x > 0, x < 2)
6938 self.assert_exprs(*args)
6940 def __iadd__(self, fml):
6944 def append(self, *args):
6945 """Assert constraints into the solver.
6949 >>> s.append(x > 0, x < 2)
6953 self.assert_exprs(*args)
6955 def insert(self, *args):
6956 """Assert constraints into the solver.
6960 >>> s.insert(x > 0, x < 2)
6964 self.assert_exprs(*args)
6966 def assert_and_track(self, a, p):
6967 """Assert constraint `a`
and track it
in the unsat core using the Boolean constant `p`.
6969 If `p`
is a string, it will be automatically converted into a Boolean constant.
6974 >>> s.set(unsat_core=
True)
6975 >>> s.assert_and_track(x > 0,
'p1')
6976 >>> s.assert_and_track(x != 1,
'p2')
6977 >>> s.assert_and_track(x < 0, p3)
6978 >>> print(s.check())
6980 >>> c = s.unsat_core()
6990 if isinstance(p, str):
6991 p = Bool(p, self.ctx)
6992 _z3_assert(isinstance(a, BoolRef), "Boolean expression expected")
6993 _z3_assert(isinstance(p, BoolRef) and is_const(p), "Boolean expression expected")
6994 Z3_solver_assert_and_track(self.ctx.ref(), self.solver, a.as_ast(), p.as_ast())
6996 def check(self, *assumptions):
6997 """Check whether the assertions
in the given solver plus the optional assumptions are consistent
or not.
7003 >>> s.add(x > 0, x < 2)
7006 >>> s.model().eval(x)
7012 >>> s.add(2**x == 4)
7016 s = BoolSort(self.ctx)
7017 assumptions = _get_args(assumptions)
7018 num = len(assumptions)
7019 _assumptions = (Ast * num)()
7020 for i in range(num):
7021 _assumptions[i] = s.cast(assumptions[i]).as_ast()
7022 r = Z3_solver_check_assumptions(self.ctx.ref(), self.solver, num, _assumptions)
7023 return CheckSatResult(r)
7026 """Return a model
for the last `
check()`.
7028 This function raises an exception
if
7029 a model
is not available (e.g., last `
check()` returned unsat).
7033 >>> s.add(a + 2 == 0)
7040 return ModelRef(Z3_solver_get_model(self.ctx.ref(), self.solver), self.ctx)
7042 raise Z3Exception("model is not available")
7044 def import_model_converter(self, other):
7045 """Import model converter
from other into the current solver
"""
7046 Z3_solver_import_model_converter(self.ctx.ref(), other.solver, self.solver)
7048 def unsat_core(self):
7049 """Return a subset (
as an AST vector) of the assumptions provided to the last
check().
7051 These are the assumptions Z3 used
in the unsatisfiability proof.
7052 Assumptions are available
in Z3. They are used to extract unsatisfiable cores.
7053 They may be also used to
"retract" assumptions. Note that, assumptions are
not really
7054 "soft constraints", but they can be used to implement them.
7056 >>> p1, p2, p3 =
Bools(
'p1 p2 p3')
7057 >>> x, y =
Ints(
'x y')
7062 >>> s.add(
Implies(p3, y > -3))
7063 >>> s.check(p1, p2, p3)
7065 >>> core = s.unsat_core()
7078 return AstVector(Z3_solver_get_unsat_core(self.ctx.ref(), self.solver), self.ctx)
7080 def consequences(self, assumptions, variables):
7081 """Determine fixed values
for the variables based on the solver state
and assumptions.
7083 >>> a, b, c, d =
Bools(
'a b c d')
7085 >>> s.consequences([a],[b,c,d])
7087 >>> s.consequences([
Not(c),d],[a,b,c,d])
7090 if isinstance(assumptions, list):
7091 _asms = AstVector(None, self.ctx)
7092 for a in assumptions:
7095 if isinstance(variables, list):
7096 _vars = AstVector(None, self.ctx)
7100 _z3_assert(isinstance(assumptions, AstVector), "ast vector expected")
7101 _z3_assert(isinstance(variables, AstVector), "ast vector expected")
7102 consequences = AstVector(None, self.ctx)
7103 r = Z3_solver_get_consequences(self.ctx.ref(), self.solver, assumptions.vector,
7104 variables.vector, consequences.vector)
7105 sz = len(consequences)
7106 consequences = [consequences[i] for i in range(sz)]
7107 return CheckSatResult(r), consequences
7109 def from_file(self, filename):
7110 """Parse assertions
from a file
"""
7111 Z3_solver_from_file(self.ctx.ref(), self.solver, filename)
7113 def from_string(self, s):
7114 """Parse assertions
from a string
"""
7115 Z3_solver_from_string(self.ctx.ref(), self.solver, s)
7117 def cube(self, vars=None):
7119 The method takes an optional set of variables that restrict which
7120 variables may be used
as a starting point
for cubing.
7121 If vars
is not None, then the first case split
is based on a variable
in
7124 self.cube_vs = AstVector(None, self.ctx)
7125 if vars is not None:
7127 self.cube_vs.push(v)
7129 lvl = self.backtrack_level
7130 self.backtrack_level = 4000000000
7131 r = AstVector(Z3_solver_cube(self.ctx.ref(), self.solver, self.cube_vs.vector, lvl), self.ctx)
7132 if (len(r) == 1 and is_false(r[0])):
7138 def cube_vars(self):
7139 """Access the set of variables that were touched by the most recently generated cube.
7140 This set of variables can be used
as a starting point
for additional cubes.
7141 The idea
is that variables that appear
in clauses that are reduced by the most recent
7142 cube are likely more useful to cube on.
"""
7146 """Return a proof
for the last `
check()`. Proof construction must be enabled.
"""
7147 return _to_expr_ref(Z3_solver_get_proof(self.ctx.ref(), self.solver), self.ctx)
7149 def assertions(self):
7150 """Return an AST vector containing all added constraints.
7161 return AstVector(Z3_solver_get_assertions(self.ctx.ref(), self.solver), self.ctx)
7164 """Return an AST vector containing all currently inferred units.
7166 return AstVector(Z3_solver_get_units(self.ctx.ref(), self.solver), self.ctx)
7168 def non_units(self):
7169 """Return an AST vector containing all atomic formulas
in solver state that are
not units.
7171 return AstVector(Z3_solver_get_non_units(self.ctx.ref(), self.solver), self.ctx)
7173 def trail_levels(self):
7174 """Return trail
and decision levels of the solver state after a
check() call.
7176 trail = self.trail()
7177 levels = (ctypes.c_uint * len(trail))()
7178 Z3_solver_get_levels(self.ctx.ref(), self.solver, trail.vector, len(trail), levels)
7179 return trail, levels
7182 """Return trail of the solver state after a
check() call.
7184 return AstVector(Z3_solver_get_trail(self.ctx.ref(), self.solver), self.ctx)
7186 def statistics(self):
7187 """Return statistics
for the last `
check()`.
7194 >>> st = s.statistics()
7195 >>> st.get_key_value(
'final checks')
7202 return Statistics(Z3_solver_get_statistics(self.ctx.ref(), self.solver), self.ctx)
7204 def reason_unknown(self):
7205 """Return a string describing why the last `
check()` returned `unknown`.
7209 >>> s.add(2**x == 4)
7212 >>> s.reason_unknown()
7213 '(incomplete (theory arithmetic))'
7215 return Z3_solver_get_reason_unknown(self.ctx.ref(), self.solver)
7218 """Display a string describing all available options.
"""
7219 print(Z3_solver_get_help(self.ctx.ref(), self.solver))
7221 def param_descrs(self):
7222 """Return the parameter description set.
"""
7223 return ParamDescrsRef(Z3_solver_get_param_descrs(self.ctx.ref(), self.solver), self.ctx)
7226 """Return a formatted string with all added constraints.
"""
7227 return obj_to_string(self)
7229 def translate(self, target):
7230 """Translate `self` to the context `target`. That
is,
return a copy of `self`
in the context `target`.
7235 >>> s2 = s1.translate(c2)
7238 _z3_assert(isinstance(target, Context), "argument must be a Z3 context")
7239 solver = Z3_solver_translate(self.ctx.ref(), self.solver, target.ref())
7240 return Solver(solver, target)
7243 return self.translate(self.ctx)
7245 def __deepcopy__(self, memo={}):
7246 return self.translate(self.ctx)
7249 """Return a formatted string (
in Lisp-like format) with all added constraints.
7250 We say the string
is in s-expression format.
7258 return Z3_solver_to_string(self.ctx.ref(), self.solver)
7260 def dimacs(self, include_names=True):
7261 """Return a textual representation of the solver
in DIMACS format.
"""
7262 return Z3_solver_to_dimacs_string(self.ctx.ref(), self.solver, include_names)
7265 """return SMTLIB2 formatted benchmark
for solver
's assertions"""
7272 for i
in range(sz1):
7273 v[i] = es[i].as_ast()
7275 e = es[sz1].as_ast()
7279 self.ctx.ref(),
"benchmark generated from python API",
"",
"unknown",
"", sz1, v, e,
7284 """Create a solver customized for the given logic.
7286 The parameter `logic` is a string. It should be contains
7287 the name of a SMT-LIB logic.
7288 See http://www.smtlib.org/ for the name of all available logics.
7290 >>> s = SolverFor("QF_LIA")
7305 """Return a simple general purpose solver with limited amount of preprocessing.
7307 >>> s = SimpleSolver()
7324 """Fixedpoint API provides methods for solving with recursive predicates"""
7327 assert fixedpoint
is None or ctx
is not None
7330 if fixedpoint
is None:
7341 if self.
fixedpoint is not None and self.ctx.ref()
is not None:
7345 """Set a configuration option. The method `help()` return a string containing all available options.
7351 """Display a string describing all available options."""
7355 """Return the parameter description set."""
7359 """Assert constraints as background axioms for the fixedpoint solver."""
7360 args = _get_args(args)
7363 if isinstance(arg, Goal)
or isinstance(arg, AstVector):
7373 """Assert constraints as background axioms for the fixedpoint solver. Alias for assert_expr."""
7381 """Assert constraints as background axioms for the fixedpoint solver. Alias for assert_expr."""
7385 """Assert constraints as background axioms for the fixedpoint solver. Alias for assert_expr."""
7389 """Assert rules defining recursive predicates to the fixedpoint solver.
7392 >>> s = Fixedpoint()
7393 >>> s.register_relation(a.decl())
7394 >>> s.register_relation(b.decl())
7407 body = _get_args(body)
7411 def rule(self, head, body=None, name=None):
7412 """Assert rules defining recursive predicates to the fixedpoint solver. Alias for add_rule."""
7416 """Assert facts defining recursive predicates to the fixedpoint solver. Alias for add_rule."""
7420 """Query the fixedpoint engine whether formula is derivable.
7421 You can also pass an tuple or list of recursive predicates.
7423 query = _get_args(query)
7425 if sz >= 1
and isinstance(query[0], FuncDeclRef):
7426 _decls = (FuncDecl * sz)()
7436 query =
And(query, self.
ctx)
7437 query = self.
abstract(query,
False)
7442 """Query the fixedpoint engine whether formula is derivable starting at the given query level.
7444 query = _get_args(query)
7446 if sz >= 1
and isinstance(query[0], FuncDecl):
7447 _z3_assert(
False,
"unsupported")
7453 query = self.
abstract(query,
False)
7454 r = Z3_fixedpoint_query_from_lvl(self.ctx.ref(), self.
fixedpoint, query.as_ast(), lvl)
7462 body = _get_args(body)
7467 """Retrieve answer from last query call."""
7469 return _to_expr_ref(r, self.
ctx)
7472 """Retrieve a ground cex from last query call."""
7473 r = Z3_fixedpoint_get_ground_sat_answer(self.ctx.ref(), self.
fixedpoint)
7474 return _to_expr_ref(r, self.
ctx)
7477 """retrieve rules along the counterexample trace"""
7481 """retrieve rule names along the counterexample trace"""
7484 names = _symbol2py(self.
ctx, Z3_fixedpoint_get_rule_names_along_trace(self.ctx.ref(), self.
fixedpoint))
7486 return names.split(
";")
7489 """Retrieve number of levels used for predicate in PDR engine"""
7493 """Retrieve properties known about predicate for the level'th unfolding.
7494 -1 is treated as the limit (infinity)
7497 return _to_expr_ref(r, self.
ctx)
7500 """Add property to predicate for the level'th unfolding.
7501 -1 is treated as infinity (infinity)
7506 """Register relation as recursive"""
7507 relations = _get_args(relations)
7512 """Control how relation is represented"""
7513 representations = _get_args(representations)
7514 representations = [
to_symbol(s)
for s
in representations]
7515 sz = len(representations)
7516 args = (Symbol * sz)()
7518 args[i] = representations[i]
7522 """Parse rules and queries from a string"""
7526 """Parse rules and queries from a file"""
7530 """retrieve rules that have been added to fixedpoint context"""
7534 """retrieve assertions that have been added to fixedpoint context"""
7538 """Return a formatted string with all added rules and constraints."""
7542 """Return a formatted string (in Lisp-like format) with all added constraints.
7543 We say the string is in s-expression format.
7548 """Return a formatted string (in Lisp-like format) with all added constraints.
7549 We say the string is in s-expression format.
7550 Include also queries.
7552 args, len = _to_ast_array(queries)
7556 """Return statistics for the last `query()`.
7561 """Return a string describing why the last `query()` returned `unknown`.
7566 """Add variable or several variables.
7567 The added variable or variables will be bound in the rules
7570 vars = _get_args(vars)
7590 """Finite domain sort."""
7593 """Return the size of the finite domain sort"""
7594 r = (ctypes.c_ulonglong * 1)()
7598 raise Z3Exception(
"Failed to retrieve finite domain sort size")
7602 """Create a named finite domain sort of a given size sz"""
7603 if not isinstance(name, Symbol):
7610 """Return True if `s` is a Z3 finite-domain sort.
7612 >>> is_finite_domain_sort(FiniteDomainSort('S', 100))
7614 >>> is_finite_domain_sort(IntSort())
7617 return isinstance(s, FiniteDomainSortRef)
7621 """Finite-domain expressions."""
7624 """Return the sort of the finite-domain expression `self`."""
7628 """Return a Z3 floating point expression as a Python string."""
7633 """Return `True` if `a` is a Z3 finite-domain expression.
7635 >>> s = FiniteDomainSort('S', 100)
7636 >>> b = Const('b', s)
7637 >>> is_finite_domain(b)
7639 >>> is_finite_domain(Int('x'))
7642 return isinstance(a, FiniteDomainRef)
7646 """Integer values."""
7649 """Return a Z3 finite-domain numeral as a Python long (bignum) numeral.
7651 >>> s = FiniteDomainSort('S', 100)
7652 >>> v = FiniteDomainVal(3, s)
7661 """Return a Z3 finite-domain numeral as a Python string.
7663 >>> s = FiniteDomainSort('S', 100)
7664 >>> v = FiniteDomainVal(42, s)
7672 """Return a Z3 finite-domain value. If `ctx=None`, then the global context is used.
7674 >>> s = FiniteDomainSort('S', 256)
7675 >>> FiniteDomainVal(255, s)
7677 >>> FiniteDomainVal('100', s)
7687 """Return `True` if `a` is a Z3 finite-domain value.
7689 >>> s = FiniteDomainSort('S', 100)
7690 >>> b = Const('b', s)
7691 >>> is_finite_domain_value(b)
7693 >>> b = FiniteDomainVal(10, s)
7696 >>> is_finite_domain_value(b)
7743 def _global_on_model(ctx):
7744 (fn, mdl) = _on_models[ctx]
7748 _on_model_eh = on_model_eh_type(_global_on_model)
7752 """Optimize API provides methods for solving using objective functions and weighted soft constraints"""
7764 if self.
optimize is not None and self.ctx.ref()
is not None:
7770 """Set a configuration option.
7771 The method `help()` return a string containing all available options.
7777 """Display a string describing all available options."""
7781 """Return the parameter description set."""
7785 """Assert constraints as background axioms for the optimize solver."""
7786 args = _get_args(args)
7789 if isinstance(arg, Goal)
or isinstance(arg, AstVector):
7797 """Assert constraints as background axioms for the optimize solver. Alias for assert_expr."""
7805 """Assert constraint `a` and track it in the unsat core using the Boolean constant `p`.
7807 If `p` is a string, it will be automatically converted into a Boolean constant.
7812 >>> s.assert_and_track(x > 0, 'p1')
7813 >>> s.assert_and_track(x != 1, 'p2')
7814 >>> s.assert_and_track(x < 0, p3)
7815 >>> print(s.check())
7817 >>> c = s.unsat_core()
7827 if isinstance(p, str):
7829 _z3_assert(isinstance(a, BoolRef),
"Boolean expression expected")
7830 _z3_assert(isinstance(p, BoolRef)
and is_const(p),
"Boolean expression expected")
7834 """Add soft constraint with optional weight and optional identifier.
7835 If no weight is supplied, then the penalty for violating the soft constraint
7837 Soft constraints are grouped by identifiers. Soft constraints that are
7838 added without identifiers are grouped by default.
7841 weight =
"%d" % weight
7842 elif isinstance(weight, float):
7843 weight =
"%f" % weight
7844 if not isinstance(weight, str):
7845 raise Z3Exception(
"weight should be a string or an integer")
7853 if sys.version_info.major >= 3
and isinstance(arg, Iterable):
7854 return [asoft(a)
for a
in arg]
7858 """Add objective function to maximize."""
7866 """Add objective function to minimize."""
7874 """create a backtracking point for added rules, facts and assertions"""
7878 """restore to previously created backtracking point"""
7882 """Check satisfiability while optimizing objective functions."""
7883 assumptions = _get_args(assumptions)
7884 num = len(assumptions)
7885 _assumptions = (Ast * num)()
7886 for i
in range(num):
7887 _assumptions[i] = assumptions[i].as_ast()
7891 """Return a string that describes why the last `check()` returned `unknown`."""
7895 """Return a model for the last check()."""
7899 raise Z3Exception(
"model is not available")
7905 if not isinstance(obj, OptimizeObjective):
7906 raise Z3Exception(
"Expecting objective handle returned by maximize/minimize")
7910 if not isinstance(obj, OptimizeObjective):
7911 raise Z3Exception(
"Expecting objective handle returned by maximize/minimize")
7915 if not isinstance(obj, OptimizeObjective):
7916 raise Z3Exception(
"Expecting objective handle returned by maximize/minimize")
7917 return obj.lower_values()
7920 if not isinstance(obj, OptimizeObjective):
7921 raise Z3Exception(
"Expecting objective handle returned by maximize/minimize")
7922 return obj.upper_values()
7925 """Parse assertions and objectives from a file"""
7929 """Parse assertions and objectives from a string"""
7933 """Return an AST vector containing all added constraints."""
7937 """returns set of objective functions"""
7941 """Return a formatted string with all added rules and constraints."""
7945 """Return a formatted string (in Lisp-like format) with all added constraints.
7946 We say the string is in s-expression format.
7951 """Return statistics for the last check`.
7956 """Register a callback that is invoked with every incremental improvement to
7957 objective values. The callback takes a model as argument.
7958 The life-time of the model is limited to the callback so the
7959 model has to be (deep) copied if it is to be used after the callback
7961 id = len(_on_models) + 41
7963 _on_models[id] = (on_model, mdl)
7966 self.ctx.ref(), self.
optimize, mdl.model, ctypes.c_void_p(id), _on_model_eh,
7976 """An ApplyResult object contains the subgoals produced by a tactic when applied to a goal.
7977 It also contains model and proof converters.
7989 if self.ctx.ref()
is not None:
7993 """Return the number of subgoals in `self`.
7995 >>> a, b = Ints('a b')
7997 >>> g.add(Or(a == 0, a == 1), Or(b == 0, b == 1), a > b)
7998 >>> t = Tactic('split-clause')
8002 >>> t = Then(Tactic('split-clause'), Tactic('split-clause'))
8005 >>> t = Then(Tactic('split-clause'), Tactic('split-clause'), Tactic('propagate-values'))
8012 """Return one of the subgoals stored in ApplyResult object `self`.
8014 >>> a, b = Ints('a b')
8016 >>> g.add(Or(a == 0, a == 1), Or(b == 0, b == 1), a > b)
8017 >>> t = Tactic('split-clause')
8020 [a == 0, Or(b == 0, b == 1), a > b]
8022 [a == 1, Or(b == 0, b == 1), a > b]
8024 if idx >= len(self):
8029 return obj_to_string(self)
8032 """Return a textual representation of the s-expression representing the set of subgoals in `self`."""
8036 """Return a Z3 expression consisting of all subgoals.
8041 >>> g.add(Or(x == 2, x == 3))
8042 >>> r = Tactic('simplify')(g)
8044 [[Not(x <= 1), Or(x == 2, x == 3)]]
8046 And(Not(x <= 1), Or(x == 2, x == 3))
8047 >>> r = Tactic('split-clause')(g)
8049 [[x > 1, x == 2], [x > 1, x == 3]]
8051 Or(And(x > 1, x == 2), And(x > 1, x == 3))
8069 """Tactics transform, solver and/or simplify sets of constraints (Goal).
8070 A Tactic can be converted into a Solver using the method solver().
8072 Several combinators are available for creating new tactics using the built-in ones:
8073 Then(), OrElse(), FailIf(), Repeat(), When(), Cond().
8079 if isinstance(tactic, TacticObj):
8083 _z3_assert(isinstance(tactic, str),
"tactic name expected")
8087 raise Z3Exception(
"unknown tactic '%s'" % tactic)
8094 if self.
tactic is not None and self.ctx.ref()
is not None:
8098 """Create a solver using the tactic `self`.
8100 The solver supports the methods `push()` and `pop()`, but it
8101 will always solve each `check()` from scratch.
8103 >>> t = Then('simplify', 'nlsat')
8106 >>> s.add(x**2 == 2, x > 0)
8114 def apply(self, goal, *arguments, **keywords):
8115 """Apply tactic `self` to the given goal or Z3 Boolean expression using the given options.
8117 >>> x, y = Ints('x y')
8118 >>> t = Tactic('solve-eqs')
8119 >>> t.apply(And(x == 0, y >= x + 1))
8123 _z3_assert(isinstance(goal, (Goal, BoolRef)),
"Z3 Goal or Boolean expressions expected")
8124 goal = _to_goal(goal)
8125 if len(arguments) > 0
or len(keywords) > 0:
8132 """Apply tactic `self` to the given goal or Z3 Boolean expression using the given options.
8134 >>> x, y = Ints('x y')
8135 >>> t = Tactic('solve-eqs')
8136 >>> t(And(x == 0, y >= x + 1))
8139 return self.
apply(goal, *arguments, **keywords)
8142 """Display a string containing a description of the available options for the `self` tactic."""
8146 """Return the parameter description set."""
8151 if isinstance(a, BoolRef):
8152 goal =
Goal(ctx=a.ctx)
8159 def _to_tactic(t, ctx=None):
8160 if isinstance(t, Tactic):
8166 def _and_then(t1, t2, ctx=None):
8167 t1 = _to_tactic(t1, ctx)
8168 t2 = _to_tactic(t2, ctx)
8170 _z3_assert(t1.ctx == t2.ctx,
"Context mismatch")
8174 def _or_else(t1, t2, ctx=None):
8175 t1 = _to_tactic(t1, ctx)
8176 t2 = _to_tactic(t2, ctx)
8178 _z3_assert(t1.ctx == t2.ctx,
"Context mismatch")
8183 """Return a tactic that applies the tactics in `*ts` in sequence.
8185 >>> x, y = Ints('x y')
8186 >>> t = AndThen(Tactic('simplify'), Tactic('solve-eqs'))
8187 >>> t(And(x == 0, y > x + 1))
8189 >>> t(And(x == 0, y > x + 1)).as_expr()
8193 _z3_assert(len(ts) >= 2,
"At least two arguments expected")
8194 ctx = ks.get(
"ctx",
None)
8197 for i
in range(num - 1):
8198 r = _and_then(r, ts[i + 1], ctx)
8203 """Return a tactic that applies the tactics in `*ts` in sequence. Shorthand for AndThen(*ts, **ks).
8205 >>> x, y = Ints('x y')
8206 >>> t = Then(Tactic('simplify'), Tactic('solve-eqs'))
8207 >>> t(And(x == 0, y > x + 1))
8209 >>> t(And(x == 0, y > x + 1)).as_expr()
8216 """Return a tactic that applies the tactics in `*ts` until one of them succeeds (it doesn't fail).
8219 >>> t = OrElse(Tactic('split-clause'), Tactic('skip'))
8220 >>> # Tactic split-clause fails if there is no clause in the given goal.
8223 >>> t(Or(x == 0, x == 1))
8224 [[x == 0], [x == 1]]
8227 _z3_assert(len(ts) >= 2,
"At least two arguments expected")
8228 ctx = ks.get(
"ctx",
None)
8231 for i
in range(num - 1):
8232 r = _or_else(r, ts[i + 1], ctx)
8237 """Return a tactic that applies the tactics in `*ts` in parallel until one of them succeeds (it doesn't fail).
8240 >>> t = ParOr(Tactic('simplify'), Tactic('fail'))
8245 _z3_assert(len(ts) >= 2,
"At least two arguments expected")
8246 ctx = _get_ctx(ks.get(
"ctx",
None))
8247 ts = [_to_tactic(t, ctx)
for t
in ts]
8249 _args = (TacticObj * sz)()
8251 _args[i] = ts[i].tactic
8256 """Return a tactic that applies t1 and then t2 to every subgoal produced by t1.
8257 The subgoals are processed in parallel.
8259 >>> x, y = Ints('x y')
8260 >>> t = ParThen(Tactic('split-clause'), Tactic('propagate-values'))
8261 >>> t(And(Or(x == 1, x == 2), y == x + 1))
8262 [[x == 1, y == 2], [x == 2, y == 3]]
8264 t1 = _to_tactic(t1, ctx)
8265 t2 = _to_tactic(t2, ctx)
8267 _z3_assert(t1.ctx == t2.ctx,
"Context mismatch")
8272 """Alias for ParThen(t1, t2, ctx)."""
8277 """Return a tactic that applies tactic `t` using the given configuration options.
8279 >>> x, y = Ints('x y')
8280 >>> t = With(Tactic('simplify'), som=True)
8281 >>> t((x + 1)*(y + 2) == 0)
8282 [[2*x + y + x*y == -2]]
8284 ctx = keys.pop(
"ctx",
None)
8285 t = _to_tactic(t, ctx)
8291 """Return a tactic that applies tactic `t` using the given configuration options.
8293 >>> x, y = Ints('x y')
8295 >>> p.set("som", True)
8296 >>> t = WithParams(Tactic('simplify'), p)
8297 >>> t((x + 1)*(y + 2) == 0)
8298 [[2*x + y + x*y == -2]]
8300 t = _to_tactic(t,
None)
8305 """Return a tactic that keeps applying `t` until the goal is not modified anymore
8306 or the maximum number of iterations `max` is reached.
8308 >>> x, y = Ints('x y')
8309 >>> c = And(Or(x == 0, x == 1), Or(y == 0, y == 1), x > y)
8310 >>> t = Repeat(OrElse(Tactic('split-clause'), Tactic('skip')))
8312 >>> for subgoal in r: print(subgoal)
8313 [x == 0, y == 0, x > y]
8314 [x == 0, y == 1, x > y]
8315 [x == 1, y == 0, x > y]
8316 [x == 1, y == 1, x > y]
8317 >>> t = Then(t, Tactic('propagate-values'))
8321 t = _to_tactic(t, ctx)
8326 """Return a tactic that applies `t` to a given goal for `ms` milliseconds.
8328 If `t` does not terminate in `ms` milliseconds, then it fails.
8330 t = _to_tactic(t, ctx)
8335 """Return a list of all available tactics in Z3.
8338 >>> l.count('simplify') == 1
8346 """Return a short description for the tactic named `name`.
8348 >>> d = tactic_description('simplify')
8355 """Display a (tabular) description of all available tactics in Z3."""
8358 print(
'<table border="1" cellpadding="2" cellspacing="0">')
8361 print(
'<tr style="background-color:#CFCFCF">')
8366 print(
"<td>%s</td><td>%s</td></tr>" % (t, insert_line_breaks(
tactic_description(t), 40)))
8374 """Probes are used to inspect a goal (aka problem) and collect information that may be used
8375 to decide which solver and/or preprocessing step will be used.
8381 if isinstance(probe, ProbeObj):
8383 elif isinstance(probe, float):
8385 elif _is_int(probe):
8387 elif isinstance(probe, bool):
8394 _z3_assert(isinstance(probe, str),
"probe name expected")
8398 raise Z3Exception(
"unknown probe '%s'" % probe)
8405 if self.
probe is not None and self.ctx.ref()
is not None:
8409 """Return a probe that evaluates to "true" when the value returned by `self`
8410 is less than the value returned by `other`.
8412 >>> p = Probe('size') < 10
8423 """Return a probe that evaluates to "true" when the value returned by `self`
8424 is greater than the value returned by `other`.
8426 >>> p = Probe('size') > 10
8437 """Return a probe that evaluates to "true" when the value returned by `self`
8438 is less than or equal to the value returned by `other`.
8440 >>> p = Probe('size') <= 2
8451 """Return a probe that evaluates to "true" when the value returned by `self`
8452 is greater than or equal to the value returned by `other`.
8454 >>> p = Probe('size') >= 2
8465 """Return a probe that evaluates to "true" when the value returned by `self`
8466 is equal to the value returned by `other`.
8468 >>> p = Probe('size') == 2
8479 """Return a probe that evaluates to "true" when the value returned by `self`
8480 is not equal to the value returned by `other`.
8482 >>> p = Probe('size') != 2
8494 """Evaluate the probe `self` in the given goal.
8496 >>> p = Probe('size')
8506 >>> p = Probe('num-consts')
8509 >>> p = Probe('is-propositional')
8512 >>> p = Probe('is-qflia')
8517 _z3_assert(isinstance(goal, (Goal, BoolRef)),
"Z3 Goal or Boolean expression expected")
8518 goal = _to_goal(goal)
8523 """Return `True` if `p` is a Z3 probe.
8525 >>> is_probe(Int('x'))
8527 >>> is_probe(Probe('memory'))
8530 return isinstance(p, Probe)
8533 def _to_probe(p, ctx=None):
8537 return Probe(p, ctx)
8541 """Return a list of all available probes in Z3.
8544 >>> l.count('memory') == 1
8552 """Return a short description for the probe named `name`.
8554 >>> d = probe_description('memory')
8561 """Display a (tabular) description of all available probes in Z3."""
8564 print(
'<table border="1" cellpadding="2" cellspacing="0">')
8567 print(
'<tr style="background-color:#CFCFCF">')
8572 print(
"<td>%s</td><td>%s</td></tr>" % (p, insert_line_breaks(
probe_description(p), 40)))
8579 def _probe_nary(f, args, ctx):
8581 _z3_assert(len(args) > 0,
"At least one argument expected")
8583 r = _to_probe(args[0], ctx)
8584 for i
in range(num - 1):
8585 r =
Probe(f(ctx.ref(), r.probe, _to_probe(args[i + 1], ctx).probe), ctx)
8589 def _probe_and(args, ctx):
8590 return _probe_nary(Z3_probe_and, args, ctx)
8593 def _probe_or(args, ctx):
8594 return _probe_nary(Z3_probe_or, args, ctx)
8598 """Return a tactic that fails if the probe `p` evaluates to true.
8599 Otherwise, it returns the input goal unmodified.
8601 In the following example, the tactic applies 'simplify' if and only if there are
8602 more than 2 constraints in the goal.
8604 >>> t = OrElse(FailIf(Probe('size') > 2), Tactic('simplify'))
8605 >>> x, y = Ints('x y')
8611 >>> g.add(x == y + 1)
8613 [[Not(x <= 0), Not(y <= 0), x == 1 + y]]
8615 p = _to_probe(p, ctx)
8620 """Return a tactic that applies tactic `t` only if probe `p` evaluates to true.
8621 Otherwise, it returns the input goal unmodified.
8623 >>> t = When(Probe('size') > 2, Tactic('simplify'))
8624 >>> x, y = Ints('x y')
8630 >>> g.add(x == y + 1)
8632 [[Not(x <= 0), Not(y <= 0), x == 1 + y]]
8634 p = _to_probe(p, ctx)
8635 t = _to_tactic(t, ctx)
8640 """Return a tactic that applies tactic `t1` to a goal if probe `p` evaluates to true, and `t2` otherwise.
8642 >>> t = Cond(Probe('is-qfnra'), Tactic('qfnra'), Tactic('smt'))
8644 p = _to_probe(p, ctx)
8645 t1 = _to_tactic(t1, ctx)
8646 t2 = _to_tactic(t2, ctx)
8657 """Simplify the expression `a` using the given options.
8659 This function has many options. Use `help_simplify` to obtain the complete list.
8663 >>> simplify(x + 1 + y + x + 1)
8665 >>> simplify((x + 1)*(y + 1), som=True)
8667 >>> simplify(Distinct(x, y, 1), blast_distinct=True)
8668 And(Not(x == y), Not(x == 1), Not(y == 1))
8669 >>> simplify(And(x == 0, y == 1), elim_and=True)
8670 Not(Or(Not(x == 0), Not(y == 1)))
8673 _z3_assert(
is_expr(a),
"Z3 expression expected")
8674 if len(arguments) > 0
or len(keywords) > 0:
8676 return _to_expr_ref(
Z3_simplify_ex(a.ctx_ref(), a.as_ast(), p.params), a.ctx)
8678 return _to_expr_ref(
Z3_simplify(a.ctx_ref(), a.as_ast()), a.ctx)
8682 """Return a string describing all options available for Z3 `simplify` procedure."""
8687 """Return the set of parameter descriptions for Z3 `simplify` procedure."""
8692 """Apply substitution m on t, m is a list of pairs of the form (from, to).
8693 Every occurrence in t of from is replaced with to.
8697 >>> substitute(x + 1, (x, y + 1))
8699 >>> f = Function('f', IntSort(), IntSort())
8700 >>> substitute(f(x) + f(y), (f(x), IntVal(1)), (f(y), IntVal(1)))
8703 if isinstance(m, tuple):
8705 if isinstance(m1, list)
and all(isinstance(p, tuple)
for p
in m1):
8708 _z3_assert(
is_expr(t),
"Z3 expression expected")
8709 _z3_assert(all([isinstance(p, tuple)
and is_expr(p[0])
and is_expr(p[1])
and p[0].sort().
eq(
8710 p[1].sort())
for p
in m]),
"Z3 invalid substitution, expression pairs expected.")
8712 _from = (Ast * num)()
8714 for i
in range(num):
8715 _from[i] = m[i][0].as_ast()
8716 _to[i] = m[i][1].as_ast()
8717 return _to_expr_ref(
Z3_substitute(t.ctx.ref(), t.as_ast(), num, _from, _to), t.ctx)
8721 """Substitute the free variables in t with the expression in m.
8723 >>> v0 = Var(0, IntSort())
8724 >>> v1 = Var(1, IntSort())
8726 >>> f = Function('f', IntSort(), IntSort(), IntSort())
8727 >>> # replace v0 with x+1 and v1 with x
8728 >>> substitute_vars(f(v0, v1), x + 1, x)
8732 _z3_assert(
is_expr(t),
"Z3 expression expected")
8733 _z3_assert(all([
is_expr(n)
for n
in m]),
"Z3 invalid substitution, list of expressions expected.")
8736 for i
in range(num):
8737 _to[i] = m[i].as_ast()
8742 """Create the sum of the Z3 expressions.
8744 >>> a, b, c = Ints('a b c')
8749 >>> A = IntVector('a', 5)
8751 a__0 + a__1 + a__2 + a__3 + a__4
8753 args = _get_args(args)
8756 ctx = _ctx_from_ast_arg_list(args)
8758 return _reduce(
lambda a, b: a + b, args, 0)
8759 args = _coerce_expr_list(args, ctx)
8761 return _reduce(
lambda a, b: a + b, args, 0)
8763 _args, sz = _to_ast_array(args)
8768 """Create the product of the Z3 expressions.
8770 >>> a, b, c = Ints('a b c')
8771 >>> Product(a, b, c)
8773 >>> Product([a, b, c])
8775 >>> A = IntVector('a', 5)
8777 a__0*a__1*a__2*a__3*a__4
8779 args = _get_args(args)
8782 ctx = _ctx_from_ast_arg_list(args)
8784 return _reduce(
lambda a, b: a * b, args, 1)
8785 args = _coerce_expr_list(args, ctx)
8787 return _reduce(
lambda a, b: a * b, args, 1)
8789 _args, sz = _to_ast_array(args)
8794 """Create an at-most Pseudo-Boolean k constraint.
8796 >>> a, b, c = Bools('a b c')
8797 >>> f = AtMost(a, b, c, 2)
8799 args = _get_args(args)
8801 _z3_assert(len(args) > 1,
"Non empty list of arguments expected")
8802 ctx = _ctx_from_ast_arg_list(args)
8804 _z3_assert(ctx
is not None,
"At least one of the arguments must be a Z3 expression")
8805 args1 = _coerce_expr_list(args[:-1], ctx)
8807 _args, sz = _to_ast_array(args1)
8812 """Create an at-most Pseudo-Boolean k constraint.
8814 >>> a, b, c = Bools('a b c')
8815 >>> f = AtLeast(a, b, c, 2)
8817 args = _get_args(args)
8819 _z3_assert(len(args) > 1,
"Non empty list of arguments expected")
8820 ctx = _ctx_from_ast_arg_list(args)
8822 _z3_assert(ctx
is not None,
"At least one of the arguments must be a Z3 expression")
8823 args1 = _coerce_expr_list(args[:-1], ctx)
8825 _args, sz = _to_ast_array(args1)
8829 def _reorder_pb_arg(arg):
8831 if not _is_int(b)
and _is_int(a):
8836 def _pb_args_coeffs(args, default_ctx=None):
8837 args = _get_args_ast_list(args)
8839 return _get_ctx(default_ctx), 0, (Ast * 0)(), (ctypes.c_int * 0)()
8840 args = [_reorder_pb_arg(arg)
for arg
in args]
8841 args, coeffs = zip(*args)
8843 _z3_assert(len(args) > 0,
"Non empty list of arguments expected")
8844 ctx = _ctx_from_ast_arg_list(args)
8846 _z3_assert(ctx
is not None,
"At least one of the arguments must be a Z3 expression")
8847 args = _coerce_expr_list(args, ctx)
8848 _args, sz = _to_ast_array(args)
8849 _coeffs = (ctypes.c_int * len(coeffs))()
8850 for i
in range(len(coeffs)):
8851 _z3_check_cint_overflow(coeffs[i],
"coefficient")
8852 _coeffs[i] = coeffs[i]
8853 return ctx, sz, _args, _coeffs
8857 """Create a Pseudo-Boolean inequality k constraint.
8859 >>> a, b, c = Bools('a b c')
8860 >>> f = PbLe(((a,1),(b,3),(c,2)), 3)
8862 _z3_check_cint_overflow(k,
"k")
8863 ctx, sz, _args, _coeffs = _pb_args_coeffs(args)
8868 """Create a Pseudo-Boolean inequality k constraint.
8870 >>> a, b, c = Bools('a b c')
8871 >>> f = PbGe(((a,1),(b,3),(c,2)), 3)
8873 _z3_check_cint_overflow(k,
"k")
8874 ctx, sz, _args, _coeffs = _pb_args_coeffs(args)
8879 """Create a Pseudo-Boolean inequality k constraint.
8881 >>> a, b, c = Bools('a b c')
8882 >>> f = PbEq(((a,1),(b,3),(c,2)), 3)
8884 _z3_check_cint_overflow(k,
"k")
8885 ctx, sz, _args, _coeffs = _pb_args_coeffs(args)
8890 """Solve the constraints `*args`.
8892 This is a simple function for creating demonstrations. It creates a solver,
8893 configure it using the options in `keywords`, adds the constraints
8894 in `args`, and invokes check.
8897 >>> solve(a > 0, a < 2)
8900 show = keywords.pop(
"show",
False)
8908 print(
"no solution")
8910 print(
"failed to solve")
8920 """Solve the constraints `*args` using solver `s`.
8922 This is a simple function for creating demonstrations. It is similar to `solve`,
8923 but it uses the given solver `s`.
8924 It configures solver `s` using the options in `keywords`, adds the constraints
8925 in `args`, and invokes check.
8927 show = keywords.pop(
"show",
False)
8929 _z3_assert(isinstance(s, Solver),
"Solver object expected")
8937 print(
"no solution")
8939 print(
"failed to solve")
8950 def prove(claim, show=False, **keywords):
8951 """Try to prove the given claim.
8953 This is a simple function for creating demonstrations. It tries to prove
8954 `claim` by showing the negation is unsatisfiable.
8956 >>> p, q = Bools('p q')
8957 >>> prove(Not(And(p, q)) == Or(Not(p), Not(q)))
8961 _z3_assert(
is_bool(claim),
"Z3 Boolean expression expected")
8971 print(
"failed to prove")
8974 print(
"counterexample")
8978 def _solve_html(*args, **keywords):
8979 """Version of function `solve` used in RiSE4Fun."""
8980 show = keywords.pop(
"show",
False)
8985 print(
"<b>Problem:</b>")
8989 print(
"<b>no solution</b>")
8991 print(
"<b>failed to solve</b>")
8998 print(
"<b>Solution:</b>")
9002 def _solve_using_html(s, *args, **keywords):
9003 """Version of function `solve_using` used in RiSE4Fun."""
9004 show = keywords.pop(
"show",
False)
9006 _z3_assert(isinstance(s, Solver),
"Solver object expected")
9010 print(
"<b>Problem:</b>")
9014 print(
"<b>no solution</b>")
9016 print(
"<b>failed to solve</b>")
9023 print(
"<b>Solution:</b>")
9027 def _prove_html(claim, show=False, **keywords):
9028 """Version of function `prove` used in RiSE4Fun."""
9030 _z3_assert(
is_bool(claim),
"Z3 Boolean expression expected")
9038 print(
"<b>proved</b>")
9040 print(
"<b>failed to prove</b>")
9043 print(
"<b>counterexample</b>")
9047 def _dict2sarray(sorts, ctx):
9049 _names = (Symbol * sz)()
9050 _sorts = (Sort * sz)()
9055 _z3_assert(isinstance(k, str),
"String expected")
9056 _z3_assert(
is_sort(v),
"Z3 sort expected")
9060 return sz, _names, _sorts
9063 def _dict2darray(decls, ctx):
9065 _names = (Symbol * sz)()
9066 _decls = (FuncDecl * sz)()
9071 _z3_assert(isinstance(k, str),
"String expected")
9075 _decls[i] = v.decl().ast
9079 return sz, _names, _decls
9083 """Parse a string in SMT 2.0 format using the given sorts and decls.
9085 The arguments sorts and decls are Python dictionaries used to initialize
9086 the symbol table used for the SMT 2.0 parser.
9088 >>> parse_smt2_string('(declare-const x Int) (assert (> x 0)) (assert (< x 10))')
9090 >>> x, y = Ints('x y')
9091 >>> f = Function('f', IntSort(), IntSort())
9092 >>> parse_smt2_string('(assert (> (+ foo (g bar)) 0))', decls={ 'foo' : x, 'bar' : y, 'g' : f})
9094 >>> parse_smt2_string('(declare-const a U) (assert (> a 0))', sorts={ 'U' : IntSort() })
9098 ssz, snames, ssorts = _dict2sarray(sorts, ctx)
9099 dsz, dnames, ddecls = _dict2darray(decls, ctx)
9100 return AstVector(Z3_parse_smtlib2_string(ctx.ref(), s, ssz, snames, ssorts, dsz, dnames, ddecls), ctx)
9103 def parse_smt2_file(f, sorts={}, decls={}, ctx=None):
9104 """Parse a file
in SMT 2.0 format using the given sorts
and decls.
9109 ssz, snames, ssorts = _dict2sarray(sorts, ctx)
9110 dsz, dnames, ddecls = _dict2darray(decls, ctx)
9111 return AstVector(Z3_parse_smtlib2_file(ctx.ref(), f, ssz, snames, ssorts, dsz, dnames, ddecls), ctx)
9114 #########################################
9116 # Floating-Point Arithmetic
9118 #########################################
9121 # Global default rounding mode
9122 _dflt_rounding_mode = Z3_OP_FPA_RM_TOWARD_ZERO
9123 _dflt_fpsort_ebits = 11
9124 _dflt_fpsort_sbits = 53
9127 def get_default_rounding_mode(ctx=None):
9128 """Retrieves the
global default rounding mode.
"""
9129 global _dflt_rounding_mode
9130 if _dflt_rounding_mode == Z3_OP_FPA_RM_TOWARD_ZERO:
9132 elif _dflt_rounding_mode == Z3_OP_FPA_RM_TOWARD_NEGATIVE:
9134 elif _dflt_rounding_mode == Z3_OP_FPA_RM_TOWARD_POSITIVE:
9136 elif _dflt_rounding_mode == Z3_OP_FPA_RM_NEAREST_TIES_TO_EVEN:
9138 elif _dflt_rounding_mode == Z3_OP_FPA_RM_NEAREST_TIES_TO_AWAY:
9142 _ROUNDING_MODES = frozenset({
9143 Z3_OP_FPA_RM_TOWARD_ZERO,
9144 Z3_OP_FPA_RM_TOWARD_NEGATIVE,
9145 Z3_OP_FPA_RM_TOWARD_POSITIVE,
9146 Z3_OP_FPA_RM_NEAREST_TIES_TO_EVEN,
9147 Z3_OP_FPA_RM_NEAREST_TIES_TO_AWAY
9151 def set_default_rounding_mode(rm, ctx=None):
9152 global _dflt_rounding_mode
9153 if is_fprm_value(rm):
9154 _dflt_rounding_mode = rm.decl().kind()
9156 _z3_assert(_dflt_rounding_mode in _ROUNDING_MODES, "illegal rounding mode")
9157 _dflt_rounding_mode = rm
9160 def get_default_fp_sort(ctx=None):
9161 return FPSort(_dflt_fpsort_ebits, _dflt_fpsort_sbits, ctx)
9164 def set_default_fp_sort(ebits, sbits, ctx=None):
9165 global _dflt_fpsort_ebits
9166 global _dflt_fpsort_sbits
9167 _dflt_fpsort_ebits = ebits
9168 _dflt_fpsort_sbits = sbits
9171 def _dflt_rm(ctx=None):
9172 return get_default_rounding_mode(ctx)
9175 def _dflt_fps(ctx=None):
9176 return get_default_fp_sort(ctx)
9179 def _coerce_fp_expr_list(alist, ctx):
9180 first_fp_sort = None
9183 if first_fp_sort is None:
9184 first_fp_sort = a.sort()
9185 elif first_fp_sort == a.sort():
9186 pass # OK, same as before
9188 # we saw at least 2 different float sorts; something will
9189 # throw a sort mismatch later, for now assume None.
9190 first_fp_sort = None
9194 for i in range(len(alist)):
9196 is_repr = isinstance(a, str) and a.contains("2**(") and a.endswith(")")
9197 if is_repr or _is_int(a) or isinstance(a, (float, bool)):
9198 r.append(FPVal(a, None, first_fp_sort, ctx))
9201 return _coerce_expr_list(r, ctx)
9206 class FPSortRef(SortRef):
9207 """Floating-point sort.
"""
9210 """Retrieves the number of bits reserved
for the exponent
in the FloatingPoint sort `self`.
9215 return int(Z3_fpa_get_ebits(self.ctx_ref(), self.ast))
9218 """Retrieves the number of bits reserved
for the significand
in the FloatingPoint sort `self`.
9223 return int(Z3_fpa_get_sbits(self.ctx_ref(), self.ast))
9225 def cast(self, val):
9226 """Try to cast `val`
as a floating-point expression.
9230 >>> b.cast(1.0).
sexpr()
9231 '(fp #b0 #x7f #b00000000000000000000000)'
9235 _z3_assert(self.ctx == val.ctx, "Context mismatch")
9238 return FPVal(val, None, self, self.ctx)
9241 def Float16(ctx=None):
9242 """Floating-point 16-bit (half) sort.
"""
9244 return FPSortRef(Z3_mk_fpa_sort_16(ctx.ref()), ctx)
9247 def FloatHalf(ctx=None):
9248 """Floating-point 16-bit (half) sort.
"""
9250 return FPSortRef(Z3_mk_fpa_sort_half(ctx.ref()), ctx)
9253 def Float32(ctx=None):
9254 """Floating-point 32-bit (single) sort.
"""
9256 return FPSortRef(Z3_mk_fpa_sort_32(ctx.ref()), ctx)
9259 def FloatSingle(ctx=None):
9260 """Floating-point 32-bit (single) sort.
"""
9262 return FPSortRef(Z3_mk_fpa_sort_single(ctx.ref()), ctx)
9265 def Float64(ctx=None):
9266 """Floating-point 64-bit (double) sort.
"""
9268 return FPSortRef(Z3_mk_fpa_sort_64(ctx.ref()), ctx)
9271 def FloatDouble(ctx=None):
9272 """Floating-point 64-bit (double) sort.
"""
9274 return FPSortRef(Z3_mk_fpa_sort_double(ctx.ref()), ctx)
9277 def Float128(ctx=None):
9278 """Floating-point 128-bit (quadruple) sort.
"""
9280 return FPSortRef(Z3_mk_fpa_sort_128(ctx.ref()), ctx)
9283 def FloatQuadruple(ctx=None):
9284 """Floating-point 128-bit (quadruple) sort.
"""
9286 return FPSortRef(Z3_mk_fpa_sort_quadruple(ctx.ref()), ctx)
9289 class FPRMSortRef(SortRef):
9290 """"Floating-point rounding mode sort."""
9294 """Return True if `s` is a Z3 floating-point sort.
9301 return isinstance(s, FPSortRef)
9304 def is_fprm_sort(s):
9305 """Return
True if `s`
is a Z3 floating-point rounding mode sort.
9312 return isinstance(s, FPRMSortRef)
9317 class FPRef(ExprRef):
9318 """Floating-point expressions.
"""
9321 """Return the sort of the floating-point expression `self`.
9326 >>> x.sort() ==
FPSort(8, 24)
9329 return FPSortRef(Z3_get_sort(self.ctx_ref(), self.as_ast()), self.ctx)
9332 """Retrieves the number of bits reserved
for the exponent
in the FloatingPoint expression `self`.
9337 return self.sort().ebits()
9340 """Retrieves the number of bits reserved
for the exponent
in the FloatingPoint expression `self`.
9345 return self.sort().sbits()
9347 def as_string(self):
9348 """Return a Z3 floating point expression
as a Python string.
"""
9349 return Z3_ast_to_string(self.ctx_ref(), self.as_ast())
9351 def __le__(self, other):
9352 return fpLEQ(self, other, self.ctx)
9354 def __lt__(self, other):
9355 return fpLT(self, other, self.ctx)
9357 def __ge__(self, other):
9358 return fpGEQ(self, other, self.ctx)
9360 def __gt__(self, other):
9361 return fpGT(self, other, self.ctx)
9363 def __add__(self, other):
9364 """Create the Z3 expression `self + other`.
9373 [a, b] = _coerce_fp_expr_list([self, other], self.ctx)
9374 return fpAdd(_dflt_rm(), a, b, self.ctx)
9376 def __radd__(self, other):
9377 """Create the Z3 expression `other + self`.
9383 [a, b] = _coerce_fp_expr_list([other, self], self.ctx)
9384 return fpAdd(_dflt_rm(), a, b, self.ctx)
9386 def __sub__(self, other):
9387 """Create the Z3 expression `self - other`.
9396 [a, b] = _coerce_fp_expr_list([self, other], self.ctx)
9397 return fpSub(_dflt_rm(), a, b, self.ctx)
9399 def __rsub__(self, other):
9400 """Create the Z3 expression `other - self`.
9406 [a, b] = _coerce_fp_expr_list([other, self], self.ctx)
9407 return fpSub(_dflt_rm(), a, b, self.ctx)
9409 def __mul__(self, other):
9410 """Create the Z3 expression `self * other`.
9421 [a, b] = _coerce_fp_expr_list([self, other], self.ctx)
9422 return fpMul(_dflt_rm(), a, b, self.ctx)
9424 def __rmul__(self, other):
9425 """Create the Z3 expression `other * self`.
9434 [a, b] = _coerce_fp_expr_list([other, self], self.ctx)
9435 return fpMul(_dflt_rm(), a, b, self.ctx)
9438 """Create the Z3 expression `+self`.
"""
9442 """Create the Z3 expression `-self`.
9450 def __div__(self, other):
9451 """Create the Z3 expression `self / other`.
9462 [a, b] = _coerce_fp_expr_list([self, other], self.ctx)
9463 return fpDiv(_dflt_rm(), a, b, self.ctx)
9465 def __rdiv__(self, other):
9466 """Create the Z3 expression `other / self`.
9475 [a, b] = _coerce_fp_expr_list([other, self], self.ctx)
9476 return fpDiv(_dflt_rm(), a, b, self.ctx)
9478 def __truediv__(self, other):
9479 """Create the Z3 expression division `self / other`.
"""
9480 return self.__div__(other)
9482 def __rtruediv__(self, other):
9483 """Create the Z3 expression division `other / self`.
"""
9484 return self.__rdiv__(other)
9486 def __mod__(self, other):
9487 """Create the Z3 expression mod `self % other`.
"""
9488 return fpRem(self, other)
9490 def __rmod__(self, other):
9491 """Create the Z3 expression mod `other % self`.
"""
9492 return fpRem(other, self)
9495 class FPRMRef(ExprRef):
9496 """Floating-point rounding mode expressions
"""
9498 def as_string(self):
9499 """Return a Z3 floating point expression
as a Python string.
"""
9500 return Z3_ast_to_string(self.ctx_ref(), self.as_ast())
9503 def RoundNearestTiesToEven(ctx=None):
9505 return FPRMRef(Z3_mk_fpa_round_nearest_ties_to_even(ctx.ref()), ctx)
9510 return FPRMRef(Z3_mk_fpa_round_nearest_ties_to_even(ctx.ref()), ctx)
9513 def RoundNearestTiesToAway(ctx=None):
9515 return FPRMRef(Z3_mk_fpa_round_nearest_ties_to_away(ctx.ref()), ctx)
9520 return FPRMRef(Z3_mk_fpa_round_nearest_ties_to_away(ctx.ref()), ctx)
9523 def RoundTowardPositive(ctx=None):
9525 return FPRMRef(Z3_mk_fpa_round_toward_positive(ctx.ref()), ctx)
9530 return FPRMRef(Z3_mk_fpa_round_toward_positive(ctx.ref()), ctx)
9533 def RoundTowardNegative(ctx=None):
9535 return FPRMRef(Z3_mk_fpa_round_toward_negative(ctx.ref()), ctx)
9540 return FPRMRef(Z3_mk_fpa_round_toward_negative(ctx.ref()), ctx)
9543 def RoundTowardZero(ctx=None):
9545 return FPRMRef(Z3_mk_fpa_round_toward_zero(ctx.ref()), ctx)
9550 return FPRMRef(Z3_mk_fpa_round_toward_zero(ctx.ref()), ctx)
9554 """Return `
True`
if `a`
is a Z3 floating-point rounding mode expression.
9563 return isinstance(a, FPRMRef)
9566 def is_fprm_value(a):
9567 """Return `
True`
if `a`
is a Z3 floating-point rounding mode numeral value.
"""
9568 return is_fprm(a) and _is_numeral(a.ctx, a.ast)
9573 class FPNumRef(FPRef):
9574 """The sign of the numeral.
9585 num = (ctypes.c_int)()
9586 nsign = Z3_fpa_get_numeral_sign(self.ctx.ref(), self.as_ast(), byref(num))
9588 raise Z3Exception("error retrieving the sign of a numeral.")
9589 return num.value != 0
9591 """The sign of a floating-point numeral
as a bit-vector expression.
9593 Remark: NaN
's are invalid arguments.
9596 def sign_as_bv(self):
9597 return BitVecNumRef(Z3_fpa_get_numeral_sign_bv(self.ctx.ref(), self.as_ast()), self.ctx)
9599 """The significand of the numeral.
9606 def significand(self):
9607 return Z3_fpa_get_numeral_significand_string(self.ctx.ref(), self.as_ast())
9609 """The significand of the numeral
as a long.
9612 >>> x.significand_as_long()
9616 def significand_as_long(self):
9617 ptr = (ctypes.c_ulonglong * 1)()
9618 if not Z3_fpa_get_numeral_significand_uint64(self.ctx.ref(), self.as_ast(), ptr):
9619 raise Z3Exception("error retrieving the significand of a numeral.")
9622 """The significand of the numeral
as a bit-vector expression.
9624 Remark: NaN are invalid arguments.
9627 def significand_as_bv(self):
9628 return BitVecNumRef(Z3_fpa_get_numeral_significand_bv(self.ctx.ref(), self.as_ast()), self.ctx)
9630 """The exponent of the numeral.
9637 def exponent(self, biased=True):
9638 return Z3_fpa_get_numeral_exponent_string(self.ctx.ref(), self.as_ast(), biased)
9640 """The exponent of the numeral
as a long.
9643 >>> x.exponent_as_long()
9647 def exponent_as_long(self, biased=True):
9648 ptr = (ctypes.c_longlong * 1)()
9649 if not Z3_fpa_get_numeral_exponent_int64(self.ctx.ref(), self.as_ast(), ptr, biased):
9650 raise Z3Exception("error retrieving the exponent of a numeral.")
9653 """The exponent of the numeral
as a bit-vector expression.
9655 Remark: NaNs are invalid arguments.
9658 def exponent_as_bv(self, biased=True):
9659 return BitVecNumRef(Z3_fpa_get_numeral_exponent_bv(self.ctx.ref(), self.as_ast(), biased), self.ctx)
9661 """Indicates whether the numeral
is a NaN.
"""
9664 return Z3_fpa_is_numeral_nan(self.ctx.ref(), self.as_ast())
9666 """Indicates whether the numeral
is +oo
or -oo.
"""
9669 return Z3_fpa_is_numeral_inf(self.ctx.ref(), self.as_ast())
9671 """Indicates whether the numeral
is +zero
or -zero.
"""
9674 return Z3_fpa_is_numeral_zero(self.ctx.ref(), self.as_ast())
9676 """Indicates whether the numeral
is normal.
"""
9679 return Z3_fpa_is_numeral_normal(self.ctx.ref(), self.as_ast())
9681 """Indicates whether the numeral
is subnormal.
"""
9683 def isSubnormal(self):
9684 return Z3_fpa_is_numeral_subnormal(self.ctx.ref(), self.as_ast())
9686 """Indicates whether the numeral
is positive.
"""
9688 def isPositive(self):
9689 return Z3_fpa_is_numeral_positive(self.ctx.ref(), self.as_ast())
9691 """Indicates whether the numeral
is negative.
"""
9693 def isNegative(self):
9694 return Z3_fpa_is_numeral_negative(self.ctx.ref(), self.as_ast())
9697 The string representation of the numeral.
9704 def as_string(self):
9705 s = Z3_get_numeral_string(self.ctx.ref(), self.as_ast())
9706 return ("FPVal(%s, %s)" % (s, self.sort()))
9710 """Return `
True`
if `a`
is a Z3 floating-point expression.
9720 return isinstance(a, FPRef)
9724 """Return `
True`
if `a`
is a Z3 floating-point numeral value.
9735 return is_fp(a) and _is_numeral(a.ctx, a.ast)
9738 def FPSort(ebits, sbits, ctx=None):
9739 """Return a Z3 floating-point sort of the given sizes. If `ctx=
None`, then the
global context
is used.
9741 >>> Single =
FPSort(8, 24)
9742 >>> Double =
FPSort(11, 53)
9745 >>> x =
Const(
'x', Single)
9750 return FPSortRef(Z3_mk_fpa_sort(ctx.ref(), ebits, sbits), ctx)
9753 def _to_float_str(val, exp=0):
9754 if isinstance(val, float):
9758 sone = math.copysign(1.0, val)
9763 elif val == float("+inf"):
9765 elif val == float("-inf"):
9768 v = val.as_integer_ratio()
9771 rvs = str(num) + "/" + str(den)
9772 res = rvs + "p" + _to_int_str(exp)
9773 elif isinstance(val, bool):
9780 elif isinstance(val, str):
9781 inx = val.find("*(2**")
9784 elif val[-1] == ")":
9786 exp = str(int(val[inx + 5:-1]) + int(exp))
9788 _z3_assert(False, "String does not have floating-point numeral form.")
9790 _z3_assert(False, "Python value cannot be used to create floating-point numerals.")
9794 return res + "p" + exp
9798 """Create a Z3 floating-point NaN term.
9801 >>> set_fpa_pretty(
True)
9804 >>> pb = get_fpa_pretty()
9805 >>> set_fpa_pretty(
False)
9808 >>> set_fpa_pretty(pb)
9810 _z3_assert(isinstance(s, FPSortRef), "sort mismatch")
9811 return FPNumRef(Z3_mk_fpa_nan(s.ctx_ref(), s.ast), s.ctx)
9814 def fpPlusInfinity(s):
9815 """Create a Z3 floating-point +oo term.
9818 >>> pb = get_fpa_pretty()
9819 >>> set_fpa_pretty(
True)
9822 >>> set_fpa_pretty(
False)
9825 >>> set_fpa_pretty(pb)
9827 _z3_assert(isinstance(s, FPSortRef), "sort mismatch")
9828 return FPNumRef(Z3_mk_fpa_inf(s.ctx_ref(), s.ast, False), s.ctx)
9831 def fpMinusInfinity(s):
9832 """Create a Z3 floating-point -oo term.
"""
9833 _z3_assert(isinstance(s, FPSortRef), "sort mismatch")
9834 return FPNumRef(Z3_mk_fpa_inf(s.ctx_ref(), s.ast, True), s.ctx)
9837 def fpInfinity(s, negative):
9838 """Create a Z3 floating-point +oo
or -oo term.
"""
9839 _z3_assert(isinstance(s, FPSortRef), "sort mismatch")
9840 _z3_assert(isinstance(negative, bool), "expected Boolean flag")
9841 return FPNumRef(Z3_mk_fpa_inf(s.ctx_ref(), s.ast, negative), s.ctx)
9845 """Create a Z3 floating-point +0.0 term.
"""
9846 _z3_assert(isinstance(s, FPSortRef), "sort mismatch")
9847 return FPNumRef(Z3_mk_fpa_zero(s.ctx_ref(), s.ast, False), s.ctx)
9851 """Create a Z3 floating-point -0.0 term.
"""
9852 _z3_assert(isinstance(s, FPSortRef), "sort mismatch")
9853 return FPNumRef(Z3_mk_fpa_zero(s.ctx_ref(), s.ast, True), s.ctx)
9856 def fpZero(s, negative):
9857 """Create a Z3 floating-point +0.0
or -0.0 term.
"""
9858 _z3_assert(isinstance(s, FPSortRef), "sort mismatch")
9859 _z3_assert(isinstance(negative, bool), "expected Boolean flag")
9860 return FPNumRef(Z3_mk_fpa_zero(s.ctx_ref(), s.ast, negative), s.ctx)
9863 def FPVal(sig, exp=None, fps=None, ctx=None):
9864 """Return a floating-point value of value `val`
and sort `fps`.
9865 If `ctx=
None`, then the
global context
is used.
9870 >>> print(
"0x%.8x" % v.exponent_as_long(
False))
9890 fps = _dflt_fps(ctx)
9891 _z3_assert(is_fp_sort(fps), "sort mismatch")
9894 val = _to_float_str(sig)
9895 if val == "NaN" or val == "nan":
9898 return fpMinusZero(fps)
9899 elif val == "0.0" or val == "+0.0":
9900 return fpPlusZero(fps)
9901 elif val == "+oo" or val == "+inf" or val == "+Inf":
9902 return fpPlusInfinity(fps)
9903 elif val == "-oo" or val == "-inf" or val == "-Inf":
9904 return fpMinusInfinity(fps)
9906 return FPNumRef(Z3_mk_numeral(ctx.ref(), val, fps.ast), ctx)
9909 def FP(name, fpsort, ctx=None):
9910 """Return a floating-point constant named `name`.
9911 `fpsort`
is the floating-point sort.
9912 If `ctx=
None`, then the
global context
is used.
9922 >>> x2 =
FP(
'x', word)
9926 if isinstance(fpsort, FPSortRef) and ctx is None:
9930 return FPRef(Z3_mk_const(ctx.ref(), to_symbol(name, ctx), fpsort.ast), ctx)
9933 def FPs(names, fpsort, ctx=None):
9934 """Return an array of floating-point constants.
9936 >>> x, y, z =
FPs(
'x y z',
FPSort(8, 24))
9947 if isinstance(names, str):
9948 names = names.split(" ")
9949 return [FP(name, fpsort, ctx) for name in names]
9952 def fpAbs(a, ctx=None):
9953 """Create a Z3 floating-point absolute value expression.
9957 >>> x =
FPVal(1.0, s)
9960 >>> y =
FPVal(-20.0, s)
9965 >>>
fpAbs(-1.25*(2**4))
9971 [a] = _coerce_fp_expr_list([a], ctx)
9972 return FPRef(Z3_mk_fpa_abs(ctx.ref(), a.as_ast()), ctx)
9975 def fpNeg(a, ctx=None):
9976 """Create a Z3 floating-point addition expression.
9987 [a] = _coerce_fp_expr_list([a], ctx)
9988 return FPRef(Z3_mk_fpa_neg(ctx.ref(), a.as_ast()), ctx)
9991 def _mk_fp_unary(f, rm, a, ctx):
9993 [a] = _coerce_fp_expr_list([a], ctx)
9995 _z3_assert(is_fprm(rm), "First argument must be a Z3 floating-point rounding mode expression")
9996 _z3_assert(is_fp(a), "Second argument must be a Z3 floating-point expression")
9997 return FPRef(f(ctx.ref(), rm.as_ast(), a.as_ast()), ctx)
10000 def _mk_fp_unary_pred(f, a, ctx):
10001 ctx = _get_ctx(ctx)
10002 [a] = _coerce_fp_expr_list([a], ctx)
10004 _z3_assert(is_fp(a), "First argument must be a Z3 floating-point expression")
10005 return BoolRef(f(ctx.ref(), a.as_ast()), ctx)
10008 def _mk_fp_bin(f, rm, a, b, ctx):
10009 ctx = _get_ctx(ctx)
10010 [a, b] = _coerce_fp_expr_list([a, b], ctx)
10012 _z3_assert(is_fprm(rm), "First argument must be a Z3 floating-point rounding mode expression")
10013 _z3_assert(is_fp(a) or is_fp(b), "Second or third argument must be a Z3 floating-point expression")
10014 return FPRef(f(ctx.ref(), rm.as_ast(), a.as_ast(), b.as_ast()), ctx)
10017 def _mk_fp_bin_norm(f, a, b, ctx):
10018 ctx = _get_ctx(ctx)
10019 [a, b] = _coerce_fp_expr_list([a, b], ctx)
10021 _z3_assert(is_fp(a) or is_fp(b), "First or second argument must be a Z3 floating-point expression")
10022 return FPRef(f(ctx.ref(), a.as_ast(), b.as_ast()), ctx)
10025 def _mk_fp_bin_pred(f, a, b, ctx):
10026 ctx = _get_ctx(ctx)
10027 [a, b] = _coerce_fp_expr_list([a, b], ctx)
10029 _z3_assert(is_fp(a) or is_fp(b), "First or second argument must be a Z3 floating-point expression")
10030 return BoolRef(f(ctx.ref(), a.as_ast(), b.as_ast()), ctx)
10033 def _mk_fp_tern(f, rm, a, b, c, ctx):
10034 ctx = _get_ctx(ctx)
10035 [a, b, c] = _coerce_fp_expr_list([a, b, c], ctx)
10037 _z3_assert(is_fprm(rm), "First argument must be a Z3 floating-point rounding mode expression")
10038 _z3_assert(is_fp(a) or is_fp(b) or is_fp(
10039 c), "Second, third or fourth argument must be a Z3 floating-point expression")
10040 return FPRef(f(ctx.ref(), rm.as_ast(), a.as_ast(), b.as_ast(), c.as_ast()), ctx)
10043 def fpAdd(rm, a, b, ctx=None):
10044 """Create a Z3 floating-point addition expression.
10050 >>>
fpAdd(rm, x, y)
10054 >>>
fpAdd(rm, x, y).sort()
10057 return _mk_fp_bin(Z3_mk_fpa_add, rm, a, b, ctx)
10060 def fpSub(rm, a, b, ctx=None):
10061 """Create a Z3 floating-point subtraction expression.
10067 >>>
fpSub(rm, x, y)
10069 >>>
fpSub(rm, x, y).sort()
10072 return _mk_fp_bin(Z3_mk_fpa_sub, rm, a, b, ctx)
10075 def fpMul(rm, a, b, ctx=None):
10076 """Create a Z3 floating-point multiplication expression.
10082 >>>
fpMul(rm, x, y)
10084 >>>
fpMul(rm, x, y).sort()
10087 return _mk_fp_bin(Z3_mk_fpa_mul, rm, a, b, ctx)
10090 def fpDiv(rm, a, b, ctx=None):
10091 """Create a Z3 floating-point division expression.
10097 >>>
fpDiv(rm, x, y)
10099 >>>
fpDiv(rm, x, y).sort()
10102 return _mk_fp_bin(Z3_mk_fpa_div, rm, a, b, ctx)
10105 def fpRem(a, b, ctx=None):
10106 """Create a Z3 floating-point remainder expression.
10113 >>>
fpRem(x, y).sort()
10116 return _mk_fp_bin_norm(Z3_mk_fpa_rem, a, b, ctx)
10119 def fpMin(a, b, ctx=None):
10120 """Create a Z3 floating-point minimum expression.
10128 >>>
fpMin(x, y).sort()
10131 return _mk_fp_bin_norm(Z3_mk_fpa_min, a, b, ctx)
10134 def fpMax(a, b, ctx=None):
10135 """Create a Z3 floating-point maximum expression.
10143 >>>
fpMax(x, y).sort()
10146 return _mk_fp_bin_norm(Z3_mk_fpa_max, a, b, ctx)
10149 def fpFMA(rm, a, b, c, ctx=None):
10150 """Create a Z3 floating-point fused multiply-add expression.
10152 return _mk_fp_tern(Z3_mk_fpa_fma, rm, a, b, c, ctx)
10155 def fpSqrt(rm, a, ctx=None):
10156 """Create a Z3 floating-point square root expression.
10158 return _mk_fp_unary(Z3_mk_fpa_sqrt, rm, a, ctx)
10161 def fpRoundToIntegral(rm, a, ctx=None):
10162 """Create a Z3 floating-point roundToIntegral expression.
10164 return _mk_fp_unary(Z3_mk_fpa_round_to_integral, rm, a, ctx)
10167 def fpIsNaN(a, ctx=None):
10168 """Create a Z3 floating-point isNaN expression.
10176 return _mk_fp_unary_pred(Z3_mk_fpa_is_nan, a, ctx)
10179 def fpIsInf(a, ctx=None):
10180 """Create a Z3 floating-point isInfinite expression.
10187 return _mk_fp_unary_pred(Z3_mk_fpa_is_infinite, a, ctx)
10190 def fpIsZero(a, ctx=None):
10191 """Create a Z3 floating-point isZero expression.
10193 return _mk_fp_unary_pred(Z3_mk_fpa_is_zero, a, ctx)
10196 def fpIsNormal(a, ctx=None):
10197 """Create a Z3 floating-point isNormal expression.
10199 return _mk_fp_unary_pred(Z3_mk_fpa_is_normal, a, ctx)
10202 def fpIsSubnormal(a, ctx=None):
10203 """Create a Z3 floating-point isSubnormal expression.
10205 return _mk_fp_unary_pred(Z3_mk_fpa_is_subnormal, a, ctx)
10208 def fpIsNegative(a, ctx=None):
10209 """Create a Z3 floating-point isNegative expression.
10211 return _mk_fp_unary_pred(Z3_mk_fpa_is_negative, a, ctx)
10214 def fpIsPositive(a, ctx=None):
10215 """Create a Z3 floating-point isPositive expression.
10217 return _mk_fp_unary_pred(Z3_mk_fpa_is_positive, a, ctx)
10220 def _check_fp_args(a, b):
10222 _z3_assert(is_fp(a) or is_fp(b), "First or second argument must be a Z3 floating-point expression")
10225 def fpLT(a, b, ctx=None):
10226 """Create the Z3 floating-point expression `other < self`.
10231 >>> (x < y).sexpr()
10234 return _mk_fp_bin_pred(Z3_mk_fpa_lt, a, b, ctx)
10237 def fpLEQ(a, b, ctx=None):
10238 """Create the Z3 floating-point expression `other <= self`.
10243 >>> (x <= y).sexpr()
10246 return _mk_fp_bin_pred(Z3_mk_fpa_leq, a, b, ctx)
10249 def fpGT(a, b, ctx=None):
10250 """Create the Z3 floating-point expression `other > self`.
10255 >>> (x > y).sexpr()
10258 return _mk_fp_bin_pred(Z3_mk_fpa_gt, a, b, ctx)
10261 def fpGEQ(a, b, ctx=None):
10262 """Create the Z3 floating-point expression `other >= self`.
10267 >>> (x >= y).sexpr()
10270 return _mk_fp_bin_pred(Z3_mk_fpa_geq, a, b, ctx)
10273 def fpEQ(a, b, ctx=None):
10274 """Create the Z3 floating-point expression `
fpEQ(other, self)`.
10279 >>>
fpEQ(x, y).sexpr()
10282 return _mk_fp_bin_pred(Z3_mk_fpa_eq, a, b, ctx)
10285 def fpNEQ(a, b, ctx=None):
10286 """Create the Z3 floating-point expression `
Not(
fpEQ(other, self))`.
10291 >>> (x != y).sexpr()
10294 return Not(fpEQ(a, b, ctx))
10297 def fpFP(sgn, exp, sig, ctx=None):
10298 """Create the Z3 floating-point value `
fpFP(sgn, sig, exp)`
from the three bit-vectors sgn, sig,
and exp.
10303 fpFP(1, 127, 4194304)
10304 >>> xv =
FPVal(-1.5, s)
10308 >>> slvr.add(
fpEQ(x, xv))
10311 >>> xv =
FPVal(+1.5, s)
10315 >>> slvr.add(
fpEQ(x, xv))
10319 _z3_assert(is_bv(sgn) and is_bv(exp) and is_bv(sig), "sort mismatch")
10320 _z3_assert(sgn.sort().size() == 1, "sort mismatch")
10321 ctx = _get_ctx(ctx)
10322 _z3_assert(ctx == sgn.ctx == exp.ctx == sig.ctx, "context mismatch")
10323 return FPRef(Z3_mk_fpa_fp(ctx.ref(), sgn.ast, exp.ast, sig.ast), ctx)
10326 def fpToFP(a1, a2=None, a3=None, ctx=None):
10327 """Create a Z3 floating-point conversion expression
from other term sorts
10330 From a bit-vector term
in IEEE 754-2008 format:
10336 From a floating-point term with different precision:
10347 From a signed bit-vector term:
10352 ctx = _get_ctx(ctx)
10353 if is_bv(a1) and is_fp_sort(a2):
10354 return FPRef(Z3_mk_fpa_to_fp_bv(ctx.ref(), a1.ast, a2.ast), ctx)
10355 elif is_fprm(a1) and is_fp(a2) and is_fp_sort(a3):
10356 return FPRef(Z3_mk_fpa_to_fp_float(ctx.ref(), a1.ast, a2.ast, a3.ast), ctx)
10357 elif is_fprm(a1) and is_real(a2) and is_fp_sort(a3):
10358 return FPRef(Z3_mk_fpa_to_fp_real(ctx.ref(), a1.ast, a2.ast, a3.ast), ctx)
10359 elif is_fprm(a1) and is_bv(a2) and is_fp_sort(a3):
10360 return FPRef(Z3_mk_fpa_to_fp_signed(ctx.ref(), a1.ast, a2.ast, a3.ast), ctx)
10362 raise Z3Exception("Unsupported combination of arguments for conversion to floating-point term.")
10365 def fpBVToFP(v, sort, ctx=None):
10366 """Create a Z3 floating-point conversion expression that represents the
10367 conversion
from a bit-vector term to a floating-point term.
10376 _z3_assert(is_bv(v), "First argument must be a Z3 bit-vector expression")
10377 _z3_assert(is_fp_sort(sort), "Second argument must be a Z3 floating-point sort.")
10378 ctx = _get_ctx(ctx)
10379 return FPRef(Z3_mk_fpa_to_fp_bv(ctx.ref(), v.ast, sort.ast), ctx)
10382 def fpFPToFP(rm, v, sort, ctx=None):
10383 """Create a Z3 floating-point conversion expression that represents the
10384 conversion
from a floating-point term to a floating-point term of different precision.
10395 _z3_assert(is_fprm(rm), "First argument must be a Z3 floating-point rounding mode expression.")
10396 _z3_assert(is_fp(v), "Second argument must be a Z3 floating-point expression.")
10397 _z3_assert(is_fp_sort(sort), "Third argument must be a Z3 floating-point sort.")
10398 ctx = _get_ctx(ctx)
10399 return FPRef(Z3_mk_fpa_to_fp_float(ctx.ref(), rm.ast, v.ast, sort.ast), ctx)
10402 def fpRealToFP(rm, v, sort, ctx=None):
10403 """Create a Z3 floating-point conversion expression that represents the
10404 conversion
from a real term to a floating-point term.
10413 _z3_assert(is_fprm(rm), "First argument must be a Z3 floating-point rounding mode expression.")
10414 _z3_assert(is_real(v), "Second argument must be a Z3 expression or real sort.")
10415 _z3_assert(is_fp_sort(sort), "Third argument must be a Z3 floating-point sort.")
10416 ctx = _get_ctx(ctx)
10417 return FPRef(Z3_mk_fpa_to_fp_real(ctx.ref(), rm.ast, v.ast, sort.ast), ctx)
10420 def fpSignedToFP(rm, v, sort, ctx=None):
10421 """Create a Z3 floating-point conversion expression that represents the
10422 conversion
from a signed bit-vector term (encoding an integer) to a floating-point term.
10431 _z3_assert(is_fprm(rm), "First argument must be a Z3 floating-point rounding mode expression.")
10432 _z3_assert(is_bv(v), "Second argument must be a Z3 bit-vector expression")
10433 _z3_assert(is_fp_sort(sort), "Third argument must be a Z3 floating-point sort.")
10434 ctx = _get_ctx(ctx)
10435 return FPRef(Z3_mk_fpa_to_fp_signed(ctx.ref(), rm.ast, v.ast, sort.ast), ctx)
10438 def fpUnsignedToFP(rm, v, sort, ctx=None):
10439 """Create a Z3 floating-point conversion expression that represents the
10440 conversion
from an unsigned bit-vector term (encoding an integer) to a floating-point term.
10449 _z3_assert(is_fprm(rm), "First argument must be a Z3 floating-point rounding mode expression.")
10450 _z3_assert(is_bv(v), "Second argument must be a Z3 bit-vector expression")
10451 _z3_assert(is_fp_sort(sort), "Third argument must be a Z3 floating-point sort.")
10452 ctx = _get_ctx(ctx)
10453 return FPRef(Z3_mk_fpa_to_fp_unsigned(ctx.ref(), rm.ast, v.ast, sort.ast), ctx)
10456 def fpToFPUnsigned(rm, x, s, ctx=None):
10457 """Create a Z3 floating-point conversion expression,
from unsigned bit-vector to floating-point expression.
"""
10459 _z3_assert(is_fprm(rm), "First argument must be a Z3 floating-point rounding mode expression")
10460 _z3_assert(is_bv(x), "Second argument must be a Z3 bit-vector expression")
10461 _z3_assert(is_fp_sort(s), "Third argument must be Z3 floating-point sort")
10462 ctx = _get_ctx(ctx)
10463 return FPRef(Z3_mk_fpa_to_fp_unsigned(ctx.ref(), rm.ast, x.ast, s.ast), ctx)
10466 def fpToSBV(rm, x, s, ctx=None):
10467 """Create a Z3 floating-point conversion expression,
from floating-point expression to signed bit-vector.
10471 >>> print(
is_fp(x))
10473 >>> print(
is_bv(y))
10475 >>> print(
is_fp(y))
10477 >>> print(
is_bv(x))
10481 _z3_assert(is_fprm(rm), "First argument must be a Z3 floating-point rounding mode expression")
10482 _z3_assert(is_fp(x), "Second argument must be a Z3 floating-point expression")
10483 _z3_assert(is_bv_sort(s), "Third argument must be Z3 bit-vector sort")
10484 ctx = _get_ctx(ctx)
10485 return BitVecRef(Z3_mk_fpa_to_sbv(ctx.ref(), rm.ast, x.ast, s.size()), ctx)
10488 def fpToUBV(rm, x, s, ctx=None):
10489 """Create a Z3 floating-point conversion expression,
from floating-point expression to unsigned bit-vector.
10493 >>> print(
is_fp(x))
10495 >>> print(
is_bv(y))
10497 >>> print(
is_fp(y))
10499 >>> print(
is_bv(x))
10503 _z3_assert(is_fprm(rm), "First argument must be a Z3 floating-point rounding mode expression")
10504 _z3_assert(is_fp(x), "Second argument must be a Z3 floating-point expression")
10505 _z3_assert(is_bv_sort(s), "Third argument must be Z3 bit-vector sort")
10506 ctx = _get_ctx(ctx)
10507 return BitVecRef(Z3_mk_fpa_to_ubv(ctx.ref(), rm.ast, x.ast, s.size()), ctx)
10510 def fpToReal(x, ctx=None):
10511 """Create a Z3 floating-point conversion expression,
from floating-point expression to real.
10515 >>> print(
is_fp(x))
10519 >>> print(
is_fp(y))
10525 _z3_assert(is_fp(x), "First argument must be a Z3 floating-point expression")
10526 ctx = _get_ctx(ctx)
10527 return ArithRef(Z3_mk_fpa_to_real(ctx.ref(), x.ast), ctx)
10530 def fpToIEEEBV(x, ctx=None):
10531 """\brief Conversion of a floating-point term into a bit-vector term
in IEEE 754-2008 format.
10533 The size of the resulting bit-vector
is automatically determined.
10535 Note that IEEE 754-2008 allows multiple different representations of NaN. This conversion
10536 knows only one NaN
and it will always produce the same bit-vector representation of
10541 >>> print(
is_fp(x))
10543 >>> print(
is_bv(y))
10545 >>> print(
is_fp(y))
10547 >>> print(
is_bv(x))
10551 _z3_assert(is_fp(x), "First argument must be a Z3 floating-point expression")
10552 ctx = _get_ctx(ctx)
10553 return BitVecRef(Z3_mk_fpa_to_ieee_bv(ctx.ref(), x.ast), ctx)
10556 #########################################
10558 # Strings, Sequences and Regular expressions
10560 #########################################
10562 class SeqSortRef(SortRef):
10563 """Sequence sort.
"""
10565 def is_string(self):
10566 """Determine
if sort
is a string
10574 return Z3_is_string_sort(self.ctx_ref(), self.ast)
10577 return _to_sort_ref(Z3_get_seq_sort_basis(self.ctx_ref(), self.ast), self.ctx)
10579 class CharSortRef(SortRef):
10580 """Character sort.
"""
10584 def StringSort(ctx=None):
10585 """Create a string sort
10590 ctx = _get_ctx(ctx)
10591 return SeqSortRef(Z3_mk_string_sort(ctx.ref()), ctx)
10593 def CharSort(ctx=None):
10594 """Create a character sort
10599 ctx = _get_ctx(ctx)
10600 return CharSortRef(Z3_mk_char_sort(ctx.ref()), ctx)
10604 """Create a sequence sort over elements provided
in the argument
10609 return SeqSortRef(Z3_mk_seq_sort(s.ctx_ref(), s.ast), s.ctx)
10612 class SeqRef(ExprRef):
10613 """Sequence expression.
"""
10616 return SeqSortRef(Z3_get_sort(self.ctx_ref(), self.as_ast()), self.ctx)
10618 def __add__(self, other):
10619 return Concat(self, other)
10621 def __radd__(self, other):
10622 return Concat(other, self)
10624 def __getitem__(self, i):
10626 i = IntVal(i, self.ctx)
10627 return _to_expr_ref(Z3_mk_seq_nth(self.ctx_ref(), self.as_ast(), i.as_ast()), self.ctx)
10631 i = IntVal(i, self.ctx)
10632 return SeqRef(Z3_mk_seq_at(self.ctx_ref(), self.as_ast(), i.as_ast()), self.ctx)
10634 def is_string(self):
10635 return Z3_is_string_sort(self.ctx_ref(), Z3_get_sort(self.ctx_ref(), self.as_ast()))
10637 def is_string_value(self):
10638 return Z3_is_string(self.ctx_ref(), self.as_ast())
10640 def as_string(self):
10641 """Return a string representation of sequence expression.
"""
10642 if self.is_string_value():
10643 string_length = ctypes.c_uint()
10644 chars = Z3_get_lstring(self.ctx_ref(), self.as_ast(), byref(string_length))
10645 return string_at(chars, size=string_length.value).decode("latin-1")
10646 return Z3_ast_to_string(self.ctx_ref(), self.as_ast())
10648 def __le__(self, other):
10649 return SeqRef(Z3_mk_str_le(self.ctx_ref(), self.as_ast(), other.as_ast()), self.ctx)
10651 def __lt__(self, other):
10652 return SeqRef(Z3_mk_str_lt(self.ctx_ref(), self.as_ast(), other.as_ast()), self.ctx)
10654 def __ge__(self, other):
10655 return SeqRef(Z3_mk_str_le(self.ctx_ref(), other.as_ast(), self.as_ast()), self.ctx)
10657 def __gt__(self, other):
10658 return SeqRef(Z3_mk_str_lt(self.ctx_ref(), other.as_ast(), self.as_ast()), self.ctx)
10661 def _coerce_seq(s, ctx=None):
10662 if isinstance(s, str):
10663 ctx = _get_ctx(ctx)
10664 s = StringVal(s, ctx)
10666 raise Z3Exception("Non-expression passed as a sequence")
10668 raise Z3Exception("Non-sequence passed as a sequence")
10672 def _get_ctx2(a, b, ctx=None):
10683 """Return `
True`
if `a`
is a Z3 sequence expression.
10689 return isinstance(a, SeqRef)
10693 """Return `
True`
if `a`
is a Z3 string expression.
10697 return isinstance(a, SeqRef) and a.is_string()
10700 def is_string_value(a):
10701 """return 'True' if 'a' is a Z3 string constant expression.
10707 return isinstance(a, SeqRef) and a.is_string_value()
10709 def StringVal(s, ctx=None):
10710 """create a string expression
"""
10711 s = "".join(str(ch) if 32 <= ord(ch) and ord(ch) < 127 else "\\u{%x}" % (ord(ch)) for ch in s)
10712 ctx = _get_ctx(ctx)
10713 return SeqRef(Z3_mk_string(ctx.ref(), s), ctx)
10716 def String(name, ctx=None):
10717 """Return a string constant named `name`. If `ctx=
None`, then the
global context
is used.
10721 ctx = _get_ctx(ctx)
10722 return SeqRef(Z3_mk_const(ctx.ref(), to_symbol(name, ctx), StringSort(ctx).ast), ctx)
10725 def Strings(names, ctx=None):
10726 """Return a tuple of String constants.
"""
10727 ctx = _get_ctx(ctx)
10728 if isinstance(names, str):
10729 names = names.split(" ")
10730 return [String(name, ctx) for name in names]
10733 def SubString(s, offset, length):
10734 """Extract substring
or subsequence starting at offset
"""
10735 return Extract(s, offset, length)
10738 def SubSeq(s, offset, length):
10739 """Extract substring
or subsequence starting at offset
"""
10740 return Extract(s, offset, length)
10744 """Create the empty sequence of the given sort
10747 >>> print(e.eq(e2))
10756 if isinstance(s, SeqSortRef):
10757 return SeqRef(Z3_mk_seq_empty(s.ctx_ref(), s.ast), s.ctx)
10758 if isinstance(s, ReSortRef):
10759 return ReRef(Z3_mk_re_empty(s.ctx_ref(), s.ast), s.ctx)
10760 raise Z3Exception("Non-sequence, non-regular expression sort passed to Empty")
10764 """Create the regular expression that accepts the universal language
10772 if isinstance(s, ReSortRef):
10773 return ReRef(Z3_mk_re_full(s.ctx_ref(), s.ast), s.ctx)
10774 raise Z3Exception("Non-sequence, non-regular expression sort passed to Full")
10778 """Create a singleton sequence
"""
10779 return SeqRef(Z3_mk_seq_unit(a.ctx_ref(), a.as_ast()), a.ctx)
10782 def PrefixOf(a, b):
10783 """Check
if 'a' is a prefix of
'b'
10791 ctx = _get_ctx2(a, b)
10792 a = _coerce_seq(a, ctx)
10793 b = _coerce_seq(b, ctx)
10794 return BoolRef(Z3_mk_seq_prefix(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)
10797 def SuffixOf(a, b):
10798 """Check
if 'a' is a suffix of
'b'
10806 ctx = _get_ctx2(a, b)
10807 a = _coerce_seq(a, ctx)
10808 b = _coerce_seq(b, ctx)
10809 return BoolRef(Z3_mk_seq_suffix(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)
10812 def Contains(a, b):
10813 """Check
if 'a' contains
'b'
10820 >>> x, y, z =
Strings(
'x y z')
10825 ctx = _get_ctx2(a, b)
10826 a = _coerce_seq(a, ctx)
10827 b = _coerce_seq(b, ctx)
10828 return BoolRef(Z3_mk_seq_contains(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)
10831 def Replace(s, src, dst):
10832 """Replace the first occurrence of
'src' by
'dst' in 's'
10833 >>> r =
Replace(
"aaa",
"a",
"b")
10837 ctx = _get_ctx2(dst, s)
10838 if ctx is None and is_expr(src):
10840 src = _coerce_seq(src, ctx)
10841 dst = _coerce_seq(dst, ctx)
10842 s = _coerce_seq(s, ctx)
10843 return SeqRef(Z3_mk_seq_replace(src.ctx_ref(), s.as_ast(), src.as_ast(), dst.as_ast()), s.ctx)
10846 def IndexOf(s, substr, offset=None):
10847 """Retrieve the index of substring within a string starting at a specified offset.
10856 if is_expr(offset):
10858 ctx = _get_ctx2(s, substr, ctx)
10859 s = _coerce_seq(s, ctx)
10860 substr = _coerce_seq(substr, ctx)
10861 if _is_int(offset):
10862 offset = IntVal(offset, ctx)
10863 return ArithRef(Z3_mk_seq_index(s.ctx_ref(), s.as_ast(), substr.as_ast(), offset.as_ast()), s.ctx)
10866 def LastIndexOf(s, substr):
10867 """Retrieve the last index of substring within a string
"""
10869 ctx = _get_ctx2(s, substr, ctx)
10870 s = _coerce_seq(s, ctx)
10871 substr = _coerce_seq(substr, ctx)
10872 return ArithRef(Z3_mk_seq_last_index(s.ctx_ref(), s.as_ast(), substr.as_ast()), s.ctx)
10876 """Obtain the length of a sequence
's'
10882 return ArithRef(Z3_mk_seq_length(s.ctx_ref(), s.as_ast()), s.ctx)
10886 """Convert string expression to integer
10898 return ArithRef(Z3_mk_str_to_int(s.ctx_ref(), s.as_ast()), s.ctx)
10902 """Convert integer expression to string
"""
10905 return SeqRef(Z3_mk_int_to_str(s.ctx_ref(), s.as_ast()), s.ctx)
10908 def Re(s, ctx=None):
10909 """The regular expression that accepts sequence
's'
10914 s = _coerce_seq(s, ctx)
10915 return ReRef(Z3_mk_seq_to_re(s.ctx_ref(), s.as_ast()), s.ctx)
10918 # Regular expressions
10920 class ReSortRef(SortRef):
10921 """Regular expression sort.
"""
10924 return _to_sort_ref(Z3_get_re_sort_basis(self.ctx_ref(), self.ast), self.ctx)
10929 return ReSortRef(Z3_mk_re_sort(s.ctx.ref(), s.ast), s.ctx)
10930 if s is None or isinstance(s, Context):
10932 return ReSortRef(Z3_mk_re_sort(ctx.ref(), Z3_mk_string_sort(ctx.ref())), s.ctx)
10933 raise Z3Exception("Regular expression sort constructor expects either a string or a context or no argument")
10936 class ReRef(ExprRef):
10937 """Regular expressions.
"""
10939 def __add__(self, other):
10940 return Union(self, other)
10944 return isinstance(s, ReRef)
10948 """Create regular expression membership test
10957 s = _coerce_seq(s, re.ctx)
10958 return BoolRef(Z3_mk_seq_in_re(s.ctx_ref(), s.as_ast(), re.as_ast()), s.ctx)
10962 """Create union of regular expressions.
10967 args = _get_args(args)
10970 _z3_assert(sz > 0, "At least one argument expected.")
10971 _z3_assert(all([is_re(a) for a in args]), "All arguments must be regular expressions.")
10976 for i in range(sz):
10977 v[i] = args[i].as_ast()
10978 return ReRef(Z3_mk_re_union(ctx.ref(), sz, v), ctx)
10981 def Intersect(*args):
10982 """Create intersection of regular expressions.
10985 args = _get_args(args)
10988 _z3_assert(sz > 0, "At least one argument expected.")
10989 _z3_assert(all([is_re(a) for a in args]), "All arguments must be regular expressions.")
10994 for i in range(sz):
10995 v[i] = args[i].as_ast()
10996 return ReRef(Z3_mk_re_intersect(ctx.ref(), sz, v), ctx)
11000 """Create the regular expression accepting one
or more repetitions of argument.
11009 return ReRef(Z3_mk_re_plus(re.ctx_ref(), re.as_ast()), re.ctx)
11013 """Create the regular expression that optionally accepts the argument.
11022 return ReRef(Z3_mk_re_option(re.ctx_ref(), re.as_ast()), re.ctx)
11025 def Complement(re):
11026 """Create the complement regular expression.
"""
11027 return ReRef(Z3_mk_re_complement(re.ctx_ref(), re.as_ast()), re.ctx)
11031 """Create the regular expression accepting zero
or more repetitions of argument.
11040 return ReRef(Z3_mk_re_star(re.ctx_ref(), re.as_ast()), re.ctx)
11043 def Loop(re, lo, hi=0):
11044 """Create the regular expression accepting between a lower
and upper bound repetitions
11045 >>> re =
Loop(
Re(
"a"), 1, 3)
11053 return ReRef(Z3_mk_re_loop(re.ctx_ref(), re.as_ast(), lo, hi), re.ctx)
11056 def Range(lo, hi, ctx=None):
11057 """Create the range regular expression over two sequences of length 1
11058 >>> range =
Range(
"a",
"z")
11064 lo = _coerce_seq(lo, ctx)
11065 hi = _coerce_seq(hi, ctx)
11066 return ReRef(Z3_mk_re_range(lo.ctx_ref(), lo.ast, hi.ast), lo.ctx)
11068 def AllChar(regex_sort, ctx=None):
11069 """Create a regular expression that accepts all single character strings
11071 return ReRef(Z3_mk_re_allchar(regex_sort.ctx_ref(), regex_sort.ast), regex_sort.ctx)
11073 # Special Relations
11076 def PartialOrder(a, index):
11077 return FuncDeclRef(Z3_mk_partial_order(a.ctx_ref(), a.ast, index), a.ctx)
11080 def LinearOrder(a, index):
11081 return FuncDeclRef(Z3_mk_linear_order(a.ctx_ref(), a.ast, index), a.ctx)
11084 def TreeOrder(a, index):
11085 return FuncDeclRef(Z3_mk_tree_order(a.ctx_ref(), a.ast, index), a.ctx)
11088 def PiecewiseLinearOrder(a, index):
11089 return FuncDeclRef(Z3_mk_piecewise_linear_order(a.ctx_ref(), a.ast, index), a.ctx)
11092 def TransitiveClosure(f):
11093 """Given a binary relation R, such that the two arguments have the same sort
11094 create the transitive closure relation R+.
11095 The transitive closure R+
is a new relation.
11097 return FuncDeclRef(Z3_mk_transitive_closure(f.ctx_ref(), f.ast), f.ctx)
11100 class PropClosures:
11101 def __init__(self):
11105 def set_threaded(self):
11106 if self.lock is None:
11108 self.lock = threading.Lock()
11110 def get(self, ctx):
11113 r = self.bases[ctx]
11115 r = self.bases[ctx]
11118 def set(self, ctx, r):
11121 self.bases[ctx] = r
11123 self.bases[ctx] = r
11125 def insert(self, r):
11128 id = len(self.bases) + 3
11131 id = len(self.bases) + 3
11136 _prop_closures = None
11139 def ensure_prop_closures():
11140 global _prop_closures
11141 if _prop_closures is None:
11142 _prop_closures = PropClosures()
11145 def user_prop_push(ctx):
11146 _prop_closures.get(ctx).push()
11149 def user_prop_pop(ctx, num_scopes):
11150 _prop_closures.get(ctx).pop(num_scopes)
11153 def user_prop_fresh(id, ctx):
11154 _prop_closures.set_threaded()
11155 prop = _prop_closures.get(id)
11156 new_prop = prop.fresh()
11157 _prop_closures.set(new_prop.id, new_prop)
11158 return ctypes.c_void_p(new_prop.id)
11161 def user_prop_fixed(ctx, cb, id, value):
11162 prop = _prop_closures.get(ctx)
11164 prop.fixed(id, _to_expr_ref(ctypes.c_void_p(value), prop.ctx()))
11168 def user_prop_final(ctx, cb):
11169 prop = _prop_closures.get(ctx)
11175 def user_prop_eq(ctx, cb, x, y):
11176 prop = _prop_closures.get(ctx)
11182 def user_prop_diseq(ctx, cb, x, y):
11183 prop = _prop_closures.get(ctx)
11189 _user_prop_push = push_eh_type(user_prop_push)
11190 _user_prop_pop = pop_eh_type(user_prop_pop)
11191 _user_prop_fresh = fresh_eh_type(user_prop_fresh)
11192 _user_prop_fixed = fixed_eh_type(user_prop_fixed)
11193 _user_prop_final = final_eh_type(user_prop_final)
11194 _user_prop_eq = eq_eh_type(user_prop_eq)
11195 _user_prop_diseq = eq_eh_type(user_prop_diseq)
11198 class UserPropagateBase:
11201 # Either solver is set or ctx is set.
11202 # Propagators that are created throuh callbacks
11203 # to "fresh" inherit the context of that is supplied
11204 # as argument to the callback.
11205 # This context should not be deleted. It is owned by the solver.
11207 def __init__(self, s, ctx=None):
11208 assert s is None or ctx is None
11209 ensure_prop_closures()
11213 self.id = _prop_closures.insert(self)
11219 # TBD fresh is broken: ctx is not of the right type when we reach here.
11220 self._ctx = Context()
11221 #Z3_del_context(self._ctx.ctx)
11222 #self._ctx.ctx = ctx
11223 #self._ctx.eh = Z3_set_error_handler(ctx, z3_error_handler)
11224 #Z3_set_ast_print_mode(ctx, Z3_PRINT_SMTLIB2_COMPLIANT)
11226 Z3_solver_propagate_init(self.ctx_ref(),
11228 ctypes.c_void_p(self.id),
11235 self._ctx.ctx = None
11241 return self.solver.ctx
11244 return self.ctx().ref()
11246 def add_fixed(self, fixed):
11247 assert not self.fixed
11248 assert not self._ctx
11249 Z3_solver_propagate_fixed(self.ctx_ref(), self.solver.solver, _user_prop_fixed)
11252 def add_final(self, final):
11253 assert not self.final
11254 assert not self._ctx
11255 Z3_solver_propagate_final(self.ctx_ref(), self.solver.solver, _user_prop_final)
11258 def add_eq(self, eq):
11260 assert not self._ctx
11261 Z3_solver_propagate_eq(self.ctx_ref(), self.solver.solver, _user_prop_eq)
11264 def add_diseq(self, diseq):
11265 assert not self.diseq
11266 assert not self._ctx
11267 Z3_solver_propagate_diseq(self.ctx_ref(), self.solver.solver, _user_prop_diseq)
11271 raise Z3Exception("push needs to be overwritten")
11273 def pop(self, num_scopes):
11274 raise Z3Exception("pop needs to be overwritten")
11277 raise Z3Exception("fresh needs to be overwritten")
11281 assert not self._ctx
11282 return Z3_solver_propagate_register(self.ctx_ref(), self.solver.solver, e.ast)
11285 # Propagation can only be invoked as during a fixed or final callback.
11287 def propagate(self, e, ids, eqs=[]):
11288 num_fixed = len(ids)
11289 _ids = (ctypes.c_uint * num_fixed)()
11290 for i in range(num_fixed):
11293 _lhs = (ctypes.c_uint * num_eqs)()
11294 _rhs = (ctypes.c_uint * num_eqs)()
11295 for i in range(num_eqs):
11296 _lhs[i] = eqs[i][0]
11297 _rhs[i] = eqs[i][1]
11298 Z3_solver_propagate_consequence(e.ctx.ref(), ctypes.c_void_p(
11299 self.cb), num_fixed, _ids, num_eqs, _lhs, _rhs, e.ast)
11301 def conflict(self, ids):
11302 self.propagate(BoolVal(False, self.ctx()), ids, eqs=[])
Z3_param_descrs Z3_API Z3_optimize_get_param_descrs(Z3_context c, Z3_optimize o)
Return the parameter description set for the given optimize object.
Z3_string Z3_API Z3_get_probe_name(Z3_context c, unsigned i)
Return the name of the i probe.
Z3_ast_vector Z3_API Z3_optimize_get_objectives(Z3_context c, Z3_optimize o)
Return objectives on the optimization context. If the objective function is a max-sat objective it is...
Z3_ast Z3_API Z3_mk_distinct(Z3_context c, unsigned num_args, Z3_ast const args[])
Create an AST node representing distinct(args[0], ..., args[num_args-1]).
Z3_fixedpoint Z3_API Z3_mk_fixedpoint(Z3_context c)
Create a new fixedpoint context.
Z3_probe Z3_API Z3_probe_le(Z3_context x, Z3_probe p1, 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 simplify(a, arguments, keywords)
Utils.
Z3_func_decl Z3_API Z3_mk_fresh_func_decl(Z3_context c, Z3_string prefix, unsigned domain_size, Z3_sort const domain[], Z3_sort range)
Declare a fresh constant or function.
void Z3_API Z3_global_param_set(Z3_string param_id, Z3_string param_value)
Set a global (or module) parameter. This setting is shared by all Z3 contexts.
Z3_string Z3_API Z3_apply_result_to_string(Z3_context c, Z3_apply_result r)
Convert the Z3_apply_result object returned by Z3_tactic_apply into a string.
Z3_string Z3_API Z3_get_decl_rational_parameter(Z3_context c, Z3_func_decl d, unsigned idx)
Return the rational value, as a string, associated with a rational parameter.
void Z3_API Z3_fixedpoint_add_rule(Z3_context c, Z3_fixedpoint d, Z3_ast rule, Z3_symbol name)
Add a universal Horn clause as a named rule. The horn_rule should be of the form: ...
def update_rule(self, head, body, name)
Z3_probe Z3_API Z3_probe_ge(Z3_context x, Z3_probe p1, Z3_probe p2)
Return a probe that evaluates to "true" when the value returned by p1 is greater than or equal to the...
Z3_param_descrs Z3_API Z3_tactic_get_param_descrs(Z3_context c, Z3_tactic t)
Return the parameter description set for the given tactic object.
def get_cover_delta(self, level, predicate)
Z3_tactic Z3_API Z3_tactic_when(Z3_context c, Z3_probe p, 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_ast Z3_API Z3_mk_bound(Z3_context c, unsigned index, Z3_sort ty)
Create a bound variable.
def upper_values(self, obj)
Z3_ast Z3_API Z3_mk_mul(Z3_context c, unsigned num_args, Z3_ast const args[])
Create an AST node representing args[0] * ... * args[num_args-1].
def __getitem__(self, idx)
void Z3_API Z3_tactic_inc_ref(Z3_context c, Z3_tactic t)
Increment the reference counter of the given tactic.
Z3_symbol Z3_API Z3_get_decl_name(Z3_context c, Z3_func_decl d)
Return the constant declaration name as a symbol.
void Z3_API Z3_disable_trace(Z3_string tag)
Disable tracing messages tagged as tag when Z3 is compiled in debug mode. It is a NOOP otherwise...
Z3_tactic Z3_API Z3_tactic_and_then(Z3_context c, Z3_tactic t1, Z3_tactic t2)
Return a tactic that applies t1 to a given goal and t2 to every subgoal produced by t1...
Z3_symbol_kind Z3_API Z3_get_symbol_kind(Z3_context c, Z3_symbol s)
Return Z3_INT_SYMBOL if the symbol was constructed using Z3_mk_int_symbol, and Z3_STRING_SYMBOL if th...
Z3_string Z3_API Z3_ast_to_string(Z3_context c, Z3_ast a)
Convert the given AST node into a string.
Z3_symbol Z3_API Z3_mk_string_symbol(Z3_context c, Z3_string s)
Create a Z3 symbol using a C string.
expr range(expr const &lo, expr const &hi)
Z3_ast_vector Z3_API Z3_fixedpoint_from_string(Z3_context c, Z3_fixedpoint f, Z3_string s)
Parse an SMT-LIB2 string with fixedpoint rules. Add the rules to the current fixedpoint context...
Z3_probe Z3_API Z3_probe_gt(Z3_context x, Z3_probe p1, Z3_probe p2)
Return a probe that evaluates to "true" when the value returned by p1 is greater than the value retur...
def substitute_vars(t, m)
Z3_ast Z3_API Z3_mk_const(Z3_context c, Z3_symbol s, Z3_sort ty)
Declare and create a constant.
Z3_tactic Z3_API Z3_mk_tactic(Z3_context c, Z3_string name)
Return a tactic associated with the given name. The complete list of tactics may be obtained using th...
Z3_probe Z3_API Z3_probe_const(Z3_context x, double val)
Return a probe that always evaluates to val.
Z3_probe Z3_API Z3_probe_eq(Z3_context x, Z3_probe p1, Z3_probe p2)
Return a probe that evaluates to "true" when the value returned by p1 is equal to the value returned ...
def declare(self, name, args)
def assert_exprs(self, args)
def set_option(args, kws)
Z3_ast Z3_API Z3_substitute(Z3_context c, Z3_ast a, unsigned num_exprs, Z3_ast const from[], 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_ast Z3_API Z3_mk_pbeq(Z3_context c, unsigned num_args, Z3_ast const args[], int const coeffs[], int k)
Pseudo-Boolean relations.
Z3_optimize Z3_API Z3_mk_optimize(Z3_context c)
Create a new optimize context.
void Z3_API Z3_optimize_assert(Z3_context c, Z3_optimize o, Z3_ast a)
Assert hard constraint to the optimization context.
void Z3_API Z3_del_context(Z3_context c)
Delete the given logical context.
Z3_ast Z3_API Z3_mk_app(Z3_context c, Z3_func_decl d, unsigned num_args, Z3_ast const args[])
Create a constant or function application.
bool Z3_API Z3_is_eq_sort(Z3_context c, Z3_sort s1, Z3_sort s2)
compare sorts.
unsigned Z3_API Z3_get_decl_num_parameters(Z3_context c, Z3_func_decl d)
Return the number of parameters associated with a declaration.
bool Z3_API Z3_is_eq_ast(Z3_context c, Z3_ast t1, Z3_ast t2)
Compare terms.
def assert_exprs(self, args)
Z3_probe Z3_API Z3_mk_probe(Z3_context c, Z3_string name)
Return a probe associated with the given name. The complete list of probes may be obtained using the ...
Z3_solver Z3_API Z3_mk_solver_for_logic(Z3_context c, Z3_symbol logic)
Create a new solver customized for the given logic. It behaves like Z3_mk_solver if the logic is unkn...
Z3_decl_kind Z3_API Z3_get_decl_kind(Z3_context c, Z3_func_decl d)
Return declaration kind corresponding to declaration.
Z3_string Z3_API Z3_get_tactic_name(Z3_context c, unsigned i)
Return the name of the idx tactic.
def lower_values(self, obj)
void Z3_API Z3_optimize_inc_ref(Z3_context c, Z3_optimize d)
Increment the reference counter of the given optimize context.
Z3_ast Z3_API Z3_func_decl_to_ast(Z3_context c, Z3_func_decl f)
Convert a Z3_func_decl into Z3_ast. This is just type casting.
Z3_ast Z3_API Z3_mk_add(Z3_context c, unsigned num_args, Z3_ast const args[])
Create an AST node representing args[0] + ... + args[num_args-1].
Z3_stats Z3_API Z3_fixedpoint_get_statistics(Z3_context c, Z3_fixedpoint d)
Retrieve statistics information from the last call to Z3_fixedpoint_query.
Z3_ast Z3_API Z3_simplify_ex(Z3_context c, Z3_ast a, Z3_params p)
Interface to simplifier.
Z3_context Z3_API Z3_mk_context_rc(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_apply_result Z3_API Z3_tactic_apply(Z3_context c, Z3_tactic t, Z3_goal g)
Apply tactic t to the goal g.
def register_relation(self, relations)
Z3_sort Z3_API Z3_mk_finite_domain_sort(Z3_context c, Z3_symbol name, uint64_t size)
Create a named finite domain sort.
Z3_parameter_kind Z3_API Z3_get_decl_parameter_kind(Z3_context c, Z3_func_decl d, unsigned idx)
Return the parameter type associated with a declaration.
Z3_ast Z3_API Z3_get_app_arg(Z3_context c, Z3_app a, unsigned i)
Return the i-th argument of the given application.
Z3_symbol Z3_API Z3_get_decl_symbol_parameter(Z3_context c, Z3_func_decl d, unsigned idx)
Return the double value associated with an double parameter.
Z3_func_decl Z3_API Z3_get_app_decl(Z3_context c, Z3_app a)
Return the declaration of a constant or function application.
void Z3_API Z3_fixedpoint_assert(Z3_context c, Z3_fixedpoint d, Z3_ast axiom)
Assert a constraint to the fixedpoint context.
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(Z3_string string)
Append user-defined string to interaction log.
unsigned Z3_API Z3_get_index_value(Z3_context c, Z3_ast a)
Return index of de-Bruijn bound variable.
def is_finite_domain_sort(s)
Z3_lbool Z3_API Z3_optimize_check(Z3_context c, Z3_optimize o, unsigned num_assumptions, Z3_ast const assumptions[])
Check consistency and produce optimal values.
def z3_error_handler(c, e)
void Z3_API Z3_optimize_set_params(Z3_context c, Z3_optimize o, Z3_params p)
Set parameters on optimization context.
Z3_tactic Z3_API Z3_tactic_or_else(Z3_context c, Z3_tactic t1, Z3_tactic t2)
Return a tactic that first applies t1 to a given goal, if it fails then returns the result of t2 appl...
def Array(name, dom, rng)
Z3_ast Z3_API Z3_optimize_get_upper(Z3_context c, Z3_optimize o, unsigned idx)
Retrieve upper bound value or approximation for the i'th optimization objective.
Z3_ast_kind Z3_API Z3_get_ast_kind(Z3_context c, Z3_ast a)
Return the kind of the given AST.
bool Z3_API Z3_open_log(Z3_string filename)
Log interaction to a file.
void Z3_API Z3_set_ast_print_mode(Z3_context c, Z3_ast_print_mode mode)
Select mode for the format used for pretty-printing AST nodes.
void Z3_API Z3_optimize_assert_and_track(Z3_context c, Z3_optimize o, Z3_ast a, Z3_ast t)
Assert tracked hard constraint to the optimization context.
Z3_probe Z3_API Z3_probe_not(Z3_context x, Z3_probe p)
Return a probe that evaluates to "true" when p does not evaluate to true.
Z3_param_descrs Z3_API Z3_simplify_get_param_descrs(Z3_context c)
Return the parameter description set for the simplify procedure.
Z3_func_decl Z3_API Z3_mk_func_decl(Z3_context c, Z3_symbol s, unsigned domain_size, Z3_sort const domain[], Z3_sort range)
Declare a constant or function.
def get_rule_names_along_trace(self)
def simplify_param_descrs()
void Z3_API Z3_fixedpoint_inc_ref(Z3_context c, Z3_fixedpoint d)
Increment the reference counter of the given fixedpoint context.
void Z3_API Z3_probe_inc_ref(Z3_context c, Z3_probe p)
Increment the reference counter of the given probe.
Z3_func_decl Z3_API Z3_get_decl_func_decl_parameter(Z3_context c, Z3_func_decl d, unsigned idx)
Return the expression value associated with an expression parameter.
Z3_sort Z3_API Z3_mk_uninterpreted_sort(Z3_context c, Z3_symbol s)
Create a free (uninterpreted) type using the given name (symbol).
void Z3_API Z3_apply_result_inc_ref(Z3_context c, Z3_apply_result r)
Increment the reference counter of the given Z3_apply_result object.
void Z3_API Z3_dec_ref(Z3_context c, Z3_ast a)
Decrement the reference counter of the given AST. The context c should have been created using Z3_mk_...
void Z3_API Z3_del_config(Z3_config c)
Delete the given configuration object.
void Z3_API Z3_optimize_push(Z3_context c, Z3_optimize d)
Create a backtracking point.
def convert_model(self, model)
Z3_ast Z3_API Z3_substitute_vars(Z3_context c, Z3_ast a, unsigned num_exprs, Z3_ast const to[])
Substitute the free variables in a with the expressions in to. For every i smaller than num_exprs...
def __init__(self, args, kws)
Z3_ast Z3_API Z3_optimize_get_lower(Z3_context c, Z3_optimize o, unsigned idx)
Retrieve lower bound value or approximation for the i'th optimization objective.
void Z3_API Z3_optimize_register_model_eh(Z3_context c, Z3_optimize o, Z3_model m, void *ctx, Z3_model_eh model_eh)
register a model event handler for new models.
double Z3_API Z3_get_decl_double_parameter(Z3_context c, Z3_func_decl d, unsigned idx)
Return the double value associated with an double parameter.
unsigned Z3_API Z3_get_ast_hash(Z3_context c, Z3_ast a)
Return a hash code for the given AST. The hash code is structural but two different AST objects can m...
def Extract(high, low, a)
Z3_string Z3_API Z3_optimize_to_string(Z3_context c, Z3_optimize o)
Print the current context as a string.
def solve_using(s, args, keywords)
Z3_string Z3_API Z3_get_full_version(void)
Return a string that fully describes the version of Z3 in use.
Z3_ast_vector Z3_API Z3_fixedpoint_from_file(Z3_context c, Z3_fixedpoint f, Z3_string s)
Parse an SMT-LIB2 file with fixedpoint rules. Add the rules to the current fixedpoint context...
def set_predicate_representation(self, f, representations)
unsigned Z3_API Z3_get_app_num_args(Z3_context c, Z3_app a)
Return the number of argument of an application. If t is an constant, then the number of arguments is...
Z3_tactic Z3_API Z3_tactic_par_and_then(Z3_context c, Z3_tactic t1, Z3_tactic t2)
Return a tactic that applies t1 to a given goal and then t2 to every subgoal produced by t1...
Z3_string Z3_API Z3_fixedpoint_get_reason_unknown(Z3_context c, Z3_fixedpoint d)
Retrieve a string that describes the last status returned by Z3_fixedpoint_query. ...
def RecFunction(name, sig)
Z3_ast Z3_API Z3_sort_to_ast(Z3_context c, Z3_sort s)
Convert a Z3_sort into Z3_ast. This is just type casting.
void Z3_API Z3_set_error_handler(Z3_context c, Z3_error_handler h)
Register a Z3 error handler.
void Z3_API Z3_enable_trace(Z3_string tag)
Enable tracing messages tagged as tag when Z3 is compiled in debug mode. It is a NOOP otherwise...
Strings, Sequences and Regular expressions.
void Z3_API Z3_fixedpoint_dec_ref(Z3_context c, Z3_fixedpoint d)
Decrement the reference counter of the given fixedpoint context.
def apply(self, goal, arguments, keywords)
def solve(args, keywords)
def __init__(self, opt, value, is_max)
def check(self, assumptions)
Z3_ast_vector Z3_API Z3_fixedpoint_get_rules(Z3_context c, Z3_fixedpoint f)
Retrieve set of rules from fixedpoint context.
int Z3_API Z3_get_symbol_int(Z3_context c, Z3_symbol s)
Return the symbol int value.
def __call__(self, goal, arguments, keywords)
void Z3_API Z3_optimize_dec_ref(Z3_context c, Z3_optimize d)
Decrement the reference counter of the given optimize context.
double Z3_API Z3_probe_apply(Z3_context c, Z3_probe p, Z3_goal g)
Execute the probe over the goal. The probe always produce a double value. "Boolean" probes return 0...
unsigned Z3_API Z3_optimize_assert_soft(Z3_context c, Z3_optimize o, Z3_ast a, Z3_string weight, Z3_symbol id)
Assert soft constraint to the optimization context.
def prove(claim, show=False, keywords)
unsigned Z3_API Z3_get_num_tactics(Z3_context c)
Return the number of builtin tactics available in Z3.
def is_finite_domain_value(a)
Z3_string Z3_API Z3_get_numeral_string(Z3_context c, Z3_ast a)
Return numeral value, as a decimal string of a numeric constant term.
def get_rules_along_trace(self)
Z3_ast Z3_API Z3_simplify(Z3_context c, Z3_ast a)
Interface to simplifier.
Z3_model Z3_API Z3_optimize_get_model(Z3_context c, Z3_optimize o)
Retrieve the model for the last Z3_optimize_check.
void Z3_API Z3_interrupt(Z3_context c)
Interrupt the execution of a Z3 procedure. This procedure can be used to interrupt: solvers...
Z3_param_descrs Z3_API Z3_fixedpoint_get_param_descrs(Z3_context c, Z3_fixedpoint f)
Return the parameter description set for the given fixedpoint object.
def assert_and_track(self, a, p)
def translate(self, target)
def from_file(self, filename)
Z3_bool Z3_API Z3_get_finite_domain_sort_size(Z3_context c, Z3_sort s, uint64_t *r)
Store the size of the sort in r. Return false if the call failed. That is, Z3_get_sort_kind(s) == Z3_...
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. ...
Z3_ast Z3_API Z3_get_decl_ast_parameter(Z3_context c, Z3_func_decl d, unsigned idx)
Return the expression value associated with an expression parameter.
def query_from_lvl(self, lvl, query)
Z3_tactic Z3_API Z3_tactic_using_params(Z3_context c, Z3_tactic t, Z3_params p)
Return a tactic that applies t using the given set of parameters.
Z3_sort Z3_API Z3_get_domain(Z3_context c, Z3_func_decl d, unsigned i)
Return the sort of the i-th parameter of the given function declaration.
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...
void Z3_API Z3_optimize_pop(Z3_context c, Z3_optimize d)
Backtrack one level.
unsigned Z3_API Z3_get_ast_id(Z3_context c, Z3_ast t)
Return a unique identifier for t. The identifier is unique up to structural equality. Thus, two ast nodes created by the same context and having the same children and same function symbols have the same identifiers. Ast nodes created in the same context, but having different children or different functions have different identifiers. Variables and quantifiers are also assigned different identifiers according to their structure.
Z3_config Z3_API Z3_mk_config(void)
Create a configuration object for the Z3 context object.
def to_string(self, queries)
Z3_sort Z3_API Z3_get_range(Z3_context c, Z3_func_decl d)
Return the range of the given declaration.
void Z3_API Z3_fixedpoint_update_rule(Z3_context c, Z3_fixedpoint d, Z3_ast a, Z3_symbol name)
Update a named rule. A rule with the same name must have been previously created. ...
Z3_tactic Z3_API Z3_tactic_repeat(Z3_context c, 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_stats Z3_API Z3_optimize_get_statistics(Z3_context c, Z3_optimize d)
Retrieve statistics information from the last call to Z3_optimize_check.
Z3_tactic Z3_API Z3_tactic_cond(Z3_context c, Z3_probe p, Z3_tactic t1, 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_tactic Z3_API Z3_tactic_par_or(Z3_context c, unsigned num, Z3_tactic const ts[])
Return a tactic that applies the given tactics in parallel.
Z3_ast Z3_API Z3_mk_pble(Z3_context c, unsigned num_args, Z3_ast const args[], int const coeffs[], int k)
Pseudo-Boolean relations.
def set_on_model(self, on_model)
def parse_string(self, s)
Z3_solver Z3_API Z3_mk_simple_solver(Z3_context c)
Create a new incremental solver.
Z3_tactic Z3_API Z3_tactic_try_for(Z3_context c, Z3_tactic t, unsigned ms)
Return a tactic that applies t to a given goal for ms milliseconds. If t does not terminate in ms mil...
void Z3_API Z3_set_param_value(Z3_config c, Z3_string param_id, Z3_string param_value)
Set a configuration parameter.
def get_ground_sat_answer(self)
Z3_ast_vector Z3_API Z3_optimize_get_assertions(Z3_context c, Z3_optimize o)
Return the set of asserted formulas on the optimization context.
def declare_var(self, vars)
Z3_probe Z3_API Z3_probe_lt(Z3_context x, Z3_probe p1, Z3_probe p2)
Return a probe that evaluates to "true" when the value returned by p1 is less than the value returned...
Z3_string Z3_API Z3_fixedpoint_get_help(Z3_context c, Z3_fixedpoint f)
Return a string describing all fixedpoint available parameters.
Z3_tactic Z3_API Z3_tactic_fail_if(Z3_context c, Z3_probe p)
Return a tactic that fails if the probe p evaluates to false.
Z3_string Z3_API Z3_optimize_get_help(Z3_context c, Z3_optimize t)
Return a string containing a description of parameters accepted by optimize.
Z3_string Z3_API Z3_get_symbol_string(Z3_context c, Z3_symbol s)
Return the symbol name.
Z3_goal Z3_API Z3_apply_result_get_subgoal(Z3_context c, Z3_apply_result r, unsigned i)
Return one of the subgoals in the Z3_apply_result object returned by Z3_tactic_apply.
Z3_sort_kind Z3_API Z3_get_sort_kind(Z3_context c, Z3_sort t)
Return the sort kind (e.g., array, tuple, int, bool, etc).
def __rmul__(self, other)
def RecAddDefinition(f, args, body)
Z3_ast Z3_API Z3_mk_pbge(Z3_context c, unsigned num_args, Z3_ast const args[], int const coeffs[], int k)
Pseudo-Boolean relations.
void Z3_API Z3_apply_result_dec_ref(Z3_context c, Z3_apply_result r)
Decrement the reference counter of the given Z3_apply_result object.
Z3_ast_vector Z3_API Z3_fixedpoint_get_assertions(Z3_context c, Z3_fixedpoint f)
Retrieve set of background assertions from fixedpoint context.
Z3_ast Z3_API Z3_mk_atleast(Z3_context c, unsigned num_args, Z3_ast const args[], unsigned k)
Pseudo-Boolean relations.
Z3_ast Z3_API Z3_fixedpoint_get_cover_delta(Z3_context c, Z3_fixedpoint d, int level, Z3_func_decl pred)
unsigned Z3_API Z3_get_num_probes(Z3_context c)
Return the number of builtin probes available in Z3.
Z3_ast Z3_API Z3_translate(Z3_context source, Z3_ast a, Z3_context target)
Translate/Copy the AST a from context source to context target. AST a must have been created using co...
void Z3_API Z3_get_version(unsigned *major, unsigned *minor, unsigned *build_number, unsigned *revision_number)
Return Z3 version number information.
Z3_lbool Z3_API Z3_fixedpoint_query(Z3_context c, Z3_fixedpoint d, Z3_ast query)
Pose a query against the asserted rules.
Z3_string Z3_API Z3_optimize_get_reason_unknown(Z3_context c, Z3_optimize d)
Retrieve a string that describes the last status returned by Z3_optimize_check.
Z3_string Z3_API Z3_tactic_get_help(Z3_context c, Z3_tactic t)
Return a string containing a description of parameters accepted by the given tactic.
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...
unsigned Z3_API Z3_optimize_minimize(Z3_context c, Z3_optimize o, Z3_ast t)
Add a minimization constraint.
def get_num_levels(self, predicate)
Z3_string Z3_API Z3_simplify_get_help(Z3_context c)
Return a string describing all available parameters.
Z3_ast Z3_API Z3_mk_eq(Z3_context c, Z3_ast l, Z3_ast r)
Create an AST node representing l = r.
Z3_ast Z3_API Z3_mk_atmost(Z3_context c, unsigned num_args, Z3_ast const args[], unsigned k)
Pseudo-Boolean relations.
Z3_symbol Z3_API Z3_mk_int_symbol(Z3_context c, int i)
Create a Z3 symbol using an integer.
void Z3_API Z3_add_rec_def(Z3_context c, Z3_func_decl f, unsigned n, Z3_ast args[], Z3_ast body)
Define the body of a recursive function.
def __init__(self, result, ctx)
def set(self, args, keys)
Z3_ast Z3_API Z3_mk_ite(Z3_context c, Z3_ast t1, Z3_ast t2, Z3_ast t3)
Create an AST node representing an if-then-else: ite(t1, t2, t3).
Z3_solver Z3_API Z3_mk_solver_from_tactic(Z3_context c, Z3_tactic t)
Create a new solver that is implemented using the given tactic. The solver supports the commands Z3_s...
Z3_ast_vector Z3_API Z3_optimize_get_upper_as_vector(Z3_context c, Z3_optimize o, unsigned idx)
Retrieve upper bound value or approximation for the i'th optimization objective.
Z3_string Z3_API Z3_benchmark_to_smtlib_string(Z3_context c, Z3_string name, Z3_string logic, Z3_string status, Z3_string attributes, unsigned num_assumptions, Z3_ast const assumptions[], Z3_ast formula)
Convert the given benchmark into SMT-LIB formatted string.
def add_cover(self, level, predicate, property)
int Z3_API Z3_get_decl_int_parameter(Z3_context c, Z3_func_decl d, unsigned idx)
Return the integer value associated with an integer parameter.
unsigned Z3_API Z3_get_arity(Z3_context c, Z3_func_decl d)
Alias for Z3_get_domain_size.
Z3_sort Z3_API Z3_get_sort(Z3_context c, Z3_ast a)
Return the sort of an AST node.
void Z3_API Z3_probe_dec_ref(Z3_context c, Z3_probe p)
Decrement the reference counter of the given probe.
void Z3_API Z3_optimize_from_file(Z3_context c, Z3_optimize o, Z3_string s)
Parse an SMT-LIB2 file with assertions, soft constraints and optimization objectives. Add the parsed constraints and objectives to the optimization context.
Z3_symbol Z3_API Z3_get_sort_name(Z3_context c, Z3_sort d)
Return the sort name as a symbol.
Z3_ast_vector Z3_API Z3_optimize_get_lower_as_vector(Z3_context c, Z3_optimize o, unsigned idx)
Retrieve lower bound value or approximation for the i'th optimization objective. The returned vector ...
void Z3_API Z3_fixedpoint_set_params(Z3_context c, Z3_fixedpoint f, Z3_params p)
Set parameters on fixedpoint context.
void Z3_API Z3_fixedpoint_set_predicate_representation(Z3_context c, Z3_fixedpoint d, Z3_func_decl f, unsigned num_relations, Z3_symbol const relation_kinds[])
Configure the predicate representation.
Z3_string Z3_API Z3_probe_get_descr(Z3_context c, Z3_string name)
Return a string containing a description of the probe with the given name.
def set(self, args, keys)
Z3_bool Z3_API Z3_global_param_get(Z3_string param_id, Z3_string_ptr param_value)
Get a global (or module) parameter.
void Z3_API Z3_fixedpoint_register_relation(Z3_context c, Z3_fixedpoint d, Z3_func_decl f)
Register relation as Fixedpoint defined. Fixedpoint defined relations have least-fixedpoint semantics...
void Z3_API Z3_inc_ref(Z3_context c, Z3_ast a)
Increment the reference counter of the given AST. The context c should have been created using Z3_mk_...
Z3_ast Z3_API Z3_mk_numeral(Z3_context c, Z3_string numeral, Z3_sort ty)
Create a numeral of a given sort.
Z3_ast_vector Z3_API Z3_optimize_get_unsat_core(Z3_context c, Z3_optimize o)
Retrieve the unsat core for the last Z3_optimize_check The unsat core is a subset of the assumptions ...
Z3_string Z3_API Z3_tactic_get_descr(Z3_context c, Z3_string name)
Return a string containing a description of the tactic with the given name.
unsigned Z3_API Z3_optimize_maximize(Z3_context c, Z3_optimize o, Z3_ast t)
Add a maximization constraint.
unsigned Z3_API Z3_apply_result_get_num_subgoals(Z3_context c, Z3_apply_result r)
Return the number of subgoals in the Z3_apply_result object returned by Z3_tactic_apply.
def check(self, assumptions)
Z3_ast Z3_API Z3_fixedpoint_get_answer(Z3_context c, Z3_fixedpoint d)
Retrieve a formula that encodes satisfying answers to the query.
Z3_string Z3_API Z3_fixedpoint_to_string(Z3_context c, Z3_fixedpoint f, unsigned num_queries, Z3_ast queries[])
Print the current rules and background axioms as a string.
Z3_lbool Z3_API Z3_fixedpoint_query_relations(Z3_context c, Z3_fixedpoint d, unsigned num_relations, Z3_func_decl const relations[])
Pose multiple queries against the asserted rules.
def is_algebraic_value(a)
void Z3_API Z3_tactic_dec_ref(Z3_context c, Z3_tactic g)
Decrement the reference counter of the given tactic.
Z3_func_decl Z3_API Z3_mk_rec_func_decl(Z3_context c, Z3_symbol s, unsigned domain_size, Z3_sort const domain[], Z3_sort range)
Declare a recursive function.
Z3_sort Z3_API Z3_get_decl_sort_parameter(Z3_context c, Z3_func_decl d, unsigned idx)
Return the sort value associated with a sort parameter.
void Z3_API Z3_optimize_from_string(Z3_context c, Z3_optimize o, Z3_string s)
Parse an SMT-LIB2 string with assertions, soft constraints and optimization objectives. Add the parsed constraints and objectives to the optimization context.
Z3_ast Z3_API Z3_mk_fresh_const(Z3_context c, Z3_string prefix, Z3_sort ty)
Declare and create a fresh constant.