Z3
Loading...
Searching...
No Matches
z3py.py
Go to the documentation of this file.
8
9"""Z3 is a high performance theorem prover developed at Microsoft Research.
10
11Z3 is used in many applications such as: software/hardware verification and testing,
12constraint solving, analysis of hybrid systems, security, biology (in silico analysis),
13and geometrical problems.
14
15
16Please send feedback, comments and/or corrections on the Issue tracker for
17https://github.com/Z3prover/z3.git. Your comments are very valuable.
18
19Small example:
20
21>>> x = Int('x')
22>>> y = Int('y')
23>>> s = Solver()
24>>> s.add(x > 0)
25>>> s.add(x < 2)
26>>> s.add(y == x + 1)
27>>> s.check()
28sat
29>>> m = s.model()
30>>> m[x]
311
32>>> m[y]
332
34
35Z3 exceptions:
36
37>>> try:
38... x = BitVec('x', 32)
39... y = Bool('y')
40... # the expression x + y is type incorrect
41... n = x + y
42... except Z3Exception as ex:
43... print("failed: %s" % ex)
44failed: sort mismatch
45"""
46from . import z3core
47from .z3core import *
48from .z3types import *
49from .z3consts import *
50from .z3printer import *
51from fractions import Fraction
52import sys
53import io
54import math
55import copy
56if sys.version_info.major >= 3:
57 from typing import Iterable, Iterator
58
59from collections.abc import Callable
60from typing import (
61 Any,
62 Iterable,
63 Sequence
64)
65
66
67Z3_DEBUG = __debug__
68
69
71 global Z3_DEBUG
72 return Z3_DEBUG
73
74
75if sys.version_info.major < 3:
76 def _is_int(v):
77 return isinstance(v, (int, long))
78else:
79 def _is_int(v):
80 return isinstance(v, int)
81
82
83def enable_trace(msg):
85
86
89
90
92 major = ctypes.c_uint(0)
93 minor = ctypes.c_uint(0)
94 build = ctypes.c_uint(0)
95 rev = ctypes.c_uint(0)
96 Z3_get_version(major, minor, build, rev)
97 return "%s.%s.%s" % (major.value, minor.value, build.value)
98
99
101 major = ctypes.c_uint(0)
102 minor = ctypes.c_uint(0)
103 build = ctypes.c_uint(0)
104 rev = ctypes.c_uint(0)
105 Z3_get_version(major, minor, build, rev)
106 return (major.value, minor.value, build.value, rev.value)
107
108
110 return Z3_get_full_version()
111
112
113def _z3_assert(cond, msg):
114 if not cond:
115 raise Z3Exception(msg)
116
117
119 _z3_assert(ctypes.c_int(n).value == n, name + " is too large")
120
121
122def open_log(fname):
123 """Log interaction to a file. This function must be invoked immediately after init(). """
124 Z3_open_log(fname)
125
126
128 """Append user-defined string to interaction log. """
130
131
132def to_symbol(s, ctx = None):
133 """Convert an integer or string into a Z3 symbol."""
134 if _is_int(s):
135 return Z3_mk_int_symbol(_get_ctx(ctx).ref(), s)
136 else:
137 return Z3_mk_string_symbol(_get_ctx(ctx).ref(), s)
138
139
140def _symbol2py(ctx, s):
141 """Convert a Z3 symbol back into a Python object. """
142 if Z3_get_symbol_kind(ctx.ref(), s) == Z3_INT_SYMBOL:
143 return "k!%s" % Z3_get_symbol_int(ctx.ref(), s)
144 else:
145 return Z3_get_symbol_string(ctx.ref(), s)
146
147# Hack for having nary functions that can receive one argument that is the
148# list of arguments.
149# Use this when function takes a single list of arguments
150
151
152def _get_args(args):
153 try:
154 if len(args) == 1 and (isinstance(args[0], tuple) or isinstance(args[0], list)):
155 return args[0]
156 elif len(args) == 1 and (isinstance(args[0], set) or isinstance(args[0], AstVector)):
157 return [arg for arg in args[0]]
158 elif len(args) == 1 and isinstance(args[0], Iterator):
159 return list(args[0])
160 else:
161 return args
162 except TypeError: # len is not necessarily defined when args is not a sequence (use reflection?)
163 return args
164
165# Use this when function takes multiple arguments
166
167
169 try:
170 if isinstance(args, (set, AstVector, tuple)):
171 return [arg for arg in args]
172 else:
173 return args
174 except Exception:
175 return args
176
177
179 if isinstance(val, bool):
180 return "true" if val else "false"
181 return str(val)
182
183
185 # Do nothing error handler, just avoid exit(0)
186 # The wrappers in z3core.py will raise a Z3Exception if an error is detected
187 return
188
189
190class Context:
191 """A Context manages all other Z3 objects, global configuration options, etc.
192
193 Z3Py uses a default global context. For most applications this is sufficient.
194 An application may use multiple Z3 contexts. Objects created in one context
195 cannot be used in another one. However, several objects may be "translated" from
196 one context to another. It is not safe to access Z3 objects from multiple threads.
197 The only exception is the method `interrupt()` that can be used to interrupt() a long
198 computation.
199 The initialization method receives global configuration options for the new context.
200 """
201
202 def __init__(self, *args, **kws):
203 if z3_debug():
204 _z3_assert(len(args) % 2 == 0, "Argument list must have an even number of elements.")
205 conf = Z3_mk_config()
206 for key in kws:
207 value = kws[key]
208 Z3_set_param_value(conf, str(key).upper(), _to_param_value(value))
209 prev = None
210 for a in args:
211 if prev is None:
212 prev = a
213 else:
214 Z3_set_param_value(conf, str(prev), _to_param_value(a))
215 prev = None
217 self.owner = True
218 self.eh = Z3_set_error_handler(self.ctx, z3_error_handler)
219 Z3_set_ast_print_mode(self.ctx, Z3_PRINT_SMTLIB2_COMPLIANT)
220 Z3_del_config(conf)
221
222 def __del__(self):
223 if Z3_del_context is not None and self.owner:
224 Z3_del_context(self.ctx)
225 self.ctx = None
226 self.eh = None
227
228 def ref(self):
229 """Return a reference to the actual C pointer to the Z3 context."""
230 return self.ctx
231
232 def interrupt(self):
233 """Interrupt a solver performing a satisfiability test, a tactic processing a goal, or simplify functions.
234
235 This method can be invoked from a thread different from the one executing the
236 interruptible procedure.
237 """
238 Z3_interrupt(self.ref())
239
240 def param_descrs(self):
241 """Return the global parameter description set."""
242 return ParamDescrsRef(Z3_get_global_param_descrs(self.ref()), self)
243
244
245# Global Z3 context
246_main_ctx = None
247
248
249def main_ctx() -> Context:
250 """Return a reference to the global Z3 context.
251
252 >>> x = Real('x')
253 >>> x.ctx == main_ctx()
254 True
255 >>> c = Context()
256 >>> c == main_ctx()
257 False
258 >>> x2 = Real('x', c)
259 >>> x2.ctx == c
260 True
261 >>> eq(x, x2)
262 False
263 """
264 global _main_ctx
265 if _main_ctx is None:
266 _main_ctx = Context()
267 return _main_ctx
268
269
270def _get_ctx(ctx) -> Context:
271 if ctx is None:
272 return main_ctx()
273 else:
274 return ctx
275
276
277def get_ctx(ctx) -> Context:
278 return _get_ctx(ctx)
279
280
281def set_param(*args, **kws):
282 """Set Z3 global (or module) parameters.
283
284 >>> set_param(precision=10)
285 """
286 if z3_debug():
287 _z3_assert(len(args) % 2 == 0, "Argument list must have an even number of elements.")
288 new_kws = {}
289 for k in kws:
290 v = kws[k]
291 if not set_pp_option(k, v):
292 new_kws[k] = v
293 for key in new_kws:
294 value = new_kws[key]
295 Z3_global_param_set(str(key).upper(), _to_param_value(value))
296 prev = None
297 for a in args:
298 if prev is None:
299 prev = a
300 else:
302 prev = None
303
304
305def reset_params() -> None:
306 """Reset all global (or module) parameters.
307 """
309
310
311def set_option(*args, **kws):
312 """Alias for 'set_param' for backward compatibility.
313 """
314 return set_param(*args, **kws)
315
316
317def get_param(name):
318 """Return the value of a Z3 global (or module) parameter
319
320 >>> get_param('nlsat.reorder')
321 'true'
322 """
323 ptr = (ctypes.c_char_p * 1)()
324 if Z3_global_param_get(str(name), ptr):
325 r = z3core._to_pystr(ptr[0])
326 return r
327 raise Z3Exception("failed to retrieve value for '%s'" % name)
328
329
334
335# Mark objects that use pretty printer
336
337
339 """Superclass for all Z3 objects that have support for pretty printing."""
340
341 def use_pp(self):
342 return True
343
344 def _repr_html_(self):
345 in_html = in_html_mode()
346 set_html_mode(True)
347 res = repr(self)
348 set_html_mode(in_html)
349 return res
350
351
353 """AST are Direct Acyclic Graphs (DAGs) used to represent sorts, declarations and expressions."""
354
355 def __init__(self, ast, ctx=None):
356 self.ast = ast
357 self.ctx = _get_ctx(ctx)
358 Z3_inc_ref(self.ctx.ref(), self.as_ast())
359
360 def __del__(self):
361 if self.ctx.ref() is not None and self.ast is not None and Z3_dec_ref is not None:
362 Z3_dec_ref(self.ctx.ref(), self.as_ast())
363 self.ast = None
364
365 def __deepcopy__(self, memo={}):
366 return _to_ast_ref(self.ast, self.ctx)
367
368 def __str__(self):
369 return obj_to_string(self)
370
371 def __repr__(self):
372 return obj_to_string(self)
373
374 def __eq__(self, other):
375 return self.eq(other)
376
377 def __hash__(self):
378 return self.hash()
379
380 def __nonzero__(self):
381 return self.__bool__()
382
383 def __bool__(self):
384 if is_true(self):
385 return True
386 elif is_false(self):
387 return False
388 elif is_eq(self) and self.num_args() == 2:
389 return self.arg(0).eq(self.arg(1))
390 else:
391 raise Z3Exception("Symbolic expressions cannot be cast to concrete Boolean values.")
392
393 def sexpr(self):
394 """Return a string representing the AST node in s-expression notation.
395
396 >>> x = Int('x')
397 >>> ((x + 1)*x).sexpr()
398 '(* (+ x 1) x)'
399 """
400 return Z3_ast_to_string(self.ctx_ref(), self.as_ast())
401
402 def as_ast(self):
403 """Return a pointer to the corresponding C Z3_ast object."""
404 return self.ast
405
406 def get_id(self):
407 """Return unique identifier for object. It can be used for hash-tables and maps."""
408 return Z3_get_ast_id(self.ctx_ref(), self.as_ast())
409
410 def ctx_ref(self):
411 """Return a reference to the C context where this AST node is stored."""
412 return self.ctx.ref()
413
414 def eq(self, other):
415 """Return `True` if `self` and `other` are structurally identical.
416
417 >>> x = Int('x')
418 >>> n1 = x + 1
419 >>> n2 = 1 + x
420 >>> n1.eq(n2)
421 False
422 >>> n1 = simplify(n1)
423 >>> n2 = simplify(n2)
424 >>> n1.eq(n2)
425 True
426 """
427 if z3_debug():
428 _z3_assert(is_ast(other), "Z3 AST expected")
429 return Z3_is_eq_ast(self.ctx_ref(), self.as_ast(), other.as_ast())
430
431 def translate(self, target):
432 """Translate `self` to the context `target`. That is, return a copy of `self` in the context `target`.
433
434 >>> c1 = Context()
435 >>> c2 = Context()
436 >>> x = Int('x', c1)
437 >>> y = Int('y', c2)
438 >>> # Nodes in different contexts can't be mixed.
439 >>> # However, we can translate nodes from one context to another.
440 >>> x.translate(c2) + y
441 x + y
442 """
443 if z3_debug():
444 _z3_assert(isinstance(target, Context), "argument must be a Z3 context")
445 return _to_ast_ref(Z3_translate(self.ctx.ref(), self.as_ast(), target.ref()), target)
446
447 def __copy__(self):
448 return self.translate(self.ctx)
449
450 def hash(self):
451 """Return a hashcode for the `self`.
452
453 >>> n1 = simplify(Int('x') + 1)
454 >>> n2 = simplify(2 + Int('x') - 1)
455 >>> n1.hash() == n2.hash()
456 True
457 """
458 return Z3_get_ast_hash(self.ctx_ref(), self.as_ast())
459
460 def py_value(self):
461 """Return a Python value that is equivalent to `self`."""
462 return None
463
464
465def is_ast(a : Any) -> bool:
466 """Return `True` if `a` is an AST node.
467
468 >>> is_ast(10)
469 False
470 >>> is_ast(IntVal(10))
471 True
472 >>> is_ast(Int('x'))
473 True
474 >>> is_ast(BoolSort())
475 True
476 >>> is_ast(Function('f', IntSort(), IntSort()))
477 True
478 >>> is_ast("x")
479 False
480 >>> is_ast(Solver())
481 False
482 """
483 return isinstance(a, AstRef)
484
485
486def eq(a : AstRef, b : AstRef) -> bool:
487 """Return `True` if `a` and `b` are structurally identical AST nodes.
488
489 >>> x = Int('x')
490 >>> y = Int('y')
491 >>> eq(x, y)
492 False
493 >>> eq(x + 1, x + 1)
494 True
495 >>> eq(x + 1, 1 + x)
496 False
497 >>> eq(simplify(x + 1), simplify(1 + x))
498 True
499 """
500 if z3_debug():
501 _z3_assert(is_ast(a) and is_ast(b), "Z3 ASTs expected")
502 return a.eq(b)
503
504
505def _ast_kind(ctx : Context, a : Any) -> int:
506 if is_ast(a):
507 a = a.as_ast()
508 return Z3_get_ast_kind(ctx.ref(), a)
509
510
511def _ctx_from_ast_arg_list(args, default_ctx=None):
512 ctx = None
513 for a in args:
514 if is_ast(a) or is_probe(a):
515 if ctx is None:
516 ctx = a.ctx
517 else:
518 if z3_debug():
519 _z3_assert(ctx == a.ctx, "Context mismatch")
520 if ctx is None:
521 ctx = default_ctx
522 return ctx
523
524
526 return _ctx_from_ast_arg_list(args)
527
528
530 sz = len(args)
531 _args = (FuncDecl * sz)()
532 for i in range(sz):
533 _args[i] = args[i].as_func_decl()
534 return _args, sz
535
536
538 sz = len(args)
539 _args = (Ast * sz)()
540 for i in range(sz):
541 _args[i] = args[i].as_ast()
542 return _args, sz
543
544
545def _to_ref_array(ref, args):
546 sz = len(args)
547 _args = (ref * sz)()
548 for i in range(sz):
549 _args[i] = args[i].as_ast()
550 return _args, sz
551
552
553def _to_ast_ref(a, ctx):
554 k = _ast_kind(ctx, a)
555 if k == Z3_SORT_AST:
556 return _to_sort_ref(a, ctx)
557 elif k == Z3_FUNC_DECL_AST:
558 return _to_func_decl_ref(a, ctx)
559 else:
560 return _to_expr_ref(a, ctx)
561
562
563
568
569def _sort_kind(ctx, s):
570 return Z3_get_sort_kind(ctx.ref(), s)
571
572
574 """A Sort is essentially a type. Every Z3 expression has a sort. A sort is an AST node."""
575
576 def as_ast(self):
577 return Z3_sort_to_ast(self.ctx_ref(), self.ast)
578
579 def get_id(self):
580 return Z3_get_ast_id(self.ctx_ref(), self.as_ast())
581
582 def kind(self):
583 """Return the Z3 internal kind of a sort.
584 This method can be used to test if `self` is one of the Z3 builtin sorts.
585
586 >>> b = BoolSort()
587 >>> b.kind() == Z3_BOOL_SORT
588 True
589 >>> b.kind() == Z3_INT_SORT
590 False
591 >>> A = ArraySort(IntSort(), IntSort())
592 >>> A.kind() == Z3_ARRAY_SORT
593 True
594 >>> A.kind() == Z3_INT_SORT
595 False
596 """
597 return _sort_kind(self.ctx, self.ast)
598
599 def subsort(self, other):
600 """Return `True` if `self` is a subsort of `other`.
601
602 >>> IntSort().subsort(RealSort())
603 True
604 """
605 return False
606
607 def cast(self, val):
608 """Try to cast `val` as an element of sort `self`.
609
610 This method is used in Z3Py to convert Python objects such as integers,
611 floats, longs and strings into Z3 expressions.
612
613 >>> x = Int('x')
614 >>> RealSort().cast(x)
615 ToReal(x)
616 """
617 if z3_debug():
618 _z3_assert(is_expr(val), "Z3 expression expected")
619 _z3_assert(self.eq(val.sort()), "Sort mismatch")
620 return val
621
622 def name(self):
623 """Return the name (string) of sort `self`.
624
625 >>> BoolSort().name()
626 'Bool'
627 >>> ArraySort(IntSort(), IntSort()).name()
628 'Array'
629 """
630 return _symbol2py(self.ctx, Z3_get_sort_name(self.ctx_ref(), self.ast))
631
632 def __eq__(self, other):
633 """Return `True` if `self` and `other` are the same Z3 sort.
634
635 >>> p = Bool('p')
636 >>> p.sort() == BoolSort()
637 True
638 >>> p.sort() == IntSort()
639 False
640 """
641 if other is None:
642 return False
643 return Z3_is_eq_sort(self.ctx_ref(), self.ast, other.ast)
644
645 def __ne__(self, other):
646 """Return `True` if `self` and `other` are not the same Z3 sort.
647
648 >>> p = Bool('p')
649 >>> p.sort() != BoolSort()
650 False
651 >>> p.sort() != IntSort()
652 True
653 """
654 return not Z3_is_eq_sort(self.ctx_ref(), self.ast, other.ast)
655
656 def __gt__(self, other):
657 """Create the function space Array(self, other)"""
658 return ArraySort(self, other)
659
660 def __hash__(self):
661 """ Hash code. """
662 return AstRef.__hash__(self)
663
664
665def is_sort(s : Any) -> bool:
666 """Return `True` if `s` is a Z3 sort.
667
668 >>> is_sort(IntSort())
669 True
670 >>> is_sort(Int('x'))
671 False
672 >>> is_expr(Int('x'))
673 True
674 """
675 return isinstance(s, SortRef)
676
677
678def _to_sort_ref(s, ctx):
679 if z3_debug():
680 _z3_assert(isinstance(s, Sort), "Z3 Sort expected")
681 k = _sort_kind(ctx, s)
682 if k == Z3_BOOL_SORT:
683 return BoolSortRef(s, ctx)
684 elif k == Z3_INT_SORT or k == Z3_REAL_SORT:
685 return ArithSortRef(s, ctx)
686 elif k == Z3_BV_SORT:
687 return BitVecSortRef(s, ctx)
688 elif k == Z3_ARRAY_SORT:
689 return ArraySortRef(s, ctx)
690 elif k == Z3_DATATYPE_SORT:
691 return DatatypeSortRef(s, ctx)
692 elif k == Z3_FINITE_DOMAIN_SORT:
693 return FiniteDomainSortRef(s, ctx)
694 elif k == Z3_FLOATING_POINT_SORT:
695 return FPSortRef(s, ctx)
696 elif k == Z3_ROUNDING_MODE_SORT:
697 return FPRMSortRef(s, ctx)
698 elif k == Z3_RE_SORT:
699 return ReSortRef(s, ctx)
700 elif k == Z3_SEQ_SORT:
701 return SeqSortRef(s, ctx)
702 elif k == Z3_CHAR_SORT:
703 return CharSortRef(s, ctx)
704 elif k == Z3_TYPE_VAR:
705 return TypeVarRef(s, ctx)
706 return SortRef(s, ctx)
707
708
709def _sort(ctx : Context, a : Any) -> SortRef:
710 return _to_sort_ref(Z3_get_sort(ctx.ref(), a), ctx)
711
712
713def DeclareSort(name, ctx= None) -> SortRef:
714 """Create a new uninterpreted sort named `name`.
715
716 If `ctx=None`, then the new sort is declared in the global Z3Py context.
717
718 >>> A = DeclareSort('A')
719 >>> a = Const('a', A)
720 >>> b = Const('b', A)
721 >>> a.sort() == A
722 True
723 >>> b.sort() == A
724 True
725 >>> a == b
726 a == b
727 """
728 ctx = _get_ctx(ctx)
729 return SortRef(Z3_mk_uninterpreted_sort(ctx.ref(), to_symbol(name, ctx)), ctx)
730
732 """Type variable reference"""
733
734 def subsort(self, other):
735 return True
736
737 def cast(self, val):
738 return val
739
740
741def DeclareTypeVar(name, ctx=None):
742 """Create a new type variable named `name`.
743
744 If `ctx=None`, then the new sort is declared in the global Z3Py context.
745
746 """
747 ctx = _get_ctx(ctx)
748 return TypeVarRef(Z3_mk_type_variable(ctx.ref(), to_symbol(name, ctx)), ctx)
749
750
751
756
757
759 """Function declaration. Every constant and function have an associated declaration.
760
761 The declaration assigns a name, a sort (i.e., type), and for function
762 the sort (i.e., type) of each of its arguments. Note that, in Z3,
763 a constant is a function with 0 arguments.
764 """
765
766 def as_ast(self):
767 return Z3_func_decl_to_ast(self.ctx_ref(), self.ast)
768
769 def get_id(self):
770 return Z3_get_ast_id(self.ctx_ref(), self.as_ast())
771
772 def as_func_decl(self):
773 return self.ast
774
775 def name(self):
776 """Return the name of the function declaration `self`.
777
778 >>> f = Function('f', IntSort(), IntSort())
779 >>> f.name()
780 'f'
781 >>> isinstance(f.name(), str)
782 True
783 """
784 return _symbol2py(self.ctx, Z3_get_decl_name(self.ctx_ref(), self.ast))
785
786 def arity(self):
787 """Return the number of arguments of a function declaration.
788 If `self` is a constant, then `self.arity()` is 0.
789
790 >>> f = Function('f', IntSort(), RealSort(), BoolSort())
791 >>> f.arity()
792 2
793 """
794 return int(Z3_get_arity(self.ctx_ref(), self.ast))
795
796 def domain(self, i):
797 """Return the sort of the argument `i` of a function declaration.
798 This method assumes that `0 <= i < self.arity()`.
799
800 >>> f = Function('f', IntSort(), RealSort(), BoolSort())
801 >>> f.domain(0)
802 Int
803 >>> f.domain(1)
804 Real
805 """
806 return _to_sort_ref(Z3_get_domain(self.ctx_ref(), self.ast, i), self.ctx)
807
808 def range(self):
809 """Return the sort of the range of a function declaration.
810 For constants, this is the sort of the constant.
811
812 >>> f = Function('f', IntSort(), RealSort(), BoolSort())
813 >>> f.range()
814 Bool
815 """
816 return _to_sort_ref(Z3_get_range(self.ctx_ref(), self.ast), self.ctx)
817
818 def kind(self):
819 """Return the internal kind of a function declaration.
820 It can be used to identify Z3 built-in functions such as addition, multiplication, etc.
821
822 >>> x = Int('x')
823 >>> d = (x + 1).decl()
824 >>> d.kind() == Z3_OP_ADD
825 True
826 >>> d.kind() == Z3_OP_MUL
827 False
828 """
829 return Z3_get_decl_kind(self.ctx_ref(), self.ast)
830
831 def params(self):
832 ctx = self.ctx
833 n = Z3_get_decl_num_parameters(self.ctx_ref(), self.ast)
834 result = [None for i in range(n)]
835 for i in range(n):
836 k = Z3_get_decl_parameter_kind(self.ctx_ref(), self.ast, i)
837 if k == Z3_PARAMETER_INT:
838 result[i] = Z3_get_decl_int_parameter(self.ctx_ref(), self.ast, i)
839 elif k == Z3_PARAMETER_DOUBLE:
840 result[i] = Z3_get_decl_double_parameter(self.ctx_ref(), self.ast, i)
841 elif k == Z3_PARAMETER_RATIONAL:
842 result[i] = Z3_get_decl_rational_parameter(self.ctx_ref(), self.ast, i)
843 elif k == Z3_PARAMETER_SYMBOL:
844 result[i] = Z3_get_decl_symbol_parameter(self.ctx_ref(), self.ast, i)
845 elif k == Z3_PARAMETER_SORT:
846 result[i] = SortRef(Z3_get_decl_sort_parameter(self.ctx_ref(), self.ast, i), ctx)
847 elif k == Z3_PARAMETER_AST:
848 result[i] = ExprRef(Z3_get_decl_ast_parameter(self.ctx_ref(), self.ast, i), ctx)
849 elif k == Z3_PARAMETER_FUNC_DECL:
850 result[i] = FuncDeclRef(Z3_get_decl_func_decl_parameter(self.ctx_ref(), self.ast, i), ctx)
851 elif k == Z3_PARAMETER_INTERNAL:
852 result[i] = "internal parameter"
853 elif k == Z3_PARAMETER_ZSTRING:
854 result[i] = "internal string"
855 else:
856 assert(False)
857 return result
858
859 def __call__(self, *args):
860 """Create a Z3 application expression using the function `self`, and the given arguments.
861
862 The arguments must be Z3 expressions. This method assumes that
863 the sorts of the elements in `args` match the sorts of the
864 domain. Limited coercion is supported. For example, if
865 args[0] is a Python integer, and the function expects a Z3
866 integer, then the argument is automatically converted into a
867 Z3 integer.
868
869 >>> f = Function('f', IntSort(), RealSort(), BoolSort())
870 >>> x = Int('x')
871 >>> y = Real('y')
872 >>> f(x, y)
873 f(x, y)
874 >>> f(x, x)
875 f(x, ToReal(x))
876 """
877 args = _get_args(args)
878 num = len(args)
879 _args = (Ast * num)()
880 saved = []
881 for i in range(num):
882 # self.domain(i).cast(args[i]) may create a new Z3 expression,
883 # then we must save in 'saved' to prevent it from being garbage collected.
884 tmp = self.domain(i).cast(args[i])
885 saved.append(tmp)
886 _args[i] = tmp.as_ast()
887 return _to_expr_ref(Z3_mk_app(self.ctx_ref(), self.ast, len(args), _args), self.ctx)
888
889
891 """Return `True` if `a` is a Z3 function declaration.
892
893 >>> f = Function('f', IntSort(), IntSort())
894 >>> is_func_decl(f)
895 True
896 >>> x = Real('x')
897 >>> is_func_decl(x)
898 False
899 """
900 return isinstance(a, FuncDeclRef)
901
902
903def Function(name, *sig):
904 """Create a new Z3 uninterpreted function with the given sorts.
905
906 >>> f = Function('f', IntSort(), IntSort())
907 >>> f(f(0))
908 f(f(0))
909 """
910 sig = _get_args(sig)
911 if z3_debug():
912 _z3_assert(len(sig) > 0, "At least two arguments expected")
913 arity = len(sig) - 1
914 rng = sig[arity]
915 if z3_debug():
916 _z3_assert(is_sort(rng), "Z3 sort expected")
917 dom = (Sort * arity)()
918 for i in range(arity):
919 if z3_debug():
920 _z3_assert(is_sort(sig[i]), "Z3 sort expected")
921 dom[i] = sig[i].ast
922 ctx = rng.ctx
923 return FuncDeclRef(Z3_mk_func_decl(ctx.ref(), to_symbol(name, ctx), arity, dom, rng.ast), ctx)
924
925
927 """Create a new fresh Z3 uninterpreted function with the given sorts.
928 """
929 sig = _get_args(sig)
930 if z3_debug():
931 _z3_assert(len(sig) > 0, "At least two arguments expected")
932 arity = len(sig) - 1
933 rng = sig[arity]
934 if z3_debug():
935 _z3_assert(is_sort(rng), "Z3 sort expected")
936 dom = (z3.Sort * arity)()
937 for i in range(arity):
938 if z3_debug():
939 _z3_assert(is_sort(sig[i]), "Z3 sort expected")
940 dom[i] = sig[i].ast
941 ctx = rng.ctx
942 return FuncDeclRef(Z3_mk_fresh_func_decl(ctx.ref(), "f", arity, dom, rng.ast), ctx)
943
944
946 return FuncDeclRef(a, ctx)
947
948
949def RecFunction(name, *sig):
950 """Create a new Z3 recursive with the given sorts."""
951 sig = _get_args(sig)
952 if z3_debug():
953 _z3_assert(len(sig) > 0, "At least two arguments expected")
954 arity = len(sig) - 1
955 rng = sig[arity]
956 if z3_debug():
957 _z3_assert(is_sort(rng), "Z3 sort expected")
958 dom = (Sort * arity)()
959 for i in range(arity):
960 if z3_debug():
961 _z3_assert(is_sort(sig[i]), "Z3 sort expected")
962 dom[i] = sig[i].ast
963 ctx = rng.ctx
964 return FuncDeclRef(Z3_mk_rec_func_decl(ctx.ref(), to_symbol(name, ctx), arity, dom, rng.ast), ctx)
965
966
967def RecAddDefinition(f, args, body):
968 """Set the body of a recursive function.
969 Recursive definitions can be simplified if they are applied to ground
970 arguments.
971 >>> ctx = Context()
972 >>> fac = RecFunction('fac', IntSort(ctx), IntSort(ctx))
973 >>> n = Int('n', ctx)
974 >>> RecAddDefinition(fac, n, If(n == 0, 1, n*fac(n-1)))
975 >>> simplify(fac(5))
976 120
977 >>> s = Solver(ctx=ctx)
978 >>> s.add(fac(n) < 3)
979 >>> s.check()
980 sat
981 >>> s.model().eval(fac(5))
982 120
983 """
984 if is_app(args):
985 args = [args]
986 ctx = body.ctx
987 args = _get_args(args)
988 n = len(args)
989 _args = (Ast * n)()
990 for i in range(n):
991 _args[i] = args[i].ast
992 Z3_add_rec_def(ctx.ref(), f.ast, n, _args, body.ast)
993
994
999
1000
1002 """Constraints, formulas and terms are expressions in Z3.
1003
1004 Expressions are ASTs. Every expression has a sort.
1005 There are three main kinds of expressions:
1006 function applications, quantifiers and bounded variables.
1007 A constant is a function application with 0 arguments.
1008 For quantifier free problems, all expressions are
1009 function applications.
1010 """
1011
1012 def as_ast(self):
1013 return self.ast
1014
1015 def get_id(self):
1016 return Z3_get_ast_id(self.ctx_ref(), self.as_ast())
1017
1018 def sort(self):
1019 """Return the sort of expression `self`.
1020
1021 >>> x = Int('x')
1022 >>> (x + 1).sort()
1023 Int
1024 >>> y = Real('y')
1025 >>> (x + y).sort()
1026 Real
1027 """
1028 return _sort(self.ctx, self.as_ast())
1029
1030 def sort_kind(self):
1031 """Shorthand for `self.sort().kind()`.
1032
1033 >>> a = Array('a', IntSort(), IntSort())
1034 >>> a.sort_kind() == Z3_ARRAY_SORT
1035 True
1036 >>> a.sort_kind() == Z3_INT_SORT
1037 False
1038 """
1039 return self.sort().kind()
1040
1041 def __eq__(self, other):
1042 """Return a Z3 expression that represents the constraint `self == other`.
1043
1044 If `other` is `None`, then this method simply returns `False`.
1045
1046 >>> a = Int('a')
1047 >>> b = Int('b')
1048 >>> a == b
1049 a == b
1050 >>> a is None
1051 False
1052 """
1053 if other is None:
1054 return False
1055 a, b = _coerce_exprs(self, other)
1056 return BoolRef(Z3_mk_eq(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
1057
1058 def __hash__(self):
1059 """ Hash code. """
1060 return AstRef.__hash__(self)
1061
1062 def __ne__(self, other):
1063 """Return a Z3 expression that represents the constraint `self != other`.
1064
1065 If `other` is `None`, then this method simply returns `True`.
1066
1067 >>> a = Int('a')
1068 >>> b = Int('b')
1069 >>> a != b
1070 a != b
1071 >>> a is not None
1072 True
1073 """
1074 if other is None:
1075 return True
1076 a, b = _coerce_exprs(self, other)
1077 _args, sz = _to_ast_array((a, b))
1078 return BoolRef(Z3_mk_distinct(self.ctx_ref(), 2, _args), self.ctx)
1079
1080 def params(self):
1081 return self.decl().params()
1082
1083 def decl(self):
1084 """Return the Z3 function declaration associated with a Z3 application.
1085
1086 >>> f = Function('f', IntSort(), IntSort())
1087 >>> a = Int('a')
1088 >>> t = f(a)
1089 >>> eq(t.decl(), f)
1090 True
1091 >>> (a + 1).decl()
1092 +
1093 """
1094 if z3_debug():
1095 _z3_assert(is_app(self), "Z3 application expected")
1096 return FuncDeclRef(Z3_get_app_decl(self.ctx_ref(), self.as_ast()), self.ctx)
1097
1098 def kind(self):
1099 """Return the Z3 internal kind of a function application."""
1100 if z3_debug():
1101 _z3_assert(is_app(self), "Z3 application expected")
1103
1104
1105 def num_args(self):
1106 """Return the number of arguments of a Z3 application.
1107
1108 >>> a = Int('a')
1109 >>> b = Int('b')
1110 >>> (a + b).num_args()
1111 2
1112 >>> f = Function('f', IntSort(), IntSort(), IntSort(), IntSort())
1113 >>> t = f(a, b, 0)
1114 >>> t.num_args()
1115 3
1116 """
1117 if z3_debug():
1118 _z3_assert(is_app(self), "Z3 application expected")
1119 return int(Z3_get_app_num_args(self.ctx_ref(), self.as_ast()))
1120
1121 def arg(self, idx):
1122 """Return argument `idx` of the application `self`.
1123
1124 This method assumes that `self` is a function application with at least `idx+1` arguments.
1125
1126 >>> a = Int('a')
1127 >>> b = Int('b')
1128 >>> f = Function('f', IntSort(), IntSort(), IntSort(), IntSort())
1129 >>> t = f(a, b, 0)
1130 >>> t.arg(0)
1131 a
1132 >>> t.arg(1)
1133 b
1134 >>> t.arg(2)
1135 0
1136 """
1137 if z3_debug():
1138 _z3_assert(is_app(self), "Z3 application expected")
1139 _z3_assert(idx < self.num_args(), "Invalid argument index")
1140 return _to_expr_ref(Z3_get_app_arg(self.ctx_ref(), self.as_ast(), idx), self.ctx)
1141
1142 def children(self):
1143 """Return a list containing the children of the given expression
1144
1145 >>> a = Int('a')
1146 >>> b = Int('b')
1147 >>> f = Function('f', IntSort(), IntSort(), IntSort(), IntSort())
1148 >>> t = f(a, b, 0)
1149 >>> t.children()
1150 [a, b, 0]
1151 """
1152 if is_app(self):
1153 return [self.arg(i) for i in range(self.num_args())]
1154 else:
1155 return []
1156
1157 def from_string(self, s):
1158 pass
1159
1160 def serialize(self):
1161 s = Solver()
1162 f = Function('F', self.sort(), BoolSort(self.ctx))
1163 s.add(f(self))
1164 return s.sexpr()
1165
1167 """inverse function to the serialize method on ExprRef.
1168 It is made available to make it easier for users to serialize expressions back and forth between
1169 strings. Solvers can be serialized using the 'sexpr()' method.
1170 """
1171 s = Solver()
1172 s.from_string(st)
1173 if len(s.assertions()) != 1:
1174 raise Z3Exception("single assertion expected")
1175 fml = s.assertions()[0]
1176 if fml.num_args() != 1:
1177 raise Z3Exception("dummy function 'F' expected")
1178 return fml.arg(0)
1179
1180def _to_expr_ref(a, ctx):
1181 if isinstance(a, Pattern):
1182 return PatternRef(a, ctx)
1183 ctx_ref = ctx.ref()
1184 k = Z3_get_ast_kind(ctx_ref, a)
1185 if k == Z3_QUANTIFIER_AST:
1186 return QuantifierRef(a, ctx)
1187 sk = Z3_get_sort_kind(ctx_ref, Z3_get_sort(ctx_ref, a))
1188 if sk == Z3_BOOL_SORT:
1189 return BoolRef(a, ctx)
1190 if sk == Z3_INT_SORT:
1191 if k == Z3_NUMERAL_AST:
1192 return IntNumRef(a, ctx)
1193 return ArithRef(a, ctx)
1194 if sk == Z3_REAL_SORT:
1195 if k == Z3_NUMERAL_AST:
1196 return RatNumRef(a, ctx)
1197 if _is_algebraic(ctx, a):
1198 return AlgebraicNumRef(a, ctx)
1199 return ArithRef(a, ctx)
1200 if sk == Z3_BV_SORT:
1201 if k == Z3_NUMERAL_AST:
1202 return BitVecNumRef(a, ctx)
1203 else:
1204 return BitVecRef(a, ctx)
1205 if sk == Z3_ARRAY_SORT:
1206 return ArrayRef(a, ctx)
1207 if sk == Z3_DATATYPE_SORT:
1208 return DatatypeRef(a, ctx)
1209 if sk == Z3_FLOATING_POINT_SORT:
1210 if k == Z3_APP_AST and _is_numeral(ctx, a):
1211 return FPNumRef(a, ctx)
1212 else:
1213 return FPRef(a, ctx)
1214 if sk == Z3_FINITE_DOMAIN_SORT:
1215 if k == Z3_NUMERAL_AST:
1216 return FiniteDomainNumRef(a, ctx)
1217 else:
1218 return FiniteDomainRef(a, ctx)
1219 if sk == Z3_ROUNDING_MODE_SORT:
1220 return FPRMRef(a, ctx)
1221 if sk == Z3_SEQ_SORT:
1222 return SeqRef(a, ctx)
1223 if sk == Z3_CHAR_SORT:
1224 return CharRef(a, ctx)
1225 if sk == Z3_RE_SORT:
1226 return ReRef(a, ctx)
1227 return ExprRef(a, ctx)
1228
1229
1231 if is_expr(a):
1232 s1 = a.sort()
1233 if s is None:
1234 return s1
1235 if s1.eq(s):
1236 return s
1237 elif s.subsort(s1):
1238 return s1
1239 elif s1.subsort(s):
1240 return s
1241 else:
1242 if z3_debug():
1243 _z3_assert(s1.ctx == s.ctx, "context mismatch")
1244 _z3_assert(False, "sort mismatch")
1245 else:
1246 return s
1247
1248
1249def _coerce_exprs(a, b, ctx=None):
1250 if not is_expr(a) and not is_expr(b):
1251 a = _py2expr(a, ctx)
1252 b = _py2expr(b, ctx)
1253 if isinstance(a, str) and isinstance(b, SeqRef):
1254 a = StringVal(a, b.ctx)
1255 if isinstance(b, str) and isinstance(a, SeqRef):
1256 b = StringVal(b, a.ctx)
1257 if isinstance(a, float) and isinstance(b, ArithRef):
1258 a = RealVal(a, b.ctx)
1259 if isinstance(b, float) and isinstance(a, ArithRef):
1260 b = RealVal(b, a.ctx)
1261
1262 s = None
1263 s = _coerce_expr_merge(s, a)
1264 s = _coerce_expr_merge(s, b)
1265 a = s.cast(a)
1266 b = s.cast(b)
1267 return (a, b)
1268
1269
1270def _reduce(func, sequence, initial):
1271 result = initial
1272 for element in sequence:
1273 result = func(result, element)
1274 return result
1275
1276
1277def _coerce_expr_list(alist, ctx=None):
1278 has_expr = False
1279 for a in alist:
1280 if is_expr(a):
1281 has_expr = True
1282 break
1283 if not has_expr:
1284 alist = [_py2expr(a, ctx) for a in alist]
1285 s = _reduce(_coerce_expr_merge, alist, None)
1286 return [s.cast(a) for a in alist]
1287
1288
1289def is_expr(a):
1290 """Return `True` if `a` is a Z3 expression.
1291
1292 >>> a = Int('a')
1293 >>> is_expr(a)
1294 True
1295 >>> is_expr(a + 1)
1296 True
1297 >>> is_expr(IntSort())
1298 False
1299 >>> is_expr(1)
1300 False
1301 >>> is_expr(IntVal(1))
1302 True
1303 >>> x = Int('x')
1304 >>> is_expr(ForAll(x, x >= 0))
1305 True
1306 >>> is_expr(FPVal(1.0))
1307 True
1308 """
1309 return isinstance(a, ExprRef)
1310
1311
1312def is_app(a):
1313 """Return `True` if `a` is a Z3 function application.
1314
1315 Note that, constants are function applications with 0 arguments.
1316
1317 >>> a = Int('a')
1318 >>> is_app(a)
1319 True
1320 >>> is_app(a + 1)
1321 True
1322 >>> is_app(IntSort())
1323 False
1324 >>> is_app(1)
1325 False
1326 >>> is_app(IntVal(1))
1327 True
1328 >>> x = Int('x')
1329 >>> is_app(ForAll(x, x >= 0))
1330 False
1331 """
1332 if not isinstance(a, ExprRef):
1333 return False
1334 k = _ast_kind(a.ctx, a)
1335 return k == Z3_NUMERAL_AST or k == Z3_APP_AST
1336
1337
1339 """Return `True` if `a` is Z3 constant/variable expression.
1340
1341 >>> a = Int('a')
1342 >>> is_const(a)
1343 True
1344 >>> is_const(a + 1)
1345 False
1346 >>> is_const(1)
1347 False
1348 >>> is_const(IntVal(1))
1349 True
1350 >>> x = Int('x')
1351 >>> is_const(ForAll(x, x >= 0))
1352 False
1353 """
1354 return is_app(a) and a.num_args() == 0
1355
1356
1357def is_var(a):
1358 """Return `True` if `a` is variable.
1359
1360 Z3 uses de-Bruijn indices for representing bound variables in
1361 quantifiers.
1362
1363 >>> x = Int('x')
1364 >>> is_var(x)
1365 False
1366 >>> is_const(x)
1367 True
1368 >>> f = Function('f', IntSort(), IntSort())
1369 >>> # Z3 replaces x with bound variables when ForAll is executed.
1370 >>> q = ForAll(x, f(x) == x)
1371 >>> b = q.body()
1372 >>> b
1373 f(Var(0)) == Var(0)
1374 >>> b.arg(1)
1375 Var(0)
1376 >>> is_var(b.arg(1))
1377 True
1378 """
1379 return is_expr(a) and _ast_kind(a.ctx, a) == Z3_VAR_AST
1380
1381
1383 """Return the de-Bruijn index of the Z3 bounded variable `a`.
1384
1385 >>> x = Int('x')
1386 >>> y = Int('y')
1387 >>> is_var(x)
1388 False
1389 >>> is_const(x)
1390 True
1391 >>> f = Function('f', IntSort(), IntSort(), IntSort())
1392 >>> # Z3 replaces x and y with bound variables when ForAll is executed.
1393 >>> q = ForAll([x, y], f(x, y) == x + y)
1394 >>> q.body()
1395 f(Var(1), Var(0)) == Var(1) + Var(0)
1396 >>> b = q.body()
1397 >>> b.arg(0)
1398 f(Var(1), Var(0))
1399 >>> v1 = b.arg(0).arg(0)
1400 >>> v2 = b.arg(0).arg(1)
1401 >>> v1
1402 Var(1)
1403 >>> v2
1404 Var(0)
1405 >>> get_var_index(v1)
1406 1
1407 >>> get_var_index(v2)
1408 0
1409 """
1410 if z3_debug():
1411 _z3_assert(is_var(a), "Z3 bound variable expected")
1412 return int(Z3_get_index_value(a.ctx.ref(), a.as_ast()))
1413
1414
1415def is_app_of(a, k):
1416 """Return `True` if `a` is an application of the given kind `k`.
1417
1418 >>> x = Int('x')
1419 >>> n = x + 1
1420 >>> is_app_of(n, Z3_OP_ADD)
1421 True
1422 >>> is_app_of(n, Z3_OP_MUL)
1423 False
1424 """
1425 return is_app(a) and a.kind() == k
1426
1427
1428def If(a, b, c, ctx=None):
1429 """Create a Z3 if-then-else expression.
1430
1431 >>> x = Int('x')
1432 >>> y = Int('y')
1433 >>> max = If(x > y, x, y)
1434 >>> max
1435 If(x > y, x, y)
1436 >>> simplify(max)
1437 If(x <= y, y, x)
1438 """
1439 if isinstance(a, Probe) or isinstance(b, Tactic) or isinstance(c, Tactic):
1440 return Cond(a, b, c, ctx)
1441 else:
1442 ctx = _get_ctx(_ctx_from_ast_arg_list([a, b, c], ctx))
1443 s = BoolSort(ctx)
1444 a = s.cast(a)
1445 b, c = _coerce_exprs(b, c, ctx)
1446 if z3_debug():
1447 _z3_assert(a.ctx == b.ctx, "Context mismatch")
1448 return _to_expr_ref(Z3_mk_ite(ctx.ref(), a.as_ast(), b.as_ast(), c.as_ast()), ctx)
1449
1450
1451def Distinct(*args):
1452 """Create a Z3 distinct expression.
1453
1454 >>> x = Int('x')
1455 >>> y = Int('y')
1456 >>> Distinct(x, y)
1457 x != y
1458 >>> z = Int('z')
1459 >>> Distinct(x, y, z)
1460 Distinct(x, y, z)
1461 >>> simplify(Distinct(x, y, z))
1462 Distinct(x, y, z)
1463 >>> simplify(Distinct(x, y, z), blast_distinct=True)
1464 And(Not(x == y), Not(x == z), Not(y == z))
1465 """
1466 args = _get_args(args)
1467 ctx = _ctx_from_ast_arg_list(args)
1468 if z3_debug():
1469 _z3_assert(ctx is not None, "At least one of the arguments must be a Z3 expression")
1470 args = _coerce_expr_list(args, ctx)
1471 _args, sz = _to_ast_array(args)
1472 return BoolRef(Z3_mk_distinct(ctx.ref(), sz, _args), ctx)
1473
1474
1475def _mk_bin(f, a, b):
1476 args = (Ast * 2)()
1477 if z3_debug():
1478 _z3_assert(a.ctx == b.ctx, "Context mismatch")
1479 args[0] = a.as_ast()
1480 args[1] = b.as_ast()
1481 return f(a.ctx.ref(), 2, args)
1482
1483
1484def Const(name, sort):
1485 """Create a constant of the given sort.
1486
1487 >>> Const('x', IntSort())
1488 x
1489 """
1490 if z3_debug():
1491 _z3_assert(isinstance(sort, SortRef), "Z3 sort expected")
1492 ctx = sort.ctx
1493 return _to_expr_ref(Z3_mk_const(ctx.ref(), to_symbol(name, ctx), sort.ast), ctx)
1494
1495
1496def Consts(names, sort):
1497 """Create several constants of the given sort.
1498
1499 `names` is a string containing the names of all constants to be created.
1500 Blank spaces separate the names of different constants.
1501
1502 >>> x, y, z = Consts('x y z', IntSort())
1503 >>> x + y + z
1504 x + y + z
1505 """
1506 if isinstance(names, str):
1507 names = names.split(" ")
1508 return [Const(name, sort) for name in names]
1509
1510
1511def FreshConst(sort, prefix="c"):
1512 """Create a fresh constant of a specified sort"""
1513 if z3_debug():
1514 _z3_assert(is_sort(sort), f"Z3 sort expected, got {type(sort)}")
1515 ctx = _get_ctx(sort.ctx)
1516 return _to_expr_ref(Z3_mk_fresh_const(ctx.ref(), prefix, sort.ast), ctx)
1517
1518
1519def Var(idx : int, s : SortRef) -> ExprRef:
1520 """Create a Z3 free variable. Free variables are used to create quantified formulas.
1521 A free variable with index n is bound when it occurs within the scope of n+1 quantified
1522 declarations.
1523
1524 >>> Var(0, IntSort())
1525 Var(0)
1526 >>> eq(Var(0, IntSort()), Var(0, BoolSort()))
1527 False
1528 """
1529 if z3_debug():
1530 _z3_assert(is_sort(s), "Z3 sort expected")
1531 return _to_expr_ref(Z3_mk_bound(s.ctx_ref(), idx, s.ast), s.ctx)
1532
1533
1534def RealVar(idx: int, ctx=None) -> ExprRef:
1535 """
1536 Create a real free variable. Free variables are used to create quantified formulas.
1537 They are also used to create polynomials.
1538
1539 >>> RealVar(0)
1540 Var(0)
1541 """
1542 return Var(idx, RealSort(ctx))
1543
1544def RealVarVector(n: int, ctx= None):
1545 """
1546 Create a list of Real free variables.
1547 The variables have ids: 0, 1, ..., n-1
1548
1549 >>> x0, x1, x2, x3 = RealVarVector(4)
1550 >>> x2
1551 Var(2)
1552 """
1553 return [RealVar(i, ctx) for i in range(n)]
1554
1555
1560
1561
1563 """Boolean sort."""
1564
1565 def cast(self, val):
1566 """Try to cast `val` as a Boolean.
1567
1568 >>> x = BoolSort().cast(True)
1569 >>> x
1570 True
1571 >>> is_expr(x)
1572 True
1573 >>> is_expr(True)
1574 False
1575 >>> x.sort()
1576 Bool
1577 """
1578 if isinstance(val, bool):
1579 return BoolVal(val, self.ctx)
1580 if z3_debug():
1581 if not is_expr(val):
1582 msg = "True, False or Z3 Boolean expression expected. Received %s of type %s"
1583 _z3_assert(is_expr(val), msg % (val, type(val)))
1584 if not self.eq(val.sort()):
1585 _z3_assert(self.eq(val.sort()), "Value cannot be converted into a Z3 Boolean value")
1586 return val
1587
1588 def subsort(self, other):
1589 return isinstance(other, ArithSortRef)
1590
1591 def is_int(self):
1592 return True
1593
1594 def is_bool(self):
1595 return True
1596
1597
1599 """All Boolean expressions are instances of this class."""
1600
1601 def sort(self):
1602 return BoolSortRef(Z3_get_sort(self.ctx_ref(), self.as_ast()), self.ctx)
1603
1604 def __add__(self, other):
1605 if isinstance(other, BoolRef):
1606 other = If(other, 1, 0)
1607 return If(self, 1, 0) + other
1608
1609 def __radd__(self, other):
1610 return self + other
1611
1612 def __rmul__(self, other):
1613 return self * other
1614
1615 def __mul__(self, other):
1616 """Create the Z3 expression `self * other`.
1617 """
1618 if isinstance(other, int) and other == 1:
1619 return If(self, 1, 0)
1620 if isinstance(other, int) and other == 0:
1621 return IntVal(0, self.ctx)
1622 if isinstance(other, BoolRef):
1623 other = If(other, 1, 0)
1624 return If(self, other, 0)
1625
1626 def __and__(self, other):
1627 return And(self, other)
1628
1629 def __or__(self, other):
1630 return Or(self, other)
1631
1632 def __xor__(self, other):
1633 return Xor(self, other)
1634
1635 def __invert__(self):
1636 return Not(self)
1637
1638 def py_value(self):
1639 if is_true(self):
1640 return True
1641 if is_false(self):
1642 return False
1643 return None
1644
1645
1646
1647
1648def is_bool(a : Any) -> bool:
1649 """Return `True` if `a` is a Z3 Boolean expression.
1650
1651 >>> p = Bool('p')
1652 >>> is_bool(p)
1653 True
1654 >>> q = Bool('q')
1655 >>> is_bool(And(p, q))
1656 True
1657 >>> x = Real('x')
1658 >>> is_bool(x)
1659 False
1660 >>> is_bool(x == 0)
1661 True
1662 """
1663 return isinstance(a, BoolRef)
1664
1665
1666def is_true(a : Any) -> bool:
1667 """Return `True` if `a` is the Z3 true expression.
1668
1669 >>> p = Bool('p')
1670 >>> is_true(p)
1671 False
1672 >>> is_true(simplify(p == p))
1673 True
1674 >>> x = Real('x')
1675 >>> is_true(x == 0)
1676 False
1677 >>> # True is a Python Boolean expression
1678 >>> is_true(True)
1679 False
1680 """
1681 return is_app_of(a, Z3_OP_TRUE)
1682
1683
1684def is_false(a : Any) -> bool:
1685 """Return `True` if `a` is the Z3 false expression.
1686
1687 >>> p = Bool('p')
1688 >>> is_false(p)
1689 False
1690 >>> is_false(False)
1691 False
1692 >>> is_false(BoolVal(False))
1693 True
1694 """
1695 return is_app_of(a, Z3_OP_FALSE)
1696
1697
1698def is_and(a : Any) -> bool:
1699 """Return `True` if `a` is a Z3 and expression.
1700
1701 >>> p, q = Bools('p q')
1702 >>> is_and(And(p, q))
1703 True
1704 >>> is_and(Or(p, q))
1705 False
1706 """
1707 return is_app_of(a, Z3_OP_AND)
1708
1709
1710def is_or(a : Any) -> bool:
1711 """Return `True` if `a` is a Z3 or expression.
1712
1713 >>> p, q = Bools('p q')
1714 >>> is_or(Or(p, q))
1715 True
1716 >>> is_or(And(p, q))
1717 False
1718 """
1719 return is_app_of(a, Z3_OP_OR)
1720
1721
1722def is_implies(a : Any) -> bool:
1723 """Return `True` if `a` is a Z3 implication expression.
1724
1725 >>> p, q = Bools('p q')
1726 >>> is_implies(Implies(p, q))
1727 True
1728 >>> is_implies(And(p, q))
1729 False
1730 """
1731 return is_app_of(a, Z3_OP_IMPLIES)
1732
1733
1734def is_not(a : Any) -> bool:
1735 """Return `True` if `a` is a Z3 not expression.
1736
1737 >>> p = Bool('p')
1738 >>> is_not(p)
1739 False
1740 >>> is_not(Not(p))
1741 True
1742 """
1743 return is_app_of(a, Z3_OP_NOT)
1744
1745
1746def is_eq(a : Any) -> bool:
1747 """Return `True` if `a` is a Z3 equality expression.
1748
1749 >>> x, y = Ints('x y')
1750 >>> is_eq(x == y)
1751 True
1752 """
1753 return is_app_of(a, Z3_OP_EQ)
1754
1755
1756def is_distinct(a : Any) -> bool:
1757 """Return `True` if `a` is a Z3 distinct expression.
1758
1759 >>> x, y, z = Ints('x y z')
1760 >>> is_distinct(x == y)
1761 False
1762 >>> is_distinct(Distinct(x, y, z))
1763 True
1764 """
1765 return is_app_of(a, Z3_OP_DISTINCT)
1766
1767
1768def BoolSort(ctx=None):
1769 """Return the Boolean Z3 sort. If `ctx=None`, then the global context is used.
1770
1771 >>> BoolSort()
1772 Bool
1773 >>> p = Const('p', BoolSort())
1774 >>> is_bool(p)
1775 True
1776 >>> r = Function('r', IntSort(), IntSort(), BoolSort())
1777 >>> r(0, 1)
1778 r(0, 1)
1779 >>> is_bool(r(0, 1))
1780 True
1781 """
1782 ctx = _get_ctx(ctx)
1783 return BoolSortRef(Z3_mk_bool_sort(ctx.ref()), ctx)
1784
1785
1786def BoolVal(val, ctx=None):
1787 """Return the Boolean value `True` or `False`. If `ctx=None`, then the global context is used.
1788
1789 >>> BoolVal(True)
1790 True
1791 >>> is_true(BoolVal(True))
1792 True
1793 >>> is_true(True)
1794 False
1795 >>> is_false(BoolVal(False))
1796 True
1797 """
1798 ctx = _get_ctx(ctx)
1799 if val:
1800 return BoolRef(Z3_mk_true(ctx.ref()), ctx)
1801 else:
1802 return BoolRef(Z3_mk_false(ctx.ref()), ctx)
1803
1804
1805def Bool(name, ctx=None):
1806 """Return a Boolean constant named `name`. If `ctx=None`, then the global context is used.
1807
1808 >>> p = Bool('p')
1809 >>> q = Bool('q')
1810 >>> And(p, q)
1811 And(p, q)
1812 """
1813 ctx = _get_ctx(ctx)
1814 return BoolRef(Z3_mk_const(ctx.ref(), to_symbol(name, ctx), BoolSort(ctx).ast), ctx)
1815
1816
1817def Bools(names, ctx=None):
1818 """Return a tuple of Boolean constants.
1819
1820 `names` is a single string containing all names separated by blank spaces.
1821 If `ctx=None`, then the global context is used.
1822
1823 >>> p, q, r = Bools('p q r')
1824 >>> And(p, Or(q, r))
1825 And(p, Or(q, r))
1826 """
1827 ctx = _get_ctx(ctx)
1828 if isinstance(names, str):
1829 names = names.split(" ")
1830 return [Bool(name, ctx) for name in names]
1831
1832
1833def BoolVector(prefix, sz, ctx=None):
1834 """Return a list of Boolean constants of size `sz`.
1835
1836 The constants are named using the given prefix.
1837 If `ctx=None`, then the global context is used.
1838
1839 >>> P = BoolVector('p', 3)
1840 >>> P
1841 [p__0, p__1, p__2]
1842 >>> And(P)
1843 And(p__0, p__1, p__2)
1844 """
1845 return [Bool("%s__%s" % (prefix, i)) for i in range(sz)]
1846
1847
1848def FreshBool(prefix="b", ctx=None):
1849 """Return a fresh Boolean constant in the given context using the given prefix.
1850
1851 If `ctx=None`, then the global context is used.
1852
1853 >>> b1 = FreshBool()
1854 >>> b2 = FreshBool()
1855 >>> eq(b1, b2)
1856 False
1857 """
1858 ctx = _get_ctx(ctx)
1859 return BoolRef(Z3_mk_fresh_const(ctx.ref(), prefix, BoolSort(ctx).ast), ctx)
1860
1861
1862def Implies(a, b, ctx=None):
1863 """Create a Z3 implies expression.
1864
1865 >>> p, q = Bools('p q')
1866 >>> Implies(p, q)
1867 Implies(p, q)
1868 """
1869 ctx = _get_ctx(_ctx_from_ast_arg_list([a, b], ctx))
1870 s = BoolSort(ctx)
1871 a = s.cast(a)
1872 b = s.cast(b)
1873 return BoolRef(Z3_mk_implies(ctx.ref(), a.as_ast(), b.as_ast()), ctx)
1874
1875
1876def Xor(a, b, ctx=None):
1877 """Create a Z3 Xor expression.
1878
1879 >>> p, q = Bools('p q')
1880 >>> Xor(p, q)
1881 Xor(p, q)
1882 >>> simplify(Xor(p, q))
1883 Not(p == q)
1884 """
1885 ctx = _get_ctx(_ctx_from_ast_arg_list([a, b], ctx))
1886 s = BoolSort(ctx)
1887 a = s.cast(a)
1888 b = s.cast(b)
1889 return BoolRef(Z3_mk_xor(ctx.ref(), a.as_ast(), b.as_ast()), ctx)
1890
1891
1892def Not(a, ctx=None):
1893 """Create a Z3 not expression or probe.
1894
1895 >>> p = Bool('p')
1896 >>> Not(Not(p))
1897 Not(Not(p))
1898 >>> simplify(Not(Not(p)))
1899 p
1900 """
1901 ctx = _get_ctx(_ctx_from_ast_arg_list([a], ctx))
1902 if is_probe(a):
1903 # Not is also used to build probes
1904 return Probe(Z3_probe_not(ctx.ref(), a.probe), ctx)
1905 else:
1906 s = BoolSort(ctx)
1907 a = s.cast(a)
1908 return BoolRef(Z3_mk_not(ctx.ref(), a.as_ast()), ctx)
1909
1910
1911def mk_not(a):
1912 if is_not(a):
1913 return a.arg(0)
1914 else:
1915 return Not(a)
1916
1917
1918def _has_probe(args):
1919 """Return `True` if one of the elements of the given collection is a Z3 probe."""
1920 for arg in args:
1921 if is_probe(arg):
1922 return True
1923 return False
1924
1925
1926def And(*args):
1927 """Create a Z3 and-expression or and-probe.
1928
1929 >>> p, q, r = Bools('p q r')
1930 >>> And(p, q, r)
1931 And(p, q, r)
1932 >>> P = BoolVector('p', 5)
1933 >>> And(P)
1934 And(p__0, p__1, p__2, p__3, p__4)
1935 """
1936 last_arg = None
1937 if len(args) > 0:
1938 last_arg = args[len(args) - 1]
1939 if isinstance(last_arg, Context):
1940 ctx = args[len(args) - 1]
1941 args = args[:len(args) - 1]
1942 elif len(args) == 1 and isinstance(args[0], AstVector):
1943 ctx = args[0].ctx
1944 args = [a for a in args[0]]
1945 else:
1946 ctx = None
1947 args = _get_args(args)
1948 ctx = _get_ctx(_ctx_from_ast_arg_list(args, ctx))
1949 if z3_debug():
1950 _z3_assert(ctx is not None, "At least one of the arguments must be a Z3 expression or probe")
1951 if _has_probe(args):
1952 return _probe_and(args, ctx)
1953 else:
1954 args = _coerce_expr_list(args, ctx)
1955 _args, sz = _to_ast_array(args)
1956 return BoolRef(Z3_mk_and(ctx.ref(), sz, _args), ctx)
1957
1958
1959def Or(*args):
1960 """Create a Z3 or-expression or or-probe.
1961
1962 >>> p, q, r = Bools('p q r')
1963 >>> Or(p, q, r)
1964 Or(p, q, r)
1965 >>> P = BoolVector('p', 5)
1966 >>> Or(P)
1967 Or(p__0, p__1, p__2, p__3, p__4)
1968 """
1969 last_arg = None
1970 if len(args) > 0:
1971 last_arg = args[len(args) - 1]
1972 if isinstance(last_arg, Context):
1973 ctx = args[len(args) - 1]
1974 args = args[:len(args) - 1]
1975 elif len(args) == 1 and isinstance(args[0], AstVector):
1976 ctx = args[0].ctx
1977 args = [a for a in args[0]]
1978 else:
1979 ctx = None
1980 args = _get_args(args)
1981 ctx = _get_ctx(_ctx_from_ast_arg_list(args, ctx))
1982 if z3_debug():
1983 _z3_assert(ctx is not None, "At least one of the arguments must be a Z3 expression or probe")
1984 if _has_probe(args):
1985 return _probe_or(args, ctx)
1986 else:
1987 args = _coerce_expr_list(args, ctx)
1988 _args, sz = _to_ast_array(args)
1989 return BoolRef(Z3_mk_or(ctx.ref(), sz, _args), ctx)
1990
1991
1996
1997
1999 """Patterns are hints for quantifier instantiation.
2000
2001 """
2002
2003 def as_ast(self):
2004 return Z3_pattern_to_ast(self.ctx_ref(), self.ast)
2005
2006 def get_id(self):
2007 return Z3_get_ast_id(self.ctx_ref(), self.as_ast())
2008
2009
2011 """Return `True` if `a` is a Z3 pattern (hint for quantifier instantiation.
2012
2013 >>> f = Function('f', IntSort(), IntSort())
2014 >>> x = Int('x')
2015 >>> q = ForAll(x, f(x) == 0, patterns = [ f(x) ])
2016 >>> q
2017 ForAll(x, f(x) == 0)
2018 >>> q.num_patterns()
2019 1
2020 >>> is_pattern(q.pattern(0))
2021 True
2022 >>> q.pattern(0)
2023 f(Var(0))
2024 """
2025 return isinstance(a, PatternRef)
2026
2027
2028def MultiPattern(*args):
2029 """Create a Z3 multi-pattern using the given expressions `*args`
2030
2031 >>> f = Function('f', IntSort(), IntSort())
2032 >>> g = Function('g', IntSort(), IntSort())
2033 >>> x = Int('x')
2034 >>> q = ForAll(x, f(x) != g(x), patterns = [ MultiPattern(f(x), g(x)) ])
2035 >>> q
2036 ForAll(x, f(x) != g(x))
2037 >>> q.num_patterns()
2038 1
2039 >>> is_pattern(q.pattern(0))
2040 True
2041 >>> q.pattern(0)
2042 MultiPattern(f(Var(0)), g(Var(0)))
2043 """
2044 if z3_debug():
2045 _z3_assert(len(args) > 0, "At least one argument expected")
2046 _z3_assert(all([is_expr(a) for a in args]), "Z3 expressions expected")
2047 ctx = args[0].ctx
2048 args, sz = _to_ast_array(args)
2049 return PatternRef(Z3_mk_pattern(ctx.ref(), sz, args), ctx)
2050
2051
2053 if is_pattern(arg):
2054 return arg
2055 else:
2056 return MultiPattern(arg)
2057
2058
2063
2064
2066 """Universally and Existentially quantified formulas."""
2067
2068 def as_ast(self):
2069 return self.ast
2070
2071 def get_id(self):
2072 return Z3_get_ast_id(self.ctx_ref(), self.as_ast())
2073
2074 def sort(self):
2075 """Return the Boolean sort or sort of Lambda."""
2076 if self.is_lambda():
2077 return _sort(self.ctx, self.as_ast())
2078 return BoolSort(self.ctx)
2079
2080 def is_forall(self):
2081 """Return `True` if `self` is a universal quantifier.
2082
2083 >>> f = Function('f', IntSort(), IntSort())
2084 >>> x = Int('x')
2085 >>> q = ForAll(x, f(x) == 0)
2086 >>> q.is_forall()
2087 True
2088 >>> q = Exists(x, f(x) != 0)
2089 >>> q.is_forall()
2090 False
2091 """
2093
2094 def is_exists(self):
2095 """Return `True` if `self` is an existential quantifier.
2096
2097 >>> f = Function('f', IntSort(), IntSort())
2098 >>> x = Int('x')
2099 >>> q = ForAll(x, f(x) == 0)
2100 >>> q.is_exists()
2101 False
2102 >>> q = Exists(x, f(x) != 0)
2103 >>> q.is_exists()
2104 True
2105 """
2106 return Z3_is_quantifier_exists(self.ctx_ref(), self.ast)
2107
2108 def is_lambda(self):
2109 """Return `True` if `self` is a lambda expression.
2110
2111 >>> f = Function('f', IntSort(), IntSort())
2112 >>> x = Int('x')
2113 >>> q = Lambda(x, f(x))
2114 >>> q.is_lambda()
2115 True
2116 >>> q = Exists(x, f(x) != 0)
2117 >>> q.is_lambda()
2118 False
2119 """
2120 return Z3_is_lambda(self.ctx_ref(), self.ast)
2121
2122 def __getitem__(self, arg):
2123 """Return the Z3 expression `self[arg]`.
2124 """
2125 if z3_debug():
2126 _z3_assert(self.is_lambda(), "quantifier should be a lambda expression")
2127 return _array_select(self, arg)
2128
2129 def weight(self):
2130 """Return the weight annotation of `self`.
2131
2132 >>> f = Function('f', IntSort(), IntSort())
2133 >>> x = Int('x')
2134 >>> q = ForAll(x, f(x) == 0)
2135 >>> q.weight()
2136 1
2137 >>> q = ForAll(x, f(x) == 0, weight=10)
2138 >>> q.weight()
2139 10
2140 """
2141 return int(Z3_get_quantifier_weight(self.ctx_ref(), self.ast))
2142
2143 def skolem_id(self):
2144 """Return the skolem id of `self`.
2145 """
2146 return _symbol2py(self.ctx, Z3_get_quantifier_skolem_id(self.ctx_ref(), self.ast))
2147
2148 def qid(self):
2149 """Return the quantifier id of `self`.
2150 """
2151 return _symbol2py(self.ctx, Z3_get_quantifier_id(self.ctx_ref(), self.ast))
2152
2153 def num_patterns(self):
2154 """Return the number of patterns (i.e., quantifier instantiation hints) in `self`.
2155
2156 >>> f = Function('f', IntSort(), IntSort())
2157 >>> g = Function('g', IntSort(), IntSort())
2158 >>> x = Int('x')
2159 >>> q = ForAll(x, f(x) != g(x), patterns = [ f(x), g(x) ])
2160 >>> q.num_patterns()
2161 2
2162 """
2163 return int(Z3_get_quantifier_num_patterns(self.ctx_ref(), self.ast))
2164
2165 def pattern(self, idx):
2166 """Return a pattern (i.e., quantifier instantiation hints) in `self`.
2167
2168 >>> f = Function('f', IntSort(), IntSort())
2169 >>> g = Function('g', IntSort(), IntSort())
2170 >>> x = Int('x')
2171 >>> q = ForAll(x, f(x) != g(x), patterns = [ f(x), g(x) ])
2172 >>> q.num_patterns()
2173 2
2174 >>> q.pattern(0)
2175 f(Var(0))
2176 >>> q.pattern(1)
2177 g(Var(0))
2178 """
2179 if z3_debug():
2180 _z3_assert(idx < self.num_patterns(), "Invalid pattern idx")
2181 return PatternRef(Z3_get_quantifier_pattern_ast(self.ctx_ref(), self.ast, idx), self.ctx)
2182
2184 """Return the number of no-patterns."""
2185 return Z3_get_quantifier_num_no_patterns(self.ctx_ref(), self.ast)
2186
2187 def no_pattern(self, idx):
2188 """Return a no-pattern."""
2189 if z3_debug():
2190 _z3_assert(idx < self.num_no_patterns(), "Invalid no-pattern idx")
2191 return _to_expr_ref(Z3_get_quantifier_no_pattern_ast(self.ctx_ref(), self.ast, idx), self.ctx)
2192
2193 def body(self):
2194 """Return the expression being quantified.
2195
2196 >>> f = Function('f', IntSort(), IntSort())
2197 >>> x = Int('x')
2198 >>> q = ForAll(x, f(x) == 0)
2199 >>> q.body()
2200 f(Var(0)) == 0
2201 """
2202 return _to_expr_ref(Z3_get_quantifier_body(self.ctx_ref(), self.ast), self.ctx)
2203
2204 def num_vars(self):
2205 """Return the number of variables bounded by this quantifier.
2206
2207 >>> f = Function('f', IntSort(), IntSort(), IntSort())
2208 >>> x = Int('x')
2209 >>> y = Int('y')
2210 >>> q = ForAll([x, y], f(x, y) >= x)
2211 >>> q.num_vars()
2212 2
2213 """
2214 return int(Z3_get_quantifier_num_bound(self.ctx_ref(), self.ast))
2215
2216 def var_name(self, idx):
2217 """Return a string representing a name used when displaying the quantifier.
2218
2219 >>> f = Function('f', IntSort(), IntSort(), IntSort())
2220 >>> x = Int('x')
2221 >>> y = Int('y')
2222 >>> q = ForAll([x, y], f(x, y) >= x)
2223 >>> q.var_name(0)
2224 'x'
2225 >>> q.var_name(1)
2226 'y'
2227 """
2228 if z3_debug():
2229 _z3_assert(idx < self.num_vars(), "Invalid variable idx")
2230 return _symbol2py(self.ctx, Z3_get_quantifier_bound_name(self.ctx_ref(), self.ast, idx))
2231
2232 def var_sort(self, idx):
2233 """Return the sort of a bound variable.
2234
2235 >>> f = Function('f', IntSort(), RealSort(), IntSort())
2236 >>> x = Int('x')
2237 >>> y = Real('y')
2238 >>> q = ForAll([x, y], f(x, y) >= x)
2239 >>> q.var_sort(0)
2240 Int
2241 >>> q.var_sort(1)
2242 Real
2243 """
2244 if z3_debug():
2245 _z3_assert(idx < self.num_vars(), "Invalid variable idx")
2246 return _to_sort_ref(Z3_get_quantifier_bound_sort(self.ctx_ref(), self.ast, idx), self.ctx)
2247
2248 def children(self):
2249 """Return a list containing a single element self.body()
2250
2251 >>> f = Function('f', IntSort(), IntSort())
2252 >>> x = Int('x')
2253 >>> q = ForAll(x, f(x) == 0)
2254 >>> q.children()
2255 [f(Var(0)) == 0]
2256 """
2257 return [self.body()]
2258
2259
2261 """Return `True` if `a` is a Z3 quantifier.
2262
2263 >>> f = Function('f', IntSort(), IntSort())
2264 >>> x = Int('x')
2265 >>> q = ForAll(x, f(x) == 0)
2266 >>> is_quantifier(q)
2267 True
2268 >>> is_quantifier(f(x))
2269 False
2270 """
2271 return isinstance(a, QuantifierRef)
2272
2273
2274def _mk_quantifier(is_forall, vs, body, weight=1, qid="", skid="", patterns=[], no_patterns=[]):
2275 if z3_debug():
2276 _z3_assert(is_bool(body) or is_app(vs) or (len(vs) > 0 and is_app(vs[0])), "Z3 expression expected")
2277 _z3_assert(is_const(vs) or (len(vs) > 0 and all([is_const(v) for v in vs])), "Invalid bounded variable(s)")
2278 _z3_assert(all([is_pattern(a) or is_expr(a) for a in patterns]), "Z3 patterns expected")
2279 _z3_assert(all([is_expr(p) for p in no_patterns]), "no patterns are Z3 expressions")
2280 if is_app(vs):
2281 ctx = vs.ctx
2282 vs = [vs]
2283 else:
2284 ctx = vs[0].ctx
2285 if not is_expr(body):
2286 body = BoolVal(body, ctx)
2287 num_vars = len(vs)
2288 if num_vars == 0:
2289 return body
2290 _vs = (Ast * num_vars)()
2291 for i in range(num_vars):
2292 # TODO: Check if is constant
2293 _vs[i] = vs[i].as_ast()
2294 patterns = [_to_pattern(p) for p in patterns]
2295 num_pats = len(patterns)
2296 _pats = (Pattern * num_pats)()
2297 for i in range(num_pats):
2298 _pats[i] = patterns[i].ast
2299 _no_pats, num_no_pats = _to_ast_array(no_patterns)
2300 qid = to_symbol(qid, ctx)
2301 skid = to_symbol(skid, ctx)
2302 return QuantifierRef(Z3_mk_quantifier_const_ex(ctx.ref(), is_forall, weight, qid, skid,
2303 num_vars, _vs,
2304 num_pats, _pats,
2305 num_no_pats, _no_pats,
2306 body.as_ast()), ctx)
2307
2308
2309def ForAll(vs, body, weight=1, qid="", skid="", patterns=[], no_patterns=[]):
2310 """Create a Z3 forall formula.
2311
2312 The parameters `weight`, `qid`, `skid`, `patterns` and `no_patterns` are optional annotations.
2313
2314 >>> f = Function('f', IntSort(), IntSort(), IntSort())
2315 >>> x = Int('x')
2316 >>> y = Int('y')
2317 >>> ForAll([x, y], f(x, y) >= x)
2318 ForAll([x, y], f(x, y) >= x)
2319 >>> ForAll([x, y], f(x, y) >= x, patterns=[ f(x, y) ])
2320 ForAll([x, y], f(x, y) >= x)
2321 >>> ForAll([x, y], f(x, y) >= x, weight=10)
2322 ForAll([x, y], f(x, y) >= x)
2323 """
2324 return _mk_quantifier(True, vs, body, weight, qid, skid, patterns, no_patterns)
2325
2326
2327def Exists(vs, body, weight=1, qid="", skid="", patterns=[], no_patterns=[]):
2328 """Create a Z3 exists formula.
2329
2330 The parameters `weight`, `qif`, `skid`, `patterns` and `no_patterns` are optional annotations.
2331
2332
2333 >>> f = Function('f', IntSort(), IntSort(), IntSort())
2334 >>> x = Int('x')
2335 >>> y = Int('y')
2336 >>> q = Exists([x, y], f(x, y) >= x, skid="foo")
2337 >>> q
2338 Exists([x, y], f(x, y) >= x)
2339 >>> is_quantifier(q)
2340 True
2341 >>> r = Tactic('nnf')(q).as_expr()
2342 >>> is_quantifier(r)
2343 False
2344 """
2345 return _mk_quantifier(False, vs, body, weight, qid, skid, patterns, no_patterns)
2346
2347
2348def Lambda(vs, body):
2349 """Create a Z3 lambda expression.
2350
2351 >>> f = Function('f', IntSort(), IntSort(), IntSort())
2352 >>> mem0 = Array('mem0', IntSort(), IntSort())
2353 >>> lo, hi, e, i = Ints('lo hi e i')
2354 >>> mem1 = Lambda([i], If(And(lo <= i, i <= hi), e, mem0[i]))
2355 >>> mem1
2356 Lambda(i, If(And(lo <= i, i <= hi), e, mem0[i]))
2357 """
2358 ctx = body.ctx
2359 if is_app(vs):
2360 vs = [vs]
2361 num_vars = len(vs)
2362 _vs = (Ast * num_vars)()
2363 for i in range(num_vars):
2364 # TODO: Check if is constant
2365 _vs[i] = vs[i].as_ast()
2366 return QuantifierRef(Z3_mk_lambda_const(ctx.ref(), num_vars, _vs, body.as_ast()), ctx)
2367
2368
2373
2374
2376 """Real and Integer sorts."""
2377
2378 def is_real(self):
2379 """Return `True` if `self` is of the sort Real.
2380
2381 >>> x = Real('x')
2382 >>> x.is_real()
2383 True
2384 >>> (x + 1).is_real()
2385 True
2386 >>> x = Int('x')
2387 >>> x.is_real()
2388 False
2389 """
2390 return self.kind() == Z3_REAL_SORT
2391
2392 def is_int(self):
2393 """Return `True` if `self` is of the sort Integer.
2394
2395 >>> x = Int('x')
2396 >>> x.is_int()
2397 True
2398 >>> (x + 1).is_int()
2399 True
2400 >>> x = Real('x')
2401 >>> x.is_int()
2402 False
2403 """
2404 return self.kind() == Z3_INT_SORT
2405
2406 def is_bool(self):
2407 return False
2408
2409 def subsort(self, other):
2410 """Return `True` if `self` is a subsort of `other`."""
2411 return self.is_int() and is_arith_sort(other) and other.is_real()
2412
2413 def cast(self, val):
2414 """Try to cast `val` as an Integer or Real.
2415
2416 >>> IntSort().cast(10)
2417 10
2418 >>> is_int(IntSort().cast(10))
2419 True
2420 >>> is_int(10)
2421 False
2422 >>> RealSort().cast(10)
2423 10
2424 >>> is_real(RealSort().cast(10))
2425 True
2426 """
2427 if is_expr(val):
2428 if z3_debug():
2429 _z3_assert(self.ctx == val.ctx, "Context mismatch")
2430 val_s = val.sort()
2431 if self.eq(val_s):
2432 return val
2433 if val_s.is_int() and self.is_real():
2434 return ToReal(val)
2435 if val_s.is_bool() and self.is_int():
2436 return If(val, 1, 0)
2437 if val_s.is_bool() and self.is_real():
2438 return ToReal(If(val, 1, 0))
2439 if z3_debug():
2440 _z3_assert(False, "Z3 Integer/Real expression expected")
2441 else:
2442 if self.is_int():
2443 return IntVal(val, self.ctx)
2444 if self.is_real():
2445 return RealVal(val, self.ctx)
2446 if z3_debug():
2447 msg = "int, long, float, string (numeral), or Z3 Integer/Real expression expected. Got %s"
2448 _z3_assert(False, msg % self)
2449
2450
2451def is_arith_sort(s : Any) -> bool:
2452 """Return `True` if s is an arithmetical sort (type).
2453
2454 >>> is_arith_sort(IntSort())
2455 True
2456 >>> is_arith_sort(RealSort())
2457 True
2458 >>> is_arith_sort(BoolSort())
2459 False
2460 >>> n = Int('x') + 1
2461 >>> is_arith_sort(n.sort())
2462 True
2463 """
2464 return isinstance(s, ArithSortRef)
2465
2466
2468 """Integer and Real expressions."""
2469
2470 def sort(self):
2471 """Return the sort (type) of the arithmetical expression `self`.
2472
2473 >>> Int('x').sort()
2474 Int
2475 >>> (Real('x') + 1).sort()
2476 Real
2477 """
2478 return ArithSortRef(Z3_get_sort(self.ctx_ref(), self.as_ast()), self.ctx)
2479
2480 def is_int(self):
2481 """Return `True` if `self` is an integer expression.
2482
2483 >>> x = Int('x')
2484 >>> x.is_int()
2485 True
2486 >>> (x + 1).is_int()
2487 True
2488 >>> y = Real('y')
2489 >>> (x + y).is_int()
2490 False
2491 """
2492 return self.sort().is_int()
2493
2494 def is_real(self):
2495 """Return `True` if `self` is an real expression.
2496
2497 >>> x = Real('x')
2498 >>> x.is_real()
2499 True
2500 >>> (x + 1).is_real()
2501 True
2502 """
2503 return self.sort().is_real()
2504
2505 def __add__(self, other):
2506 """Create the Z3 expression `self + other`.
2507
2508 >>> x = Int('x')
2509 >>> y = Int('y')
2510 >>> x + y
2511 x + y
2512 >>> (x + y).sort()
2513 Int
2514 """
2515 a, b = _coerce_exprs(self, other)
2516 return ArithRef(_mk_bin(Z3_mk_add, a, b), self.ctx)
2517
2518 def __radd__(self, other):
2519 """Create the Z3 expression `other + self`.
2520
2521 >>> x = Int('x')
2522 >>> 10 + x
2523 10 + x
2524 """
2525 a, b = _coerce_exprs(self, other)
2526 return ArithRef(_mk_bin(Z3_mk_add, b, a), self.ctx)
2527
2528 def __mul__(self, other):
2529 """Create the Z3 expression `self * other`.
2530
2531 >>> x = Real('x')
2532 >>> y = Real('y')
2533 >>> x * y
2534 x*y
2535 >>> (x * y).sort()
2536 Real
2537 """
2538 if isinstance(other, BoolRef):
2539 return If(other, self, 0)
2540 a, b = _coerce_exprs(self, other)
2541 return ArithRef(_mk_bin(Z3_mk_mul, a, b), self.ctx)
2542
2543 def __rmul__(self, other):
2544 """Create the Z3 expression `other * self`.
2545
2546 >>> x = Real('x')
2547 >>> 10 * x
2548 10*x
2549 """
2550 a, b = _coerce_exprs(self, other)
2551 return ArithRef(_mk_bin(Z3_mk_mul, b, a), self.ctx)
2552
2553 def __sub__(self, other):
2554 """Create the Z3 expression `self - other`.
2555
2556 >>> x = Int('x')
2557 >>> y = Int('y')
2558 >>> x - y
2559 x - y
2560 >>> (x - y).sort()
2561 Int
2562 """
2563 a, b = _coerce_exprs(self, other)
2564 return ArithRef(_mk_bin(Z3_mk_sub, a, b), self.ctx)
2565
2566 def __rsub__(self, other):
2567 """Create the Z3 expression `other - self`.
2568
2569 >>> x = Int('x')
2570 >>> 10 - x
2571 10 - x
2572 """
2573 a, b = _coerce_exprs(self, other)
2574 return ArithRef(_mk_bin(Z3_mk_sub, b, a), self.ctx)
2575
2576 def __pow__(self, other):
2577 """Create the Z3 expression `self**other` (** is the power operator).
2578
2579 >>> x = Real('x')
2580 >>> x**3
2581 x**3
2582 >>> (x**3).sort()
2583 Real
2584 >>> simplify(IntVal(2)**8)
2585 256
2586 """
2587 a, b = _coerce_exprs(self, other)
2588 return ArithRef(Z3_mk_power(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
2589
2590 def __rpow__(self, other):
2591 """Create the Z3 expression `other**self` (** is the power operator).
2592
2593 >>> x = Real('x')
2594 >>> 2**x
2595 2**x
2596 >>> (2**x).sort()
2597 Real
2598 >>> simplify(2**IntVal(8))
2599 256
2600 """
2601 a, b = _coerce_exprs(self, other)
2602 return ArithRef(Z3_mk_power(self.ctx_ref(), b.as_ast(), a.as_ast()), self.ctx)
2603
2604 def __div__(self, other):
2605 """Create the Z3 expression `other/self`.
2606
2607 >>> x = Int('x')
2608 >>> y = Int('y')
2609 >>> x/y
2610 x/y
2611 >>> (x/y).sort()
2612 Int
2613 >>> (x/y).sexpr()
2614 '(div x y)'
2615 >>> x = Real('x')
2616 >>> y = Real('y')
2617 >>> x/y
2618 x/y
2619 >>> (x/y).sort()
2620 Real
2621 >>> (x/y).sexpr()
2622 '(/ x y)'
2623 """
2624 a, b = _coerce_exprs(self, other)
2625 return ArithRef(Z3_mk_div(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
2626
2627 def __truediv__(self, other):
2628 """Create the Z3 expression `other/self`."""
2629 return self.__div__(other)
2630
2631 def __rdiv__(self, other):
2632 """Create the Z3 expression `other/self`.
2633
2634 >>> x = Int('x')
2635 >>> 10/x
2636 10/x
2637 >>> (10/x).sexpr()
2638 '(div 10 x)'
2639 >>> x = Real('x')
2640 >>> 10/x
2641 10/x
2642 >>> (10/x).sexpr()
2643 '(/ 10.0 x)'
2644 """
2645 a, b = _coerce_exprs(self, other)
2646 return ArithRef(Z3_mk_div(self.ctx_ref(), b.as_ast(), a.as_ast()), self.ctx)
2647
2648 def __rtruediv__(self, other):
2649 """Create the Z3 expression `other/self`."""
2650 return self.__rdiv__(other)
2651
2652 def __mod__(self, other):
2653 """Create the Z3 expression `other%self`.
2654
2655 >>> x = Int('x')
2656 >>> y = Int('y')
2657 >>> x % y
2658 x%y
2659 >>> simplify(IntVal(10) % IntVal(3))
2660 1
2661 """
2662 a, b = _coerce_exprs(self, other)
2663 if z3_debug():
2664 _z3_assert(a.is_int(), "Z3 integer expression expected")
2665 return ArithRef(Z3_mk_mod(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
2666
2667 def __rmod__(self, other):
2668 """Create the Z3 expression `other%self`.
2669
2670 >>> x = Int('x')
2671 >>> 10 % x
2672 10%x
2673 """
2674 a, b = _coerce_exprs(self, other)
2675 if z3_debug():
2676 _z3_assert(a.is_int(), "Z3 integer expression expected")
2677 return ArithRef(Z3_mk_mod(self.ctx_ref(), b.as_ast(), a.as_ast()), self.ctx)
2678
2679 def __neg__(self):
2680 """Return an expression representing `-self`.
2681
2682 >>> x = Int('x')
2683 >>> -x
2684 -x
2685 >>> simplify(-(-x))
2686 x
2687 """
2688 return ArithRef(Z3_mk_unary_minus(self.ctx_ref(), self.as_ast()), self.ctx)
2689
2690 def __pos__(self):
2691 """Return `self`.
2692
2693 >>> x = Int('x')
2694 >>> +x
2695 x
2696 """
2697 return self
2698
2699 def __le__(self, other):
2700 """Create the Z3 expression `other <= self`.
2701
2702 >>> x, y = Ints('x y')
2703 >>> x <= y
2704 x <= y
2705 >>> y = Real('y')
2706 >>> x <= y
2707 ToReal(x) <= y
2708 """
2709 a, b = _coerce_exprs(self, other)
2710 return BoolRef(Z3_mk_le(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
2711
2712 def __lt__(self, other):
2713 """Create the Z3 expression `other < self`.
2714
2715 >>> x, y = Ints('x y')
2716 >>> x < y
2717 x < y
2718 >>> y = Real('y')
2719 >>> x < y
2720 ToReal(x) < y
2721 """
2722 a, b = _coerce_exprs(self, other)
2723 return BoolRef(Z3_mk_lt(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
2724
2725 def __gt__(self, other):
2726 """Create the Z3 expression `other > self`.
2727
2728 >>> x, y = Ints('x y')
2729 >>> x > y
2730 x > y
2731 >>> y = Real('y')
2732 >>> x > y
2733 ToReal(x) > y
2734 """
2735 a, b = _coerce_exprs(self, other)
2736 return BoolRef(Z3_mk_gt(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
2737
2738 def __ge__(self, other):
2739 """Create the Z3 expression `other >= self`.
2740
2741 >>> x, y = Ints('x y')
2742 >>> x >= y
2743 x >= y
2744 >>> y = Real('y')
2745 >>> x >= y
2746 ToReal(x) >= y
2747 """
2748 a, b = _coerce_exprs(self, other)
2749 return BoolRef(Z3_mk_ge(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
2750
2751
2753 """Return `True` if `a` is an arithmetical expression.
2754
2755 >>> x = Int('x')
2756 >>> is_arith(x)
2757 True
2758 >>> is_arith(x + 1)
2759 True
2760 >>> is_arith(1)
2761 False
2762 >>> is_arith(IntVal(1))
2763 True
2764 >>> y = Real('y')
2765 >>> is_arith(y)
2766 True
2767 >>> is_arith(y + 1)
2768 True
2769 """
2770 return isinstance(a, ArithRef)
2771
2772
2773def is_int(a) -> bool:
2774 """Return `True` if `a` is an integer expression.
2775
2776 >>> x = Int('x')
2777 >>> is_int(x + 1)
2778 True
2779 >>> is_int(1)
2780 False
2781 >>> is_int(IntVal(1))
2782 True
2783 >>> y = Real('y')
2784 >>> is_int(y)
2785 False
2786 >>> is_int(y + 1)
2787 False
2788 """
2789 return is_arith(a) and a.is_int()
2790
2791
2792def is_real(a):
2793 """Return `True` if `a` is a real expression.
2794
2795 >>> x = Int('x')
2796 >>> is_real(x + 1)
2797 False
2798 >>> y = Real('y')
2799 >>> is_real(y)
2800 True
2801 >>> is_real(y + 1)
2802 True
2803 >>> is_real(1)
2804 False
2805 >>> is_real(RealVal(1))
2806 True
2807 """
2808 return is_arith(a) and a.is_real()
2809
2810
2811def _is_numeral(ctx, a):
2812 return Z3_is_numeral_ast(ctx.ref(), a)
2813
2814
2815def _is_algebraic(ctx, a):
2816 return Z3_is_algebraic_number(ctx.ref(), a)
2817
2818
2820 """Return `True` if `a` is an integer value of sort Int.
2821
2822 >>> is_int_value(IntVal(1))
2823 True
2824 >>> is_int_value(1)
2825 False
2826 >>> is_int_value(Int('x'))
2827 False
2828 >>> n = Int('x') + 1
2829 >>> n
2830 x + 1
2831 >>> n.arg(1)
2832 1
2833 >>> is_int_value(n.arg(1))
2834 True
2835 >>> is_int_value(RealVal("1/3"))
2836 False
2837 >>> is_int_value(RealVal(1))
2838 False
2839 """
2840 return is_arith(a) and a.is_int() and _is_numeral(a.ctx, a.as_ast())
2841
2842
2844 """Return `True` if `a` is rational value of sort Real.
2845
2846 >>> is_rational_value(RealVal(1))
2847 True
2848 >>> is_rational_value(RealVal("3/5"))
2849 True
2850 >>> is_rational_value(IntVal(1))
2851 False
2852 >>> is_rational_value(1)
2853 False
2854 >>> n = Real('x') + 1
2855 >>> n.arg(1)
2856 1
2857 >>> is_rational_value(n.arg(1))
2858 True
2859 >>> is_rational_value(Real('x'))
2860 False
2861 """
2862 return is_arith(a) and a.is_real() and _is_numeral(a.ctx, a.as_ast())
2863
2864
2866 """Return `True` if `a` is an algebraic value of sort Real.
2867
2868 >>> is_algebraic_value(RealVal("3/5"))
2869 False
2870 >>> n = simplify(Sqrt(2))
2871 >>> n
2872 1.4142135623?
2873 >>> is_algebraic_value(n)
2874 True
2875 """
2876 return is_arith(a) and a.is_real() and _is_algebraic(a.ctx, a.as_ast())
2877
2878
2879def is_add(a : Any) -> bool:
2880 """Return `True` if `a` is an expression of the form b + c.
2881
2882 >>> x, y = Ints('x y')
2883 >>> is_add(x + y)
2884 True
2885 >>> is_add(x - y)
2886 False
2887 """
2888 return is_app_of(a, Z3_OP_ADD)
2889
2890
2891def is_mul(a : Any) -> bool:
2892 """Return `True` if `a` is an expression of the form b * c.
2893
2894 >>> x, y = Ints('x y')
2895 >>> is_mul(x * y)
2896 True
2897 >>> is_mul(x - y)
2898 False
2899 """
2900 return is_app_of(a, Z3_OP_MUL)
2901
2902
2903def is_sub(a : Any) -> bool:
2904 """Return `True` if `a` is an expression of the form b - c.
2905
2906 >>> x, y = Ints('x y')
2907 >>> is_sub(x - y)
2908 True
2909 >>> is_sub(x + y)
2910 False
2911 """
2912 return is_app_of(a, Z3_OP_SUB)
2913
2914
2915def is_div(a : Any) -> bool:
2916 """Return `True` if `a` is an expression of the form b / c.
2917
2918 >>> x, y = Reals('x y')
2919 >>> is_div(x / y)
2920 True
2921 >>> is_div(x + y)
2922 False
2923 >>> x, y = Ints('x y')
2924 >>> is_div(x / y)
2925 False
2926 >>> is_idiv(x / y)
2927 True
2928 """
2929 return is_app_of(a, Z3_OP_DIV)
2930
2931
2932def is_idiv(a : Any) -> bool:
2933 """Return `True` if `a` is an expression of the form b div c.
2934
2935 >>> x, y = Ints('x y')
2936 >>> is_idiv(x / y)
2937 True
2938 >>> is_idiv(x + y)
2939 False
2940 """
2941 return is_app_of(a, Z3_OP_IDIV)
2942
2943
2944def is_mod(a : Any) -> bool:
2945 """Return `True` if `a` is an expression of the form b % c.
2946
2947 >>> x, y = Ints('x y')
2948 >>> is_mod(x % y)
2949 True
2950 >>> is_mod(x + y)
2951 False
2952 """
2953 return is_app_of(a, Z3_OP_MOD)
2954
2955
2956def is_le(a : Any) -> bool:
2957 """Return `True` if `a` is an expression of the form b <= c.
2958
2959 >>> x, y = Ints('x y')
2960 >>> is_le(x <= y)
2961 True
2962 >>> is_le(x < y)
2963 False
2964 """
2965 return is_app_of(a, Z3_OP_LE)
2966
2967
2968def is_lt(a : Any) -> bool:
2969 """Return `True` if `a` is an expression of the form b < c.
2970
2971 >>> x, y = Ints('x y')
2972 >>> is_lt(x < y)
2973 True
2974 >>> is_lt(x == y)
2975 False
2976 """
2977 return is_app_of(a, Z3_OP_LT)
2978
2979
2980def is_ge(a : Any) -> bool:
2981 """Return `True` if `a` is an expression of the form b >= c.
2982
2983 >>> x, y = Ints('x y')
2984 >>> is_ge(x >= y)
2985 True
2986 >>> is_ge(x == y)
2987 False
2988 """
2989 return is_app_of(a, Z3_OP_GE)
2990
2991
2992def is_gt(a : Any) -> bool:
2993 """Return `True` if `a` is an expression of the form b > c.
2994
2995 >>> x, y = Ints('x y')
2996 >>> is_gt(x > y)
2997 True
2998 >>> is_gt(x == y)
2999 False
3000 """
3001 return is_app_of(a, Z3_OP_GT)
3002
3003
3004def is_is_int(a : Any) -> bool:
3005 """Return `True` if `a` is an expression of the form IsInt(b).
3006
3007 >>> x = Real('x')
3008 >>> is_is_int(IsInt(x))
3009 True
3010 >>> is_is_int(x)
3011 False
3012 """
3013 return is_app_of(a, Z3_OP_IS_INT)
3014
3015
3016def is_to_real(a : Any) -> bool:
3017 """Return `True` if `a` is an expression of the form ToReal(b).
3018
3019 >>> x = Int('x')
3020 >>> n = ToReal(x)
3021 >>> n
3022 ToReal(x)
3023 >>> is_to_real(n)
3024 True
3025 >>> is_to_real(x)
3026 False
3027 """
3028 return is_app_of(a, Z3_OP_TO_REAL)
3029
3030
3031def is_to_int(a : Any) -> bool:
3032 """Return `True` if `a` is an expression of the form ToInt(b).
3033
3034 >>> x = Real('x')
3035 >>> n = ToInt(x)
3036 >>> n
3037 ToInt(x)
3038 >>> is_to_int(n)
3039 True
3040 >>> is_to_int(x)
3041 False
3042 """
3043 return is_app_of(a, Z3_OP_TO_INT)
3044
3045
3047 """Integer values."""
3048
3049 def as_long(self):
3050 """Return a Z3 integer numeral as a Python long (bignum) numeral.
3051
3052 >>> v = IntVal(1)
3053 >>> v + 1
3054 1 + 1
3055 >>> v.as_long() + 1
3056 2
3057 """
3058 if z3_debug():
3059 _z3_assert(self.is_int(), "Integer value expected")
3060 return int(self.as_string())
3061
3062 def as_string(self):
3063 """Return a Z3 integer numeral as a Python string.
3064 >>> v = IntVal(100)
3065 >>> v.as_string()
3066 '100'
3067 """
3068 return Z3_get_numeral_string(self.ctx_ref(), self.as_ast())
3069
3071 """Return a Z3 integer numeral as a Python binary string.
3072 >>> v = IntVal(10)
3073 >>> v.as_binary_string()
3074 '1010'
3075 """
3076 return Z3_get_numeral_binary_string(self.ctx_ref(), self.as_ast())
3077
3078 def py_value(self):
3079 return self.as_long()
3080
3081
3083 """Rational values."""
3084
3085 def numerator(self):
3086 """ Return the numerator of a Z3 rational numeral.
3087
3088 >>> is_rational_value(RealVal("3/5"))
3089 True
3090 >>> n = RealVal("3/5")
3091 >>> n.numerator()
3092 3
3093 >>> is_rational_value(Q(3,5))
3094 True
3095 >>> Q(3,5).numerator()
3096 3
3097 """
3098 return IntNumRef(Z3_get_numerator(self.ctx_ref(), self.as_ast()), self.ctx)
3099
3100 def denominator(self):
3101 """ Return the denominator of a Z3 rational numeral.
3102
3103 >>> is_rational_value(Q(3,5))
3104 True
3105 >>> n = Q(3,5)
3106 >>> n.denominator()
3107 5
3108 """
3109 return IntNumRef(Z3_get_denominator(self.ctx_ref(), self.as_ast()), self.ctx)
3110
3112 """ Return the numerator as a Python long.
3113
3114 >>> v = RealVal(10000000000)
3115 >>> v
3116 10000000000
3117 >>> v + 1
3118 10000000000 + 1
3119 >>> v.numerator_as_long() + 1 == 10000000001
3120 True
3121 """
3122 return self.numerator().as_long()
3123
3125 """ Return the denominator as a Python long.
3126
3127 >>> v = RealVal("1/3")
3128 >>> v
3129 1/3
3130 >>> v.denominator_as_long()
3131 3
3132 """
3133 return self.denominator().as_long()
3134
3135 def is_int(self):
3136 return False
3137
3138 def is_real(self):
3139 return True
3140
3141 def is_int_value(self):
3142 return self.denominator().is_int() and self.denominator_as_long() == 1
3143
3144 def as_long(self):
3145 _z3_assert(self.is_int_value(), "Expected integer fraction")
3146 return self.numerator_as_long()
3147
3148 def as_decimal(self, prec):
3149 """ Return a Z3 rational value as a string in decimal notation using at most `prec` decimal places.
3150
3151 >>> v = RealVal("1/5")
3152 >>> v.as_decimal(3)
3153 '0.2'
3154 >>> v = RealVal("1/3")
3155 >>> v.as_decimal(3)
3156 '0.333?'
3157 """
3158 return Z3_get_numeral_decimal_string(self.ctx_ref(), self.as_ast(), prec)
3159
3160 def as_string(self):
3161 """Return a Z3 rational numeral as a Python string.
3162
3163 >>> v = Q(3,6)
3164 >>> v.as_string()
3165 '1/2'
3166 """
3167 return Z3_get_numeral_string(self.ctx_ref(), self.as_ast())
3168
3169 def as_fraction(self):
3170 """Return a Z3 rational as a Python Fraction object.
3171
3172 >>> v = RealVal("1/5")
3173 >>> v.as_fraction()
3174 Fraction(1, 5)
3175 """
3176 return Fraction(self.numerator_as_long(), self.denominator_as_long())
3177
3178 def py_value(self):
3179 return Z3_get_numeral_double(self.ctx_ref(), self.as_ast())
3180
3181
3183 """Algebraic irrational values."""
3184
3185 def approx(self, precision=10):
3186 """Return a Z3 rational number that approximates the algebraic number `self`.
3187 The result `r` is such that |r - self| <= 1/10^precision
3188
3189 >>> x = simplify(Sqrt(2))
3190 >>> x.approx(20)
3191 6838717160008073720548335/4835703278458516698824704
3192 >>> x.approx(5)
3193 2965821/2097152
3194 """
3195 return RatNumRef(Z3_get_algebraic_number_upper(self.ctx_ref(), self.as_ast(), precision), self.ctx)
3196
3197 def as_decimal(self, prec):
3198 """Return a string representation of the algebraic number `self` in decimal notation
3199 using `prec` decimal places.
3200
3201 >>> x = simplify(Sqrt(2))
3202 >>> x.as_decimal(10)
3203 '1.4142135623?'
3204 >>> x.as_decimal(20)
3205 '1.41421356237309504880?'
3206 """
3207 return Z3_get_numeral_decimal_string(self.ctx_ref(), self.as_ast(), prec)
3208
3209 def poly(self):
3210 return AstVector(Z3_algebraic_get_poly(self.ctx_ref(), self.as_ast()), self.ctx)
3211
3212 def index(self):
3213 return Z3_algebraic_get_i(self.ctx_ref(), self.as_ast())
3214
3215
3216def _py2expr(a, ctx=None):
3217 if isinstance(a, bool):
3218 return BoolVal(a, ctx)
3219 if _is_int(a):
3220 return IntVal(a, ctx)
3221 if isinstance(a, float):
3222 return RealVal(a, ctx)
3223 if isinstance(a, str):
3224 return StringVal(a, ctx)
3225 if is_expr(a):
3226 return a
3227 if z3_debug():
3228 _z3_assert(False, "Python bool, int, long or float expected")
3229
3230
3231def IntSort(ctx=None):
3232 """Return the integer sort in the given context. If `ctx=None`, then the global context is used.
3233
3234 >>> IntSort()
3235 Int
3236 >>> x = Const('x', IntSort())
3237 >>> is_int(x)
3238 True
3239 >>> x.sort() == IntSort()
3240 True
3241 >>> x.sort() == BoolSort()
3242 False
3243 """
3244 ctx = _get_ctx(ctx)
3245 return ArithSortRef(Z3_mk_int_sort(ctx.ref()), ctx)
3246
3247
3248def RealSort(ctx=None):
3249 """Return the real sort in the given context. If `ctx=None`, then the global context is used.
3250
3251 >>> RealSort()
3252 Real
3253 >>> x = Const('x', RealSort())
3254 >>> is_real(x)
3255 True
3256 >>> is_int(x)
3257 False
3258 >>> x.sort() == RealSort()
3259 True
3260 """
3261 ctx = _get_ctx(ctx)
3262 return ArithSortRef(Z3_mk_real_sort(ctx.ref()), ctx)
3263
3264
3266 if isinstance(val, float):
3267 return str(int(val))
3268 elif isinstance(val, bool):
3269 if val:
3270 return "1"
3271 else:
3272 return "0"
3273 else:
3274 return str(val)
3275
3276
3277def IntVal(val, ctx=None):
3278 """Return a Z3 integer value. If `ctx=None`, then the global context is used.
3279
3280 >>> IntVal(1)
3281 1
3282 >>> IntVal("100")
3283 100
3284 """
3285 ctx = _get_ctx(ctx)
3286 return IntNumRef(Z3_mk_numeral(ctx.ref(), _to_int_str(val), IntSort(ctx).ast), ctx)
3287
3288
3289def RealVal(val, ctx=None):
3290 """Return a Z3 real value.
3291
3292 `val` may be a Python int, long, float or string representing a number in decimal or rational notation.
3293 If `ctx=None`, then the global context is used.
3294
3295 >>> RealVal(1)
3296 1
3297 >>> RealVal(1).sort()
3298 Real
3299 >>> RealVal("3/5")
3300 3/5
3301 >>> RealVal("1.5")
3302 3/2
3303 """
3304 ctx = _get_ctx(ctx)
3305 return RatNumRef(Z3_mk_numeral(ctx.ref(), str(val), RealSort(ctx).ast), ctx)
3306
3307
3308def RatVal(a, b, ctx=None):
3309 """Return a Z3 rational a/b.
3310
3311 If `ctx=None`, then the global context is used.
3312
3313 >>> RatVal(3,5)
3314 3/5
3315 >>> RatVal(3,5).sort()
3316 Real
3317 """
3318 if z3_debug():
3319 _z3_assert(_is_int(a) or isinstance(a, str), "First argument cannot be converted into an integer")
3320 _z3_assert(_is_int(b) or isinstance(b, str), "Second argument cannot be converted into an integer")
3321 return simplify(RealVal(a, ctx) / RealVal(b, ctx))
3322
3323
3324def Q(a, b, ctx=None):
3325 """Return a Z3 rational a/b.
3326
3327 If `ctx=None`, then the global context is used.
3328
3329 >>> Q(3,5)
3330 3/5
3331 >>> Q(3,5).sort()
3332 Real
3333 """
3334 return simplify(RatVal(a, b, ctx=ctx))
3335
3336
3337def Int(name, ctx=None):
3338 """Return an integer constant named `name`. If `ctx=None`, then the global context is used.
3339
3340 >>> x = Int('x')
3341 >>> is_int(x)
3342 True
3343 >>> is_int(x + 1)
3344 True
3345 """
3346 ctx = _get_ctx(ctx)
3347 return ArithRef(Z3_mk_const(ctx.ref(), to_symbol(name, ctx), IntSort(ctx).ast), ctx)
3348
3349
3350def Ints(names, ctx=None):
3351 """Return a tuple of Integer constants.
3352
3353 >>> x, y, z = Ints('x y z')
3354 >>> Sum(x, y, z)
3355 x + y + z
3356 """
3357 ctx = _get_ctx(ctx)
3358 if isinstance(names, str):
3359 names = names.split(" ")
3360 return [Int(name, ctx) for name in names]
3361
3362
3363def IntVector(prefix, sz, ctx=None):
3364 """Return a list of integer constants of size `sz`.
3365
3366 >>> X = IntVector('x', 3)
3367 >>> X
3368 [x__0, x__1, x__2]
3369 >>> Sum(X)
3370 x__0 + x__1 + x__2
3371 """
3372 ctx = _get_ctx(ctx)
3373 return [Int("%s__%s" % (prefix, i), ctx) for i in range(sz)]
3374
3375
3376def FreshInt(prefix="x", ctx=None):
3377 """Return a fresh integer constant in the given context using the given prefix.
3378
3379 >>> x = FreshInt()
3380 >>> y = FreshInt()
3381 >>> eq(x, y)
3382 False
3383 >>> x.sort()
3384 Int
3385 """
3386 ctx = _get_ctx(ctx)
3387 return ArithRef(Z3_mk_fresh_const(ctx.ref(), prefix, IntSort(ctx).ast), ctx)
3388
3389
3390def Real(name, ctx=None):
3391 """Return a real constant named `name`. If `ctx=None`, then the global context is used.
3392
3393 >>> x = Real('x')
3394 >>> is_real(x)
3395 True
3396 >>> is_real(x + 1)
3397 True
3398 """
3399 ctx = _get_ctx(ctx)
3400 return ArithRef(Z3_mk_const(ctx.ref(), to_symbol(name, ctx), RealSort(ctx).ast), ctx)
3401
3402
3403def Reals(names, ctx=None):
3404 """Return a tuple of real constants.
3405
3406 >>> x, y, z = Reals('x y z')
3407 >>> Sum(x, y, z)
3408 x + y + z
3409 >>> Sum(x, y, z).sort()
3410 Real
3411 """
3412 ctx = _get_ctx(ctx)
3413 if isinstance(names, str):
3414 names = names.split(" ")
3415 return [Real(name, ctx) for name in names]
3416
3417
3418def RealVector(prefix, sz, ctx=None):
3419 """Return a list of real constants of size `sz`.
3420
3421 >>> X = RealVector('x', 3)
3422 >>> X
3423 [x__0, x__1, x__2]
3424 >>> Sum(X)
3425 x__0 + x__1 + x__2
3426 >>> Sum(X).sort()
3427 Real
3428 """
3429 ctx = _get_ctx(ctx)
3430 return [Real("%s__%s" % (prefix, i), ctx) for i in range(sz)]
3431
3432
3433def FreshReal(prefix="b", ctx=None):
3434 """Return a fresh real constant in the given context using the given prefix.
3435
3436 >>> x = FreshReal()
3437 >>> y = FreshReal()
3438 >>> eq(x, y)
3439 False
3440 >>> x.sort()
3441 Real
3442 """
3443 ctx = _get_ctx(ctx)
3444 return ArithRef(Z3_mk_fresh_const(ctx.ref(), prefix, RealSort(ctx).ast), ctx)
3445
3446
3447def ToReal(a):
3448 """ Return the Z3 expression ToReal(a).
3449
3450 >>> x = Int('x')
3451 >>> x.sort()
3452 Int
3453 >>> n = ToReal(x)
3454 >>> n
3455 ToReal(x)
3456 >>> n.sort()
3457 Real
3458 """
3459 ctx = a.ctx
3460 if isinstance(a, BoolRef):
3461 return If(a, RealVal(1, ctx), RealVal(0, ctx))
3462 if z3_debug():
3463 _z3_assert(a.is_int(), "Z3 integer expression expected.")
3464 return ArithRef(Z3_mk_int2real(ctx.ref(), a.as_ast()), ctx)
3465
3466
3467def ToInt(a):
3468 """ Return the Z3 expression ToInt(a).
3469
3470 >>> x = Real('x')
3471 >>> x.sort()
3472 Real
3473 >>> n = ToInt(x)
3474 >>> n
3475 ToInt(x)
3476 >>> n.sort()
3477 Int
3478 """
3479 if z3_debug():
3480 _z3_assert(a.is_real(), "Z3 real expression expected.")
3481 ctx = a.ctx
3482 return ArithRef(Z3_mk_real2int(ctx.ref(), a.as_ast()), ctx)
3483
3484
3485def IsInt(a):
3486 """ Return the Z3 predicate IsInt(a).
3487
3488 >>> x = Real('x')
3489 >>> IsInt(x + "1/2")
3490 IsInt(x + 1/2)
3491 >>> solve(IsInt(x + "1/2"), x > 0, x < 1)
3492 [x = 1/2]
3493 >>> solve(IsInt(x + "1/2"), x > 0, x < 1, x != "1/2")
3494 no solution
3495 """
3496 if z3_debug():
3497 _z3_assert(a.is_real(), "Z3 real expression expected.")
3498 ctx = a.ctx
3499 return BoolRef(Z3_mk_is_int(ctx.ref(), a.as_ast()), ctx)
3500
3501
3502def Sqrt(a, ctx=None):
3503 """ Return a Z3 expression which represents the square root of a.
3504
3505 >>> x = Real('x')
3506 >>> Sqrt(x)
3507 x**(1/2)
3508 """
3509 if not is_expr(a):
3510 ctx = _get_ctx(ctx)
3511 a = RealVal(a, ctx)
3512 return a ** "1/2"
3513
3514
3515def Cbrt(a, ctx=None):
3516 """ Return a Z3 expression which represents the cubic root of a.
3517
3518 >>> x = Real('x')
3519 >>> Cbrt(x)
3520 x**(1/3)
3521 """
3522 if not is_expr(a):
3523 ctx = _get_ctx(ctx)
3524 a = RealVal(a, ctx)
3525 return a ** "1/3"
3526
3527
3532
3533
3535 """Bit-vector sort."""
3536
3537 def size(self):
3538 """Return the size (number of bits) of the bit-vector sort `self`.
3539
3540 >>> b = BitVecSort(32)
3541 >>> b.size()
3542 32
3543 """
3544 return int(Z3_get_bv_sort_size(self.ctx_ref(), self.ast))
3545
3546 def subsort(self, other):
3547 return is_bv_sort(other) and self.size() < other.size()
3548
3549 def cast(self, val):
3550 """Try to cast `val` as a Bit-Vector.
3551
3552 >>> b = BitVecSort(32)
3553 >>> b.cast(10)
3554 10
3555 >>> b.cast(10).sexpr()
3556 '#x0000000a'
3557 """
3558 if is_expr(val):
3559 if z3_debug():
3560 _z3_assert(self.ctx == val.ctx, "Context mismatch")
3561 # Idea: use sign_extend if sort of val is a bitvector of smaller size
3562 return val
3563 else:
3564 return BitVecVal(val, self)
3565
3566
3568 """Return True if `s` is a Z3 bit-vector sort.
3569
3570 >>> is_bv_sort(BitVecSort(32))
3571 True
3572 >>> is_bv_sort(IntSort())
3573 False
3574 """
3575 return isinstance(s, BitVecSortRef)
3576
3577
3579 """Bit-vector expressions."""
3580
3581 def sort(self):
3582 """Return the sort of the bit-vector expression `self`.
3583
3584 >>> x = BitVec('x', 32)
3585 >>> x.sort()
3586 BitVec(32)
3587 >>> x.sort() == BitVecSort(32)
3588 True
3589 """
3590 return BitVecSortRef(Z3_get_sort(self.ctx_ref(), self.as_ast()), self.ctx)
3591
3592 def size(self):
3593 """Return the number of bits of the bit-vector expression `self`.
3594
3595 >>> x = BitVec('x', 32)
3596 >>> (x + 1).size()
3597 32
3598 >>> Concat(x, x).size()
3599 64
3600 """
3601 return self.sort().size()
3602
3603 def __add__(self, other):
3604 """Create the Z3 expression `self + other`.
3605
3606 >>> x = BitVec('x', 32)
3607 >>> y = BitVec('y', 32)
3608 >>> x + y
3609 x + y
3610 >>> (x + y).sort()
3611 BitVec(32)
3612 """
3613 a, b = _coerce_exprs(self, other)
3614 return BitVecRef(Z3_mk_bvadd(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
3615
3616 def __radd__(self, other):
3617 """Create the Z3 expression `other + self`.
3618
3619 >>> x = BitVec('x', 32)
3620 >>> 10 + x
3621 10 + x
3622 """
3623 a, b = _coerce_exprs(self, other)
3624 return BitVecRef(Z3_mk_bvadd(self.ctx_ref(), b.as_ast(), a.as_ast()), self.ctx)
3625
3626 def __mul__(self, other):
3627 """Create the Z3 expression `self * other`.
3628
3629 >>> x = BitVec('x', 32)
3630 >>> y = BitVec('y', 32)
3631 >>> x * y
3632 x*y
3633 >>> (x * y).sort()
3634 BitVec(32)
3635 """
3636 a, b = _coerce_exprs(self, other)
3637 return BitVecRef(Z3_mk_bvmul(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
3638
3639 def __rmul__(self, other):
3640 """Create the Z3 expression `other * self`.
3641
3642 >>> x = BitVec('x', 32)
3643 >>> 10 * x
3644 10*x
3645 """
3646 a, b = _coerce_exprs(self, other)
3647 return BitVecRef(Z3_mk_bvmul(self.ctx_ref(), b.as_ast(), a.as_ast()), self.ctx)
3648
3649 def __sub__(self, other):
3650 """Create the Z3 expression `self - other`.
3651
3652 >>> x = BitVec('x', 32)
3653 >>> y = BitVec('y', 32)
3654 >>> x - y
3655 x - y
3656 >>> (x - y).sort()
3657 BitVec(32)
3658 """
3659 a, b = _coerce_exprs(self, other)
3660 return BitVecRef(Z3_mk_bvsub(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
3661
3662 def __rsub__(self, other):
3663 """Create the Z3 expression `other - self`.
3664
3665 >>> x = BitVec('x', 32)
3666 >>> 10 - x
3667 10 - x
3668 """
3669 a, b = _coerce_exprs(self, other)
3670 return BitVecRef(Z3_mk_bvsub(self.ctx_ref(), b.as_ast(), a.as_ast()), self.ctx)
3671
3672 def __or__(self, other):
3673 """Create the Z3 expression bitwise-or `self | other`.
3674
3675 >>> x = BitVec('x', 32)
3676 >>> y = BitVec('y', 32)
3677 >>> x | y
3678 x | y
3679 >>> (x | y).sort()
3680 BitVec(32)
3681 """
3682 a, b = _coerce_exprs(self, other)
3683 return BitVecRef(Z3_mk_bvor(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
3684
3685 def __ror__(self, other):
3686 """Create the Z3 expression bitwise-or `other | self`.
3687
3688 >>> x = BitVec('x', 32)
3689 >>> 10 | x
3690 10 | x
3691 """
3692 a, b = _coerce_exprs(self, other)
3693 return BitVecRef(Z3_mk_bvor(self.ctx_ref(), b.as_ast(), a.as_ast()), self.ctx)
3694
3695 def __and__(self, other):
3696 """Create the Z3 expression bitwise-and `self & other`.
3697
3698 >>> x = BitVec('x', 32)
3699 >>> y = BitVec('y', 32)
3700 >>> x & y
3701 x & y
3702 >>> (x & y).sort()
3703 BitVec(32)
3704 """
3705 a, b = _coerce_exprs(self, other)
3706 return BitVecRef(Z3_mk_bvand(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
3707
3708 def __rand__(self, other):
3709 """Create the Z3 expression bitwise-or `other & self`.
3710
3711 >>> x = BitVec('x', 32)
3712 >>> 10 & x
3713 10 & x
3714 """
3715 a, b = _coerce_exprs(self, other)
3716 return BitVecRef(Z3_mk_bvand(self.ctx_ref(), b.as_ast(), a.as_ast()), self.ctx)
3717
3718 def __xor__(self, other):
3719 """Create the Z3 expression bitwise-xor `self ^ other`.
3720
3721 >>> x = BitVec('x', 32)
3722 >>> y = BitVec('y', 32)
3723 >>> x ^ y
3724 x ^ y
3725 >>> (x ^ y).sort()
3726 BitVec(32)
3727 """
3728 a, b = _coerce_exprs(self, other)
3729 return BitVecRef(Z3_mk_bvxor(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
3730
3731 def __rxor__(self, other):
3732 """Create the Z3 expression bitwise-xor `other ^ self`.
3733
3734 >>> x = BitVec('x', 32)
3735 >>> 10 ^ x
3736 10 ^ x
3737 """
3738 a, b = _coerce_exprs(self, other)
3739 return BitVecRef(Z3_mk_bvxor(self.ctx_ref(), b.as_ast(), a.as_ast()), self.ctx)
3740
3741 def __pos__(self):
3742 """Return `self`.
3743
3744 >>> x = BitVec('x', 32)
3745 >>> +x
3746 x
3747 """
3748 return self
3749
3750 def __neg__(self):
3751 """Return an expression representing `-self`.
3752
3753 >>> x = BitVec('x', 32)
3754 >>> -x
3755 -x
3756 >>> simplify(-(-x))
3757 x
3758 """
3759 return BitVecRef(Z3_mk_bvneg(self.ctx_ref(), self.as_ast()), self.ctx)
3760
3761 def __invert__(self):
3762 """Create the Z3 expression bitwise-not `~self`.
3763
3764 >>> x = BitVec('x', 32)
3765 >>> ~x
3766 ~x
3767 >>> simplify(~(~x))
3768 x
3769 """
3770 return BitVecRef(Z3_mk_bvnot(self.ctx_ref(), self.as_ast()), self.ctx)
3771
3772 def __div__(self, other):
3773 """Create the Z3 expression (signed) division `self / other`.
3774
3775 Use the function UDiv() for unsigned division.
3776
3777 >>> x = BitVec('x', 32)
3778 >>> y = BitVec('y', 32)
3779 >>> x / y
3780 x/y
3781 >>> (x / y).sort()
3782 BitVec(32)
3783 >>> (x / y).sexpr()
3784 '(bvsdiv x y)'
3785 >>> UDiv(x, y).sexpr()
3786 '(bvudiv x y)'
3787 """
3788 a, b = _coerce_exprs(self, other)
3789 return BitVecRef(Z3_mk_bvsdiv(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
3790
3791 def __truediv__(self, other):
3792 """Create the Z3 expression (signed) division `self / other`."""
3793 return self.__div__(other)
3794
3795 def __rdiv__(self, other):
3796 """Create the Z3 expression (signed) division `other / self`.
3797
3798 Use the function UDiv() for unsigned division.
3799
3800 >>> x = BitVec('x', 32)
3801 >>> 10 / x
3802 10/x
3803 >>> (10 / x).sexpr()
3804 '(bvsdiv #x0000000a x)'
3805 >>> UDiv(10, x).sexpr()
3806 '(bvudiv #x0000000a x)'
3807 """
3808 a, b = _coerce_exprs(self, other)
3809 return BitVecRef(Z3_mk_bvsdiv(self.ctx_ref(), b.as_ast(), a.as_ast()), self.ctx)
3810
3811 def __rtruediv__(self, other):
3812 """Create the Z3 expression (signed) division `other / self`."""
3813 return self.__rdiv__(other)
3814
3815 def __mod__(self, other):
3816 """Create the Z3 expression (signed) mod `self % other`.
3817
3818 Use the function URem() for unsigned remainder, and SRem() for signed remainder.
3819
3820 >>> x = BitVec('x', 32)
3821 >>> y = BitVec('y', 32)
3822 >>> x % y
3823 x%y
3824 >>> (x % y).sort()
3825 BitVec(32)
3826 >>> (x % y).sexpr()
3827 '(bvsmod x y)'
3828 >>> URem(x, y).sexpr()
3829 '(bvurem x y)'
3830 >>> SRem(x, y).sexpr()
3831 '(bvsrem x y)'
3832 """
3833 a, b = _coerce_exprs(self, other)
3834 return BitVecRef(Z3_mk_bvsmod(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
3835
3836 def __rmod__(self, other):
3837 """Create the Z3 expression (signed) mod `other % self`.
3838
3839 Use the function URem() for unsigned remainder, and SRem() for signed remainder.
3840
3841 >>> x = BitVec('x', 32)
3842 >>> 10 % x
3843 10%x
3844 >>> (10 % x).sexpr()
3845 '(bvsmod #x0000000a x)'
3846 >>> URem(10, x).sexpr()
3847 '(bvurem #x0000000a x)'
3848 >>> SRem(10, x).sexpr()
3849 '(bvsrem #x0000000a x)'
3850 """
3851 a, b = _coerce_exprs(self, other)
3852 return BitVecRef(Z3_mk_bvsmod(self.ctx_ref(), b.as_ast(), a.as_ast()), self.ctx)
3853
3854 def __le__(self, other):
3855 """Create the Z3 expression (signed) `other <= self`.
3856
3857 Use the function ULE() for unsigned less than or equal to.
3858
3859 >>> x, y = BitVecs('x y', 32)
3860 >>> x <= y
3861 x <= y
3862 >>> (x <= y).sexpr()
3863 '(bvsle x y)'
3864 >>> ULE(x, y).sexpr()
3865 '(bvule x y)'
3866 """
3867 a, b = _coerce_exprs(self, other)
3868 return BoolRef(Z3_mk_bvsle(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
3869
3870 def __lt__(self, other):
3871 """Create the Z3 expression (signed) `other < self`.
3872
3873 Use the function ULT() for unsigned less than.
3874
3875 >>> x, y = BitVecs('x y', 32)
3876 >>> x < y
3877 x < y
3878 >>> (x < y).sexpr()
3879 '(bvslt x y)'
3880 >>> ULT(x, y).sexpr()
3881 '(bvult x y)'
3882 """
3883 a, b = _coerce_exprs(self, other)
3884 return BoolRef(Z3_mk_bvslt(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
3885
3886 def __gt__(self, other):
3887 """Create the Z3 expression (signed) `other > self`.
3888
3889 Use the function UGT() for unsigned greater than.
3890
3891 >>> x, y = BitVecs('x y', 32)
3892 >>> x > y
3893 x > y
3894 >>> (x > y).sexpr()
3895 '(bvsgt x y)'
3896 >>> UGT(x, y).sexpr()
3897 '(bvugt x y)'
3898 """
3899 a, b = _coerce_exprs(self, other)
3900 return BoolRef(Z3_mk_bvsgt(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
3901
3902 def __ge__(self, other):
3903 """Create the Z3 expression (signed) `other >= self`.
3904
3905 Use the function UGE() for unsigned greater than or equal to.
3906
3907 >>> x, y = BitVecs('x y', 32)
3908 >>> x >= y
3909 x >= y
3910 >>> (x >= y).sexpr()
3911 '(bvsge x y)'
3912 >>> UGE(x, y).sexpr()
3913 '(bvuge x y)'
3914 """
3915 a, b = _coerce_exprs(self, other)
3916 return BoolRef(Z3_mk_bvsge(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
3917
3918 def __rshift__(self, other):
3919 """Create the Z3 expression (arithmetical) right shift `self >> other`
3920
3921 Use the function LShR() for the right logical shift
3922
3923 >>> x, y = BitVecs('x y', 32)
3924 >>> x >> y
3925 x >> y
3926 >>> (x >> y).sexpr()
3927 '(bvashr x y)'
3928 >>> LShR(x, y).sexpr()
3929 '(bvlshr x y)'
3930 >>> BitVecVal(4, 3)
3931 4
3932 >>> BitVecVal(4, 3).as_signed_long()
3933 -4
3934 >>> simplify(BitVecVal(4, 3) >> 1).as_signed_long()
3935 -2
3936 >>> simplify(BitVecVal(4, 3) >> 1)
3937 6
3938 >>> simplify(LShR(BitVecVal(4, 3), 1))
3939 2
3940 >>> simplify(BitVecVal(2, 3) >> 1)
3941 1
3942 >>> simplify(LShR(BitVecVal(2, 3), 1))
3943 1
3944 """
3945 a, b = _coerce_exprs(self, other)
3946 return BitVecRef(Z3_mk_bvashr(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
3947
3948 def __lshift__(self, other):
3949 """Create the Z3 expression left shift `self << other`
3950
3951 >>> x, y = BitVecs('x y', 32)
3952 >>> x << y
3953 x << y
3954 >>> (x << y).sexpr()
3955 '(bvshl x y)'
3956 >>> simplify(BitVecVal(2, 3) << 1)
3957 4
3958 """
3959 a, b = _coerce_exprs(self, other)
3960 return BitVecRef(Z3_mk_bvshl(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
3961
3962 def __rrshift__(self, other):
3963 """Create the Z3 expression (arithmetical) right shift `other` >> `self`.
3964
3965 Use the function LShR() for the right logical shift
3966
3967 >>> x = BitVec('x', 32)
3968 >>> 10 >> x
3969 10 >> x
3970 >>> (10 >> x).sexpr()
3971 '(bvashr #x0000000a x)'
3972 """
3973 a, b = _coerce_exprs(self, other)
3974 return BitVecRef(Z3_mk_bvashr(self.ctx_ref(), b.as_ast(), a.as_ast()), self.ctx)
3975
3976 def __rlshift__(self, other):
3977 """Create the Z3 expression left shift `other << self`.
3978
3979 Use the function LShR() for the right logical shift
3980
3981 >>> x = BitVec('x', 32)
3982 >>> 10 << x
3983 10 << x
3984 >>> (10 << x).sexpr()
3985 '(bvshl #x0000000a x)'
3986 """
3987 a, b = _coerce_exprs(self, other)
3988 return BitVecRef(Z3_mk_bvshl(self.ctx_ref(), b.as_ast(), a.as_ast()), self.ctx)
3989
3990
3992 """Bit-vector values."""
3993
3994 def as_long(self):
3995 """Return a Z3 bit-vector numeral as a Python long (bignum) numeral.
3996
3997 >>> v = BitVecVal(0xbadc0de, 32)
3998 >>> v
3999 195936478
4000 >>> print("0x%.8x" % v.as_long())
4001 0x0badc0de
4002 """
4003 return int(self.as_string())
4004
4006 """Return a Z3 bit-vector numeral as a Python long (bignum) numeral.
4007 The most significant bit is assumed to be the sign.
4008
4009 >>> BitVecVal(4, 3).as_signed_long()
4010 -4
4011 >>> BitVecVal(7, 3).as_signed_long()
4012 -1
4013 >>> BitVecVal(3, 3).as_signed_long()
4014 3
4015 >>> BitVecVal(2**32 - 1, 32).as_signed_long()
4016 -1
4017 >>> BitVecVal(2**64 - 1, 64).as_signed_long()
4018 -1
4019 """
4020 sz = self.size()
4021 val = self.as_long()
4022 if val >= 2**(sz - 1):
4023 val = val - 2**sz
4024 if val < -2**(sz - 1):
4025 val = val + 2**sz
4026 return int(val)
4027
4028 def as_string(self):
4029 return Z3_get_numeral_string(self.ctx_ref(), self.as_ast())
4030
4032 return Z3_get_numeral_binary_string(self.ctx_ref(), self.as_ast())
4033
4034 def py_value(self):
4035 """Return the Python value of a Z3 bit-vector numeral."""
4036 return self.as_long()
4037
4038
4039
4040def is_bv(a):
4041 """Return `True` if `a` is a Z3 bit-vector expression.
4042
4043 >>> b = BitVec('b', 32)
4044 >>> is_bv(b)
4045 True
4046 >>> is_bv(b + 10)
4047 True
4048 >>> is_bv(Int('x'))
4049 False
4050 """
4051 return isinstance(a, BitVecRef)
4052
4053
4055 """Return `True` if `a` is a Z3 bit-vector numeral value.
4056
4057 >>> b = BitVec('b', 32)
4058 >>> is_bv_value(b)
4059 False
4060 >>> b = BitVecVal(10, 32)
4061 >>> b
4062 10
4063 >>> is_bv_value(b)
4064 True
4065 """
4066 return is_bv(a) and _is_numeral(a.ctx, a.as_ast())
4067
4068
4069def BV2Int(a, is_signed=False):
4070 """Return the Z3 expression BV2Int(a).
4071
4072 >>> b = BitVec('b', 3)
4073 >>> BV2Int(b).sort()
4074 Int
4075 >>> x = Int('x')
4076 >>> x > BV2Int(b)
4077 x > BV2Int(b)
4078 >>> x > BV2Int(b, is_signed=False)
4079 x > BV2Int(b)
4080 >>> x > BV2Int(b, is_signed=True)
4081 x > If(b < 0, BV2Int(b) - 8, BV2Int(b))
4082 >>> solve(x > BV2Int(b), b == 1, x < 3)
4083 [x = 2, b = 1]
4084 """
4085 if z3_debug():
4086 _z3_assert(is_bv(a), "First argument must be a Z3 bit-vector expression")
4087 ctx = a.ctx
4088 # investigate problem with bv2int
4089 return ArithRef(Z3_mk_bv2int(ctx.ref(), a.as_ast(), is_signed), ctx)
4090
4091
4092def Int2BV(a, num_bits):
4093 """Return the z3 expression Int2BV(a, num_bits).
4094 It is a bit-vector of width num_bits and represents the
4095 modulo of a by 2^num_bits
4096 """
4097 ctx = a.ctx
4098 return BitVecRef(Z3_mk_int2bv(ctx.ref(), num_bits, a.as_ast()), ctx)
4099
4100
4101def BitVecSort(sz, ctx=None):
4102 """Return a Z3 bit-vector sort of the given size. If `ctx=None`, then the global context is used.
4103
4104 >>> Byte = BitVecSort(8)
4105 >>> Word = BitVecSort(16)
4106 >>> Byte
4107 BitVec(8)
4108 >>> x = Const('x', Byte)
4109 >>> eq(x, BitVec('x', 8))
4110 True
4111 """
4112 ctx = _get_ctx(ctx)
4113 return BitVecSortRef(Z3_mk_bv_sort(ctx.ref(), sz), ctx)
4114
4115
4116def BitVecVal(val, bv, ctx=None):
4117 """Return a bit-vector value with the given number of bits. If `ctx=None`, then the global context is used.
4118
4119 >>> v = BitVecVal(10, 32)
4120 >>> v
4121 10
4122 >>> print("0x%.8x" % v.as_long())
4123 0x0000000a
4124 """
4125 if is_bv_sort(bv):
4126 ctx = bv.ctx
4127 return BitVecNumRef(Z3_mk_numeral(ctx.ref(), _to_int_str(val), bv.ast), ctx)
4128 else:
4129 ctx = _get_ctx(ctx)
4130 return BitVecNumRef(Z3_mk_numeral(ctx.ref(), _to_int_str(val), BitVecSort(bv, ctx).ast), ctx)
4131
4132
4133def BitVec(name, bv, ctx=None):
4134 """Return a bit-vector constant named `name`. `bv` may be the number of bits of a bit-vector sort.
4135 If `ctx=None`, then the global context is used.
4136
4137 >>> x = BitVec('x', 16)
4138 >>> is_bv(x)
4139 True
4140 >>> x.size()
4141 16
4142 >>> x.sort()
4143 BitVec(16)
4144 >>> word = BitVecSort(16)
4145 >>> x2 = BitVec('x', word)
4146 >>> eq(x, x2)
4147 True
4148 """
4149 if isinstance(bv, BitVecSortRef):
4150 ctx = bv.ctx
4151 else:
4152 ctx = _get_ctx(ctx)
4153 bv = BitVecSort(bv, ctx)
4154 return BitVecRef(Z3_mk_const(ctx.ref(), to_symbol(name, ctx), bv.ast), ctx)
4155
4156
4157def BitVecs(names, bv, ctx=None):
4158 """Return a tuple of bit-vector constants of size bv.
4159
4160 >>> x, y, z = BitVecs('x y z', 16)
4161 >>> x.size()
4162 16
4163 >>> x.sort()
4164 BitVec(16)
4165 >>> Sum(x, y, z)
4166 0 + x + y + z
4167 >>> Product(x, y, z)
4168 1*x*y*z
4169 >>> simplify(Product(x, y, z))
4170 x*y*z
4171 """
4172 ctx = _get_ctx(ctx)
4173 if isinstance(names, str):
4174 names = names.split(" ")
4175 return [BitVec(name, bv, ctx) for name in names]
4176
4177
4178def Concat(*args):
4179 """Create a Z3 bit-vector concatenation expression.
4180
4181 >>> v = BitVecVal(1, 4)
4182 >>> Concat(v, v+1, v)
4183 Concat(Concat(1, 1 + 1), 1)
4184 >>> simplify(Concat(v, v+1, v))
4185 289
4186 >>> print("%.3x" % simplify(Concat(v, v+1, v)).as_long())
4187 121
4188 """
4189 args = _get_args(args)
4190 sz = len(args)
4191 if z3_debug():
4192 _z3_assert(sz >= 2, "At least two arguments expected.")
4193
4194 ctx = None
4195 for a in args:
4196 if is_expr(a):
4197 ctx = a.ctx
4198 break
4199 if is_seq(args[0]) or isinstance(args[0], str):
4200 args = [_coerce_seq(s, ctx) for s in args]
4201 if z3_debug():
4202 _z3_assert(all([is_seq(a) for a in args]), "All arguments must be sequence expressions.")
4203 v = (Ast * sz)()
4204 for i in range(sz):
4205 v[i] = args[i].as_ast()
4206 return SeqRef(Z3_mk_seq_concat(ctx.ref(), sz, v), ctx)
4207
4208 if is_re(args[0]):
4209 if z3_debug():
4210 _z3_assert(all([is_re(a) for a in args]), "All arguments must be regular expressions.")
4211 v = (Ast * sz)()
4212 for i in range(sz):
4213 v[i] = args[i].as_ast()
4214 return ReRef(Z3_mk_re_concat(ctx.ref(), sz, v), ctx)
4215
4216 if z3_debug():
4217 _z3_assert(all([is_bv(a) for a in args]), "All arguments must be Z3 bit-vector expressions.")
4218 r = args[0]
4219 for i in range(sz - 1):
4220 r = BitVecRef(Z3_mk_concat(ctx.ref(), r.as_ast(), args[i + 1].as_ast()), ctx)
4221 return r
4222
4223
4224def Extract(high, low, a):
4225 """Create a Z3 bit-vector extraction expression or sequence extraction expression.
4226
4227 Extract is overloaded to work with both bit-vectors and sequences:
4228
4229 **Bit-vector extraction**: Extract(high, low, bitvector)
4230 Extracts bits from position `high` down to position `low` (both inclusive).
4231 - high: int - the highest bit position to extract (0-indexed from right)
4232 - low: int - the lowest bit position to extract (0-indexed from right)
4233 - bitvector: BitVecRef - the bit-vector to extract from
4234 Returns a new bit-vector containing bits [high:low]
4235
4236 **Sequence extraction**: Extract(sequence, offset, length)
4237 Extracts a subsequence starting at the given offset with the specified length.
4238 The functions SubString and SubSeq are redirected to this form of Extract.
4239 - sequence: SeqRef or str - the sequence to extract from
4240 - offset: int - the starting position (0-indexed)
4241 - length: int - the number of elements to extract
4242 Returns a new sequence containing the extracted subsequence
4243
4244 >>> # Bit-vector extraction examples
4245 >>> x = BitVec('x', 8)
4246 >>> Extract(6, 2, x) # Extract bits 6 down to 2 (5 bits total)
4247 Extract(6, 2, x)
4248 >>> Extract(6, 2, x).sort() # Result is a 5-bit vector
4249 BitVec(5)
4250 >>> Extract(7, 0, x) # Extract all 8 bits
4251 Extract(7, 0, x)
4252 >>> Extract(3, 3, x) # Extract single bit at position 3
4253 Extract(3, 3, x)
4254
4255 >>> # Sequence extraction examples
4256 >>> s = StringVal("hello")
4257 >>> Extract(s, 1, 3) # Extract 3 characters starting at position 1
4258 str.substr("hello", 1, 3)
4259 >>> simplify(Extract(StringVal("abcd"), 2, 1)) # Extract 1 character at position 2
4260 "c"
4261 >>> simplify(Extract(StringVal("abcd"), 0, 2)) # Extract first 2 characters
4262 "ab"
4263 """
4264 if isinstance(high, str):
4265 high = StringVal(high)
4266 if is_seq(high):
4267 s = high
4268 offset, length = _coerce_exprs(low, a, s.ctx)
4269 return SeqRef(Z3_mk_seq_extract(s.ctx_ref(), s.as_ast(), offset.as_ast(), length.as_ast()), s.ctx)
4270 if z3_debug():
4271 _z3_assert(low <= high, "First argument must be greater than or equal to second argument")
4272 _z3_assert(_is_int(high) and high >= 0 and _is_int(low) and low >= 0,
4273 "First and second arguments must be non negative integers")
4274 _z3_assert(is_bv(a), "Third argument must be a Z3 bit-vector expression")
4275 return BitVecRef(Z3_mk_extract(a.ctx_ref(), high, low, a.as_ast()), a.ctx)
4276
4277
4279 if z3_debug():
4280 _z3_assert(is_bv(a) or is_bv(b), "First or second argument must be a Z3 bit-vector expression")
4281
4282
4283def ULE(a, b):
4284 """Create the Z3 expression (unsigned) `other <= self`.
4285
4286 Use the operator <= for signed less than or equal to.
4287
4288 >>> x, y = BitVecs('x y', 32)
4289 >>> ULE(x, y)
4290 ULE(x, y)
4291 >>> (x <= y).sexpr()
4292 '(bvsle x y)'
4293 >>> ULE(x, y).sexpr()
4294 '(bvule x y)'
4295 """
4296 _check_bv_args(a, b)
4297 a, b = _coerce_exprs(a, b)
4298 return BoolRef(Z3_mk_bvule(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)
4299
4300
4301def ULT(a, b):
4302 """Create the Z3 expression (unsigned) `other < self`.
4303
4304 Use the operator < for signed less than.
4305
4306 >>> x, y = BitVecs('x y', 32)
4307 >>> ULT(x, y)
4308 ULT(x, y)
4309 >>> (x < y).sexpr()
4310 '(bvslt x y)'
4311 >>> ULT(x, y).sexpr()
4312 '(bvult x y)'
4313 """
4314 _check_bv_args(a, b)
4315 a, b = _coerce_exprs(a, b)
4316 return BoolRef(Z3_mk_bvult(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)
4317
4318
4319def UGE(a, b):
4320 """Create the Z3 expression (unsigned) `other >= self`.
4321
4322 Use the operator >= for signed greater than or equal to.
4323
4324 >>> x, y = BitVecs('x y', 32)
4325 >>> UGE(x, y)
4326 UGE(x, y)
4327 >>> (x >= y).sexpr()
4328 '(bvsge x y)'
4329 >>> UGE(x, y).sexpr()
4330 '(bvuge x y)'
4331 """
4332 _check_bv_args(a, b)
4333 a, b = _coerce_exprs(a, b)
4334 return BoolRef(Z3_mk_bvuge(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)
4335
4336
4337def UGT(a, b):
4338 """Create the Z3 expression (unsigned) `other > self`.
4339
4340 Use the operator > for signed greater than.
4341
4342 >>> x, y = BitVecs('x y', 32)
4343 >>> UGT(x, y)
4344 UGT(x, y)
4345 >>> (x > y).sexpr()
4346 '(bvsgt x y)'
4347 >>> UGT(x, y).sexpr()
4348 '(bvugt x y)'
4349 """
4350 _check_bv_args(a, b)
4351 a, b = _coerce_exprs(a, b)
4352 return BoolRef(Z3_mk_bvugt(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)
4353
4354
4355def UDiv(a, b):
4356 """Create the Z3 expression (unsigned) division `self / other`.
4357
4358 Use the operator / for signed division.
4359
4360 >>> x = BitVec('x', 32)
4361 >>> y = BitVec('y', 32)
4362 >>> UDiv(x, y)
4363 UDiv(x, y)
4364 >>> UDiv(x, y).sort()
4365 BitVec(32)
4366 >>> (x / y).sexpr()
4367 '(bvsdiv x y)'
4368 >>> UDiv(x, y).sexpr()
4369 '(bvudiv x y)'
4370 """
4371 _check_bv_args(a, b)
4372 a, b = _coerce_exprs(a, b)
4373 return BitVecRef(Z3_mk_bvudiv(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)
4374
4375
4376def URem(a, b):
4377 """Create the Z3 expression (unsigned) remainder `self % other`.
4378
4379 Use the operator % for signed modulus, and SRem() for signed remainder.
4380
4381 >>> x = BitVec('x', 32)
4382 >>> y = BitVec('y', 32)
4383 >>> URem(x, y)
4384 URem(x, y)
4385 >>> URem(x, y).sort()
4386 BitVec(32)
4387 >>> (x % y).sexpr()
4388 '(bvsmod x y)'
4389 >>> URem(x, y).sexpr()
4390 '(bvurem x y)'
4391 """
4392 _check_bv_args(a, b)
4393 a, b = _coerce_exprs(a, b)
4394 return BitVecRef(Z3_mk_bvurem(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)
4395
4396
4397def SRem(a, b):
4398 """Create the Z3 expression signed remainder.
4399
4400 Use the operator % for signed modulus, and URem() for unsigned remainder.
4401
4402 >>> x = BitVec('x', 32)
4403 >>> y = BitVec('y', 32)
4404 >>> SRem(x, y)
4405 SRem(x, y)
4406 >>> SRem(x, y).sort()
4407 BitVec(32)
4408 >>> (x % y).sexpr()
4409 '(bvsmod x y)'
4410 >>> SRem(x, y).sexpr()
4411 '(bvsrem x y)'
4412 """
4413 _check_bv_args(a, b)
4414 a, b = _coerce_exprs(a, b)
4415 return BitVecRef(Z3_mk_bvsrem(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)
4416
4417
4418def LShR(a, b):
4419 """Create the Z3 expression logical right shift.
4420
4421 Use the operator >> for the arithmetical right shift.
4422
4423 >>> x, y = BitVecs('x y', 32)
4424 >>> LShR(x, y)
4425 LShR(x, y)
4426 >>> (x >> y).sexpr()
4427 '(bvashr x y)'
4428 >>> LShR(x, y).sexpr()
4429 '(bvlshr x y)'
4430 >>> BitVecVal(4, 3)
4431 4
4432 >>> BitVecVal(4, 3).as_signed_long()
4433 -4
4434 >>> simplify(BitVecVal(4, 3) >> 1).as_signed_long()
4435 -2
4436 >>> simplify(BitVecVal(4, 3) >> 1)
4437 6
4438 >>> simplify(LShR(BitVecVal(4, 3), 1))
4439 2
4440 >>> simplify(BitVecVal(2, 3) >> 1)
4441 1
4442 >>> simplify(LShR(BitVecVal(2, 3), 1))
4443 1
4444 """
4445 _check_bv_args(a, b)
4446 a, b = _coerce_exprs(a, b)
4447 return BitVecRef(Z3_mk_bvlshr(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)
4448
4449
4450def RotateLeft(a, b):
4451 """Return an expression representing `a` rotated to the left `b` times.
4452
4453 >>> a, b = BitVecs('a b', 16)
4454 >>> RotateLeft(a, b)
4455 RotateLeft(a, b)
4456 >>> simplify(RotateLeft(a, 0))
4457 a
4458 >>> simplify(RotateLeft(a, 16))
4459 a
4460 """
4461 _check_bv_args(a, b)
4462 a, b = _coerce_exprs(a, b)
4463 return BitVecRef(Z3_mk_ext_rotate_left(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)
4464
4465
4466def RotateRight(a, b):
4467 """Return an expression representing `a` rotated to the right `b` times.
4468
4469 >>> a, b = BitVecs('a b', 16)
4470 >>> RotateRight(a, b)
4471 RotateRight(a, b)
4472 >>> simplify(RotateRight(a, 0))
4473 a
4474 >>> simplify(RotateRight(a, 16))
4475 a
4476 """
4477 _check_bv_args(a, b)
4478 a, b = _coerce_exprs(a, b)
4479 return BitVecRef(Z3_mk_ext_rotate_right(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)
4480
4481
4482def SignExt(n, a):
4483 """Return a bit-vector expression with `n` extra sign-bits.
4484
4485 >>> x = BitVec('x', 16)
4486 >>> n = SignExt(8, x)
4487 >>> n.size()
4488 24
4489 >>> n
4490 SignExt(8, x)
4491 >>> n.sort()
4492 BitVec(24)
4493 >>> v0 = BitVecVal(2, 2)
4494 >>> v0
4495 2
4496 >>> v0.size()
4497 2
4498 >>> v = simplify(SignExt(6, v0))
4499 >>> v
4500 254
4501 >>> v.size()
4502 8
4503 >>> print("%.x" % v.as_long())
4504 fe
4505 """
4506 if z3_debug():
4507 _z3_assert(_is_int(n), "First argument must be an integer")
4508 _z3_assert(is_bv(a), "Second argument must be a Z3 bit-vector expression")
4509 return BitVecRef(Z3_mk_sign_ext(a.ctx_ref(), n, a.as_ast()), a.ctx)
4510
4511
4512def ZeroExt(n, a):
4513 """Return a bit-vector expression with `n` extra zero-bits.
4514
4515 >>> x = BitVec('x', 16)
4516 >>> n = ZeroExt(8, x)
4517 >>> n.size()
4518 24
4519 >>> n
4520 ZeroExt(8, x)
4521 >>> n.sort()
4522 BitVec(24)
4523 >>> v0 = BitVecVal(2, 2)
4524 >>> v0
4525 2
4526 >>> v0.size()
4527 2
4528 >>> v = simplify(ZeroExt(6, v0))
4529 >>> v
4530 2
4531 >>> v.size()
4532 8
4533 """
4534 if z3_debug():
4535 _z3_assert(_is_int(n), "First argument must be an integer")
4536 _z3_assert(is_bv(a), "Second argument must be a Z3 bit-vector expression")
4537 return BitVecRef(Z3_mk_zero_ext(a.ctx_ref(), n, a.as_ast()), a.ctx)
4538
4539
4541 """Return an expression representing `n` copies of `a`.
4542
4543 >>> x = BitVec('x', 8)
4544 >>> n = RepeatBitVec(4, x)
4545 >>> n
4546 RepeatBitVec(4, x)
4547 >>> n.size()
4548 32
4549 >>> v0 = BitVecVal(10, 4)
4550 >>> print("%.x" % v0.as_long())
4551 a
4552 >>> v = simplify(RepeatBitVec(4, v0))
4553 >>> v.size()
4554 16
4555 >>> print("%.x" % v.as_long())
4556 aaaa
4557 """
4558 if z3_debug():
4559 _z3_assert(_is_int(n), "First argument must be an integer")
4560 _z3_assert(is_bv(a), "Second argument must be a Z3 bit-vector expression")
4561 return BitVecRef(Z3_mk_repeat(a.ctx_ref(), n, a.as_ast()), a.ctx)
4562
4563
4565 """Return the reduction-and expression of `a`."""
4566 if z3_debug():
4567 _z3_assert(is_bv(a), "First argument must be a Z3 bit-vector expression")
4568 return BitVecRef(Z3_mk_bvredand(a.ctx_ref(), a.as_ast()), a.ctx)
4569
4570
4571def BVRedOr(a):
4572 """Return the reduction-or expression of `a`."""
4573 if z3_debug():
4574 _z3_assert(is_bv(a), "First argument must be a Z3 bit-vector expression")
4575 return BitVecRef(Z3_mk_bvredor(a.ctx_ref(), a.as_ast()), a.ctx)
4576
4577
4578def BVAddNoOverflow(a, b, signed):
4579 """A predicate the determines that bit-vector addition does not overflow"""
4580 _check_bv_args(a, b)
4581 a, b = _coerce_exprs(a, b)
4582 return BoolRef(Z3_mk_bvadd_no_overflow(a.ctx_ref(), a.as_ast(), b.as_ast(), signed), a.ctx)
4583
4584
4586 """A predicate the determines that signed bit-vector addition does not underflow"""
4587 _check_bv_args(a, b)
4588 a, b = _coerce_exprs(a, b)
4589 return BoolRef(Z3_mk_bvadd_no_underflow(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)
4590
4591
4593 """A predicate the determines that bit-vector subtraction does not overflow"""
4594 _check_bv_args(a, b)
4595 a, b = _coerce_exprs(a, b)
4596 return BoolRef(Z3_mk_bvsub_no_overflow(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)
4597
4598
4599def BVSubNoUnderflow(a, b, signed):
4600 """A predicate the determines that bit-vector subtraction does not underflow"""
4601 _check_bv_args(a, b)
4602 a, b = _coerce_exprs(a, b)
4603 return BoolRef(Z3_mk_bvsub_no_underflow(a.ctx_ref(), a.as_ast(), b.as_ast(), signed), a.ctx)
4604
4605
4607 """A predicate the determines that bit-vector signed division does not overflow"""
4608 _check_bv_args(a, b)
4609 a, b = _coerce_exprs(a, b)
4610 return BoolRef(Z3_mk_bvsdiv_no_overflow(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)
4611
4612
4614 """A predicate the determines that bit-vector unary negation does not overflow"""
4615 if z3_debug():
4616 _z3_assert(is_bv(a), "First argument must be a Z3 bit-vector expression")
4617 return BoolRef(Z3_mk_bvneg_no_overflow(a.ctx_ref(), a.as_ast()), a.ctx)
4618
4619
4620def BVMulNoOverflow(a, b, signed):
4621 """A predicate the determines that bit-vector multiplication does not overflow"""
4622 _check_bv_args(a, b)
4623 a, b = _coerce_exprs(a, b)
4624 return BoolRef(Z3_mk_bvmul_no_overflow(a.ctx_ref(), a.as_ast(), b.as_ast(), signed), a.ctx)
4625
4626
4628 """A predicate the determines that bit-vector signed multiplication does not underflow"""
4629 _check_bv_args(a, b)
4630 a, b = _coerce_exprs(a, b)
4631 return BoolRef(Z3_mk_bvmul_no_underflow(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)
4632
4633
4634
4639
4641 """Array sorts."""
4642
4643 def domain(self):
4644 """Return the domain of the array sort `self`.
4645
4646 >>> A = ArraySort(IntSort(), BoolSort())
4647 >>> A.domain()
4648 Int
4649 """
4651
4652 def domain_n(self, i):
4653 """Return the domain of the array sort `self`.
4654 """
4655 return _to_sort_ref(Z3_get_array_sort_domain_n(self.ctx_ref(), self.ast, i), self.ctx)
4656
4657 def range(self):
4658 """Return the range of the array sort `self`.
4659
4660 >>> A = ArraySort(IntSort(), BoolSort())
4661 >>> A.range()
4662 Bool
4663 """
4664 return _to_sort_ref(Z3_get_array_sort_range(self.ctx_ref(), self.ast), self.ctx)
4665
4666
4668 """Array expressions. """
4669
4670 def sort(self):
4671 """Return the array sort of the array expression `self`.
4672
4673 >>> a = Array('a', IntSort(), BoolSort())
4674 >>> a.sort()
4675 Array(Int, Bool)
4676 """
4677 return ArraySortRef(Z3_get_sort(self.ctx_ref(), self.as_ast()), self.ctx)
4678
4679 def domain(self):
4680 """Shorthand for `self.sort().domain()`.
4681
4682 >>> a = Array('a', IntSort(), BoolSort())
4683 >>> a.domain()
4684 Int
4685 """
4686 return self.sort().domain()
4687
4688 def domain_n(self, i):
4689 """Shorthand for self.sort().domain_n(i)`."""
4690 return self.sort().domain_n(i)
4691
4692 def range(self):
4693 """Shorthand for `self.sort().range()`.
4694
4695 >>> a = Array('a', IntSort(), BoolSort())
4696 >>> a.range()
4697 Bool
4698 """
4699 return self.sort().range()
4700
4701 def __getitem__(self, arg):
4702 """Return the Z3 expression `self[arg]`.
4703
4704 >>> a = Array('a', IntSort(), BoolSort())
4705 >>> i = Int('i')
4706 >>> a[i]
4707 a[i]
4708 >>> a[i].sexpr()
4709 '(select a i)'
4710 """
4711 return _array_select(self, arg)
4712
4713 def default(self):
4714 return _to_expr_ref(Z3_mk_array_default(self.ctx_ref(), self.as_ast()), self.ctx)
4715
4716
4717def _array_select(ar, arg):
4718 if isinstance(arg, tuple):
4719 args = [ar.sort().domain_n(i).cast(arg[i]) for i in range(len(arg))]
4720 _args, sz = _to_ast_array(args)
4721 return _to_expr_ref(Z3_mk_select_n(ar.ctx_ref(), ar.as_ast(), sz, _args), ar.ctx)
4722 arg = ar.sort().domain().cast(arg)
4723 return _to_expr_ref(Z3_mk_select(ar.ctx_ref(), ar.as_ast(), arg.as_ast()), ar.ctx)
4724
4725
4727 return Z3_get_sort_kind(a.ctx.ref(), Z3_get_sort(a.ctx.ref(), a.ast)) == Z3_ARRAY_SORT
4728
4729
4730def is_array(a : Any) -> bool:
4731 """Return `True` if `a` is a Z3 array expression.
4732
4733 >>> a = Array('a', IntSort(), IntSort())
4734 >>> is_array(a)
4735 True
4736 >>> is_array(Store(a, 0, 1))
4737 True
4738 >>> is_array(a[0])
4739 False
4740 """
4741 return isinstance(a, ArrayRef)
4742
4743
4745 """Return `True` if `a` is a Z3 constant array.
4746
4747 >>> a = K(IntSort(), 10)
4748 >>> is_const_array(a)
4749 True
4750 >>> a = Array('a', IntSort(), IntSort())
4751 >>> is_const_array(a)
4752 False
4753 """
4754 return is_app_of(a, Z3_OP_CONST_ARRAY)
4755
4756
4757def is_K(a):
4758 """Return `True` if `a` is a Z3 constant array.
4759
4760 >>> a = K(IntSort(), 10)
4761 >>> is_K(a)
4762 True
4763 >>> a = Array('a', IntSort(), IntSort())
4764 >>> is_K(a)
4765 False
4766 """
4767 return is_app_of(a, Z3_OP_CONST_ARRAY)
4768
4769
4770def is_map(a):
4771 """Return `True` if `a` is a Z3 map array expression.
4772
4773 >>> f = Function('f', IntSort(), IntSort())
4774 >>> b = Array('b', IntSort(), IntSort())
4775 >>> a = Map(f, b)
4776 >>> a
4777 Map(f, b)
4778 >>> is_map(a)
4779 True
4780 >>> is_map(b)
4781 False
4782 """
4783 return is_app_of(a, Z3_OP_ARRAY_MAP)
4784
4785
4787 """Return `True` if `a` is a Z3 default array expression.
4788 >>> d = Default(K(IntSort(), 10))
4789 >>> is_default(d)
4790 True
4791 """
4792 return is_app_of(a, Z3_OP_ARRAY_DEFAULT)
4793
4794
4796 """Return the function declaration associated with a Z3 map array expression.
4797
4798 >>> f = Function('f', IntSort(), IntSort())
4799 >>> b = Array('b', IntSort(), IntSort())
4800 >>> a = Map(f, b)
4801 >>> eq(f, get_map_func(a))
4802 True
4803 >>> get_map_func(a)
4804 f
4805 >>> get_map_func(a)(0)
4806 f(0)
4807 """
4808 if z3_debug():
4809 _z3_assert(is_map(a), "Z3 array map expression expected.")
4810 return FuncDeclRef(
4812 a.ctx_ref(),
4813 Z3_get_decl_ast_parameter(a.ctx_ref(), a.decl().ast, 0),
4814 ),
4815 ctx=a.ctx,
4816 )
4817
4818
4819def ArraySort(*sig):
4820 """Return the Z3 array sort with the given domain and range sorts.
4821
4822 >>> A = ArraySort(IntSort(), BoolSort())
4823 >>> A
4824 Array(Int, Bool)
4825 >>> A.domain()
4826 Int
4827 >>> A.range()
4828 Bool
4829 >>> AA = ArraySort(IntSort(), A)
4830 >>> AA
4831 Array(Int, Array(Int, Bool))
4832 """
4833 sig = _get_args(sig)
4834 if z3_debug():
4835 _z3_assert(len(sig) > 1, "At least two arguments expected")
4836 arity = len(sig) - 1
4837 r = sig[arity]
4838 d = sig[0]
4839 if z3_debug():
4840 for s in sig:
4841 _z3_assert(is_sort(s), "Z3 sort expected")
4842 _z3_assert(s.ctx == r.ctx, "Context mismatch")
4843 ctx = d.ctx
4844 if len(sig) == 2:
4845 return ArraySortRef(Z3_mk_array_sort(ctx.ref(), d.ast, r.ast), ctx)
4846 dom = (Sort * arity)()
4847 for i in range(arity):
4848 dom[i] = sig[i].ast
4849 return ArraySortRef(Z3_mk_array_sort_n(ctx.ref(), arity, dom, r.ast), ctx)
4850
4851
4852def Array(name, *sorts):
4853 """Return an array constant named `name` with the given domain and range sorts.
4854
4855 >>> a = Array('a', IntSort(), IntSort())
4856 >>> a.sort()
4857 Array(Int, Int)
4858 >>> a[0]
4859 a[0]
4860 """
4861 s = ArraySort(sorts)
4862 ctx = s.ctx
4863 return ArrayRef(Z3_mk_const(ctx.ref(), to_symbol(name, ctx), s.ast), ctx)
4864
4865
4866def Update(a, *args):
4867 """Return a Z3 store array expression.
4868
4869 >>> a = Array('a', IntSort(), IntSort())
4870 >>> i, v = Ints('i v')
4871 >>> s = Update(a, i, v)
4872 >>> s.sort()
4873 Array(Int, Int)
4874 >>> prove(s[i] == v)
4875 proved
4876 >>> j = Int('j')
4877 >>> prove(Implies(i != j, s[j] == a[j]))
4878 proved
4879 """
4880 if z3_debug():
4881 _z3_assert(is_array_sort(a), "First argument must be a Z3 array expression")
4882 args = _get_args(args)
4883 ctx = a.ctx
4884 if len(args) <= 1:
4885 raise Z3Exception("array update requires index and value arguments")
4886 if len(args) == 2:
4887 i = args[0]
4888 v = args[1]
4889 i = a.sort().domain().cast(i)
4890 v = a.sort().range().cast(v)
4891 return _to_expr_ref(Z3_mk_store(ctx.ref(), a.as_ast(), i.as_ast(), v.as_ast()), ctx)
4892 v = a.sort().range().cast(args[-1])
4893 idxs = [a.sort().domain_n(i).cast(args[i]) for i in range(len(args)-1)]
4894 _args, sz = _to_ast_array(idxs)
4895 return _to_expr_ref(Z3_mk_store_n(ctx.ref(), a.as_ast(), sz, _args, v.as_ast()), ctx)
4896
4897
4898def Default(a):
4899 """ Return a default value for array expression.
4900 >>> b = K(IntSort(), 1)
4901 >>> prove(Default(b) == 1)
4902 proved
4903 """
4904 if z3_debug():
4905 _z3_assert(is_array_sort(a), "First argument must be a Z3 array expression")
4906 return a.default()
4907
4908
4909def Store(a, *args):
4910 """Return a Z3 store array expression.
4911
4912 >>> a = Array('a', IntSort(), IntSort())
4913 >>> i, v = Ints('i v')
4914 >>> s = Store(a, i, v)
4915 >>> s.sort()
4916 Array(Int, Int)
4917 >>> prove(s[i] == v)
4918 proved
4919 >>> j = Int('j')
4920 >>> prove(Implies(i != j, s[j] == a[j]))
4921 proved
4922 """
4923 return Update(a, args)
4924
4925
4926def Select(a, *args):
4927 """Return a Z3 select array expression.
4928
4929 >>> a = Array('a', IntSort(), IntSort())
4930 >>> i = Int('i')
4931 >>> Select(a, i)
4932 a[i]
4933 >>> eq(Select(a, i), a[i])
4934 True
4935 """
4936 args = _get_args(args)
4937 if z3_debug():
4938 _z3_assert(is_array_sort(a), "First argument must be a Z3 array expression")
4939 return a[args]
4940
4941
4942def Map(f, *args):
4943 """Return a Z3 map array expression.
4944
4945 >>> f = Function('f', IntSort(), IntSort(), IntSort())
4946 >>> a1 = Array('a1', IntSort(), IntSort())
4947 >>> a2 = Array('a2', IntSort(), IntSort())
4948 >>> b = Map(f, a1, a2)
4949 >>> b
4950 Map(f, a1, a2)
4951 >>> prove(b[0] == f(a1[0], a2[0]))
4952 proved
4953 """
4954 args = _get_args(args)
4955 if z3_debug():
4956 _z3_assert(len(args) > 0, "At least one Z3 array expression expected")
4957 _z3_assert(is_func_decl(f), "First argument must be a Z3 function declaration")
4958 _z3_assert(all([is_array(a) for a in args]), "Z3 array expected expected")
4959 _z3_assert(len(args) == f.arity(), "Number of arguments mismatch")
4960 _args, sz = _to_ast_array(args)
4961 ctx = f.ctx
4962 return ArrayRef(Z3_mk_map(ctx.ref(), f.ast, sz, _args), ctx)
4963
4964
4965def K(dom, v):
4966 """Return a Z3 constant array expression.
4967
4968 >>> a = K(IntSort(), 10)
4969 >>> a
4970 K(Int, 10)
4971 >>> a.sort()
4972 Array(Int, Int)
4973 >>> i = Int('i')
4974 >>> a[i]
4975 K(Int, 10)[i]
4976 >>> simplify(a[i])
4977 10
4978 """
4979 if z3_debug():
4980 _z3_assert(is_sort(dom), "Z3 sort expected")
4981 ctx = dom.ctx
4982 if not is_expr(v):
4983 v = _py2expr(v, ctx)
4984 return ArrayRef(Z3_mk_const_array(ctx.ref(), dom.ast, v.as_ast()), ctx)
4985
4986
4987def Ext(a, b):
4988 """Return extensionality index for one-dimensional arrays.
4989 >> a, b = Consts('a b', SetSort(IntSort()))
4990 >> Ext(a, b)
4991 Ext(a, b)
4992 """
4993 ctx = a.ctx
4994 if z3_debug():
4995 _z3_assert(is_array_sort(a) and (is_array(b) or b.is_lambda()), "arguments must be arrays")
4996 return _to_expr_ref(Z3_mk_array_ext(ctx.ref(), a.as_ast(), b.as_ast()), ctx)
4997
4998
4999def SetHasSize(a, k):
5000 ctx = a.ctx
5001 k = _py2expr(k, ctx)
5002 return _to_expr_ref(Z3_mk_set_has_size(ctx.ref(), a.as_ast(), k.as_ast()), ctx)
5003
5004
5006 """Return `True` if `a` is a Z3 array select application.
5007
5008 >>> a = Array('a', IntSort(), IntSort())
5009 >>> is_select(a)
5010 False
5011 >>> i = Int('i')
5012 >>> is_select(a[i])
5013 True
5014 """
5015 return is_app_of(a, Z3_OP_SELECT)
5016
5017
5019 """Return `True` if `a` is a Z3 array store application.
5020
5021 >>> a = Array('a', IntSort(), IntSort())
5022 >>> is_store(a)
5023 False
5024 >>> is_store(Store(a, 0, 1))
5025 True
5026 """
5027 return is_app_of(a, Z3_OP_STORE)
5028
5029
5034
5035
5036def SetSort(s):
5037 """ Create a set sort over element sort s"""
5038 return ArraySort(s, BoolSort())
5039
5040
5042 """Create the empty set
5043 >>> EmptySet(IntSort())
5044 K(Int, False)
5045 """
5046 ctx = s.ctx
5047 return ArrayRef(Z3_mk_empty_set(ctx.ref(), s.ast), ctx)
5048
5049
5050def FullSet(s):
5051 """Create the full set
5052 >>> FullSet(IntSort())
5053 K(Int, True)
5054 """
5055 ctx = s.ctx
5056 return ArrayRef(Z3_mk_full_set(ctx.ref(), s.ast), ctx)
5057
5058
5059def SetUnion(*args):
5060 """ Take the union of sets
5061 >>> a = Const('a', SetSort(IntSort()))
5062 >>> b = Const('b', SetSort(IntSort()))
5063 >>> SetUnion(a, b)
5064 union(a, b)
5065 """
5066 args = _get_args(args)
5067 ctx = _ctx_from_ast_arg_list(args)
5068 _args, sz = _to_ast_array(args)
5069 return ArrayRef(Z3_mk_set_union(ctx.ref(), sz, _args), ctx)
5070
5071
5072def SetIntersect(*args):
5073 """ Take the union of sets
5074 >>> a = Const('a', SetSort(IntSort()))
5075 >>> b = Const('b', SetSort(IntSort()))
5076 >>> SetIntersect(a, b)
5077 intersection(a, b)
5078 """
5079 args = _get_args(args)
5080 ctx = _ctx_from_ast_arg_list(args)
5081 _args, sz = _to_ast_array(args)
5082 return ArrayRef(Z3_mk_set_intersect(ctx.ref(), sz, _args), ctx)
5083
5084
5085def SetAdd(s, e):
5086 """ Add element e to set s
5087 >>> a = Const('a', SetSort(IntSort()))
5088 >>> SetAdd(a, 1)
5089 Store(a, 1, True)
5090 """
5091 ctx = _ctx_from_ast_arg_list([s, e])
5092 e = _py2expr(e, ctx)
5093 return ArrayRef(Z3_mk_set_add(ctx.ref(), s.as_ast(), e.as_ast()), ctx)
5094
5095
5096def SetDel(s, e):
5097 """ Remove element e to set s
5098 >>> a = Const('a', SetSort(IntSort()))
5099 >>> SetDel(a, 1)
5100 Store(a, 1, False)
5101 """
5102 ctx = _ctx_from_ast_arg_list([s, e])
5103 e = _py2expr(e, ctx)
5104 return ArrayRef(Z3_mk_set_del(ctx.ref(), s.as_ast(), e.as_ast()), ctx)
5105
5106
5108 """ The complement of set s
5109 >>> a = Const('a', SetSort(IntSort()))
5110 >>> SetComplement(a)
5111 complement(a)
5112 """
5113 ctx = s.ctx
5114 return ArrayRef(Z3_mk_set_complement(ctx.ref(), s.as_ast()), ctx)
5115
5116
5118 """ The set difference of a and b
5119 >>> a = Const('a', SetSort(IntSort()))
5120 >>> b = Const('b', SetSort(IntSort()))
5121 >>> SetDifference(a, b)
5122 setminus(a, b)
5123 """
5124 ctx = _ctx_from_ast_arg_list([a, b])
5125 return ArrayRef(Z3_mk_set_difference(ctx.ref(), a.as_ast(), b.as_ast()), ctx)
5126
5127
5128def IsMember(e, s):
5129 """ Check if e is a member of set s
5130 >>> a = Const('a', SetSort(IntSort()))
5131 >>> IsMember(1, a)
5132 a[1]
5133 """
5134 ctx = _ctx_from_ast_arg_list([s, e])
5135 e = _py2expr(e, ctx)
5136 return BoolRef(Z3_mk_set_member(ctx.ref(), e.as_ast(), s.as_ast()), ctx)
5137
5138
5139def IsSubset(a, b):
5140 """ Check if a is a subset of b
5141 >>> a = Const('a', SetSort(IntSort()))
5142 >>> b = Const('b', SetSort(IntSort()))
5143 >>> IsSubset(a, b)
5144 subset(a, b)
5145 """
5146 ctx = _ctx_from_ast_arg_list([a, b])
5147 return BoolRef(Z3_mk_set_subset(ctx.ref(), a.as_ast(), b.as_ast()), ctx)
5148
5149
5150
5155
5157 """Return `True` if acc is pair of the form (String, Datatype or Sort). """
5158 if not isinstance(acc, tuple):
5159 return False
5160 if len(acc) != 2:
5161 return False
5162 return isinstance(acc[0], str) and (isinstance(acc[1], Datatype) or is_sort(acc[1]))
5163
5164
5166 """Helper class for declaring Z3 datatypes.
5167
5168 >>> List = Datatype('List')
5169 >>> List.declare('cons', ('car', IntSort()), ('cdr', List))
5170 >>> List.declare('nil')
5171 >>> List = List.create()
5172 >>> # List is now a Z3 declaration
5173 >>> List.nil
5174 nil
5175 >>> List.cons(10, List.nil)
5176 cons(10, nil)
5177 >>> List.cons(10, List.nil).sort()
5178 List
5179 >>> cons = List.cons
5180 >>> nil = List.nil
5181 >>> car = List.car
5182 >>> cdr = List.cdr
5183 >>> n = cons(1, cons(0, nil))
5184 >>> n
5185 cons(1, cons(0, nil))
5186 >>> simplify(cdr(n))
5187 cons(0, nil)
5188 >>> simplify(car(n))
5189 1
5190 """
5191
5192 def __init__(self, name, ctx=None):
5193 self.ctx = _get_ctx(ctx)
5194 self.name = name
5196
5197 def __deepcopy__(self, memo={}):
5198 r = Datatype(self.name, self.ctx)
5199 r.constructors = copy.deepcopy(self.constructors)
5200 return r
5201
5202 def declare_core(self, name, rec_name, *args):
5203 if z3_debug():
5204 _z3_assert(isinstance(name, str), "String expected")
5205 _z3_assert(isinstance(rec_name, str), "String expected")
5206 _z3_assert(
5207 all([_valid_accessor(a) for a in args]),
5208 "Valid list of accessors expected. An accessor is a pair of the form (String, Datatype|Sort)",
5209 )
5210 self.constructors.append((name, rec_name, args))
5211
5212 def declare(self, name, *args):
5213 """Declare constructor named `name` with the given accessors `args`.
5214 Each accessor is a pair `(name, sort)`, where `name` is a string and `sort` a Z3 sort
5215 or a reference to the datatypes being declared.
5216
5217 In the following example `List.declare('cons', ('car', IntSort()), ('cdr', List))`
5218 declares the constructor named `cons` that builds a new List using an integer and a List.
5219 It also declares the accessors `car` and `cdr`. The accessor `car` extracts the integer
5220 of a `cons` cell, and `cdr` the list of a `cons` cell. After all constructors were declared,
5221 we use the method create() to create the actual datatype in Z3.
5222
5223 >>> List = Datatype('List')
5224 >>> List.declare('cons', ('car', IntSort()), ('cdr', List))
5225 >>> List.declare('nil')
5226 >>> List = List.create()
5227 """
5228 if z3_debug():
5229 _z3_assert(isinstance(name, str), "String expected")
5230 _z3_assert(name != "", "Constructor name cannot be empty")
5231 return self.declare_core(name, "is-" + name, *args)
5232
5233 def __repr__(self):
5234 return "Datatype(%s, %s)" % (self.name, self.constructors)
5235
5236 def create(self):
5237 """Create a Z3 datatype based on the constructors declared using the method `declare()`.
5238
5239 The function `CreateDatatypes()` must be used to define mutually recursive datatypes.
5240
5241 >>> List = Datatype('List')
5242 >>> List.declare('cons', ('car', IntSort()), ('cdr', List))
5243 >>> List.declare('nil')
5244 >>> List = List.create()
5245 >>> List.nil
5246 nil
5247 >>> List.cons(10, List.nil)
5248 cons(10, nil)
5249 """
5250 return CreateDatatypes([self])[0]
5251
5252
5254 """Auxiliary object used to create Z3 datatypes."""
5255
5256 def __init__(self, c, ctx):
5257 self.c = c
5258 self.ctx = ctx
5259
5260 def __del__(self):
5261 if self.ctx.ref() is not None and Z3_del_constructor is not None:
5262 Z3_del_constructor(self.ctx.ref(), self.c)
5263
5264
5266 """Auxiliary object used to create Z3 datatypes."""
5267
5268 def __init__(self, c, ctx):
5269 self.c = c
5270 self.ctx = ctx
5271
5272 def __del__(self):
5273 if self.ctx.ref() is not None and Z3_del_constructor_list is not None:
5274 Z3_del_constructor_list(self.ctx.ref(), self.c)
5275
5276
5278 """Create mutually recursive Z3 datatypes using 1 or more Datatype helper objects.
5279
5280 In the following example we define a Tree-List using two mutually recursive datatypes.
5281
5282 >>> TreeList = Datatype('TreeList')
5283 >>> Tree = Datatype('Tree')
5284 >>> # Tree has two constructors: leaf and node
5285 >>> Tree.declare('leaf', ('val', IntSort()))
5286 >>> # a node contains a list of trees
5287 >>> Tree.declare('node', ('children', TreeList))
5288 >>> TreeList.declare('nil')
5289 >>> TreeList.declare('cons', ('car', Tree), ('cdr', TreeList))
5290 >>> Tree, TreeList = CreateDatatypes(Tree, TreeList)
5291 >>> Tree.val(Tree.leaf(10))
5292 val(leaf(10))
5293 >>> simplify(Tree.val(Tree.leaf(10)))
5294 10
5295 >>> n1 = Tree.node(TreeList.cons(Tree.leaf(10), TreeList.cons(Tree.leaf(20), TreeList.nil)))
5296 >>> n1
5297 node(cons(leaf(10), cons(leaf(20), nil)))
5298 >>> n2 = Tree.node(TreeList.cons(n1, TreeList.nil))
5299 >>> simplify(n2 == n1)
5300 False
5301 >>> simplify(TreeList.car(Tree.children(n2)) == n1)
5302 True
5303 """
5304 ds = _get_args(ds)
5305 if z3_debug():
5306 _z3_assert(len(ds) > 0, "At least one Datatype must be specified")
5307 _z3_assert(all([isinstance(d, Datatype) for d in ds]), "Arguments must be Datatypes")
5308 _z3_assert(all([d.ctx == ds[0].ctx for d in ds]), "Context mismatch")
5309 _z3_assert(all([d.constructors != [] for d in ds]), "Non-empty Datatypes expected")
5310 ctx = ds[0].ctx
5311 num = len(ds)
5312 names = (Symbol * num)()
5313 out = (Sort * num)()
5314 clists = (ConstructorList * num)()
5315 to_delete = []
5316 for i in range(num):
5317 d = ds[i]
5318 names[i] = to_symbol(d.name, ctx)
5319 num_cs = len(d.constructors)
5320 cs = (Constructor * num_cs)()
5321 for j in range(num_cs):
5322 c = d.constructors[j]
5323 cname = to_symbol(c[0], ctx)
5324 rname = to_symbol(c[1], ctx)
5325 fs = c[2]
5326 num_fs = len(fs)
5327 fnames = (Symbol * num_fs)()
5328 sorts = (Sort * num_fs)()
5329 refs = (ctypes.c_uint * num_fs)()
5330 for k in range(num_fs):
5331 fname = fs[k][0]
5332 ftype = fs[k][1]
5333 fnames[k] = to_symbol(fname, ctx)
5334 if isinstance(ftype, Datatype):
5335 if z3_debug():
5336 _z3_assert(
5337 ds.count(ftype) == 1,
5338 "One and only one occurrence of each datatype is expected",
5339 )
5340 sorts[k] = None
5341 refs[k] = ds.index(ftype)
5342 else:
5343 if z3_debug():
5344 _z3_assert(is_sort(ftype), "Z3 sort expected")
5345 sorts[k] = ftype.ast
5346 refs[k] = 0
5347 cs[j] = Z3_mk_constructor(ctx.ref(), cname, rname, num_fs, fnames, sorts, refs)
5348 to_delete.append(ScopedConstructor(cs[j], ctx))
5349 clists[i] = Z3_mk_constructor_list(ctx.ref(), num_cs, cs)
5350 to_delete.append(ScopedConstructorList(clists[i], ctx))
5351 Z3_mk_datatypes(ctx.ref(), num, names, out, clists)
5352 result = []
5353 # Create a field for every constructor, recognizer and accessor
5354 for i in range(num):
5355 dref = DatatypeSortRef(out[i], ctx)
5356 num_cs = dref.num_constructors()
5357 for j in range(num_cs):
5358 cref = dref.constructor(j)
5359 cref_name = cref.name()
5360 cref_arity = cref.arity()
5361 if cref.arity() == 0:
5362 cref = cref()
5363 setattr(dref, cref_name, cref)
5364 rref = dref.recognizer(j)
5365 setattr(dref, "is_" + cref_name, rref)
5366 for k in range(cref_arity):
5367 aref = dref.accessor(j, k)
5368 setattr(dref, aref.name(), aref)
5369 result.append(dref)
5370 return tuple(result)
5371
5372
5374 """Datatype sorts."""
5375
5377 """Return the number of constructors in the given Z3 datatype.
5378
5379 >>> List = Datatype('List')
5380 >>> List.declare('cons', ('car', IntSort()), ('cdr', List))
5381 >>> List.declare('nil')
5382 >>> List = List.create()
5383 >>> # List is now a Z3 declaration
5384 >>> List.num_constructors()
5385 2
5386 """
5388
5389 def constructor(self, idx):
5390 """Return a constructor of the datatype `self`.
5391
5392 >>> List = Datatype('List')
5393 >>> List.declare('cons', ('car', IntSort()), ('cdr', List))
5394 >>> List.declare('nil')
5395 >>> List = List.create()
5396 >>> # List is now a Z3 declaration
5397 >>> List.num_constructors()
5398 2
5399 >>> List.constructor(0)
5400 cons
5401 >>> List.constructor(1)
5402 nil
5403 """
5404 if z3_debug():
5405 _z3_assert(idx < self.num_constructors(), "Invalid constructor index")
5407
5408 def recognizer(self, idx):
5409 """In Z3, each constructor has an associated recognizer predicate.
5410
5411 If the constructor is named `name`, then the recognizer `is_name`.
5412
5413 >>> List = Datatype('List')
5414 >>> List.declare('cons', ('car', IntSort()), ('cdr', List))
5415 >>> List.declare('nil')
5416 >>> List = List.create()
5417 >>> # List is now a Z3 declaration
5418 >>> List.num_constructors()
5419 2
5420 >>> List.recognizer(0)
5421 is(cons)
5422 >>> List.recognizer(1)
5423 is(nil)
5424 >>> simplify(List.is_nil(List.cons(10, List.nil)))
5425 False
5426 >>> simplify(List.is_cons(List.cons(10, List.nil)))
5427 True
5428 >>> l = Const('l', List)
5429 >>> simplify(List.is_cons(l))
5430 is(cons, l)
5431 """
5432 if z3_debug():
5433 _z3_assert(idx < self.num_constructors(), "Invalid recognizer index")
5434 return FuncDeclRef(Z3_get_datatype_sort_recognizer(self.ctx_ref(), self.ast, idx), self.ctx)
5435
5436 def accessor(self, i, j):
5437 """In Z3, each constructor has 0 or more accessor.
5438 The number of accessors is equal to the arity of the constructor.
5439
5440 >>> List = Datatype('List')
5441 >>> List.declare('cons', ('car', IntSort()), ('cdr', List))
5442 >>> List.declare('nil')
5443 >>> List = List.create()
5444 >>> List.num_constructors()
5445 2
5446 >>> List.constructor(0)
5447 cons
5448 >>> num_accs = List.constructor(0).arity()
5449 >>> num_accs
5450 2
5451 >>> List.accessor(0, 0)
5452 car
5453 >>> List.accessor(0, 1)
5454 cdr
5455 >>> List.constructor(1)
5456 nil
5457 >>> num_accs = List.constructor(1).arity()
5458 >>> num_accs
5459 0
5460 """
5461 if z3_debug():
5462 _z3_assert(i < self.num_constructors(), "Invalid constructor index")
5463 _z3_assert(j < self.constructor(i).arity(), "Invalid accessor index")
5464 return FuncDeclRef(
5466 ctx=self.ctx,
5467 )
5468
5469
5471 """Datatype expressions."""
5472
5473 def sort(self):
5474 """Return the datatype sort of the datatype expression `self`."""
5475 return DatatypeSortRef(Z3_get_sort(self.ctx_ref(), self.as_ast()), self.ctx)
5476
5477def DatatypeSort(name, ctx = None):
5478 """Create a reference to a sort that was declared, or will be declared, as a recursive datatype"""
5479 ctx = _get_ctx(ctx)
5480 return DatatypeSortRef(Z3_mk_datatype_sort(ctx.ref(), to_symbol(name, ctx)), ctx)
5481
5482def TupleSort(name, sorts, ctx=None):
5483 """Create a named tuple sort base on a set of underlying sorts
5484 Example:
5485 >>> pair, mk_pair, (first, second) = TupleSort("pair", [IntSort(), StringSort()])
5486 """
5487 tuple = Datatype(name, ctx)
5488 projects = [("project%d" % i, sorts[i]) for i in range(len(sorts))]
5489 tuple.declare(name, *projects)
5490 tuple = tuple.create()
5491 return tuple, tuple.constructor(0), [tuple.accessor(0, i) for i in range(len(sorts))]
5492
5493
5494def DisjointSum(name, sorts, ctx=None):
5495 """Create a named tagged union sort base on a set of underlying sorts
5496 Example:
5497 >>> sum, ((inject0, extract0), (inject1, extract1)) = DisjointSum("+", [IntSort(), StringSort()])
5498 """
5499 sum = Datatype(name, ctx)
5500 for i in range(len(sorts)):
5501 sum.declare("inject%d" % i, ("project%d" % i, sorts[i]))
5502 sum = sum.create()
5503 return sum, [(sum.constructor(i), sum.accessor(i, 0)) for i in range(len(sorts))]
5504
5505
5506def EnumSort(name, values, ctx=None):
5507 """Return a new enumeration sort named `name` containing the given values.
5508
5509 The result is a pair (sort, list of constants).
5510 Example:
5511 >>> Color, (red, green, blue) = EnumSort('Color', ['red', 'green', 'blue'])
5512 """
5513 if z3_debug():
5514 _z3_assert(isinstance(name, str), "Name must be a string")
5515 _z3_assert(all([isinstance(v, str) for v in values]), "Enumeration sort values must be strings")
5516 _z3_assert(len(values) > 0, "At least one value expected")
5517 ctx = _get_ctx(ctx)
5518 num = len(values)
5519 _val_names = (Symbol * num)()
5520 for i in range(num):
5521 _val_names[i] = to_symbol(values[i], ctx)
5522 _values = (FuncDecl * num)()
5523 _testers = (FuncDecl * num)()
5524 name = to_symbol(name, ctx)
5525 S = DatatypeSortRef(Z3_mk_enumeration_sort(ctx.ref(), name, num, _val_names, _values, _testers), ctx)
5526 V = []
5527 for i in range(num):
5528 V.append(FuncDeclRef(_values[i], ctx))
5529 V = [a() for a in V]
5530 return S, V
5531
5532
5537
5538
5540 """Set of parameters used to configure Solvers, Tactics and Simplifiers in Z3.
5541
5542 Consider using the function `args2params` to create instances of this object.
5543 """
5544
5545 def __init__(self, ctx=None, params=None):
5546 self.ctx = _get_ctx(ctx)
5547 if params is None:
5548 self.params = Z3_mk_params(self.ctx.ref())
5549 else:
5550 self.params = params
5551 Z3_params_inc_ref(self.ctx.ref(), self.params)
5552
5553 def __deepcopy__(self, memo={}):
5554 return ParamsRef(self.ctx, self.params)
5555
5556 def __del__(self):
5557 if self.ctx.ref() is not None and Z3_params_dec_ref is not None:
5558 Z3_params_dec_ref(self.ctx.ref(), self.params)
5559
5560 def set(self, name, val):
5561 """Set parameter name with value val."""
5562 if z3_debug():
5563 _z3_assert(isinstance(name, str), "parameter name must be a string")
5564 name_sym = to_symbol(name, self.ctx)
5565 if isinstance(val, bool):
5566 Z3_params_set_bool(self.ctx.ref(), self.params, name_sym, val)
5567 elif _is_int(val):
5568 Z3_params_set_uint(self.ctx.ref(), self.params, name_sym, val)
5569 elif isinstance(val, float):
5570 Z3_params_set_double(self.ctx.ref(), self.params, name_sym, val)
5571 elif isinstance(val, str):
5572 Z3_params_set_symbol(self.ctx.ref(), self.params, name_sym, to_symbol(val, self.ctx))
5573 else:
5574 if z3_debug():
5575 _z3_assert(False, "invalid parameter value")
5576
5577 def __repr__(self):
5578 return Z3_params_to_string(self.ctx.ref(), self.params)
5579
5580 def validate(self, ds):
5581 _z3_assert(isinstance(ds, ParamDescrsRef), "parameter description set expected")
5582 Z3_params_validate(self.ctx.ref(), self.params, ds.descr)
5583
5584
5585def args2params(arguments, keywords, ctx=None):
5586 """Convert python arguments into a Z3_params object.
5587 A ':' is added to the keywords, and '_' is replaced with '-'
5588
5589 >>> args2params(['model', True, 'relevancy', 2], {'elim_and' : True})
5590 (params model true relevancy 2 elim_and true)
5591 """
5592 if z3_debug():
5593 _z3_assert(len(arguments) % 2 == 0, "Argument list must have an even number of elements.")
5594 prev = None
5595 r = ParamsRef(ctx)
5596 for a in arguments:
5597 if prev is None:
5598 prev = a
5599 else:
5600 r.set(prev, a)
5601 prev = None
5602 for k in keywords:
5603 v = keywords[k]
5604 r.set(k, v)
5605 return r
5606
5607
5609 """Set of parameter descriptions for Solvers, Tactics and Simplifiers in Z3.
5610 """
5611
5612 def __init__(self, descr, ctx=None):
5613 _z3_assert(isinstance(descr, ParamDescrs), "parameter description object expected")
5614 self.ctx = _get_ctx(ctx)
5615 self.descr = descr
5616 Z3_param_descrs_inc_ref(self.ctx.ref(), self.descr)
5617
5618 def __deepcopy__(self, memo={}):
5619 return ParamsDescrsRef(self.descr, self.ctx)
5620
5621 def __del__(self):
5622 if self.ctx.ref() is not None and Z3_param_descrs_dec_ref is not None:
5623 Z3_param_descrs_dec_ref(self.ctx.ref(), self.descr)
5624
5625 def size(self):
5626 """Return the size of in the parameter description `self`.
5627 """
5628 return int(Z3_param_descrs_size(self.ctx.ref(), self.descr))
5629
5630 def __len__(self):
5631 """Return the size of in the parameter description `self`.
5632 """
5633 return self.size()
5634
5635 def get_name(self, i):
5636 """Return the i-th parameter name in the parameter description `self`.
5637 """
5638 return _symbol2py(self.ctx, Z3_param_descrs_get_name(self.ctx.ref(), self.descr, i))
5639
5640 def get_kind(self, n):
5641 """Return the kind of the parameter named `n`.
5642 """
5643 return Z3_param_descrs_get_kind(self.ctx.ref(), self.descr, to_symbol(n, self.ctx))
5644
5645 def get_documentation(self, n):
5646 """Return the documentation string of the parameter named `n`.
5647 """
5648 return Z3_param_descrs_get_documentation(self.ctx.ref(), self.descr, to_symbol(n, self.ctx))
5649
5650 def __getitem__(self, arg):
5651 if _is_int(arg):
5652 return self.get_name(arg)
5653 else:
5654 return self.get_kind(arg)
5655
5656 def __repr__(self):
5657 return Z3_param_descrs_to_string(self.ctx.ref(), self.descr)
5658
5659
5664
5665
5667 """Goal is a collection of constraints we want to find a solution or show to be unsatisfiable (infeasible).
5668
5669 Goals are processed using Tactics. A Tactic transforms a goal into a set of subgoals.
5670 A goal has a solution if one of its subgoals has a solution.
5671 A goal is unsatisfiable if all subgoals are unsatisfiable.
5672 """
5673
5674 def __init__(self, models=True, unsat_cores=False, proofs=False, ctx=None, goal=None):
5675 if z3_debug():
5676 _z3_assert(goal is None or ctx is not None,
5677 "If goal is different from None, then ctx must be also different from None")
5678 self.ctx = _get_ctx(ctx)
5679 self.goal = goal
5680 if self.goal is None:
5681 self.goal = Z3_mk_goal(self.ctx.ref(), models, unsat_cores, proofs)
5682 Z3_goal_inc_ref(self.ctx.ref(), self.goal)
5683
5684 def __del__(self):
5685 if self.goal is not None and self.ctx.ref() is not None and Z3_goal_dec_ref is not None:
5686 Z3_goal_dec_ref(self.ctx.ref(), self.goal)
5687
5688 def depth(self):
5689 """Return the depth of the goal `self`.
5690 The depth corresponds to the number of tactics applied to `self`.
5691
5692 >>> x, y = Ints('x y')
5693 >>> g = Goal()
5694 >>> g.add(x == 0, y >= x + 1)
5695 >>> g.depth()
5696 0
5697 >>> r = Then('simplify', 'solve-eqs')(g)
5698 >>> # r has 1 subgoal
5699 >>> len(r)
5700 1
5701 >>> r[0].depth()
5702 2
5703 """
5704 return int(Z3_goal_depth(self.ctx.ref(), self.goal))
5705
5706 def inconsistent(self):
5707 """Return `True` if `self` contains the `False` constraints.
5708
5709 >>> x, y = Ints('x y')
5710 >>> g = Goal()
5711 >>> g.inconsistent()
5712 False
5713 >>> g.add(x == 0, x == 1)
5714 >>> g
5715 [x == 0, x == 1]
5716 >>> g.inconsistent()
5717 False
5718 >>> g2 = Tactic('propagate-values')(g)[0]
5719 >>> g2.inconsistent()
5720 True
5721 """
5722 return Z3_goal_inconsistent(self.ctx.ref(), self.goal)
5723
5724 def prec(self):
5725 """Return the precision (under-approximation, over-approximation, or precise) of the goal `self`.
5726
5727 >>> g = Goal()
5728 >>> g.prec() == Z3_GOAL_PRECISE
5729 True
5730 >>> x, y = Ints('x y')
5731 >>> g.add(x == y + 1)
5732 >>> g.prec() == Z3_GOAL_PRECISE
5733 True
5734 >>> t = With(Tactic('add-bounds'), add_bound_lower=0, add_bound_upper=10)
5735 >>> g2 = t(g)[0]
5736 >>> g2
5737 [x == y + 1, x <= 10, x >= 0, y <= 10, y >= 0]
5738 >>> g2.prec() == Z3_GOAL_PRECISE
5739 False
5740 >>> g2.prec() == Z3_GOAL_UNDER
5741 True
5742 """
5743 return Z3_goal_precision(self.ctx.ref(), self.goal)
5744
5745 def precision(self):
5746 """Alias for `prec()`.
5747
5748 >>> g = Goal()
5749 >>> g.precision() == Z3_GOAL_PRECISE
5750 True
5751 """
5752 return self.prec()
5753
5754 def size(self):
5755 """Return the number of constraints in the goal `self`.
5756
5757 >>> g = Goal()
5758 >>> g.size()
5759 0
5760 >>> x, y = Ints('x y')
5761 >>> g.add(x == 0, y > x)
5762 >>> g.size()
5763 2
5764 """
5765 return int(Z3_goal_size(self.ctx.ref(), self.goal))
5766
5767 def __len__(self):
5768 """Return the number of constraints in the goal `self`.
5769
5770 >>> g = Goal()
5771 >>> len(g)
5772 0
5773 >>> x, y = Ints('x y')
5774 >>> g.add(x == 0, y > x)
5775 >>> len(g)
5776 2
5777 """
5778 return self.size()
5779
5780 def get(self, i):
5781 """Return a constraint in the goal `self`.
5782
5783 >>> g = Goal()
5784 >>> x, y = Ints('x y')
5785 >>> g.add(x == 0, y > x)
5786 >>> g.get(0)
5787 x == 0
5788 >>> g.get(1)
5789 y > x
5790 """
5791 return _to_expr_ref(Z3_goal_formula(self.ctx.ref(), self.goal, i), self.ctx)
5792
5793 def __getitem__(self, arg):
5794 """Return a constraint in the goal `self`.
5795
5796 >>> g = Goal()
5797 >>> x, y = Ints('x y')
5798 >>> g.add(x == 0, y > x)
5799 >>> g[0]
5800 x == 0
5801 >>> g[1]
5802 y > x
5803 """
5804 if arg >= len(self):
5805 raise IndexError
5806 return self.get(arg)
5807
5808 def assert_exprs(self, *args):
5809 """Assert constraints into the goal.
5810
5811 >>> x = Int('x')
5812 >>> g = Goal()
5813 >>> g.assert_exprs(x > 0, x < 2)
5814 >>> g
5815 [x > 0, x < 2]
5816 """
5817 args = _get_args(args)
5818 s = BoolSort(self.ctx)
5819 for arg in args:
5820 arg = s.cast(arg)
5821 Z3_goal_assert(self.ctx.ref(), self.goal, arg.as_ast())
5822
5823 def append(self, *args):
5824 """Add constraints.
5825
5826 >>> x = Int('x')
5827 >>> g = Goal()
5828 >>> g.append(x > 0, x < 2)
5829 >>> g
5830 [x > 0, x < 2]
5831 """
5832 self.assert_exprs(*args)
5833
5834 def insert(self, *args):
5835 """Add constraints.
5836
5837 >>> x = Int('x')
5838 >>> g = Goal()
5839 >>> g.insert(x > 0, x < 2)
5840 >>> g
5841 [x > 0, x < 2]
5842 """
5843 self.assert_exprs(*args)
5844
5845 def add(self, *args):
5846 """Add constraints.
5847
5848 >>> x = Int('x')
5849 >>> g = Goal()
5850 >>> g.add(x > 0, x < 2)
5851 >>> g
5852 [x > 0, x < 2]
5853 """
5854 self.assert_exprs(*args)
5855
5856 def convert_model(self, model):
5857 """Retrieve model from a satisfiable goal
5858 >>> a, b = Ints('a b')
5859 >>> g = Goal()
5860 >>> g.add(Or(a == 0, a == 1), Or(b == 0, b == 1), a > b)
5861 >>> t = Then(Tactic('split-clause'), Tactic('solve-eqs'))
5862 >>> r = t(g)
5863 >>> r[0]
5864 [Or(b == 0, b == 1), Not(0 <= b)]
5865 >>> r[1]
5866 [Or(b == 0, b == 1), Not(1 <= b)]
5867 >>> # Remark: the subgoal r[0] is unsatisfiable
5868 >>> # Creating a solver for solving the second subgoal
5869 >>> s = Solver()
5870 >>> s.add(r[1])
5871 >>> s.check()
5872 sat
5873 >>> s.model()
5874 [b = 0]
5875 >>> # Model s.model() does not assign a value to `a`
5876 >>> # It is a model for subgoal `r[1]`, but not for goal `g`
5877 >>> # The method convert_model creates a model for `g` from a model for `r[1]`.
5878 >>> r[1].convert_model(s.model())
5879 [b = 0, a = 1]
5880 """
5881 if z3_debug():
5882 _z3_assert(isinstance(model, ModelRef), "Z3 Model expected")
5883 return ModelRef(Z3_goal_convert_model(self.ctx.ref(), self.goal, model.model), self.ctx)
5884
5885 def __repr__(self):
5886 return obj_to_string(self)
5887
5888 def sexpr(self):
5889 """Return a textual representation of the s-expression representing the goal."""
5890 return Z3_goal_to_string(self.ctx.ref(), self.goal)
5891
5892 def dimacs(self, include_names=True):
5893 """Return a textual representation of the goal in DIMACS format."""
5894 return Z3_goal_to_dimacs_string(self.ctx.ref(), self.goal, include_names)
5895
5896 def translate(self, target):
5897 """Copy goal `self` to context `target`.
5898
5899 >>> x = Int('x')
5900 >>> g = Goal()
5901 >>> g.add(x > 10)
5902 >>> g
5903 [x > 10]
5904 >>> c2 = Context()
5905 >>> g2 = g.translate(c2)
5906 >>> g2
5907 [x > 10]
5908 >>> g.ctx == main_ctx()
5909 True
5910 >>> g2.ctx == c2
5911 True
5912 >>> g2.ctx == main_ctx()
5913 False
5914 """
5915 if z3_debug():
5916 _z3_assert(isinstance(target, Context), "target must be a context")
5917 return Goal(goal=Z3_goal_translate(self.ctx.ref(), self.goal, target.ref()), ctx=target)
5918
5919 def __copy__(self):
5920 return self.translate(self.ctx)
5921
5922 def __deepcopy__(self, memo={}):
5923 return self.translate(self.ctx)
5924
5925 def simplify(self, *arguments, **keywords):
5926 """Return a new simplified goal.
5927
5928 This method is essentially invoking the simplify tactic.
5929
5930 >>> g = Goal()
5931 >>> x = Int('x')
5932 >>> g.add(x + 1 >= 2)
5933 >>> g
5934 [x + 1 >= 2]
5935 >>> g2 = g.simplify()
5936 >>> g2
5937 [x >= 1]
5938 >>> # g was not modified
5939 >>> g
5940 [x + 1 >= 2]
5941 """
5942 t = Tactic("simplify")
5943 return t.apply(self, *arguments, **keywords)[0]
5944
5945 def as_expr(self):
5946 """Return goal `self` as a single Z3 expression.
5947
5948 >>> x = Int('x')
5949 >>> g = Goal()
5950 >>> g.as_expr()
5951 True
5952 >>> g.add(x > 1)
5953 >>> g.as_expr()
5954 x > 1
5955 >>> g.add(x < 10)
5956 >>> g.as_expr()
5957 And(x > 1, x < 10)
5958 """
5959 sz = len(self)
5960 if sz == 0:
5961 return BoolVal(True, self.ctx)
5962 elif sz == 1:
5963 return self.get(0)
5964 else:
5965 return And([self.get(i) for i in range(len(self))], self.ctx)
5966
5967
5972
5973
5975 """A collection (vector) of ASTs."""
5976
5977 def __init__(self, v=None, ctx=None):
5978 self.vector = None
5979 if v is None:
5980 self.ctx = _get_ctx(ctx)
5981 self.vector = Z3_mk_ast_vector(self.ctx.ref())
5982 else:
5983 self.vector = v
5984 assert ctx is not None
5985 self.ctx = ctx
5986 Z3_ast_vector_inc_ref(self.ctx.ref(), self.vector)
5987
5988 def __del__(self):
5989 if self.vector is not None and self.ctx.ref() is not None and Z3_ast_vector_dec_ref is not None:
5990 Z3_ast_vector_dec_ref(self.ctx.ref(), self.vector)
5991
5992 def __len__(self):
5993 """Return the size of the vector `self`.
5994
5995 >>> A = AstVector()
5996 >>> len(A)
5997 0
5998 >>> A.push(Int('x'))
5999 >>> A.push(Int('x'))
6000 >>> len(A)
6001 2
6002 """
6003 return int(Z3_ast_vector_size(self.ctx.ref(), self.vector))
6004
6005 def __getitem__(self, i):
6006 """Return the AST at position `i`.
6007
6008 >>> A = AstVector()
6009 >>> A.push(Int('x') + 1)
6010 >>> A.push(Int('y'))
6011 >>> A[0]
6012 x + 1
6013 >>> A[1]
6014 y
6015 """
6016
6017 if isinstance(i, int):
6018 if i < 0:
6019 i += self.__len__()
6020
6021 if i >= self.__len__():
6022 raise IndexError
6023 return _to_ast_ref(Z3_ast_vector_get(self.ctx.ref(), self.vector, i), self.ctx)
6024
6025 elif isinstance(i, slice):
6026 result = []
6027 for ii in range(*i.indices(self.__len__())):
6028 result.append(_to_ast_ref(
6029 Z3_ast_vector_get(self.ctx.ref(), self.vector, ii),
6030 self.ctx,
6031 ))
6032 return result
6033
6034 def __setitem__(self, i, v):
6035 """Update AST at position `i`.
6036
6037 >>> A = AstVector()
6038 >>> A.push(Int('x') + 1)
6039 >>> A.push(Int('y'))
6040 >>> A[0]
6041 x + 1
6042 >>> A[0] = Int('x')
6043 >>> A[0]
6044 x
6045 """
6046 if i >= self.__len__():
6047 raise IndexError
6048 Z3_ast_vector_set(self.ctx.ref(), self.vector, i, v.as_ast())
6049
6050 def push(self, v):
6051 """Add `v` in the end of the vector.
6052
6053 >>> A = AstVector()
6054 >>> len(A)
6055 0
6056 >>> A.push(Int('x'))
6057 >>> len(A)
6058 1
6059 """
6060 Z3_ast_vector_push(self.ctx.ref(), self.vector, v.as_ast())
6061
6062 def resize(self, sz):
6063 """Resize the vector to `sz` elements.
6064
6065 >>> A = AstVector()
6066 >>> A.resize(10)
6067 >>> len(A)
6068 10
6069 >>> for i in range(10): A[i] = Int('x')
6070 >>> A[5]
6071 x
6072 """
6073 Z3_ast_vector_resize(self.ctx.ref(), self.vector, sz)
6074
6075 def __contains__(self, item):
6076 """Return `True` if the vector contains `item`.
6077
6078 >>> x = Int('x')
6079 >>> A = AstVector()
6080 >>> x in A
6081 False
6082 >>> A.push(x)
6083 >>> x in A
6084 True
6085 >>> (x+1) in A
6086 False
6087 >>> A.push(x+1)
6088 >>> (x+1) in A
6089 True
6090 >>> A
6091 [x, x + 1]
6092 """
6093 for elem in self:
6094 if elem.eq(item):
6095 return True
6096 return False
6097
6098 def translate(self, other_ctx):
6099 """Copy vector `self` to context `other_ctx`.
6100
6101 >>> x = Int('x')
6102 >>> A = AstVector()
6103 >>> A.push(x)
6104 >>> c2 = Context()
6105 >>> B = A.translate(c2)
6106 >>> B
6107 [x]
6108 """
6109 return AstVector(
6110 Z3_ast_vector_translate(self.ctx.ref(), self.vector, other_ctx.ref()),
6111 ctx=other_ctx,
6112 )
6113
6114 def __copy__(self):
6115 return self.translate(self.ctx)
6116
6117 def __deepcopy__(self, memo={}):
6118 return self.translate(self.ctx)
6119
6120 def __repr__(self):
6121 return obj_to_string(self)
6122
6123 def sexpr(self):
6124 """Return a textual representation of the s-expression representing the vector."""
6125 return Z3_ast_vector_to_string(self.ctx.ref(), self.vector)
6126
6127
6132
6133
6135 """A mapping from ASTs to ASTs."""
6136
6137 def __init__(self, m=None, ctx=None):
6138 self.map = None
6139 if m is None:
6140 self.ctx = _get_ctx(ctx)
6141 self.map = Z3_mk_ast_map(self.ctx.ref())
6142 else:
6143 self.map = m
6144 assert ctx is not None
6145 self.ctx = ctx
6146 Z3_ast_map_inc_ref(self.ctx.ref(), self.map)
6147
6148 def __deepcopy__(self, memo={}):
6149 return AstMap(self.map, self.ctx)
6150
6151 def __del__(self):
6152 if self.map is not None and self.ctx.ref() is not None and Z3_ast_map_dec_ref is not None:
6153 Z3_ast_map_dec_ref(self.ctx.ref(), self.map)
6154
6155 def __len__(self):
6156 """Return the size of the map.
6157
6158 >>> M = AstMap()
6159 >>> len(M)
6160 0
6161 >>> x = Int('x')
6162 >>> M[x] = IntVal(1)
6163 >>> len(M)
6164 1
6165 """
6166 return int(Z3_ast_map_size(self.ctx.ref(), self.map))
6167
6168 def __contains__(self, key):
6169 """Return `True` if the map contains key `key`.
6170
6171 >>> M = AstMap()
6172 >>> x = Int('x')
6173 >>> M[x] = x + 1
6174 >>> x in M
6175 True
6176 >>> x+1 in M
6177 False
6178 """
6179 return Z3_ast_map_contains(self.ctx.ref(), self.map, key.as_ast())
6180
6181 def __getitem__(self, key):
6182 """Retrieve the value associated with key `key`.
6183
6184 >>> M = AstMap()
6185 >>> x = Int('x')
6186 >>> M[x] = x + 1
6187 >>> M[x]
6188 x + 1
6189 """
6190 return _to_ast_ref(Z3_ast_map_find(self.ctx.ref(), self.map, key.as_ast()), self.ctx)
6191
6192 def __setitem__(self, k, v):
6193 """Add/Update key `k` with value `v`.
6194
6195 >>> M = AstMap()
6196 >>> x = Int('x')
6197 >>> M[x] = x + 1
6198 >>> len(M)
6199 1
6200 >>> M[x]
6201 x + 1
6202 >>> M[x] = IntVal(1)
6203 >>> M[x]
6204 1
6205 """
6206 Z3_ast_map_insert(self.ctx.ref(), self.map, k.as_ast(), v.as_ast())
6207
6208 def __repr__(self):
6209 return Z3_ast_map_to_string(self.ctx.ref(), self.map)
6210
6211 def erase(self, k):
6212 """Remove the entry associated with key `k`.
6213
6214 >>> M = AstMap()
6215 >>> x = Int('x')
6216 >>> M[x] = x + 1
6217 >>> len(M)
6218 1
6219 >>> M.erase(x)
6220 >>> len(M)
6221 0
6222 """
6223 Z3_ast_map_erase(self.ctx.ref(), self.map, k.as_ast())
6224
6225 def reset(self):
6226 """Remove all entries from the map.
6227
6228 >>> M = AstMap()
6229 >>> x = Int('x')
6230 >>> M[x] = x + 1
6231 >>> M[x+x] = IntVal(1)
6232 >>> len(M)
6233 2
6234 >>> M.reset()
6235 >>> len(M)
6236 0
6237 """
6238 Z3_ast_map_reset(self.ctx.ref(), self.map)
6239
6240 def keys(self):
6241 """Return an AstVector containing all keys in the map.
6242
6243 >>> M = AstMap()
6244 >>> x = Int('x')
6245 >>> M[x] = x + 1
6246 >>> M[x+x] = IntVal(1)
6247 >>> M.keys()
6248 [x, x + x]
6249 """
6250 return AstVector(Z3_ast_map_keys(self.ctx.ref(), self.map), self.ctx)
6251
6252
6257
6258
6260 """Store the value of the interpretation of a function in a particular point."""
6261
6262 def __init__(self, entry, ctx):
6263 self.entry = entry
6264 self.ctx = ctx
6265 Z3_func_entry_inc_ref(self.ctx.ref(), self.entry)
6266
6267 def __deepcopy__(self, memo={}):
6268 return FuncEntry(self.entry, self.ctx)
6269
6270 def __del__(self):
6271 if self.ctx.ref() is not None and Z3_func_entry_dec_ref is not None:
6272 Z3_func_entry_dec_ref(self.ctx.ref(), self.entry)
6273
6274 def num_args(self):
6275 """Return the number of arguments in the given entry.
6276
6277 >>> f = Function('f', IntSort(), IntSort(), IntSort())
6278 >>> s = Solver()
6279 >>> s.add(f(0, 1) == 10, f(1, 2) == 20, f(1, 0) == 10)
6280 >>> s.check()
6281 sat
6282 >>> m = s.model()
6283 >>> f_i = m[f]
6284 >>> f_i.num_entries()
6285 1
6286 >>> e = f_i.entry(0)
6287 >>> e.num_args()
6288 2
6289 """
6290 return int(Z3_func_entry_get_num_args(self.ctx.ref(), self.entry))
6291
6292 def arg_value(self, idx):
6293 """Return the value of argument `idx`.
6294
6295 >>> f = Function('f', IntSort(), IntSort(), IntSort())
6296 >>> s = Solver()
6297 >>> s.add(f(0, 1) == 10, f(1, 2) == 20, f(1, 0) == 10)
6298 >>> s.check()
6299 sat
6300 >>> m = s.model()
6301 >>> f_i = m[f]
6302 >>> f_i.num_entries()
6303 1
6304 >>> e = f_i.entry(0)
6305 >>> e
6306 [1, 2, 20]
6307 >>> e.num_args()
6308 2
6309 >>> e.arg_value(0)
6310 1
6311 >>> e.arg_value(1)
6312 2
6313 >>> try:
6314 ... e.arg_value(2)
6315 ... except IndexError:
6316 ... print("index error")
6317 index error
6318 """
6319 if idx >= self.num_args():
6320 raise IndexError
6321 return _to_expr_ref(Z3_func_entry_get_arg(self.ctx.ref(), self.entry, idx), self.ctx)
6322
6323 def value(self):
6324 """Return the value of the function at point `self`.
6325
6326 >>> f = Function('f', IntSort(), IntSort(), IntSort())
6327 >>> s = Solver()
6328 >>> s.add(f(0, 1) == 10, f(1, 2) == 20, f(1, 0) == 10)
6329 >>> s.check()
6330 sat
6331 >>> m = s.model()
6332 >>> f_i = m[f]
6333 >>> f_i.num_entries()
6334 1
6335 >>> e = f_i.entry(0)
6336 >>> e
6337 [1, 2, 20]
6338 >>> e.num_args()
6339 2
6340 >>> e.value()
6341 20
6342 """
6343 return _to_expr_ref(Z3_func_entry_get_value(self.ctx.ref(), self.entry), self.ctx)
6344
6345 def as_list(self):
6346 """Return entry `self` as a Python list.
6347 >>> f = Function('f', IntSort(), IntSort(), IntSort())
6348 >>> s = Solver()
6349 >>> s.add(f(0, 1) == 10, f(1, 2) == 20, f(1, 0) == 10)
6350 >>> s.check()
6351 sat
6352 >>> m = s.model()
6353 >>> f_i = m[f]
6354 >>> f_i.num_entries()
6355 1
6356 >>> e = f_i.entry(0)
6357 >>> e.as_list()
6358 [1, 2, 20]
6359 """
6360 args = [self.arg_value(i) for i in range(self.num_args())]
6361 args.append(self.value())
6362 return args
6363
6364 def __repr__(self):
6365 return repr(self.as_list())
6366
6367
6369 """Stores the interpretation of a function in a Z3 model."""
6370
6371 def __init__(self, f, ctx):
6372 self.f = f
6373 self.ctx = ctx
6374 if self.f is not None:
6375 Z3_func_interp_inc_ref(self.ctx.ref(), self.f)
6376
6377 def __del__(self):
6378 if self.f is not None and self.ctx.ref() is not None and Z3_func_interp_dec_ref is not None:
6379 Z3_func_interp_dec_ref(self.ctx.ref(), self.f)
6380
6381 def else_value(self):
6382 """
6383 Return the `else` value for a function interpretation.
6384 Return None if Z3 did not specify the `else` value for
6385 this object.
6386
6387 >>> f = Function('f', IntSort(), IntSort())
6388 >>> s = Solver()
6389 >>> s.add(f(0) == 1, f(1) == 1, f(2) == 0)
6390 >>> s.check()
6391 sat
6392 >>> m = s.model()
6393 >>> m[f]
6394 [2 -> 0, else -> 1]
6395 >>> m[f].else_value()
6396 1
6397 """
6398 r = Z3_func_interp_get_else(self.ctx.ref(), self.f)
6399 if r:
6400 return _to_expr_ref(r, self.ctx)
6401 else:
6402 return None
6403
6404 def num_entries(self):
6405 """Return the number of entries/points in the function interpretation `self`.
6406
6407 >>> f = Function('f', IntSort(), IntSort())
6408 >>> s = Solver()
6409 >>> s.add(f(0) == 1, f(1) == 1, f(2) == 0)
6410 >>> s.check()
6411 sat
6412 >>> m = s.model()
6413 >>> m[f]
6414 [2 -> 0, else -> 1]
6415 >>> m[f].num_entries()
6416 1
6417 """
6418 return int(Z3_func_interp_get_num_entries(self.ctx.ref(), self.f))
6419
6420 def arity(self):
6421 """Return the number of arguments for each entry in the function interpretation `self`.
6422
6423 >>> f = Function('f', IntSort(), IntSort())
6424 >>> s = Solver()
6425 >>> s.add(f(0) == 1, f(1) == 1, f(2) == 0)
6426 >>> s.check()
6427 sat
6428 >>> m = s.model()
6429 >>> m[f].arity()
6430 1
6431 """
6432 return int(Z3_func_interp_get_arity(self.ctx.ref(), self.f))
6433
6434 def entry(self, idx):
6435 """Return an entry at position `idx < self.num_entries()` in the function interpretation `self`.
6436
6437 >>> f = Function('f', IntSort(), IntSort())
6438 >>> s = Solver()
6439 >>> s.add(f(0) == 1, f(1) == 1, f(2) == 0)
6440 >>> s.check()
6441 sat
6442 >>> m = s.model()
6443 >>> m[f]
6444 [2 -> 0, else -> 1]
6445 >>> m[f].num_entries()
6446 1
6447 >>> m[f].entry(0)
6448 [2, 0]
6449 """
6450 if idx >= self.num_entries():
6451 raise IndexError
6452 return FuncEntry(Z3_func_interp_get_entry(self.ctx.ref(), self.f, idx), self.ctx)
6453
6454 def translate(self, other_ctx):
6455 """Copy model 'self' to context 'other_ctx'.
6456 """
6457 return ModelRef(Z3_model_translate(self.ctx.ref(), self.model, other_ctx.ref()), other_ctx)
6458
6459 def __copy__(self):
6460 return self.translate(self.ctx)
6461
6462 def __deepcopy__(self, memo={}):
6463 return self.translate(self.ctx)
6464
6465 def as_list(self):
6466 """Return the function interpretation as a Python list.
6467 >>> f = Function('f', IntSort(), IntSort())
6468 >>> s = Solver()
6469 >>> s.add(f(0) == 1, f(1) == 1, f(2) == 0)
6470 >>> s.check()
6471 sat
6472 >>> m = s.model()
6473 >>> m[f]
6474 [2 -> 0, else -> 1]
6475 >>> m[f].as_list()
6476 [[2, 0], 1]
6477 """
6478 r = [self.entry(i).as_list() for i in range(self.num_entries())]
6479 r.append(self.else_value())
6480 return r
6481
6482 def __repr__(self):
6483 return obj_to_string(self)
6484
6485
6487 """Model/Solution of a satisfiability problem (aka system of constraints)."""
6488
6489 def __init__(self, m, ctx):
6490 assert ctx is not None
6491 self.model = m
6492 self.ctx = ctx
6493 Z3_model_inc_ref(self.ctx.ref(), self.model)
6494
6495 def __del__(self):
6496 if self.ctx.ref() is not None and Z3_model_dec_ref is not None:
6497 Z3_model_dec_ref(self.ctx.ref(), self.model)
6498
6499 def __repr__(self):
6500 return obj_to_string(self)
6501
6502 def sexpr(self):
6503 """Return a textual representation of the s-expression representing the model."""
6504 return Z3_model_to_string(self.ctx.ref(), self.model)
6505
6506 def eval(self, t, model_completion=False):
6507 """Evaluate the expression `t` in the model `self`.
6508 If `model_completion` is enabled, then a default interpretation is automatically added
6509 for symbols that do not have an interpretation in the model `self`.
6510
6511 >>> x = Int('x')
6512 >>> s = Solver()
6513 >>> s.add(x > 0, x < 2)
6514 >>> s.check()
6515 sat
6516 >>> m = s.model()
6517 >>> m.eval(x + 1)
6518 2
6519 >>> m.eval(x == 1)
6520 True
6521 >>> y = Int('y')
6522 >>> m.eval(y + x)
6523 1 + y
6524 >>> m.eval(y)
6525 y
6526 >>> m.eval(y, model_completion=True)
6527 0
6528 >>> # Now, m contains an interpretation for y
6529 >>> m.eval(y + x)
6530 1
6531 """
6532 r = (Ast * 1)()
6533 if Z3_model_eval(self.ctx.ref(), self.model, t.as_ast(), model_completion, r):
6534 return _to_expr_ref(r[0], self.ctx)
6535 raise Z3Exception("failed to evaluate expression in the model")
6536
6537 def evaluate(self, t, model_completion=False):
6538 """Alias for `eval`.
6539
6540 >>> x = Int('x')
6541 >>> s = Solver()
6542 >>> s.add(x > 0, x < 2)
6543 >>> s.check()
6544 sat
6545 >>> m = s.model()
6546 >>> m.evaluate(x + 1)
6547 2
6548 >>> m.evaluate(x == 1)
6549 True
6550 >>> y = Int('y')
6551 >>> m.evaluate(y + x)
6552 1 + y
6553 >>> m.evaluate(y)
6554 y
6555 >>> m.evaluate(y, model_completion=True)
6556 0
6557 >>> # Now, m contains an interpretation for y
6558 >>> m.evaluate(y + x)
6559 1
6560 """
6561 return self.eval(t, model_completion)
6562
6563 def __len__(self):
6564 """Return the number of constant and function declarations in the model `self`.
6565
6566 >>> f = Function('f', IntSort(), IntSort())
6567 >>> x = Int('x')
6568 >>> s = Solver()
6569 >>> s.add(x > 0, f(x) != x)
6570 >>> s.check()
6571 sat
6572 >>> m = s.model()
6573 >>> len(m)
6574 2
6575 """
6576 num_consts = int(Z3_model_get_num_consts(self.ctx.ref(), self.model))
6577 num_funcs = int(Z3_model_get_num_funcs(self.ctx.ref(), self.model))
6578 return num_consts + num_funcs
6579
6580 def get_interp(self, decl):
6581 """Return the interpretation for a given declaration or constant.
6582
6583 >>> f = Function('f', IntSort(), IntSort())
6584 >>> x = Int('x')
6585 >>> s = Solver()
6586 >>> s.add(x > 0, x < 2, f(x) == 0)
6587 >>> s.check()
6588 sat
6589 >>> m = s.model()
6590 >>> m[x]
6591 1
6592 >>> m[f]
6593 [else -> 0]
6594 """
6595 if z3_debug():
6596 _z3_assert(isinstance(decl, FuncDeclRef) or is_const(decl), "Z3 declaration expected")
6597 if is_const(decl):
6598 decl = decl.decl()
6599 try:
6600 if decl.arity() == 0:
6601 _r = Z3_model_get_const_interp(self.ctx.ref(), self.model, decl.ast)
6602 if _r.value is None:
6603 return None
6604 r = _to_expr_ref(_r, self.ctx)
6605 if is_as_array(r):
6606 fi = self.get_interp(get_as_array_func(r))
6607 if fi is None:
6608 return fi
6609 e = fi.else_value()
6610 if e is None:
6611 return fi
6612 if fi.arity() != 1:
6613 return fi
6614 srt = decl.range()
6615 dom = srt.domain()
6616 e = K(dom, e)
6617 i = 0
6618 sz = fi.num_entries()
6619 n = fi.arity()
6620 while i < sz:
6621 fe = fi.entry(i)
6622 e = Store(e, fe.arg_value(0), fe.value())
6623 i += 1
6624 return e
6625 else:
6626 return r
6627 else:
6628 return FuncInterp(Z3_model_get_func_interp(self.ctx.ref(), self.model, decl.ast), self.ctx)
6629 except Z3Exception:
6630 return None
6631
6632 def num_sorts(self):
6633 """Return the number of uninterpreted sorts that contain an interpretation in the model `self`.
6634
6635 >>> A = DeclareSort('A')
6636 >>> a, b = Consts('a b', A)
6637 >>> s = Solver()
6638 >>> s.add(a != b)
6639 >>> s.check()
6640 sat
6641 >>> m = s.model()
6642 >>> m.num_sorts()
6643 1
6644 """
6645 return int(Z3_model_get_num_sorts(self.ctx.ref(), self.model))
6646
6647 def get_sort(self, idx):
6648 """Return the uninterpreted sort at position `idx` < self.num_sorts().
6649
6650 >>> A = DeclareSort('A')
6651 >>> B = DeclareSort('B')
6652 >>> a1, a2 = Consts('a1 a2', A)
6653 >>> b1, b2 = Consts('b1 b2', B)
6654 >>> s = Solver()
6655 >>> s.add(a1 != a2, b1 != b2)
6656 >>> s.check()
6657 sat
6658 >>> m = s.model()
6659 >>> m.num_sorts()
6660 2
6661 >>> m.get_sort(0)
6662 A
6663 >>> m.get_sort(1)
6664 B
6665 """
6666 if idx >= self.num_sorts():
6667 raise IndexError
6668 return _to_sort_ref(Z3_model_get_sort(self.ctx.ref(), self.model, idx), self.ctx)
6669
6670 def sorts(self):
6671 """Return all uninterpreted sorts that have an interpretation in the model `self`.
6672
6673 >>> A = DeclareSort('A')
6674 >>> B = DeclareSort('B')
6675 >>> a1, a2 = Consts('a1 a2', A)
6676 >>> b1, b2 = Consts('b1 b2', B)
6677 >>> s = Solver()
6678 >>> s.add(a1 != a2, b1 != b2)
6679 >>> s.check()
6680 sat
6681 >>> m = s.model()
6682 >>> m.sorts()
6683 [A, B]
6684 """
6685 return [self.get_sort(i) for i in range(self.num_sorts())]
6686
6687 def get_universe(self, s):
6688 """Return the interpretation for the uninterpreted sort `s` in the model `self`.
6689
6690 >>> A = DeclareSort('A')
6691 >>> a, b = Consts('a b', A)
6692 >>> s = Solver()
6693 >>> s.add(a != b)
6694 >>> s.check()
6695 sat
6696 >>> m = s.model()
6697 >>> m.get_universe(A)
6698 [A!val!1, A!val!0]
6699 """
6700 if z3_debug():
6701 _z3_assert(isinstance(s, SortRef), "Z3 sort expected")
6702 try:
6703 return AstVector(Z3_model_get_sort_universe(self.ctx.ref(), self.model, s.ast), self.ctx)
6704 except Z3Exception:
6705 return None
6706
6707 def __getitem__(self, idx):
6708 """If `idx` is an integer, then the declaration at position `idx` in the model `self` is returned.
6709 If `idx` is a declaration, then the actual interpretation is returned.
6710
6711 The elements can be retrieved using position or the actual declaration.
6712
6713 >>> f = Function('f', IntSort(), IntSort())
6714 >>> x = Int('x')
6715 >>> s = Solver()
6716 >>> s.add(x > 0, x < 2, f(x) == 0)
6717 >>> s.check()
6718 sat
6719 >>> m = s.model()
6720 >>> len(m)
6721 2
6722 >>> m[0]
6723 x
6724 >>> m[1]
6725 f
6726 >>> m[x]
6727 1
6728 >>> m[f]
6729 [else -> 0]
6730 >>> for d in m: print("%s -> %s" % (d, m[d]))
6731 x -> 1
6732 f -> [else -> 0]
6733 """
6734 if _is_int(idx):
6735 if idx >= len(self):
6736 raise IndexError
6737 num_consts = Z3_model_get_num_consts(self.ctx.ref(), self.model)
6738 if (idx < num_consts):
6739 return FuncDeclRef(Z3_model_get_const_decl(self.ctx.ref(), self.model, idx), self.ctx)
6740 else:
6741 return FuncDeclRef(Z3_model_get_func_decl(self.ctx.ref(), self.model, idx - num_consts), self.ctx)
6742 if isinstance(idx, FuncDeclRef):
6743 return self.get_interp(idx)
6744 if is_const(idx):
6745 return self.get_interp(idx.decl())
6746 if isinstance(idx, SortRef):
6747 return self.get_universe(idx)
6748 if z3_debug():
6749 _z3_assert(False, "Integer, Z3 declaration, or Z3 constant expected")
6750 return None
6751
6752 def decls(self):
6753 """Return a list with all symbols that have an interpretation in the model `self`.
6754 >>> f = Function('f', IntSort(), IntSort())
6755 >>> x = Int('x')
6756 >>> s = Solver()
6757 >>> s.add(x > 0, x < 2, f(x) == 0)
6758 >>> s.check()
6759 sat
6760 >>> m = s.model()
6761 >>> m.decls()
6762 [x, f]
6763 """
6764 r = []
6765 for i in range(Z3_model_get_num_consts(self.ctx.ref(), self.model)):
6766 r.append(FuncDeclRef(Z3_model_get_const_decl(self.ctx.ref(), self.model, i), self.ctx))
6767 for i in range(Z3_model_get_num_funcs(self.ctx.ref(), self.model)):
6768 r.append(FuncDeclRef(Z3_model_get_func_decl(self.ctx.ref(), self.model, i), self.ctx))
6769 return r
6770
6771 def update_value(self, x, value):
6772 """Update the interpretation of a constant"""
6773 if is_expr(x):
6774 x = x.decl()
6775 if is_func_decl(x) and x.arity() != 0 and isinstance(value, FuncInterp):
6776 fi1 = value.f
6777 fi2 = Z3_add_func_interp(x.ctx_ref(), self.model, x.ast, value.else_value().ast);
6778 fi2 = FuncInterp(fi2, x.ctx)
6779 for i in range(value.num_entries()):
6780 e = value.entry(i)
6781 n = Z3_func_entry_get_num_args(x.ctx_ref(), e.entry)
6782 v = AstVector()
6783 for j in range(n):
6784 v.push(e.arg_value(j))
6785 val = Z3_func_entry_get_value(x.ctx_ref(), e.entry)
6786 Z3_func_interp_add_entry(x.ctx_ref(), fi2.f, v.vector, val)
6787 return
6788 if not is_func_decl(x) or x.arity() != 0:
6789 raise Z3Exception("Expecting 0-ary function or constant expression")
6790 value = _py2expr(value)
6791 Z3_add_const_interp(x.ctx_ref(), self.model, x.ast, value.ast)
6792
6793 def translate(self, target):
6794 """Translate `self` to the context `target`. That is, return a copy of `self` in the context `target`.
6795 """
6796 if z3_debug():
6797 _z3_assert(isinstance(target, Context), "argument must be a Z3 context")
6798 model = Z3_model_translate(self.ctx.ref(), self.model, target.ref())
6799 return ModelRef(model, target)
6800
6801 def project(self, vars, fml):
6802 """Perform model-based projection on fml with respect to vars.
6803 Assume that the model satisfies fml. Then compute a projection fml_p, such
6804 that vars do not occur free in fml_p, fml_p is true in the model and
6805 fml_p => exists vars . fml
6806 """
6807 ctx = self.ctx.ref()
6808 _vars = (Ast * len(vars))()
6809 for i in range(len(vars)):
6810 _vars[i] = vars[i].as_ast()
6811 return _to_expr_ref(Z3_qe_model_project(ctx, self.model, len(vars), _vars, fml.ast), self.ctx)
6812
6813 def project_with_witness(self, vars, fml):
6814 """Perform model-based projection, but also include realizer terms for the projected variables"""
6815 ctx = self.ctx.ref()
6816 _vars = (Ast * len(vars))()
6817 for i in range(len(vars)):
6818 _vars[i] = vars[i].as_ast()
6819 defs = AstMap()
6820 result = Z3_qe_model_project_with_witness(ctx, self.model, len(vars), _vars, fml.ast, defs.map)
6821 result = _to_expr_ref(result, self.ctx)
6822 return result, defs
6823
6824
6825 def __copy__(self):
6826 return self.translate(self.ctx)
6827
6828 def __deepcopy__(self, memo={}):
6829 return self.translate(self.ctx)
6830
6831
6832def Model(ctx=None, eval = {}):
6833 ctx = _get_ctx(ctx)
6834 mdl = ModelRef(Z3_mk_model(ctx.ref()), ctx)
6835 for k, v in eval.items():
6836 mdl.update_value(k, v)
6837 return mdl
6838
6839
6841 """Return true if n is a Z3 expression of the form (_ as-array f)."""
6842 return isinstance(n, ExprRef) and Z3_is_as_array(n.ctx.ref(), n.as_ast())
6843
6844
6846 """Return the function declaration f associated with a Z3 expression of the form (_ as-array f)."""
6847 if z3_debug():
6848 _z3_assert(is_as_array(n), "as-array Z3 expression expected.")
6849 return FuncDeclRef(Z3_get_as_array_func_decl(n.ctx.ref(), n.as_ast()), n.ctx)
6850
6851
6856
6857
6859 """Statistics for `Solver.check()`."""
6860
6861 def __init__(self, stats, ctx):
6862 self.stats = stats
6863 self.ctx = ctx
6864 Z3_stats_inc_ref(self.ctx.ref(), self.stats)
6865
6866 def __deepcopy__(self, memo={}):
6867 return Statistics(self.stats, self.ctx)
6868
6869 def __del__(self):
6870 if self.ctx.ref() is not None and Z3_stats_dec_ref is not None:
6871 Z3_stats_dec_ref(self.ctx.ref(), self.stats)
6872
6873 def __repr__(self):
6874 if in_html_mode():
6875 out = io.StringIO()
6876 even = True
6877 out.write(u('<table border="1" cellpadding="2" cellspacing="0">'))
6878 for k, v in self:
6879 if even:
6880 out.write(u('<tr style="background-color:#CFCFCF">'))
6881 even = False
6882 else:
6883 out.write(u("<tr>"))
6884 even = True
6885 out.write(u("<td>%s</td><td>%s</td></tr>" % (k, v)))
6886 out.write(u("</table>"))
6887 return out.getvalue()
6888 else:
6889 return Z3_stats_to_string(self.ctx.ref(), self.stats)
6890
6891 def __len__(self):
6892 """Return the number of statistical counters.
6893
6894 >>> x = Int('x')
6895 >>> s = Then('simplify', 'nlsat').solver()
6896 >>> s.add(x > 0)
6897 >>> s.check()
6898 sat
6899 >>> st = s.statistics()
6900 >>> len(st)
6901 7
6902 """
6903 return int(Z3_stats_size(self.ctx.ref(), self.stats))
6904
6905 def __getitem__(self, idx):
6906 """Return the value of statistical counter at position `idx`. The result is a pair (key, value).
6907
6908 >>> x = Int('x')
6909 >>> s = Then('simplify', 'nlsat').solver()
6910 >>> s.add(x > 0)
6911 >>> s.check()
6912 sat
6913 >>> st = s.statistics()
6914 >>> len(st)
6915 7
6916 >>> st[0]
6917 ('nlsat propagations', 2)
6918 >>> st[1]
6919 ('nlsat restarts', 1)
6920 """
6921 if idx >= len(self):
6922 raise IndexError
6923 if Z3_stats_is_uint(self.ctx.ref(), self.stats, idx):
6924 val = int(Z3_stats_get_uint_value(self.ctx.ref(), self.stats, idx))
6925 else:
6926 val = Z3_stats_get_double_value(self.ctx.ref(), self.stats, idx)
6927 return (Z3_stats_get_key(self.ctx.ref(), self.stats, idx), val)
6928
6929 def keys(self):
6930 """Return the list of statistical counters.
6931
6932 >>> x = Int('x')
6933 >>> s = Then('simplify', 'nlsat').solver()
6934 >>> s.add(x > 0)
6935 >>> s.check()
6936 sat
6937 >>> st = s.statistics()
6938 """
6939 return [Z3_stats_get_key(self.ctx.ref(), self.stats, idx) for idx in range(len(self))]
6940
6941 def get_key_value(self, key):
6942 """Return the value of a particular statistical counter.
6943
6944 >>> x = Int('x')
6945 >>> s = Then('simplify', 'nlsat').solver()
6946 >>> s.add(x > 0)
6947 >>> s.check()
6948 sat
6949 >>> st = s.statistics()
6950 >>> st.get_key_value('nlsat propagations')
6951 2
6952 """
6953 for idx in range(len(self)):
6954 if key == Z3_stats_get_key(self.ctx.ref(), self.stats, idx):
6955 if Z3_stats_is_uint(self.ctx.ref(), self.stats, idx):
6956 return int(Z3_stats_get_uint_value(self.ctx.ref(), self.stats, idx))
6957 else:
6958 return Z3_stats_get_double_value(self.ctx.ref(), self.stats, idx)
6959 raise Z3Exception("unknown key")
6960
6961 def __getattr__(self, name):
6962 """Access the value of statistical using attributes.
6963
6964 Remark: to access a counter containing blank spaces (e.g., 'nlsat propagations'),
6965 we should use '_' (e.g., 'nlsat_propagations').
6966
6967 >>> x = Int('x')
6968 >>> s = Then('simplify', 'nlsat').solver()
6969 >>> s.add(x > 0)
6970 >>> s.check()
6971 sat
6972 >>> st = s.statistics()
6973 >>> st.nlsat_propagations
6974 2
6975 >>> st.nlsat_stages
6976 2
6977 """
6978 key = name.replace("_", " ")
6979 try:
6980 return self.get_key_value(key)
6981 except Z3Exception:
6982 raise AttributeError
6983
6984
6989
6990
6992 """Represents the result of a satisfiability check: sat, unsat, unknown.
6993
6994 >>> s = Solver()
6995 >>> s.check()
6996 sat
6997 >>> r = s.check()
6998 >>> isinstance(r, CheckSatResult)
6999 True
7000 """
7001
7002 def __init__(self, r):
7003 self.r = r
7004
7005 def __deepcopy__(self, memo={}):
7006 return CheckSatResult(self.r)
7007
7008 def __eq__(self, other):
7009 return isinstance(other, CheckSatResult) and self.r == other.r
7010
7011 def __ne__(self, other):
7012 return not self.__eq__(other)
7013
7014 def __repr__(self):
7015 if in_html_mode():
7016 if self.r == Z3_L_TRUE:
7017 return "<b>sat</b>"
7018 elif self.r == Z3_L_FALSE:
7019 return "<b>unsat</b>"
7020 else:
7021 return "<b>unknown</b>"
7022 else:
7023 if self.r == Z3_L_TRUE:
7024 return "sat"
7025 elif self.r == Z3_L_FALSE:
7026 return "unsat"
7027 else:
7028 return "unknown"
7029
7030 def _repr_html_(self):
7031 in_html = in_html_mode()
7032 set_html_mode(True)
7033 res = repr(self)
7034 set_html_mode(in_html)
7035 return res
7036
7037
7038sat = CheckSatResult(Z3_L_TRUE)
7039unsat = CheckSatResult(Z3_L_FALSE)
7040unknown = CheckSatResult(Z3_L_UNDEF)
7041
7042
7044 """
7045 Solver API provides methods for implementing the main SMT 2.0 commands:
7046 push, pop, check, get-model, etc.
7047 """
7048
7049 def __init__(self, solver=None, ctx=None, logFile=None):
7050 assert solver is None or ctx is not None
7051 self.ctx = _get_ctx(ctx)
7052 self.backtrack_level = 4000000000
7053 self.solver = None
7054 if solver is None:
7055 self.solver = Z3_mk_solver(self.ctx.ref())
7056 else:
7057 self.solver = solver
7058 Z3_solver_inc_ref(self.ctx.ref(), self.solver)
7059 if logFile is not None:
7060 self.set("smtlib2_log", logFile)
7061
7062 def __del__(self):
7063 if self.solver is not None and self.ctx.ref() is not None and Z3_solver_dec_ref is not None:
7064 Z3_solver_dec_ref(self.ctx.ref(), self.solver)
7065
7066 def __enter__(self):
7067 self.push()
7068 return self
7069
7070 def __exit__(self, *exc_info):
7071 self.pop()
7072
7073 def set(self, *args, **keys):
7074 """Set a configuration option.
7075 The method `help()` return a string containing all available options.
7076
7077 >>> s = Solver()
7078 >>> # The option MBQI can be set using three different approaches.
7079 >>> s.set(mbqi=True)
7080 >>> s.set('MBQI', True)
7081 >>> s.set(':mbqi', True)
7082 """
7083 p = args2params(args, keys, self.ctx)
7084 Z3_solver_set_params(self.ctx.ref(), self.solver, p.params)
7085
7086 def push(self):
7087 """Create a backtracking point.
7088
7089 >>> x = Int('x')
7090 >>> s = Solver()
7091 >>> s.add(x > 0)
7092 >>> s
7093 [x > 0]
7094 >>> s.push()
7095 >>> s.add(x < 1)
7096 >>> s
7097 [x > 0, x < 1]
7098 >>> s.check()
7099 unsat
7100 >>> s.pop()
7101 >>> s.check()
7102 sat
7103 >>> s
7104 [x > 0]
7105 """
7106 Z3_solver_push(self.ctx.ref(), self.solver)
7107
7108 def pop(self, num=1):
7109 """Backtrack \\c num backtracking points.
7110
7111 >>> x = Int('x')
7112 >>> s = Solver()
7113 >>> s.add(x > 0)
7114 >>> s
7115 [x > 0]
7116 >>> s.push()
7117 >>> s.add(x < 1)
7118 >>> s
7119 [x > 0, x < 1]
7120 >>> s.check()
7121 unsat
7122 >>> s.pop()
7123 >>> s.check()
7124 sat
7125 >>> s
7126 [x > 0]
7127 """
7128 Z3_solver_pop(self.ctx.ref(), self.solver, num)
7129
7130 def num_scopes(self):
7131 """Return the current number of backtracking points.
7132
7133 >>> s = Solver()
7134 >>> s.num_scopes()
7135 0
7136 >>> s.push()
7137 >>> s.num_scopes()
7138 1
7139 >>> s.push()
7140 >>> s.num_scopes()
7141 2
7142 >>> s.pop()
7143 >>> s.num_scopes()
7144 1
7145 """
7146 return Z3_solver_get_num_scopes(self.ctx.ref(), self.solver)
7147
7148 def reset(self):
7149 """Remove all asserted constraints and backtracking points created using `push()`.
7150
7151 >>> x = Int('x')
7152 >>> s = Solver()
7153 >>> s.add(x > 0)
7154 >>> s
7155 [x > 0]
7156 >>> s.reset()
7157 >>> s
7158 []
7159 """
7160 Z3_solver_reset(self.ctx.ref(), self.solver)
7161
7162 def assert_exprs(self, *args):
7163 """Assert constraints into the solver.
7164
7165 >>> x = Int('x')
7166 >>> s = Solver()
7167 >>> s.assert_exprs(x > 0, x < 2)
7168 >>> s
7169 [x > 0, x < 2]
7170 """
7171 args = _get_args(args)
7172 s = BoolSort(self.ctx)
7173 for arg in args:
7174 if isinstance(arg, Goal) or isinstance(arg, AstVector):
7175 for f in arg:
7176 Z3_solver_assert(self.ctx.ref(), self.solver, f.as_ast())
7177 else:
7178 arg = s.cast(arg)
7179 Z3_solver_assert(self.ctx.ref(), self.solver, arg.as_ast())
7180
7181 def add(self, *args):
7182 """Assert constraints into the solver.
7183
7184 >>> x = Int('x')
7185 >>> s = Solver()
7186 >>> s.add(x > 0, x < 2)
7187 >>> s
7188 [x > 0, x < 2]
7189 """
7190 self.assert_exprs(*args)
7191
7192 def __iadd__(self, fml):
7193 self.add(fml)
7194 return self
7195
7196 def append(self, *args):
7197 """Assert constraints into the solver.
7198
7199 >>> x = Int('x')
7200 >>> s = Solver()
7201 >>> s.append(x > 0, x < 2)
7202 >>> s
7203 [x > 0, x < 2]
7204 """
7205 self.assert_exprs(*args)
7206
7207 def insert(self, *args):
7208 """Assert constraints into the solver.
7209
7210 >>> x = Int('x')
7211 >>> s = Solver()
7212 >>> s.insert(x > 0, x < 2)
7213 >>> s
7214 [x > 0, x < 2]
7215 """
7216 self.assert_exprs(*args)
7217
7218 def assert_and_track(self, a, p):
7219 """Assert constraint `a` and track it in the unsat core using the Boolean constant `p`.
7220
7221 If `p` is a string, it will be automatically converted into a Boolean constant.
7222
7223 >>> x = Int('x')
7224 >>> p3 = Bool('p3')
7225 >>> s = Solver()
7226 >>> s.set(unsat_core=True)
7227 >>> s.assert_and_track(x > 0, 'p1')
7228 >>> s.assert_and_track(x != 1, 'p2')
7229 >>> s.assert_and_track(x < 0, p3)
7230 >>> print(s.check())
7231 unsat
7232 >>> c = s.unsat_core()
7233 >>> len(c)
7234 2
7235 >>> Bool('p1') in c
7236 True
7237 >>> Bool('p2') in c
7238 False
7239 >>> p3 in c
7240 True
7241 """
7242 if isinstance(p, str):
7243 p = Bool(p, self.ctx)
7244 _z3_assert(isinstance(a, BoolRef), "Boolean expression expected")
7245 _z3_assert(isinstance(p, BoolRef) and is_const(p), "Boolean expression expected")
7246 Z3_solver_assert_and_track(self.ctx.ref(), self.solver, a.as_ast(), p.as_ast())
7247
7248 def check(self, *assumptions):
7249 """Check whether the assertions in the given solver plus the optional assumptions are consistent or not.
7250
7251 >>> x = Int('x')
7252 >>> s = Solver()
7253 >>> s.check()
7254 sat
7255 >>> s.add(x > 0, x < 2)
7256 >>> s.check()
7257 sat
7258 >>> s.model().eval(x)
7259 1
7260 >>> s.add(x < 1)
7261 >>> s.check()
7262 unsat
7263 >>> s.reset()
7264 >>> s.add(2**x == 4)
7265 >>> s.check()
7266 unknown
7267 """
7268 s = BoolSort(self.ctx)
7269 assumptions = _get_args(assumptions)
7270 num = len(assumptions)
7271 _assumptions = (Ast * num)()
7272 for i in range(num):
7273 _assumptions[i] = s.cast(assumptions[i]).as_ast()
7274 r = Z3_solver_check_assumptions(self.ctx.ref(), self.solver, num, _assumptions)
7275 return CheckSatResult(r)
7276
7277 def model(self):
7278 """Return a model for the last `check()`.
7279
7280 This function raises an exception if
7281 a model is not available (e.g., last `check()` returned unsat).
7282
7283 >>> s = Solver()
7284 >>> a = Int('a')
7285 >>> s.add(a + 2 == 0)
7286 >>> s.check()
7287 sat
7288 >>> s.model()
7289 [a = -2]
7290 """
7291 try:
7292 return ModelRef(Z3_solver_get_model(self.ctx.ref(), self.solver), self.ctx)
7293 except Z3Exception:
7294 raise Z3Exception("model is not available")
7295
7296 def import_model_converter(self, other):
7297 """Import model converter from other into the current solver"""
7298 Z3_solver_import_model_converter(self.ctx.ref(), other.solver, self.solver)
7299
7300 def interrupt(self):
7301 """Interrupt the execution of the solver object.
7302 Remarks: This ensures that the interrupt applies only
7303 to the given solver object and it applies only if it is running.
7304 """
7305 Z3_solver_interrupt(self.ctx.ref(), self.solver)
7306
7307 def unsat_core(self):
7308 """Return a subset (as an AST vector) of the assumptions provided to the last check().
7309
7310 These are the assumptions Z3 used in the unsatisfiability proof.
7311 Assumptions are available in Z3. They are used to extract unsatisfiable cores.
7312 They may be also used to "retract" assumptions. Note that, assumptions are not really
7313 "soft constraints", but they can be used to implement them.
7314
7315 >>> p1, p2, p3 = Bools('p1 p2 p3')
7316 >>> x, y = Ints('x y')
7317 >>> s = Solver()
7318 >>> s.add(Implies(p1, x > 0))
7319 >>> s.add(Implies(p2, y > x))
7320 >>> s.add(Implies(p2, y < 1))
7321 >>> s.add(Implies(p3, y > -3))
7322 >>> s.check(p1, p2, p3)
7323 unsat
7324 >>> core = s.unsat_core()
7325 >>> len(core)
7326 2
7327 >>> p1 in core
7328 True
7329 >>> p2 in core
7330 True
7331 >>> p3 in core
7332 False
7333 >>> # "Retracting" p2
7334 >>> s.check(p1, p3)
7335 sat
7336 """
7337 return AstVector(Z3_solver_get_unsat_core(self.ctx.ref(), self.solver), self.ctx)
7338
7339 def consequences(self, assumptions, variables):
7340 """Determine fixed values for the variables based on the solver state and assumptions.
7341 >>> s = Solver()
7342 >>> a, b, c, d = Bools('a b c d')
7343 >>> s.add(Implies(a,b), Implies(b, c))
7344 >>> s.consequences([a],[b,c,d])
7345 (sat, [Implies(a, b), Implies(a, c)])
7346 >>> s.consequences([Not(c),d],[a,b,c,d])
7347 (sat, [Implies(d, d), Implies(Not(c), Not(c)), Implies(Not(c), Not(b)), Implies(Not(c), Not(a))])
7348 """
7349 if isinstance(assumptions, list):
7350 _asms = AstVector(None, self.ctx)
7351 for a in assumptions:
7352 _asms.push(a)
7353 assumptions = _asms
7354 if isinstance(variables, list):
7355 _vars = AstVector(None, self.ctx)
7356 for a in variables:
7357 _vars.push(a)
7358 variables = _vars
7359 _z3_assert(isinstance(assumptions, AstVector), "ast vector expected")
7360 _z3_assert(isinstance(variables, AstVector), "ast vector expected")
7361 consequences = AstVector(None, self.ctx)
7362 r = Z3_solver_get_consequences(self.ctx.ref(), self.solver, assumptions.vector,
7363 variables.vector, consequences.vector)
7364 sz = len(consequences)
7365 consequences = [consequences[i] for i in range(sz)]
7366 return CheckSatResult(r), consequences
7367
7368 def from_file(self, filename):
7369 """Parse assertions from a file"""
7370 Z3_solver_from_file(self.ctx.ref(), self.solver, filename)
7371
7372 def from_string(self, s):
7373 """Parse assertions from a string"""
7374 Z3_solver_from_string(self.ctx.ref(), self.solver, s)
7375
7376 def cube(self, vars=None):
7377 """Get set of cubes
7378 The method takes an optional set of variables that restrict which
7379 variables may be used as a starting point for cubing.
7380 If vars is not None, then the first case split is based on a variable in
7381 this set.
7382 """
7383 self.cube_vs = AstVector(None, self.ctx)
7384 if vars is not None:
7385 for v in vars:
7386 self.cube_vs.push(v)
7387 while True:
7388 lvl = self.backtrack_level
7389 self.backtrack_level = 4000000000
7390 r = AstVector(Z3_solver_cube(self.ctx.ref(), self.solver, self.cube_vs.vector, lvl), self.ctx)
7391 if (len(r) == 1 and is_false(r[0])):
7392 return
7393 yield r
7394 if (len(r) == 0):
7395 return
7396
7397 def cube_vars(self):
7398 """Access the set of variables that were touched by the most recently generated cube.
7399 This set of variables can be used as a starting point for additional cubes.
7400 The idea is that variables that appear in clauses that are reduced by the most recent
7401 cube are likely more useful to cube on."""
7402 return self.cube_vs
7403
7404 def root(self, t):
7405 """Retrieve congruence closure root of the term t relative to the current search state
7406 The function primarily works for SimpleSolver. Terms and variables that are
7407 eliminated during pre-processing are not visible to the congruence closure.
7408 """
7409 t = _py2expr(t, self.ctx)
7410 return _to_expr_ref(Z3_solver_congruence_root(self.ctx.ref(), self.solver, t.ast), self.ctx)
7411
7412 def next(self, t):
7413 """Retrieve congruence closure sibling of the term t relative to the current search state
7414 The function primarily works for SimpleSolver. Terms and variables that are
7415 eliminated during pre-processing are not visible to the congruence closure.
7416 """
7417 t = _py2expr(t, self.ctx)
7418 return _to_expr_ref(Z3_solver_congruence_next(self.ctx.ref(), self.solver, t.ast), self.ctx)
7419
7420 def explain_congruent(self, a, b):
7421 """Explain congruence of a and b relative to the current search state"""
7422 a = _py2expr(a, self.ctx)
7423 b = _py2expr(b, self.ctx)
7424 return _to_expr_ref(Z3_solver_congruence_explain(self.ctx.ref(), self.solver, a.ast, b.ast), self.ctx)
7425
7426
7427 def solve_for(self, ts):
7428 """Retrieve a solution for t relative to linear equations maintained in the current state."""
7429 vars = AstVector(ctx=self.ctx);
7430 terms = AstVector(ctx=self.ctx);
7431 guards = AstVector(ctx=self.ctx);
7432 for t in ts:
7433 t = _py2expr(t, self.ctx)
7434 vars.push(t)
7435 Z3_solver_solve_for(self.ctx.ref(), self.solver, vars.vector, terms.vector, guards.vector)
7436 return [(vars[i], terms[i], guards[i]) for i in range(len(vars))]
7437
7438
7439 def proof(self):
7440 """Return a proof for the last `check()`. Proof construction must be enabled."""
7441 return _to_expr_ref(Z3_solver_get_proof(self.ctx.ref(), self.solver), self.ctx)
7442
7443 def assertions(self):
7444 """Return an AST vector containing all added constraints.
7445
7446 >>> s = Solver()
7447 >>> s.assertions()
7448 []
7449 >>> a = Int('a')
7450 >>> s.add(a > 0)
7451 >>> s.add(a < 10)
7452 >>> s.assertions()
7453 [a > 0, a < 10]
7454 """
7455 return AstVector(Z3_solver_get_assertions(self.ctx.ref(), self.solver), self.ctx)
7456
7457 def units(self):
7458 """Return an AST vector containing all currently inferred units.
7459 """
7460 return AstVector(Z3_solver_get_units(self.ctx.ref(), self.solver), self.ctx)
7461
7462 def non_units(self):
7463 """Return an AST vector containing all atomic formulas in solver state that are not units.
7464 """
7465 return AstVector(Z3_solver_get_non_units(self.ctx.ref(), self.solver), self.ctx)
7466
7467 def trail_levels(self):
7468 """Return trail and decision levels of the solver state after a check() call.
7469 """
7470 trail = self.trail()
7471 levels = (ctypes.c_uint * len(trail))()
7472 Z3_solver_get_levels(self.ctx.ref(), self.solver, trail.vector, len(trail), levels)
7473 return trail, levels
7474
7475 def set_initial_value(self, var, value):
7476 """initialize the solver's state by setting the initial value of var to value
7477 """
7478 s = var.sort()
7479 value = s.cast(value)
7480 Z3_solver_set_initial_value(self.ctx.ref(), self.solver, var.ast, value.ast)
7481
7482 def trail(self):
7483 """Return trail of the solver state after a check() call.
7484 """
7485 return AstVector(Z3_solver_get_trail(self.ctx.ref(), self.solver), self.ctx)
7486
7487 def statistics(self):
7488 """Return statistics for the last `check()`.
7489
7490 >>> s = SimpleSolver()
7491 >>> x = Int('x')
7492 >>> s.add(x > 0)
7493 >>> s.check()
7494 sat
7495 >>> st = s.statistics()
7496 >>> st.get_key_value('final checks')
7497 1
7498 >>> len(st) > 0
7499 True
7500 >>> st[0] != 0
7501 True
7502 """
7503 return Statistics(Z3_solver_get_statistics(self.ctx.ref(), self.solver), self.ctx)
7504
7505 def reason_unknown(self):
7506 """Return a string describing why the last `check()` returned `unknown`.
7507
7508 >>> x = Int('x')
7509 >>> s = SimpleSolver()
7510 >>> s.add(2**x == 4)
7511 >>> s.check()
7512 unknown
7513 >>> s.reason_unknown()
7514 '(incomplete (theory arithmetic))'
7515 """
7516 return Z3_solver_get_reason_unknown(self.ctx.ref(), self.solver)
7517
7518 def help(self):
7519 """Display a string describing all available options."""
7520 print(Z3_solver_get_help(self.ctx.ref(), self.solver))
7521
7522 def param_descrs(self):
7523 """Return the parameter description set."""
7524 return ParamDescrsRef(Z3_solver_get_param_descrs(self.ctx.ref(), self.solver), self.ctx)
7525
7526 def __repr__(self):
7527 """Return a formatted string with all added constraints."""
7528 return obj_to_string(self)
7529
7530 def translate(self, target):
7531 """Translate `self` to the context `target`. That is, return a copy of `self` in the context `target`.
7532
7533 >>> c1 = Context()
7534 >>> c2 = Context()
7535 >>> s1 = Solver(ctx=c1)
7536 >>> s2 = s1.translate(c2)
7537 """
7538 if z3_debug():
7539 _z3_assert(isinstance(target, Context), "argument must be a Z3 context")
7540 solver = Z3_solver_translate(self.ctx.ref(), self.solver, target.ref())
7541 return Solver(solver, target)
7542
7543 def __copy__(self):
7544 return self.translate(self.ctx)
7545
7546 def __deepcopy__(self, memo={}):
7547 return self.translate(self.ctx)
7548
7549 def sexpr(self):
7550 """Return a formatted string (in Lisp-like format) with all added constraints.
7551 We say the string is in s-expression format.
7552
7553 >>> x = Int('x')
7554 >>> s = Solver()
7555 >>> s.add(x > 0)
7556 >>> s.add(x < 2)
7557 >>> r = s.sexpr()
7558 """
7559 return Z3_solver_to_string(self.ctx.ref(), self.solver)
7560
7561 def dimacs(self, include_names=True):
7562 """Return a textual representation of the solver in DIMACS format."""
7563 return Z3_solver_to_dimacs_string(self.ctx.ref(), self.solver, include_names)
7564
7565 def to_smt2(self):
7566 """return SMTLIB2 formatted benchmark for solver's assertions"""
7567 es = self.assertions()
7568 sz = len(es)
7569 sz1 = sz
7570 if sz1 > 0:
7571 sz1 -= 1
7572 v = (Ast * sz1)()
7573 for i in range(sz1):
7574 v[i] = es[i].as_ast()
7575 if sz > 0:
7576 e = es[sz1].as_ast()
7577 else:
7578 e = BoolVal(True, self.ctx).as_ast()
7579 return Z3_benchmark_to_smtlib_string(
7580 self.ctx.ref(), "benchmark generated from python API", "", "unknown", "", sz1, v, e,
7581 )
7582
7583
7584def SolverFor(logic, ctx=None, logFile=None):
7585 """Create a solver customized for the given logic.
7586
7587 The parameter `logic` is a string. It should be contains
7588 the name of a SMT-LIB logic.
7589 See http://www.smtlib.org/ for the name of all available logics.
7590
7591 >>> s = SolverFor("QF_LIA")
7592 >>> x = Int('x')
7593 >>> s.add(x > 0)
7594 >>> s.add(x < 2)
7595 >>> s.check()
7596 sat
7597 >>> s.model()
7598 [x = 1]
7599 """
7600 ctx = _get_ctx(ctx)
7601 logic = to_symbol(logic)
7602 return Solver(Z3_mk_solver_for_logic(ctx.ref(), logic), ctx, logFile)
7603
7604
7605def SimpleSolver(ctx=None, logFile=None):
7606 """Return a simple general purpose solver with limited amount of preprocessing.
7607
7608 >>> s = SimpleSolver()
7609 >>> x = Int('x')
7610 >>> s.add(x > 0)
7611 >>> s.check()
7612 sat
7613 """
7614 ctx = _get_ctx(ctx)
7615 return Solver(Z3_mk_simple_solver(ctx.ref()), ctx, logFile)
7616
7617#########################################
7618#
7619# Fixedpoint
7620#
7621#########################################
7622
7623
7624class Fixedpoint(Z3PPObject):
7625 """Fixedpoint API provides methods for solving with recursive predicates"""
7626
7627 def __init__(self, fixedpoint=None, ctx=None):
7628 assert fixedpoint is None or ctx is not None
7629 self.ctx = _get_ctx(ctx)
7630 self.fixedpoint = None
7631 if fixedpoint is None:
7632 self.fixedpoint = Z3_mk_fixedpoint(self.ctx.ref())
7633 else:
7634 self.fixedpoint = fixedpoint
7635 Z3_fixedpoint_inc_ref(self.ctx.ref(), self.fixedpoint)
7636 self.vars = []
7637
7638 def __deepcopy__(self, memo={}):
7639 return FixedPoint(self.fixedpoint, self.ctx)
7640
7641 def __del__(self):
7642 if self.fixedpoint is not None and self.ctx.ref() is not None and Z3_fixedpoint_dec_ref is not None:
7643 Z3_fixedpoint_dec_ref(self.ctx.ref(), self.fixedpoint)
7644
7645 def set(self, *args, **keys):
7646 """Set a configuration option. The method `help()` return a string containing all available options.
7647 """
7648 p = args2params(args, keys, self.ctx)
7649 Z3_fixedpoint_set_params(self.ctx.ref(), self.fixedpoint, p.params)
7650
7651 def help(self):
7652 """Display a string describing all available options."""
7653 print(Z3_fixedpoint_get_help(self.ctx.ref(), self.fixedpoint))
7654
7655 def param_descrs(self):
7656 """Return the parameter description set."""
7657 return ParamDescrsRef(Z3_fixedpoint_get_param_descrs(self.ctx.ref(), self.fixedpoint), self.ctx)
7658
7659 def assert_exprs(self, *args):
7660 """Assert constraints as background axioms for the fixedpoint solver."""
7661 args = _get_args(args)
7662 s = BoolSort(self.ctx)
7663 for arg in args:
7664 if isinstance(arg, Goal) or isinstance(arg, AstVector):
7665 for f in arg:
7666 f = self.abstract(f)
7667 Z3_fixedpoint_assert(self.ctx.ref(), self.fixedpoint, f.as_ast())
7668 else:
7669 arg = s.cast(arg)
7670 arg = self.abstract(arg)
7671 Z3_fixedpoint_assert(self.ctx.ref(), self.fixedpoint, arg.as_ast())
7672
7673 def add(self, *args):
7674 """Assert constraints as background axioms for the fixedpoint solver. Alias for assert_expr."""
7675 self.assert_exprs(*args)
7676
7677 def __iadd__(self, fml):
7678 self.add(fml)
7679 return self
7680
7681 def append(self, *args):
7682 """Assert constraints as background axioms for the fixedpoint solver. Alias for assert_expr."""
7683 self.assert_exprs(*args)
7684
7685 def insert(self, *args):
7686 """Assert constraints as background axioms for the fixedpoint solver. Alias for assert_expr."""
7687 self.assert_exprs(*args)
7688
7689 def add_rule(self, head, body=None, name=None):
7690 """Assert rules defining recursive predicates to the fixedpoint solver.
7691 >>> a = Bool('a')
7692 >>> b = Bool('b')
7693 >>> s = Fixedpoint()
7694 >>> s.register_relation(a.decl())
7695 >>> s.register_relation(b.decl())
7696 >>> s.fact(a)
7697 >>> s.rule(b, a)
7698 >>> s.query(b)
7699 sat
7700 """
7701 if name is None:
7702 name = ""
7703 name = to_symbol(name, self.ctx)
7704 if body is None:
7705 head = self.abstract(head)
7706 Z3_fixedpoint_add_rule(self.ctx.ref(), self.fixedpoint, head.as_ast(), name)
7707 else:
7708 body = _get_args(body)
7709 f = self.abstract(Implies(And(body, self.ctx), head))
7710 Z3_fixedpoint_add_rule(self.ctx.ref(), self.fixedpoint, f.as_ast(), name)
7711
7712 def rule(self, head, body=None, name=None):
7713 """Assert rules defining recursive predicates to the fixedpoint solver. Alias for add_rule."""
7714 self.add_rule(head, body, name)
7715
7716 def fact(self, head, name=None):
7717 """Assert facts defining recursive predicates to the fixedpoint solver. Alias for add_rule."""
7718 self.add_rule(head, None, name)
7719
7720 def query(self, *query):
7721 """Query the fixedpoint engine whether formula is derivable.
7722 You can also pass an tuple or list of recursive predicates.
7723 """
7724 query = _get_args(query)
7725 sz = len(query)
7726 if sz >= 1 and isinstance(query[0], FuncDeclRef):
7727 _decls = (FuncDecl * sz)()
7728 i = 0
7729 for q in query:
7730 _decls[i] = q.ast
7731 i = i + 1
7732 r = Z3_fixedpoint_query_relations(self.ctx.ref(), self.fixedpoint, sz, _decls)
7733 else:
7734 if sz == 1:
7735 query = query[0]
7736 else:
7737 query = And(query, self.ctx)
7738 query = self.abstract(query, False)
7739 r = Z3_fixedpoint_query(self.ctx.ref(), self.fixedpoint, query.as_ast())
7740 return CheckSatResult(r)
7741
7742 def query_from_lvl(self, lvl, *query):
7743 """Query the fixedpoint engine whether formula is derivable starting at the given query level.
7744 """
7745 query = _get_args(query)
7746 sz = len(query)
7747 if sz >= 1 and isinstance(query[0], FuncDecl):
7748 _z3_assert(False, "unsupported")
7749 else:
7750 if sz == 1:
7751 query = query[0]
7752 else:
7753 query = And(query)
7754 query = self.abstract(query, False)
7755 r = Z3_fixedpoint_query_from_lvl(self.ctx.ref(), self.fixedpoint, query.as_ast(), lvl)
7756 return CheckSatResult(r)
7757
7758 def update_rule(self, head, body, name):
7759 """update rule"""
7760 if name is None:
7761 name = ""
7762 name = to_symbol(name, self.ctx)
7763 body = _get_args(body)
7764 f = self.abstract(Implies(And(body, self.ctx), head))
7765 Z3_fixedpoint_update_rule(self.ctx.ref(), self.fixedpoint, f.as_ast(), name)
7766
7767 def get_answer(self):
7768 """Retrieve answer from last query call."""
7769 r = Z3_fixedpoint_get_answer(self.ctx.ref(), self.fixedpoint)
7770 return _to_expr_ref(r, self.ctx)
7771
7772 def get_ground_sat_answer(self):
7773 """Retrieve a ground cex from last query call."""
7774 r = Z3_fixedpoint_get_ground_sat_answer(self.ctx.ref(), self.fixedpoint)
7775 return _to_expr_ref(r, self.ctx)
7776
7777 def get_rules_along_trace(self):
7778 """retrieve rules along the counterexample trace"""
7779 return AstVector(Z3_fixedpoint_get_rules_along_trace(self.ctx.ref(), self.fixedpoint), self.ctx)
7780
7781 def get_rule_names_along_trace(self):
7782 """retrieve rule names along the counterexample trace"""
7783 # this is a hack as I don't know how to return a list of symbols from C++;
7784 # obtain names as a single string separated by semicolons
7785 names = _symbol2py(self.ctx, Z3_fixedpoint_get_rule_names_along_trace(self.ctx.ref(), self.fixedpoint))
7786 # split into individual names
7787 return names.split(";")
7788
7789 def get_num_levels(self, predicate):
7790 """Retrieve number of levels used for predicate in PDR engine"""
7791 return Z3_fixedpoint_get_num_levels(self.ctx.ref(), self.fixedpoint, predicate.ast)
7792
7793 def get_cover_delta(self, level, predicate):
7794 """Retrieve properties known about predicate for the level'th unfolding.
7795 -1 is treated as the limit (infinity)
7796 """
7797 r = Z3_fixedpoint_get_cover_delta(self.ctx.ref(), self.fixedpoint, level, predicate.ast)
7798 return _to_expr_ref(r, self.ctx)
7799
7800 def add_cover(self, level, predicate, property):
7801 """Add property to predicate for the level'th unfolding.
7802 -1 is treated as infinity (infinity)
7803 """
7804 Z3_fixedpoint_add_cover(self.ctx.ref(), self.fixedpoint, level, predicate.ast, property.ast)
7805
7806 def register_relation(self, *relations):
7807 """Register relation as recursive"""
7808 relations = _get_args(relations)
7809 for f in relations:
7810 Z3_fixedpoint_register_relation(self.ctx.ref(), self.fixedpoint, f.ast)
7811
7812 def set_predicate_representation(self, f, *representations):
7813 """Control how relation is represented"""
7814 representations = _get_args(representations)
7815 representations = [to_symbol(s) for s in representations]
7816 sz = len(representations)
7817 args = (Symbol * sz)()
7818 for i in range(sz):
7819 args[i] = representations[i]
7820 Z3_fixedpoint_set_predicate_representation(self.ctx.ref(), self.fixedpoint, f.ast, sz, args)
7821
7822 def parse_string(self, s):
7823 """Parse rules and queries from a string"""
7824 return AstVector(Z3_fixedpoint_from_string(self.ctx.ref(), self.fixedpoint, s), self.ctx)
7825
7826 def parse_file(self, f):
7827 """Parse rules and queries from a file"""
7828 return AstVector(Z3_fixedpoint_from_file(self.ctx.ref(), self.fixedpoint, f), self.ctx)
7829
7830 def get_rules(self):
7831 """retrieve rules that have been added to fixedpoint context"""
7832 return AstVector(Z3_fixedpoint_get_rules(self.ctx.ref(), self.fixedpoint), self.ctx)
7833
7834 def get_assertions(self):
7835 """retrieve assertions that have been added to fixedpoint context"""
7836 return AstVector(Z3_fixedpoint_get_assertions(self.ctx.ref(), self.fixedpoint), self.ctx)
7837
7838 def __repr__(self):
7839 """Return a formatted string with all added rules and constraints."""
7840 return self.sexpr()
7841
7842 def sexpr(self):
7843 """Return a formatted string (in Lisp-like format) with all added constraints.
7844 We say the string is in s-expression format.
7845 """
7846 return Z3_fixedpoint_to_string(self.ctx.ref(), self.fixedpoint, 0, (Ast * 0)())
7847
7848 def to_string(self, queries):
7849 """Return a formatted string (in Lisp-like format) with all added constraints.
7850 We say the string is in s-expression format.
7851 Include also queries.
7852 """
7853 args, len = _to_ast_array(queries)
7854 return Z3_fixedpoint_to_string(self.ctx.ref(), self.fixedpoint, len, args)
7855
7856 def statistics(self):
7857 """Return statistics for the last `query()`.
7858 """
7859 return Statistics(Z3_fixedpoint_get_statistics(self.ctx.ref(), self.fixedpoint), self.ctx)
7860
7861 def reason_unknown(self):
7862 """Return a string describing why the last `query()` returned `unknown`.
7863 """
7864 return Z3_fixedpoint_get_reason_unknown(self.ctx.ref(), self.fixedpoint)
7865
7866 def declare_var(self, *vars):
7867 """Add variable or several variables.
7868 The added variable or variables will be bound in the rules
7869 and queries
7870 """
7871 vars = _get_args(vars)
7872 for v in vars:
7873 self.vars += [v]
7874
7875 def abstract(self, fml, is_forall=True):
7876 if self.vars == []:
7877 return fml
7878 if is_forall:
7879 return ForAll(self.vars, fml)
7880 else:
7881 return Exists(self.vars, fml)
7882
7883
7884#########################################
7885#
7886# Finite domains
7887#
7888#########################################
7889
7890class FiniteDomainSortRef(SortRef):
7891 """Finite domain sort."""
7892
7893 def size(self):
7894 """Return the size of the finite domain sort"""
7895 r = (ctypes.c_ulonglong * 1)()
7896 if Z3_get_finite_domain_sort_size(self.ctx_ref(), self.ast, r):
7897 return r[0]
7898 else:
7899 raise Z3Exception("Failed to retrieve finite domain sort size")
7900
7901
7902def FiniteDomainSort(name, sz, ctx=None):
7903 """Create a named finite domain sort of a given size sz"""
7904 if not isinstance(name, Symbol):
7905 name = to_symbol(name)
7906 ctx = _get_ctx(ctx)
7907 return FiniteDomainSortRef(Z3_mk_finite_domain_sort(ctx.ref(), name, sz), ctx)
7908
7909
7910def is_finite_domain_sort(s):
7911 """Return True if `s` is a Z3 finite-domain sort.
7912
7913 >>> is_finite_domain_sort(FiniteDomainSort('S', 100))
7914 True
7915 >>> is_finite_domain_sort(IntSort())
7916 False
7917 """
7918 return isinstance(s, FiniteDomainSortRef)
7919
7920
7921class FiniteDomainRef(ExprRef):
7922 """Finite-domain expressions."""
7923
7924 def sort(self):
7925 """Return the sort of the finite-domain expression `self`."""
7926 return FiniteDomainSortRef(Z3_get_sort(self.ctx_ref(), self.as_ast()), self.ctx)
7927
7928 def as_string(self):
7929 """Return a Z3 floating point expression as a Python string."""
7930 return Z3_ast_to_string(self.ctx_ref(), self.as_ast())
7931
7932
7933def is_finite_domain(a):
7934 """Return `True` if `a` is a Z3 finite-domain expression.
7935
7936 >>> s = FiniteDomainSort('S', 100)
7937 >>> b = Const('b', s)
7938 >>> is_finite_domain(b)
7939 True
7940 >>> is_finite_domain(Int('x'))
7941 False
7942 """
7943 return isinstance(a, FiniteDomainRef)
7944
7945
7946class FiniteDomainNumRef(FiniteDomainRef):
7947 """Integer values."""
7948
7949 def as_long(self):
7950 """Return a Z3 finite-domain numeral as a Python long (bignum) numeral.
7951
7952 >>> s = FiniteDomainSort('S', 100)
7953 >>> v = FiniteDomainVal(3, s)
7954 >>> v
7955 3
7956 >>> v.as_long() + 1
7957 4
7958 """
7959 return int(self.as_string())
7960
7961 def as_string(self):
7962 """Return a Z3 finite-domain numeral as a Python string.
7963
7964 >>> s = FiniteDomainSort('S', 100)
7965 >>> v = FiniteDomainVal(42, s)
7966 >>> v.as_string()
7967 '42'
7968 """
7969 return Z3_get_numeral_string(self.ctx_ref(), self.as_ast())
7970
7971
7972def FiniteDomainVal(val, sort, ctx=None):
7973 """Return a Z3 finite-domain value. If `ctx=None`, then the global context is used.
7974
7975 >>> s = FiniteDomainSort('S', 256)
7976 >>> FiniteDomainVal(255, s)
7977 255
7978 >>> FiniteDomainVal('100', s)
7979 100
7980 """
7981 if z3_debug():
7982 _z3_assert(is_finite_domain_sort(sort), "Expected finite-domain sort")
7983 ctx = sort.ctx
7984 return FiniteDomainNumRef(Z3_mk_numeral(ctx.ref(), _to_int_str(val), sort.ast), ctx)
7985
7986
7987def is_finite_domain_value(a):
7988 """Return `True` if `a` is a Z3 finite-domain value.
7989
7990 >>> s = FiniteDomainSort('S', 100)
7991 >>> b = Const('b', s)
7992 >>> is_finite_domain_value(b)
7993 False
7994 >>> b = FiniteDomainVal(10, s)
7995 >>> b
7996 10
7997 >>> is_finite_domain_value(b)
7998 True
7999 """
8000 return is_finite_domain(a) and _is_numeral(a.ctx, a.as_ast())
8001
8002
8003#########################################
8004#
8005# Optimize
8006#
8007#########################################
8008
8009class OptimizeObjective:
8010 def __init__(self, opt, value, is_max):
8011 self._opt = opt
8012 self._value = value
8013 self._is_max = is_max
8014
8015 def lower(self):
8016 opt = self._opt
8017 return _to_expr_ref(Z3_optimize_get_lower(opt.ctx.ref(), opt.optimize, self._value), opt.ctx)
8018
8019 def upper(self):
8020 opt = self._opt
8021 return _to_expr_ref(Z3_optimize_get_upper(opt.ctx.ref(), opt.optimize, self._value), opt.ctx)
8022
8023 def lower_values(self):
8024 opt = self._opt
8025 return AstVector(Z3_optimize_get_lower_as_vector(opt.ctx.ref(), opt.optimize, self._value), opt.ctx)
8026
8027 def upper_values(self):
8028 opt = self._opt
8029 return AstVector(Z3_optimize_get_upper_as_vector(opt.ctx.ref(), opt.optimize, self._value), opt.ctx)
8030
8031 def value(self):
8032 if self._is_max:
8033 return self.upper()
8034 else:
8035 return self.lower()
8036
8037 def __str__(self):
8038 return "%s:%s" % (self._value, self._is_max)
8039
8040
8041_on_models = {}
8042
8043
8044def _global_on_model(ctx):
8045 (fn, mdl) = _on_models[ctx]
8046 fn(mdl)
8047
8048
8049_on_model_eh = on_model_eh_type(_global_on_model)
8050
8051
8052class Optimize(Z3PPObject):
8053 """Optimize API provides methods for solving using objective functions and weighted soft constraints"""
8054
8055 def __init__(self, optimize=None, ctx=None):
8056 self.ctx = _get_ctx(ctx)
8057 if optimize is None:
8058 self.optimize = Z3_mk_optimize(self.ctx.ref())
8059 else:
8060 self.optimize = optimize
8061 self._on_models_id = None
8062 Z3_optimize_inc_ref(self.ctx.ref(), self.optimize)
8063
8064 def __deepcopy__(self, memo={}):
8065 return Optimize(self.optimize, self.ctx)
8066
8067 def __del__(self):
8068 if self.optimize is not None and self.ctx.ref() is not None and Z3_optimize_dec_ref is not None:
8069 Z3_optimize_dec_ref(self.ctx.ref(), self.optimize)
8070 if self._on_models_id is not None:
8071 del _on_models[self._on_models_id]
8072
8073 def __enter__(self):
8074 self.push()
8075 return self
8076
8077 def __exit__(self, *exc_info):
8078 self.pop()
8079
8080 def set(self, *args, **keys):
8081 """Set a configuration option.
8082 The method `help()` return a string containing all available options.
8083 """
8084 p = args2params(args, keys, self.ctx)
8085 Z3_optimize_set_params(self.ctx.ref(), self.optimize, p.params)
8086
8087 def help(self):
8088 """Display a string describing all available options."""
8089 print(Z3_optimize_get_help(self.ctx.ref(), self.optimize))
8090
8091 def param_descrs(self):
8092 """Return the parameter description set."""
8093 return ParamDescrsRef(Z3_optimize_get_param_descrs(self.ctx.ref(), self.optimize), self.ctx)
8094
8095 def assert_exprs(self, *args):
8096 """Assert constraints as background axioms for the optimize solver."""
8097 args = _get_args(args)
8098 s = BoolSort(self.ctx)
8099 for arg in args:
8100 if isinstance(arg, Goal) or isinstance(arg, AstVector):
8101 for f in arg:
8102 Z3_optimize_assert(self.ctx.ref(), self.optimize, f.as_ast())
8103 else:
8104 arg = s.cast(arg)
8105 Z3_optimize_assert(self.ctx.ref(), self.optimize, arg.as_ast())
8106
8107 def add(self, *args):
8108 """Assert constraints as background axioms for the optimize solver. Alias for assert_expr."""
8109 self.assert_exprs(*args)
8110
8111 def __iadd__(self, fml):
8112 self.add(fml)
8113 return self
8114
8115 def assert_and_track(self, a, p):
8116 """Assert constraint `a` and track it in the unsat core using the Boolean constant `p`.
8117
8118 If `p` is a string, it will be automatically converted into a Boolean constant.
8119
8120 >>> x = Int('x')
8121 >>> p3 = Bool('p3')
8122 >>> s = Optimize()
8123 >>> s.assert_and_track(x > 0, 'p1')
8124 >>> s.assert_and_track(x != 1, 'p2')
8125 >>> s.assert_and_track(x < 0, p3)
8126 >>> print(s.check())
8127 unsat
8128 >>> c = s.unsat_core()
8129 >>> len(c)
8130 2
8131 >>> Bool('p1') in c
8132 True
8133 >>> Bool('p2') in c
8134 False
8135 >>> p3 in c
8136 True
8137 """
8138 if isinstance(p, str):
8139 p = Bool(p, self.ctx)
8140 _z3_assert(isinstance(a, BoolRef), "Boolean expression expected")
8141 _z3_assert(isinstance(p, BoolRef) and is_const(p), "Boolean expression expected")
8142 Z3_optimize_assert_and_track(self.ctx.ref(), self.optimize, a.as_ast(), p.as_ast())
8143
8144 def add_soft(self, arg, weight="1", id=None):
8145 """Add soft constraint with optional weight and optional identifier.
8146 If no weight is supplied, then the penalty for violating the soft constraint
8147 is 1.
8148 Soft constraints are grouped by identifiers. Soft constraints that are
8149 added without identifiers are grouped by default.
8150 """
8151 if _is_int(weight):
8152 weight = "%d" % weight
8153 elif isinstance(weight, float):
8154 weight = "%f" % weight
8155 if not isinstance(weight, str):
8156 raise Z3Exception("weight should be a string or an integer")
8157 if id is None:
8158 id = ""
8159 id = to_symbol(id, self.ctx)
8160
8161 def asoft(a):
8162 v = Z3_optimize_assert_soft(self.ctx.ref(), self.optimize, a.as_ast(), weight, id)
8163 return OptimizeObjective(self, v, False)
8164 if sys.version_info.major >= 3 and isinstance(arg, Iterable):
8165 return [asoft(a) for a in arg]
8166 return asoft(arg)
8167
8168 def set_initial_value(self, var, value):
8169 """initialize the solver's state by setting the initial value of var to value
8170 """
8171 s = var.sort()
8172 value = s.cast(value)
8173 Z3_optimize_set_initial_value(self.ctx.ref(), self.optimize, var.ast, value.ast)
8174
8175 def maximize(self, arg):
8176 """Add objective function to maximize."""
8177 return OptimizeObjective(
8178 self,
8179 Z3_optimize_maximize(self.ctx.ref(), self.optimize, arg.as_ast()),
8180 is_max=True,
8181 )
8182
8183 def minimize(self, arg):
8184 """Add objective function to minimize."""
8185 return OptimizeObjective(
8186 self,
8187 Z3_optimize_minimize(self.ctx.ref(), self.optimize, arg.as_ast()),
8188 is_max=False,
8189 )
8190
8191 def push(self):
8192 """create a backtracking point for added rules, facts and assertions"""
8193 Z3_optimize_push(self.ctx.ref(), self.optimize)
8194
8195 def pop(self):
8196 """restore to previously created backtracking point"""
8197 Z3_optimize_pop(self.ctx.ref(), self.optimize)
8198
8199 def check(self, *assumptions):
8200 """Check consistency and produce optimal values."""
8201 assumptions = _get_args(assumptions)
8202 num = len(assumptions)
8203 _assumptions = (Ast * num)()
8204 for i in range(num):
8205 _assumptions[i] = assumptions[i].as_ast()
8206 return CheckSatResult(Z3_optimize_check(self.ctx.ref(), self.optimize, num, _assumptions))
8207
8208 def reason_unknown(self):
8209 """Return a string that describes why the last `check()` returned `unknown`."""
8210 return Z3_optimize_get_reason_unknown(self.ctx.ref(), self.optimize)
8211
8212 def model(self):
8213 """Return a model for the last check()."""
8214 try:
8215 return ModelRef(Z3_optimize_get_model(self.ctx.ref(), self.optimize), self.ctx)
8216 except Z3Exception:
8217 raise Z3Exception("model is not available")
8218
8219 def unsat_core(self):
8220 return AstVector(Z3_optimize_get_unsat_core(self.ctx.ref(), self.optimize), self.ctx)
8221
8222 def lower(self, obj):
8223 if not isinstance(obj, OptimizeObjective):
8224 raise Z3Exception("Expecting objective handle returned by maximize/minimize")
8225 return obj.lower()
8226
8227 def upper(self, obj):
8228 if not isinstance(obj, OptimizeObjective):
8229 raise Z3Exception("Expecting objective handle returned by maximize/minimize")
8230 return obj.upper()
8231
8232 def lower_values(self, obj):
8233 if not isinstance(obj, OptimizeObjective):
8234 raise Z3Exception("Expecting objective handle returned by maximize/minimize")
8235 return obj.lower_values()
8236
8237 def upper_values(self, obj):
8238 if not isinstance(obj, OptimizeObjective):
8239 raise Z3Exception("Expecting objective handle returned by maximize/minimize")
8240 return obj.upper_values()
8241
8242 def from_file(self, filename):
8243 """Parse assertions and objectives from a file"""
8244 Z3_optimize_from_file(self.ctx.ref(), self.optimize, filename)
8245
8246 def from_string(self, s):
8247 """Parse assertions and objectives from a string"""
8248 Z3_optimize_from_string(self.ctx.ref(), self.optimize, s)
8249
8250 def assertions(self):
8251 """Return an AST vector containing all added constraints."""
8252 return AstVector(Z3_optimize_get_assertions(self.ctx.ref(), self.optimize), self.ctx)
8253
8254 def objectives(self):
8255 """returns set of objective functions"""
8256 return AstVector(Z3_optimize_get_objectives(self.ctx.ref(), self.optimize), self.ctx)
8257
8258 def __repr__(self):
8259 """Return a formatted string with all added rules and constraints."""
8260 return self.sexpr()
8261
8262 def sexpr(self):
8263 """Return a formatted string (in Lisp-like format) with all added constraints.
8264 We say the string is in s-expression format.
8265 """
8266 return Z3_optimize_to_string(self.ctx.ref(), self.optimize)
8267
8268 def statistics(self):
8269 """Return statistics for the last check`.
8270 """
8271 return Statistics(Z3_optimize_get_statistics(self.ctx.ref(), self.optimize), self.ctx)
8272
8273 def set_on_model(self, on_model):
8274 """Register a callback that is invoked with every incremental improvement to
8275 objective values. The callback takes a model as argument.
8276 The life-time of the model is limited to the callback so the
8277 model has to be (deep) copied if it is to be used after the callback
8278 """
8279 id = len(_on_models) + 41
8280 mdl = Model(self.ctx)
8281 _on_models[id] = (on_model, mdl)
8282 self._on_models_id = id
8283 Z3_optimize_register_model_eh(
8284 self.ctx.ref(), self.optimize, mdl.model, ctypes.c_void_p(id), _on_model_eh,
8285 )
8286
8287
8288#########################################
8289#
8290# ApplyResult
8291#
8292#########################################
8293class ApplyResult(Z3PPObject):
8294 """An ApplyResult object contains the subgoals produced by a tactic when applied to a goal.
8295 It also contains model and proof converters.
8296 """
8297
8298 def __init__(self, result, ctx):
8299 self.result = result
8300 self.ctx = ctx
8301 Z3_apply_result_inc_ref(self.ctx.ref(), self.result)
8302
8303 def __deepcopy__(self, memo={}):
8304 return ApplyResult(self.result, self.ctx)
8305
8306 def __del__(self):
8307 if self.ctx.ref() is not None and Z3_apply_result_dec_ref is not None:
8308 Z3_apply_result_dec_ref(self.ctx.ref(), self.result)
8309
8310 def __len__(self):
8311 """Return the number of subgoals in `self`.
8312
8313 >>> a, b = Ints('a b')
8314 >>> g = Goal()
8315 >>> g.add(Or(a == 0, a == 1), Or(b == 0, b == 1), a > b)
8316 >>> t = Tactic('split-clause')
8317 >>> r = t(g)
8318 >>> len(r)
8319 2
8320 >>> t = Then(Tactic('split-clause'), Tactic('split-clause'))
8321 >>> len(t(g))
8322 4
8323 >>> t = Then(Tactic('split-clause'), Tactic('split-clause'), Tactic('propagate-values'))
8324 >>> len(t(g))
8325 1
8326 """
8327 return int(Z3_apply_result_get_num_subgoals(self.ctx.ref(), self.result))
8328
8329 def __getitem__(self, idx):
8330 """Return one of the subgoals stored in ApplyResult object `self`.
8331
8332 >>> a, b = Ints('a b')
8333 >>> g = Goal()
8334 >>> g.add(Or(a == 0, a == 1), Or(b == 0, b == 1), a > b)
8335 >>> t = Tactic('split-clause')
8336 >>> r = t(g)
8337 >>> r[0]
8338 [a == 0, Or(b == 0, b == 1), a > b]
8339 >>> r[1]
8340 [a == 1, Or(b == 0, b == 1), a > b]
8341 """
8342 if idx >= len(self):
8343 raise IndexError
8344 return Goal(goal=Z3_apply_result_get_subgoal(self.ctx.ref(), self.result, idx), ctx=self.ctx)
8345
8346 def __repr__(self):
8347 return obj_to_string(self)
8348
8349 def sexpr(self):
8350 """Return a textual representation of the s-expression representing the set of subgoals in `self`."""
8351 return Z3_apply_result_to_string(self.ctx.ref(), self.result)
8352
8353 def as_expr(self):
8354 """Return a Z3 expression consisting of all subgoals.
8355
8356 >>> x = Int('x')
8357 >>> g = Goal()
8358 >>> g.add(x > 1)
8359 >>> g.add(Or(x == 2, x == 3))
8360 >>> r = Tactic('simplify')(g)
8361 >>> r
8362 [[Not(x <= 1), Or(x == 2, x == 3)]]
8363 >>> r.as_expr()
8364 And(Not(x <= 1), Or(x == 2, x == 3))
8365 >>> r = Tactic('split-clause')(g)
8366 >>> r
8367 [[x > 1, x == 2], [x > 1, x == 3]]
8368 >>> r.as_expr()
8369 Or(And(x > 1, x == 2), And(x > 1, x == 3))
8370 """
8371 sz = len(self)
8372 if sz == 0:
8373 return BoolVal(False, self.ctx)
8374 elif sz == 1:
8375 return self[0].as_expr()
8376 else:
8377 return Or([self[i].as_expr() for i in range(len(self))])
8378
8379#########################################
8380#
8381# Simplifiers
8382#
8383#########################################
8384
8385class Simplifier:
8386 """Simplifiers act as pre-processing utilities for solvers.
8387 Build a custom simplifier and add it to a solver"""
8388
8389 def __init__(self, simplifier, ctx=None):
8390 self.ctx = _get_ctx(ctx)
8391 self.simplifier = None
8392 if isinstance(simplifier, SimplifierObj):
8393 self.simplifier = simplifier
8394 elif isinstance(simplifier, list):
8395 simps = [Simplifier(s, ctx) for s in simplifier]
8396 self.simplifier = simps[0].simplifier
8397 for i in range(1, len(simps)):
8398 self.simplifier = Z3_simplifier_and_then(self.ctx.ref(), self.simplifier, simps[i].simplifier)
8399 Z3_simplifier_inc_ref(self.ctx.ref(), self.simplifier)
8400 return
8401 else:
8402 if z3_debug():
8403 _z3_assert(isinstance(simplifier, str), "simplifier name expected")
8404 try:
8405 self.simplifier = Z3_mk_simplifier(self.ctx.ref(), str(simplifier))
8406 except Z3Exception:
8407 raise Z3Exception("unknown simplifier '%s'" % simplifier)
8408 Z3_simplifier_inc_ref(self.ctx.ref(), self.simplifier)
8409
8410 def __deepcopy__(self, memo={}):
8411 return Simplifier(self.simplifier, self.ctx)
8412
8413 def __del__(self):
8414 if self.simplifier is not None and self.ctx.ref() is not None and Z3_simplifier_dec_ref is not None:
8415 Z3_simplifier_dec_ref(self.ctx.ref(), self.simplifier)
8416
8417 def using_params(self, *args, **keys):
8418 """Return a simplifier that uses the given configuration options"""
8419 p = args2params(args, keys, self.ctx)
8420 return Simplifier(Z3_simplifier_using_params(self.ctx.ref(), self.simplifier, p.params), self.ctx)
8421
8422 def add(self, solver):
8423 """Return a solver that applies the simplification pre-processing specified by the simplifier"""
8424 return Solver(Z3_solver_add_simplifier(self.ctx.ref(), solver.solver, self.simplifier), self.ctx)
8425
8426 def help(self):
8427 """Display a string containing a description of the available options for the `self` simplifier."""
8428 print(Z3_simplifier_get_help(self.ctx.ref(), self.simplifier))
8429
8430 def param_descrs(self):
8431 """Return the parameter description set."""
8432 return ParamDescrsRef(Z3_simplifier_get_param_descrs(self.ctx.ref(), self.simplifier), self.ctx)
8433
8434
8435#########################################
8436#
8437# Tactics
8438#
8439#########################################
8440
8441
8442class Tactic:
8443 """Tactics transform, solver and/or simplify sets of constraints (Goal).
8444 A Tactic can be converted into a Solver using the method solver().
8445
8446 Several combinators are available for creating new tactics using the built-in ones:
8447 Then(), OrElse(), FailIf(), Repeat(), When(), Cond().
8448 """
8449
8450 def __init__(self, tactic, ctx=None):
8451 self.ctx = _get_ctx(ctx)
8452 self.tactic = None
8453 if isinstance(tactic, TacticObj):
8454 self.tactic = tactic
8455 else:
8456 if z3_debug():
8457 _z3_assert(isinstance(tactic, str), "tactic name expected")
8458 try:
8459 self.tactic = Z3_mk_tactic(self.ctx.ref(), str(tactic))
8460 except Z3Exception:
8461 raise Z3Exception("unknown tactic '%s'" % tactic)
8462 Z3_tactic_inc_ref(self.ctx.ref(), self.tactic)
8463
8464 def __deepcopy__(self, memo={}):
8465 return Tactic(self.tactic, self.ctx)
8466
8467 def __del__(self):
8468 if self.tactic is not None and self.ctx.ref() is not None and Z3_tactic_dec_ref is not None:
8469 Z3_tactic_dec_ref(self.ctx.ref(), self.tactic)
8470
8471 def solver(self, logFile=None):
8472 """Create a solver using the tactic `self`.
8473
8474 The solver supports the methods `push()` and `pop()`, but it
8475 will always solve each `check()` from scratch.
8476
8477 >>> t = Then('simplify', 'nlsat')
8478 >>> s = t.solver()
8479 >>> x = Real('x')
8480 >>> s.add(x**2 == 2, x > 0)
8481 >>> s.check()
8482 sat
8483 >>> s.model()
8484 [x = 1.4142135623?]
8485 """
8486 return Solver(Z3_mk_solver_from_tactic(self.ctx.ref(), self.tactic), self.ctx, logFile)
8487
8488 def apply(self, goal, *arguments, **keywords):
8489 """Apply tactic `self` to the given goal or Z3 Boolean expression using the given options.
8490
8491 >>> x, y = Ints('x y')
8492 >>> t = Tactic('solve-eqs')
8493 >>> t.apply(And(x == 0, y >= x + 1))
8494 [[y >= 1]]
8495 """
8496 if z3_debug():
8497 _z3_assert(isinstance(goal, (Goal, BoolRef)), "Z3 Goal or Boolean expressions expected")
8498 goal = _to_goal(goal)
8499 if len(arguments) > 0 or len(keywords) > 0:
8500 p = args2params(arguments, keywords, self.ctx)
8501 return ApplyResult(Z3_tactic_apply_ex(self.ctx.ref(), self.tactic, goal.goal, p.params), self.ctx)
8502 else:
8503 return ApplyResult(Z3_tactic_apply(self.ctx.ref(), self.tactic, goal.goal), self.ctx)
8504
8505 def __call__(self, goal, *arguments, **keywords):
8506 """Apply tactic `self` to the given goal or Z3 Boolean expression using the given options.
8507
8508 >>> x, y = Ints('x y')
8509 >>> t = Tactic('solve-eqs')
8510 >>> t(And(x == 0, y >= x + 1))
8511 [[y >= 1]]
8512 """
8513 return self.apply(goal, *arguments, **keywords)
8514
8515 def help(self):
8516 """Display a string containing a description of the available options for the `self` tactic."""
8517 print(Z3_tactic_get_help(self.ctx.ref(), self.tactic))
8518
8519 def param_descrs(self):
8520 """Return the parameter description set."""
8521 return ParamDescrsRef(Z3_tactic_get_param_descrs(self.ctx.ref(), self.tactic), self.ctx)
8522
8523
8524def _to_goal(a):
8525 if isinstance(a, BoolRef):
8526 goal = Goal(ctx=a.ctx)
8527 goal.add(a)
8528 return goal
8529 else:
8530 return a
8531
8532
8533def _to_tactic(t, ctx=None):
8534 if isinstance(t, Tactic):
8535 return t
8536 else:
8537 return Tactic(t, ctx)
8538
8539
8540def _and_then(t1, t2, ctx=None):
8541 t1 = _to_tactic(t1, ctx)
8542 t2 = _to_tactic(t2, ctx)
8543 if z3_debug():
8544 _z3_assert(t1.ctx == t2.ctx, "Context mismatch")
8545 return Tactic(Z3_tactic_and_then(t1.ctx.ref(), t1.tactic, t2.tactic), t1.ctx)
8546
8547
8548def _or_else(t1, t2, ctx=None):
8549 t1 = _to_tactic(t1, ctx)
8550 t2 = _to_tactic(t2, ctx)
8551 if z3_debug():
8552 _z3_assert(t1.ctx == t2.ctx, "Context mismatch")
8553 return Tactic(Z3_tactic_or_else(t1.ctx.ref(), t1.tactic, t2.tactic), t1.ctx)
8554
8555
8556def AndThen(*ts, **ks):
8557 """Return a tactic that applies the tactics in `*ts` in sequence.
8558
8559 >>> x, y = Ints('x y')
8560 >>> t = AndThen(Tactic('simplify'), Tactic('solve-eqs'))
8561 >>> t(And(x == 0, y > x + 1))
8562 [[Not(y <= 1)]]
8563 >>> t(And(x == 0, y > x + 1)).as_expr()
8564 Not(y <= 1)
8565 """
8566 if z3_debug():
8567 _z3_assert(len(ts) >= 2, "At least two arguments expected")
8568 ctx = ks.get("ctx", None)
8569 num = len(ts)
8570 r = ts[0]
8571 for i in range(num - 1):
8572 r = _and_then(r, ts[i + 1], ctx)
8573 return r
8574
8575
8576def Then(*ts, **ks):
8577 """Return a tactic that applies the tactics in `*ts` in sequence. Shorthand for AndThen(*ts, **ks).
8578
8579 >>> x, y = Ints('x y')
8580 >>> t = Then(Tactic('simplify'), Tactic('solve-eqs'))
8581 >>> t(And(x == 0, y > x + 1))
8582 [[Not(y <= 1)]]
8583 >>> t(And(x == 0, y > x + 1)).as_expr()
8584 Not(y <= 1)
8585 """
8586 return AndThen(*ts, **ks)
8587
8588
8589def OrElse(*ts, **ks):
8590 """Return a tactic that applies the tactics in `*ts` until one of them succeeds (it doesn't fail).
8591
8592 >>> x = Int('x')
8593 >>> t = OrElse(Tactic('split-clause'), Tactic('skip'))
8594 >>> # Tactic split-clause fails if there is no clause in the given goal.
8595 >>> t(x == 0)
8596 [[x == 0]]
8597 >>> t(Or(x == 0, x == 1))
8598 [[x == 0], [x == 1]]
8599 """
8600 if z3_debug():
8601 _z3_assert(len(ts) >= 2, "At least two arguments expected")
8602 ctx = ks.get("ctx", None)
8603 num = len(ts)
8604 r = ts[0]
8605 for i in range(num - 1):
8606 r = _or_else(r, ts[i + 1], ctx)
8607 return r
8608
8609
8610def ParOr(*ts, **ks):
8611 """Return a tactic that applies the tactics in `*ts` in parallel until one of them succeeds (it doesn't fail).
8612
8613 >>> x = Int('x')
8614 >>> t = ParOr(Tactic('simplify'), Tactic('fail'))
8615 >>> t(x + 1 == 2)
8616 [[x == 1]]
8617 """
8618 if z3_debug():
8619 _z3_assert(len(ts) >= 2, "At least two arguments expected")
8620 ctx = _get_ctx(ks.get("ctx", None))
8621 ts = [_to_tactic(t, ctx) for t in ts]
8622 sz = len(ts)
8623 _args = (TacticObj * sz)()
8624 for i in range(sz):
8625 _args[i] = ts[i].tactic
8626 return Tactic(Z3_tactic_par_or(ctx.ref(), sz, _args), ctx)
8627
8628
8629def ParThen(t1, t2, ctx=None):
8630 """Return a tactic that applies t1 and then t2 to every subgoal produced by t1.
8631 The subgoals are processed in parallel.
8632
8633 >>> x, y = Ints('x y')
8634 >>> t = ParThen(Tactic('split-clause'), Tactic('propagate-values'))
8635 >>> t(And(Or(x == 1, x == 2), y == x + 1))
8636 [[x == 1, y == 2], [x == 2, y == 3]]
8637 """
8638 t1 = _to_tactic(t1, ctx)
8639 t2 = _to_tactic(t2, ctx)
8640 if z3_debug():
8641 _z3_assert(t1.ctx == t2.ctx, "Context mismatch")
8642 return Tactic(Z3_tactic_par_and_then(t1.ctx.ref(), t1.tactic, t2.tactic), t1.ctx)
8643
8644
8645def ParAndThen(t1, t2, ctx=None):
8646 """Alias for ParThen(t1, t2, ctx)."""
8647 return ParThen(t1, t2, ctx)
8648
8649
8650def With(t, *args, **keys):
8651 """Return a tactic that applies tactic `t` using the given configuration options.
8652
8653 >>> x, y = Ints('x y')
8654 >>> t = With(Tactic('simplify'), som=True)
8655 >>> t((x + 1)*(y + 2) == 0)
8656 [[2*x + y + x*y == -2]]
8657 """
8658 ctx = keys.pop("ctx", None)
8659 t = _to_tactic(t, ctx)
8660 p = args2params(args, keys, t.ctx)
8661 return Tactic(Z3_tactic_using_params(t.ctx.ref(), t.tactic, p.params), t.ctx)
8662
8663
8664def WithParams(t, p):
8665 """Return a tactic that applies tactic `t` using the given configuration options.
8666
8667 >>> x, y = Ints('x y')
8668 >>> p = ParamsRef()
8669 >>> p.set("som", True)
8670 >>> t = WithParams(Tactic('simplify'), p)
8671 >>> t((x + 1)*(y + 2) == 0)
8672 [[2*x + y + x*y == -2]]
8673 """
8674 t = _to_tactic(t, None)
8675 return Tactic(Z3_tactic_using_params(t.ctx.ref(), t.tactic, p.params), t.ctx)
8676
8677
8678def Repeat(t, max=4294967295, ctx=None):
8679 """Return a tactic that keeps applying `t` until the goal is not modified anymore
8680 or the maximum number of iterations `max` is reached.
8681
8682 >>> x, y = Ints('x y')
8683 >>> c = And(Or(x == 0, x == 1), Or(y == 0, y == 1), x > y)
8684 >>> t = Repeat(OrElse(Tactic('split-clause'), Tactic('skip')))
8685 >>> r = t(c)
8686 >>> for subgoal in r: print(subgoal)
8687 [x == 0, y == 0, x > y]
8688 [x == 0, y == 1, x > y]
8689 [x == 1, y == 0, x > y]
8690 [x == 1, y == 1, x > y]
8691 >>> t = Then(t, Tactic('propagate-values'))
8692 >>> t(c)
8693 [[x == 1, y == 0]]
8694 """
8695 t = _to_tactic(t, ctx)
8696 return Tactic(Z3_tactic_repeat(t.ctx.ref(), t.tactic, max), t.ctx)
8697
8698
8699def TryFor(t, ms, ctx=None):
8700 """Return a tactic that applies `t` to a given goal for `ms` milliseconds.
8701
8702 If `t` does not terminate in `ms` milliseconds, then it fails.
8703 """
8704 t = _to_tactic(t, ctx)
8705 return Tactic(Z3_tactic_try_for(t.ctx.ref(), t.tactic, ms), t.ctx)
8706
8707
8708def tactics(ctx=None):
8709 """Return a list of all available tactics in Z3.
8710
8711 >>> l = tactics()
8712 >>> l.count('simplify') == 1
8713 True
8714 """
8715 ctx = _get_ctx(ctx)
8716 return [Z3_get_tactic_name(ctx.ref(), i) for i in range(Z3_get_num_tactics(ctx.ref()))]
8717
8718
8719def tactic_description(name, ctx=None):
8720 """Return a short description for the tactic named `name`.
8721
8722 >>> d = tactic_description('simplify')
8723 """
8724 ctx = _get_ctx(ctx)
8725 return Z3_tactic_get_descr(ctx.ref(), name)
8726
8727
8728def describe_tactics():
8729 """Display a (tabular) description of all available tactics in Z3."""
8730 if in_html_mode():
8731 even = True
8732 print('<table border="1" cellpadding="2" cellspacing="0">')
8733 for t in tactics():
8734 if even:
8735 print('<tr style="background-color:#CFCFCF">')
8736 even = False
8737 else:
8738 print("<tr>")
8739 even = True
8740 print("<td>%s</td><td>%s</td></tr>" % (t, insert_line_breaks(tactic_description(t), 40)))
8741 print("</table>")
8742 else:
8743 for t in tactics():
8744 print("%s : %s" % (t, tactic_description(t)))
8745
8746
8747class Probe:
8748 """Probes are used to inspect a goal (aka problem) and collect information that may be used
8749 to decide which solver and/or preprocessing step will be used.
8750 """
8751
8752 def __init__(self, probe, ctx=None):
8753 self.ctx = _get_ctx(ctx)
8754 self.probe = None
8755 if isinstance(probe, ProbeObj):
8756 self.probe = probe
8757 elif isinstance(probe, float):
8758 self.probe = Z3_probe_const(self.ctx.ref(), probe)
8759 elif _is_int(probe):
8760 self.probe = Z3_probe_const(self.ctx.ref(), float(probe))
8761 elif isinstance(probe, bool):
8762 if probe:
8763 self.probe = Z3_probe_const(self.ctx.ref(), 1.0)
8764 else:
8765 self.probe = Z3_probe_const(self.ctx.ref(), 0.0)
8766 else:
8767 if z3_debug():
8768 _z3_assert(isinstance(probe, str), "probe name expected")
8769 try:
8770 self.probe = Z3_mk_probe(self.ctx.ref(), probe)
8771 except Z3Exception:
8772 raise Z3Exception("unknown probe '%s'" % probe)
8773 Z3_probe_inc_ref(self.ctx.ref(), self.probe)
8774
8775 def __deepcopy__(self, memo={}):
8776 return Probe(self.probe, self.ctx)
8777
8778 def __del__(self):
8779 if self.probe is not None and self.ctx.ref() is not None and Z3_probe_dec_ref is not None:
8780 Z3_probe_dec_ref(self.ctx.ref(), self.probe)
8781
8782 def __lt__(self, other):
8783 """Return a probe that evaluates to "true" when the value returned by `self`
8784 is less than the value returned by `other`.
8785
8786 >>> p = Probe('size') < 10
8787 >>> x = Int('x')
8788 >>> g = Goal()
8789 >>> g.add(x > 0)
8790 >>> g.add(x < 10)
8791 >>> p(g)
8792 1.0
8793 """
8794 return Probe(Z3_probe_lt(self.ctx.ref(), self.probe, _to_probe(other, self.ctx).probe), self.ctx)
8795
8796 def __gt__(self, other):
8797 """Return a probe that evaluates to "true" when the value returned by `self`
8798 is greater than the value returned by `other`.
8799
8800 >>> p = Probe('size') > 10
8801 >>> x = Int('x')
8802 >>> g = Goal()
8803 >>> g.add(x > 0)
8804 >>> g.add(x < 10)
8805 >>> p(g)
8806 0.0
8807 """
8808 return Probe(Z3_probe_gt(self.ctx.ref(), self.probe, _to_probe(other, self.ctx).probe), self.ctx)
8809
8810 def __le__(self, other):
8811 """Return a probe that evaluates to "true" when the value returned by `self`
8812 is less than or equal to the value returned by `other`.
8813
8814 >>> p = Probe('size') <= 2
8815 >>> x = Int('x')
8816 >>> g = Goal()
8817 >>> g.add(x > 0)
8818 >>> g.add(x < 10)
8819 >>> p(g)
8820 1.0
8821 """
8822 return Probe(Z3_probe_le(self.ctx.ref(), self.probe, _to_probe(other, self.ctx).probe), self.ctx)
8823
8824 def __ge__(self, other):
8825 """Return a probe that evaluates to "true" when the value returned by `self`
8826 is greater than or equal to the value returned by `other`.
8827
8828 >>> p = Probe('size') >= 2
8829 >>> x = Int('x')
8830 >>> g = Goal()
8831 >>> g.add(x > 0)
8832 >>> g.add(x < 10)
8833 >>> p(g)
8834 1.0
8835 """
8836 return Probe(Z3_probe_ge(self.ctx.ref(), self.probe, _to_probe(other, self.ctx).probe), self.ctx)
8837
8838 def __eq__(self, other):
8839 """Return a probe that evaluates to "true" when the value returned by `self`
8840 is equal to the value returned by `other`.
8841
8842 >>> p = Probe('size') == 2
8843 >>> x = Int('x')
8844 >>> g = Goal()
8845 >>> g.add(x > 0)
8846 >>> g.add(x < 10)
8847 >>> p(g)
8848 1.0
8849 """
8850 return Probe(Z3_probe_eq(self.ctx.ref(), self.probe, _to_probe(other, self.ctx).probe), self.ctx)
8851
8852 def __ne__(self, other):
8853 """Return a probe that evaluates to "true" when the value returned by `self`
8854 is not equal to the value returned by `other`.
8855
8856 >>> p = Probe('size') != 2
8857 >>> x = Int('x')
8858 >>> g = Goal()
8859 >>> g.add(x > 0)
8860 >>> g.add(x < 10)
8861 >>> p(g)
8862 0.0
8863 """
8864 p = self.__eq__(other)
8865 return Probe(Z3_probe_not(self.ctx.ref(), p.probe), self.ctx)
8866
8867 def __call__(self, goal):
8868 """Evaluate the probe `self` in the given goal.
8869
8870 >>> p = Probe('size')
8871 >>> x = Int('x')
8872 >>> g = Goal()
8873 >>> g.add(x > 0)
8874 >>> g.add(x < 10)
8875 >>> p(g)
8876 2.0
8877 >>> g.add(x < 20)
8878 >>> p(g)
8879 3.0
8880 >>> p = Probe('num-consts')
8881 >>> p(g)
8882 1.0
8883 >>> p = Probe('is-propositional')
8884 >>> p(g)
8885 0.0
8886 >>> p = Probe('is-qflia')
8887 >>> p(g)
8888 1.0
8889 """
8890 if z3_debug():
8891 _z3_assert(isinstance(goal, (Goal, BoolRef)), "Z3 Goal or Boolean expression expected")
8892 goal = _to_goal(goal)
8893 return Z3_probe_apply(self.ctx.ref(), self.probe, goal.goal)
8894
8895
8896def is_probe(p):
8897 """Return `True` if `p` is a Z3 probe.
8898
8899 >>> is_probe(Int('x'))
8900 False
8901 >>> is_probe(Probe('memory'))
8902 True
8903 """
8904 return isinstance(p, Probe)
8905
8906
8907def _to_probe(p, ctx=None):
8908 if is_probe(p):
8909 return p
8910 else:
8911 return Probe(p, ctx)
8912
8913
8914def probes(ctx=None):
8915 """Return a list of all available probes in Z3.
8916
8917 >>> l = probes()
8918 >>> l.count('memory') == 1
8919 True
8920 """
8921 ctx = _get_ctx(ctx)
8922 return [Z3_get_probe_name(ctx.ref(), i) for i in range(Z3_get_num_probes(ctx.ref()))]
8923
8924
8925def probe_description(name, ctx=None):
8926 """Return a short description for the probe named `name`.
8927
8928 >>> d = probe_description('memory')
8929 """
8930 ctx = _get_ctx(ctx)
8931 return Z3_probe_get_descr(ctx.ref(), name)
8932
8933
8934def describe_probes():
8935 """Display a (tabular) description of all available probes in Z3."""
8936 if in_html_mode():
8937 even = True
8938 print('<table border="1" cellpadding="2" cellspacing="0">')
8939 for p in probes():
8940 if even:
8941 print('<tr style="background-color:#CFCFCF">')
8942 even = False
8943 else:
8944 print("<tr>")
8945 even = True
8946 print("<td>%s</td><td>%s</td></tr>" % (p, insert_line_breaks(probe_description(p), 40)))
8947 print("</table>")
8948 else:
8949 for p in probes():
8950 print("%s : %s" % (p, probe_description(p)))
8951
8952
8953def _probe_nary(f, args, ctx):
8954 if z3_debug():
8955 _z3_assert(len(args) > 0, "At least one argument expected")
8956 num = len(args)
8957 r = _to_probe(args[0], ctx)
8958 for i in range(num - 1):
8959 r = Probe(f(ctx.ref(), r.probe, _to_probe(args[i + 1], ctx).probe), ctx)
8960 return r
8961
8962
8963def _probe_and(args, ctx):
8964 return _probe_nary(Z3_probe_and, args, ctx)
8965
8966
8967def _probe_or(args, ctx):
8968 return _probe_nary(Z3_probe_or, args, ctx)
8969
8970
8971def FailIf(p, ctx=None):
8972 """Return a tactic that fails if the probe `p` evaluates to true.
8973 Otherwise, it returns the input goal unmodified.
8974
8975 In the following example, the tactic applies 'simplify' if and only if there are
8976 more than 2 constraints in the goal.
8977
8978 >>> t = OrElse(FailIf(Probe('size') > 2), Tactic('simplify'))
8979 >>> x, y = Ints('x y')
8980 >>> g = Goal()
8981 >>> g.add(x > 0)
8982 >>> g.add(y > 0)
8983 >>> t(g)
8984 [[x > 0, y > 0]]
8985 >>> g.add(x == y + 1)
8986 >>> t(g)
8987 [[Not(x <= 0), Not(y <= 0), x == 1 + y]]
8988 """
8989 p = _to_probe(p, ctx)
8990 return Tactic(Z3_tactic_fail_if(p.ctx.ref(), p.probe), p.ctx)
8991
8992
8993def When(p, t, ctx=None):
8994 """Return a tactic that applies tactic `t` only if probe `p` evaluates to true.
8995 Otherwise, it returns the input goal unmodified.
8996
8997 >>> t = When(Probe('size') > 2, Tactic('simplify'))
8998 >>> x, y = Ints('x y')
8999 >>> g = Goal()
9000 >>> g.add(x > 0)
9001 >>> g.add(y > 0)
9002 >>> t(g)
9003 [[x > 0, y > 0]]
9004 >>> g.add(x == y + 1)
9005 >>> t(g)
9006 [[Not(x <= 0), Not(y <= 0), x == 1 + y]]
9007 """
9008 p = _to_probe(p, ctx)
9009 t = _to_tactic(t, ctx)
9010 return Tactic(Z3_tactic_when(t.ctx.ref(), p.probe, t.tactic), t.ctx)
9011
9012
9013def Cond(p, t1, t2, ctx=None):
9014 """Return a tactic that applies tactic `t1` to a goal if probe `p` evaluates to true, and `t2` otherwise.
9015
9016 >>> t = Cond(Probe('is-qfnra'), Tactic('qfnra'), Tactic('smt'))
9017 """
9018 p = _to_probe(p, ctx)
9019 t1 = _to_tactic(t1, ctx)
9020 t2 = _to_tactic(t2, ctx)
9021 return Tactic(Z3_tactic_cond(t1.ctx.ref(), p.probe, t1.tactic, t2.tactic), t1.ctx)
9022
9023#########################################
9024#
9025# Utils
9026#
9027#########################################
9028
9029
9030def simplify(a, *arguments, **keywords):
9031 """Simplify the expression `a` using the given options.
9032
9033 This function has many options. Use `help_simplify` to obtain the complete list.
9034
9035 >>> x = Int('x')
9036 >>> y = Int('y')
9037 >>> simplify(x + 1 + y + x + 1)
9038 2 + 2*x + y
9039 >>> simplify((x + 1)*(y + 1), som=True)
9040 1 + x + y + x*y
9041 >>> simplify(Distinct(x, y, 1), blast_distinct=True)
9042 And(Not(x == y), Not(x == 1), Not(y == 1))
9043 >>> simplify(And(x == 0, y == 1), elim_and=True)
9044 Not(Or(Not(x == 0), Not(y == 1)))
9045 """
9046 if z3_debug():
9047 _z3_assert(is_expr(a), "Z3 expression expected")
9048 if len(arguments) > 0 or len(keywords) > 0:
9049 p = args2params(arguments, keywords, a.ctx)
9050 return _to_expr_ref(Z3_simplify_ex(a.ctx_ref(), a.as_ast(), p.params), a.ctx)
9051 else:
9052 return _to_expr_ref(Z3_simplify(a.ctx_ref(), a.as_ast()), a.ctx)
9053
9054
9055def help_simplify():
9056 """Return a string describing all options available for Z3 `simplify` procedure."""
9057 print(Z3_simplify_get_help(main_ctx().ref()))
9058
9059
9060def simplify_param_descrs():
9061 """Return the set of parameter descriptions for Z3 `simplify` procedure."""
9062 return ParamDescrsRef(Z3_simplify_get_param_descrs(main_ctx().ref()), main_ctx())
9063
9064
9065def substitute(t, *m):
9066 """Apply substitution m on t, m is a list of pairs of the form (from, to).
9067 Every occurrence in t of from is replaced with to.
9068
9069 >>> x = Int('x')
9070 >>> y = Int('y')
9071 >>> substitute(x + 1, (x, y + 1))
9072 y + 1 + 1
9073 >>> f = Function('f', IntSort(), IntSort())
9074 >>> substitute(f(x) + f(y), (f(x), IntVal(1)), (f(y), IntVal(1)))
9075 1 + 1
9076 """
9077 if isinstance(m, tuple):
9078 m1 = _get_args(m)
9079 if isinstance(m1, list) and all(isinstance(p, tuple) for p in m1):
9080 m = m1
9081 if z3_debug():
9082 _z3_assert(is_expr(t), "Z3 expression expected")
9083 _z3_assert(
9084 all([isinstance(p, tuple) and is_expr(p[0]) and is_expr(p[1]) for p in m]),
9085 "Z3 invalid substitution, expression pairs expected.")
9086 _z3_assert(
9087 all([p[0].sort().eq(p[1].sort()) for p in m]),
9088 'Z3 invalid substitution, mismatching "from" and "to" sorts.')
9089 num = len(m)
9090 _from = (Ast * num)()
9091 _to = (Ast * num)()
9092 for i in range(num):
9093 _from[i] = m[i][0].as_ast()
9094 _to[i] = m[i][1].as_ast()
9095 return _to_expr_ref(Z3_substitute(t.ctx.ref(), t.as_ast(), num, _from, _to), t.ctx)
9096
9097
9098def substitute_vars(t, *m):
9099 """Substitute the free variables in t with the expression in m.
9100
9101 >>> v0 = Var(0, IntSort())
9102 >>> v1 = Var(1, IntSort())
9103 >>> x = Int('x')
9104 >>> f = Function('f', IntSort(), IntSort(), IntSort())
9105 >>> # replace v0 with x+1 and v1 with x
9106 >>> substitute_vars(f(v0, v1), x + 1, x)
9107 f(x + 1, x)
9108 """
9109 if z3_debug():
9110 _z3_assert(is_expr(t), "Z3 expression expected")
9111 _z3_assert(all([is_expr(n) for n in m]), "Z3 invalid substitution, list of expressions expected.")
9112 num = len(m)
9113 _to = (Ast * num)()
9114 for i in range(num):
9115 _to[i] = m[i].as_ast()
9116 return _to_expr_ref(Z3_substitute_vars(t.ctx.ref(), t.as_ast(), num, _to), t.ctx)
9117
9118def substitute_funs(t, *m):
9119 """Apply substitution m on t, m is a list of pairs of a function and expression (from, to)
9120 Every occurrence in to of the function from is replaced with the expression to.
9121 The expression to can have free variables, that refer to the arguments of from.
9122 For examples, see
9123 """
9124 if isinstance(m, tuple):
9125 m1 = _get_args(m)
9126 if isinstance(m1, list) and all(isinstance(p, tuple) for p in m1):
9127 m = m1
9128 if z3_debug():
9129 _z3_assert(is_expr(t), "Z3 expression expected")
9130 _z3_assert(all([isinstance(p, tuple) and is_func_decl(p[0]) and is_expr(p[1]) for p in m]), "Z3 invalid substitution, function pairs expected.")
9131 num = len(m)
9132 _from = (FuncDecl * num)()
9133 _to = (Ast * num)()
9134 for i in range(num):
9135 _from[i] = m[i][0].as_func_decl()
9136 _to[i] = m[i][1].as_ast()
9137 return _to_expr_ref(Z3_substitute_funs(t.ctx.ref(), t.as_ast(), num, _from, _to), t.ctx)
9138
9139
9140def Sum(*args):
9141 """Create the sum of the Z3 expressions.
9142
9143 >>> a, b, c = Ints('a b c')
9144 >>> Sum(a, b, c)
9145 a + b + c
9146 >>> Sum([a, b, c])
9147 a + b + c
9148 >>> A = IntVector('a', 5)
9149 >>> Sum(A)
9150 a__0 + a__1 + a__2 + a__3 + a__4
9151 """
9152 args = _get_args(args)
9153 if len(args) == 0:
9154 return 0
9155 ctx = _ctx_from_ast_arg_list(args)
9156 if ctx is None:
9157 return _reduce(lambda a, b: a + b, args, 0)
9158 args = _coerce_expr_list(args, ctx)
9159 if is_bv(args[0]):
9160 return _reduce(lambda a, b: a + b, args, 0)
9161 else:
9162 _args, sz = _to_ast_array(args)
9163 return ArithRef(Z3_mk_add(ctx.ref(), sz, _args), ctx)
9164
9165
9166def Product(*args):
9167 """Create the product of the Z3 expressions.
9168
9169 >>> a, b, c = Ints('a b c')
9170 >>> Product(a, b, c)
9171 a*b*c
9172 >>> Product([a, b, c])
9173 a*b*c
9174 >>> A = IntVector('a', 5)
9175 >>> Product(A)
9176 a__0*a__1*a__2*a__3*a__4
9177 """
9178 args = _get_args(args)
9179 if len(args) == 0:
9180 return 1
9181 ctx = _ctx_from_ast_arg_list(args)
9182 if ctx is None:
9183 return _reduce(lambda a, b: a * b, args, 1)
9184 args = _coerce_expr_list(args, ctx)
9185 if is_bv(args[0]):
9186 return _reduce(lambda a, b: a * b, args, 1)
9187 else:
9188 _args, sz = _to_ast_array(args)
9189 return ArithRef(Z3_mk_mul(ctx.ref(), sz, _args), ctx)
9190
9191def Abs(arg):
9192 """Create the absolute value of an arithmetic expression"""
9193 return If(arg > 0, arg, -arg)
9194
9195
9196def AtMost(*args):
9197 """Create an at-most Pseudo-Boolean k constraint.
9198
9199 >>> a, b, c = Bools('a b c')
9200 >>> f = AtMost(a, b, c, 2)
9201 """
9202 args = _get_args(args)
9203 if z3_debug():
9204 _z3_assert(len(args) > 1, "Non empty list of arguments expected")
9205 ctx = _ctx_from_ast_arg_list(args)
9206 if z3_debug():
9207 _z3_assert(ctx is not None, "At least one of the arguments must be a Z3 expression")
9208 args1 = _coerce_expr_list(args[:-1], ctx)
9209 k = args[-1]
9210 _args, sz = _to_ast_array(args1)
9211 return BoolRef(Z3_mk_atmost(ctx.ref(), sz, _args, k), ctx)
9212
9213
9214def AtLeast(*args):
9215 """Create an at-least Pseudo-Boolean k constraint.
9216
9217 >>> a, b, c = Bools('a b c')
9218 >>> f = AtLeast(a, b, c, 2)
9219 """
9220 args = _get_args(args)
9221 if z3_debug():
9222 _z3_assert(len(args) > 1, "Non empty list of arguments expected")
9223 ctx = _ctx_from_ast_arg_list(args)
9224 if z3_debug():
9225 _z3_assert(ctx is not None, "At least one of the arguments must be a Z3 expression")
9226 args1 = _coerce_expr_list(args[:-1], ctx)
9227 k = args[-1]
9228 _args, sz = _to_ast_array(args1)
9229 return BoolRef(Z3_mk_atleast(ctx.ref(), sz, _args, k), ctx)
9230
9231
9232def _reorder_pb_arg(arg):
9233 a, b = arg
9234 if not _is_int(b) and _is_int(a):
9235 return b, a
9236 return arg
9237
9238
9239def _pb_args_coeffs(args, default_ctx=None):
9240 args = _get_args_ast_list(args)
9241 if len(args) == 0:
9242 return _get_ctx(default_ctx), 0, (Ast * 0)(), (ctypes.c_int * 0)()
9243 args = [_reorder_pb_arg(arg) for arg in args]
9244 args, coeffs = zip(*args)
9245 if z3_debug():
9246 _z3_assert(len(args) > 0, "Non empty list of arguments expected")
9247 ctx = _ctx_from_ast_arg_list(args)
9248 if z3_debug():
9249 _z3_assert(ctx is not None, "At least one of the arguments must be a Z3 expression")
9250 args = _coerce_expr_list(args, ctx)
9251 _args, sz = _to_ast_array(args)
9252 _coeffs = (ctypes.c_int * len(coeffs))()
9253 for i in range(len(coeffs)):
9254 _z3_check_cint_overflow(coeffs[i], "coefficient")
9255 _coeffs[i] = coeffs[i]
9256 return ctx, sz, _args, _coeffs, args
9257
9258
9259def PbLe(args, k):
9260 """Create a Pseudo-Boolean inequality k constraint.
9261
9262 >>> a, b, c = Bools('a b c')
9263 >>> f = PbLe(((a,1),(b,3),(c,2)), 3)
9264 """
9265 _z3_check_cint_overflow(k, "k")
9266 ctx, sz, _args, _coeffs, args = _pb_args_coeffs(args)
9267 return BoolRef(Z3_mk_pble(ctx.ref(), sz, _args, _coeffs, k), ctx)
9268
9269
9270def PbGe(args, k):
9271 """Create a Pseudo-Boolean inequality k constraint.
9272
9273 >>> a, b, c = Bools('a b c')
9274 >>> f = PbGe(((a,1),(b,3),(c,2)), 3)
9275 """
9276 _z3_check_cint_overflow(k, "k")
9277 ctx, sz, _args, _coeffs, args = _pb_args_coeffs(args)
9278 return BoolRef(Z3_mk_pbge(ctx.ref(), sz, _args, _coeffs, k), ctx)
9279
9280
9281def PbEq(args, k, ctx=None):
9282 """Create a Pseudo-Boolean equality k constraint.
9283
9284 >>> a, b, c = Bools('a b c')
9285 >>> f = PbEq(((a,1),(b,3),(c,2)), 3)
9286 """
9287 _z3_check_cint_overflow(k, "k")
9288 ctx, sz, _args, _coeffs, args = _pb_args_coeffs(args)
9289 return BoolRef(Z3_mk_pbeq(ctx.ref(), sz, _args, _coeffs, k), ctx)
9290
9291
9292def solve(*args, **keywords):
9293 """Solve the constraints `*args`.
9294
9295 This is a simple function for creating demonstrations. It creates a solver,
9296 configure it using the options in `keywords`, adds the constraints
9297 in `args`, and invokes check.
9298
9299 >>> a = Int('a')
9300 >>> solve(a > 0, a < 2)
9301 [a = 1]
9302 """
9303 show = keywords.pop("show", False)
9304 s = Solver()
9305 s.set(**keywords)
9306 s.add(*args)
9307 if show:
9308 print(s)
9309 r = s.check()
9310 if r == unsat:
9311 print("no solution")
9312 elif r == unknown:
9313 print("failed to solve")
9314 try:
9315 print(s.model())
9316 except Z3Exception:
9317 return
9318 else:
9319 print(s.model())
9320
9321
9322def solve_using(s, *args, **keywords):
9323 """Solve the constraints `*args` using solver `s`.
9324
9325 This is a simple function for creating demonstrations. It is similar to `solve`,
9326 but it uses the given solver `s`.
9327 It configures solver `s` using the options in `keywords`, adds the constraints
9328 in `args`, and invokes check.
9329 """
9330 show = keywords.pop("show", False)
9331 if z3_debug():
9332 _z3_assert(isinstance(s, Solver), "Solver object expected")
9333 s.set(**keywords)
9334 s.add(*args)
9335 if show:
9336 print("Problem:")
9337 print(s)
9338 r = s.check()
9339 if r == unsat:
9340 print("no solution")
9341 elif r == unknown:
9342 print("failed to solve")
9343 try:
9344 print(s.model())
9345 except Z3Exception:
9346 return
9347 else:
9348 if show:
9349 print("Solution:")
9350 print(s.model())
9351
9352
9353def prove(claim, show=False, **keywords):
9354 """Try to prove the given claim.
9355
9356 This is a simple function for creating demonstrations. It tries to prove
9357 `claim` by showing the negation is unsatisfiable.
9358
9359 >>> p, q = Bools('p q')
9360 >>> prove(Not(And(p, q)) == Or(Not(p), Not(q)))
9361 proved
9362 """
9363 if z3_debug():
9364 _z3_assert(is_bool(claim), "Z3 Boolean expression expected")
9365 s = Solver()
9366 s.set(**keywords)
9367 s.add(Not(claim))
9368 if show:
9369 print(s)
9370 r = s.check()
9371 if r == unsat:
9372 print("proved")
9373 elif r == unknown:
9374 print("failed to prove")
9375 print(s.model())
9376 else:
9377 print("counterexample")
9378 print(s.model())
9379
9380
9381def _solve_html(*args, **keywords):
9382 """Version of function `solve` that renders HTML output."""
9383 show = keywords.pop("show", False)
9384 s = Solver()
9385 s.set(**keywords)
9386 s.add(*args)
9387 if show:
9388 print("<b>Problem:</b>")
9389 print(s)
9390 r = s.check()
9391 if r == unsat:
9392 print("<b>no solution</b>")
9393 elif r == unknown:
9394 print("<b>failed to solve</b>")
9395 try:
9396 print(s.model())
9397 except Z3Exception:
9398 return
9399 else:
9400 if show:
9401 print("<b>Solution:</b>")
9402 print(s.model())
9403
9404
9405def _solve_using_html(s, *args, **keywords):
9406 """Version of function `solve_using` that renders HTML."""
9407 show = keywords.pop("show", False)
9408 if z3_debug():
9409 _z3_assert(isinstance(s, Solver), "Solver object expected")
9410 s.set(**keywords)
9411 s.add(*args)
9412 if show:
9413 print("<b>Problem:</b>")
9414 print(s)
9415 r = s.check()
9416 if r == unsat:
9417 print("<b>no solution</b>")
9418 elif r == unknown:
9419 print("<b>failed to solve</b>")
9420 try:
9421 print(s.model())
9422 except Z3Exception:
9423 return
9424 else:
9425 if show:
9426 print("<b>Solution:</b>")
9427 print(s.model())
9428
9429
9430def _prove_html(claim, show=False, **keywords):
9431 """Version of function `prove` that renders HTML."""
9432 if z3_debug():
9433 _z3_assert(is_bool(claim), "Z3 Boolean expression expected")
9434 s = Solver()
9435 s.set(**keywords)
9436 s.add(Not(claim))
9437 if show:
9438 print(s)
9439 r = s.check()
9440 if r == unsat:
9441 print("<b>proved</b>")
9442 elif r == unknown:
9443 print("<b>failed to prove</b>")
9444 print(s.model())
9445 else:
9446 print("<b>counterexample</b>")
9447 print(s.model())
9448
9449
9450def _dict2sarray(sorts, ctx):
9451 sz = len(sorts)
9452 _names = (Symbol * sz)()
9453 _sorts = (Sort * sz)()
9454 i = 0
9455 for k in sorts:
9456 v = sorts[k]
9457 if z3_debug():
9458 _z3_assert(isinstance(k, str), "String expected")
9459 _z3_assert(is_sort(v), "Z3 sort expected")
9460 _names[i] = to_symbol(k, ctx)
9461 _sorts[i] = v.ast
9462 i = i + 1
9463 return sz, _names, _sorts
9464
9465
9466def _dict2darray(decls, ctx):
9467 sz = len(decls)
9468 _names = (Symbol * sz)()
9469 _decls = (FuncDecl * sz)()
9470 i = 0
9471 for k in decls:
9472 v = decls[k]
9473 if z3_debug():
9474 _z3_assert(isinstance(k, str), "String expected")
9475 _z3_assert(is_func_decl(v) or is_const(v), "Z3 declaration or constant expected")
9476 _names[i] = to_symbol(k, ctx)
9477 if is_const(v):
9478 _decls[i] = v.decl().ast
9479 else:
9480 _decls[i] = v.ast
9481 i = i + 1
9482 return sz, _names, _decls
9483
9484class ParserContext:
9485 def __init__(self, ctx= None):
9486 self.ctx = _get_ctx(ctx)
9487 self.pctx = Z3_mk_parser_context(self.ctx.ref())
9488 Z3_parser_context_inc_ref(self.ctx.ref(), self.pctx)
9489
9490 def __del__(self):
9491 if self.ctx.ref() is not None and self.pctx is not None and Z3_parser_context_dec_ref is not None:
9492 Z3_parser_context_dec_ref(self.ctx.ref(), self.pctx)
9493 self.pctx = None
9494
9495 def add_sort(self, sort):
9496 Z3_parser_context_add_sort(self.ctx.ref(), self.pctx, sort.as_ast())
9497
9498 def add_decl(self, decl):
9499 Z3_parser_context_add_decl(self.ctx.ref(), self.pctx, decl.as_ast())
9500
9501 def from_string(self, s):
9502 return AstVector(Z3_parser_context_from_string(self.ctx.ref(), self.pctx, s), self.ctx)
9503
9504def parse_smt2_string(s, sorts={}, decls={}, ctx=None):
9505 """Parse a string in SMT 2.0 format using the given sorts and decls.
9506
9507 The arguments sorts and decls are Python dictionaries used to initialize
9508 the symbol table used for the SMT 2.0 parser.
9509
9510 >>> parse_smt2_string('(declare-const x Int) (assert (> x 0)) (assert (< x 10))')
9511 [x > 0, x < 10]
9512 >>> x, y = Ints('x y')
9513 >>> f = Function('f', IntSort(), IntSort())
9514 >>> parse_smt2_string('(assert (> (+ foo (g bar)) 0))', decls={ 'foo' : x, 'bar' : y, 'g' : f})
9515 [x + f(y) > 0]
9516 >>> parse_smt2_string('(declare-const a U) (assert (> a 0))', sorts={ 'U' : IntSort() })
9517 [a > 0]
9518 """
9519 ctx = _get_ctx(ctx)
9520 ssz, snames, ssorts = _dict2sarray(sorts, ctx)
9521 dsz, dnames, ddecls = _dict2darray(decls, ctx)
9522 return AstVector(Z3_parse_smtlib2_string(ctx.ref(), s, ssz, snames, ssorts, dsz, dnames, ddecls), ctx)
9523
9524
9525def parse_smt2_file(f, sorts={}, decls={}, ctx=None):
9526 """Parse a file in SMT 2.0 format using the given sorts and decls.
9527
9528 This function is similar to parse_smt2_string().
9529 """
9530 ctx = _get_ctx(ctx)
9531 ssz, snames, ssorts = _dict2sarray(sorts, ctx)
9532 dsz, dnames, ddecls = _dict2darray(decls, ctx)
9533 return AstVector(Z3_parse_smtlib2_file(ctx.ref(), f, ssz, snames, ssorts, dsz, dnames, ddecls), ctx)
9534
9535
9536#########################################
9537#
9538# Floating-Point Arithmetic
9539#
9540#########################################
9541
9542
9543# Global default rounding mode
9544_dflt_rounding_mode = Z3_OP_FPA_RM_NEAREST_TIES_TO_EVEN
9545_dflt_fpsort_ebits = 11
9546_dflt_fpsort_sbits = 53
9547
9548
9549def get_default_rounding_mode(ctx=None):
9550 """Retrieves the global default rounding mode."""
9551 global _dflt_rounding_mode
9552 if _dflt_rounding_mode == Z3_OP_FPA_RM_TOWARD_ZERO:
9553 return RTZ(ctx)
9554 elif _dflt_rounding_mode == Z3_OP_FPA_RM_TOWARD_NEGATIVE:
9555 return RTN(ctx)
9556 elif _dflt_rounding_mode == Z3_OP_FPA_RM_TOWARD_POSITIVE:
9557 return RTP(ctx)
9558 elif _dflt_rounding_mode == Z3_OP_FPA_RM_NEAREST_TIES_TO_EVEN:
9559 return RNE(ctx)
9560 elif _dflt_rounding_mode == Z3_OP_FPA_RM_NEAREST_TIES_TO_AWAY:
9561 return RNA(ctx)
9562
9563
9564_ROUNDING_MODES = frozenset({
9565 Z3_OP_FPA_RM_TOWARD_ZERO,
9566 Z3_OP_FPA_RM_TOWARD_NEGATIVE,
9567 Z3_OP_FPA_RM_TOWARD_POSITIVE,
9568 Z3_OP_FPA_RM_NEAREST_TIES_TO_EVEN,
9569 Z3_OP_FPA_RM_NEAREST_TIES_TO_AWAY
9570})
9571
9572
9573def set_default_rounding_mode(rm, ctx=None):
9574 global _dflt_rounding_mode
9575 if is_fprm_value(rm):
9576 _dflt_rounding_mode = rm.kind()
9577 else:
9578 _z3_assert(_dflt_rounding_mode in _ROUNDING_MODES, "illegal rounding mode")
9579 _dflt_rounding_mode = rm
9580
9581
9582def get_default_fp_sort(ctx=None):
9583 return FPSort(_dflt_fpsort_ebits, _dflt_fpsort_sbits, ctx)
9584
9585
9586def set_default_fp_sort(ebits, sbits, ctx=None):
9587 global _dflt_fpsort_ebits
9588 global _dflt_fpsort_sbits
9589 _dflt_fpsort_ebits = ebits
9590 _dflt_fpsort_sbits = sbits
9591
9592
9593def _dflt_rm(ctx=None):
9594 return get_default_rounding_mode(ctx)
9595
9596
9597def _dflt_fps(ctx=None):
9598 return get_default_fp_sort(ctx)
9599
9600
9601def _coerce_fp_expr_list(alist, ctx):
9602 first_fp_sort = None
9603 for a in alist:
9604 if is_fp(a):
9605 if first_fp_sort is None:
9606 first_fp_sort = a.sort()
9607 elif first_fp_sort == a.sort():
9608 pass # OK, same as before
9609 else:
9610 # we saw at least 2 different float sorts; something will
9611 # throw a sort mismatch later, for now assume None.
9612 first_fp_sort = None
9613 break
9614
9615 r = []
9616 for i in range(len(alist)):
9617 a = alist[i]
9618 is_repr = isinstance(a, str) and a.contains("2**(") and a.endswith(")")
9619 if is_repr or _is_int(a) or isinstance(a, (float, bool)):
9620 r.append(FPVal(a, None, first_fp_sort, ctx))
9621 else:
9622 r.append(a)
9623 return _coerce_expr_list(r, ctx)
9624
9625
9626# FP Sorts
9627
9628class FPSortRef(SortRef):
9629 """Floating-point sort."""
9630
9631 def ebits(self):
9632 """Retrieves the number of bits reserved for the exponent in the FloatingPoint sort `self`.
9633 >>> b = FPSort(8, 24)
9634 >>> b.ebits()
9635 8
9636 """
9637 return int(Z3_fpa_get_ebits(self.ctx_ref(), self.ast))
9638
9639 def sbits(self):
9640 """Retrieves the number of bits reserved for the significand in the FloatingPoint sort `self`.
9641 >>> b = FPSort(8, 24)
9642 >>> b.sbits()
9643 24
9644 """
9645 return int(Z3_fpa_get_sbits(self.ctx_ref(), self.ast))
9646
9647 def cast(self, val):
9648 """Try to cast `val` as a floating-point expression.
9649 >>> b = FPSort(8, 24)
9650 >>> b.cast(1.0)
9651 1
9652 >>> b.cast(1.0).sexpr()
9653 '(fp #b0 #x7f #b00000000000000000000000)'
9654 """
9655 if is_expr(val):
9656 if z3_debug():
9657 _z3_assert(self.ctx == val.ctx, "Context mismatch")
9658 return val
9659 else:
9660 return FPVal(val, None, self, self.ctx)
9661
9662
9663def Float16(ctx=None):
9664 """Floating-point 16-bit (half) sort."""
9665 ctx = _get_ctx(ctx)
9666 return FPSortRef(Z3_mk_fpa_sort_16(ctx.ref()), ctx)
9667
9668
9669def FloatHalf(ctx=None):
9670 """Floating-point 16-bit (half) sort."""
9671 ctx = _get_ctx(ctx)
9672 return FPSortRef(Z3_mk_fpa_sort_half(ctx.ref()), ctx)
9673
9674
9675def Float32(ctx=None):
9676 """Floating-point 32-bit (single) sort."""
9677 ctx = _get_ctx(ctx)
9678 return FPSortRef(Z3_mk_fpa_sort_32(ctx.ref()), ctx)
9679
9680
9681def FloatSingle(ctx=None):
9682 """Floating-point 32-bit (single) sort."""
9683 ctx = _get_ctx(ctx)
9684 return FPSortRef(Z3_mk_fpa_sort_single(ctx.ref()), ctx)
9685
9686
9687def Float64(ctx=None):
9688 """Floating-point 64-bit (double) sort."""
9689 ctx = _get_ctx(ctx)
9690 return FPSortRef(Z3_mk_fpa_sort_64(ctx.ref()), ctx)
9691
9692
9693def FloatDouble(ctx=None):
9694 """Floating-point 64-bit (double) sort."""
9695 ctx = _get_ctx(ctx)
9696 return FPSortRef(Z3_mk_fpa_sort_double(ctx.ref()), ctx)
9697
9698
9699def Float128(ctx=None):
9700 """Floating-point 128-bit (quadruple) sort."""
9701 ctx = _get_ctx(ctx)
9702 return FPSortRef(Z3_mk_fpa_sort_128(ctx.ref()), ctx)
9703
9704
9705def FloatQuadruple(ctx=None):
9706 """Floating-point 128-bit (quadruple) sort."""
9707 ctx = _get_ctx(ctx)
9708 return FPSortRef(Z3_mk_fpa_sort_quadruple(ctx.ref()), ctx)
9709
9710
9711class FPRMSortRef(SortRef):
9712 """"Floating-point rounding mode sort."""
9713
9714
9715def is_fp_sort(s):
9716 """Return True if `s` is a Z3 floating-point sort.
9717
9718 >>> is_fp_sort(FPSort(8, 24))
9719 True
9720 >>> is_fp_sort(IntSort())
9721 False
9722 """
9723 return isinstance(s, FPSortRef)
9724
9725
9726def is_fprm_sort(s):
9727 """Return True if `s` is a Z3 floating-point rounding mode sort.
9728
9729 >>> is_fprm_sort(FPSort(8, 24))
9730 False
9731 >>> is_fprm_sort(RNE().sort())
9732 True
9733 """
9734 return isinstance(s, FPRMSortRef)
9735
9736# FP Expressions
9737
9738
9739class FPRef(ExprRef):
9740 """Floating-point expressions."""
9741
9742 def sort(self):
9743 """Return the sort of the floating-point expression `self`.
9744
9745 >>> x = FP('1.0', FPSort(8, 24))
9746 >>> x.sort()
9747 FPSort(8, 24)
9748 >>> x.sort() == FPSort(8, 24)
9749 True
9750 """
9751 return FPSortRef(Z3_get_sort(self.ctx_ref(), self.as_ast()), self.ctx)
9752
9753 def ebits(self):
9754 """Retrieves the number of bits reserved for the exponent in the FloatingPoint expression `self`.
9755 >>> b = FPSort(8, 24)
9756 >>> b.ebits()
9757 8
9758 """
9759 return self.sort().ebits()
9760
9761 def sbits(self):
9762 """Retrieves the number of bits reserved for the exponent in the FloatingPoint expression `self`.
9763 >>> b = FPSort(8, 24)
9764 >>> b.sbits()
9765 24
9766 """
9767 return self.sort().sbits()
9768
9769 def as_string(self):
9770 """Return a Z3 floating point expression as a Python string."""
9771 return Z3_ast_to_string(self.ctx_ref(), self.as_ast())
9772
9773 def __le__(self, other):
9774 return fpLEQ(self, other, self.ctx)
9775
9776 def __lt__(self, other):
9777 return fpLT(self, other, self.ctx)
9778
9779 def __ge__(self, other):
9780 return fpGEQ(self, other, self.ctx)
9781
9782 def __gt__(self, other):
9783 return fpGT(self, other, self.ctx)
9784
9785 def __add__(self, other):
9786 """Create the Z3 expression `self + other`.
9787
9788 >>> x = FP('x', FPSort(8, 24))
9789 >>> y = FP('y', FPSort(8, 24))
9790 >>> x + y
9791 x + y
9792 >>> (x + y).sort()
9793 FPSort(8, 24)
9794 """
9795 [a, b] = _coerce_fp_expr_list([self, other], self.ctx)
9796 return fpAdd(_dflt_rm(), a, b, self.ctx)
9797
9798 def __radd__(self, other):
9799 """Create the Z3 expression `other + self`.
9800
9801 >>> x = FP('x', FPSort(8, 24))
9802 >>> 10 + x
9803 1.25*(2**3) + x
9804 """
9805 [a, b] = _coerce_fp_expr_list([other, self], self.ctx)
9806 return fpAdd(_dflt_rm(), a, b, self.ctx)
9807
9808 def __sub__(self, other):
9809 """Create the Z3 expression `self - other`.
9810
9811 >>> x = FP('x', FPSort(8, 24))
9812 >>> y = FP('y', FPSort(8, 24))
9813 >>> x - y
9814 x - y
9815 >>> (x - y).sort()
9816 FPSort(8, 24)
9817 """
9818 [a, b] = _coerce_fp_expr_list([self, other], self.ctx)
9819 return fpSub(_dflt_rm(), a, b, self.ctx)
9820
9821 def __rsub__(self, other):
9822 """Create the Z3 expression `other - self`.
9823
9824 >>> x = FP('x', FPSort(8, 24))
9825 >>> 10 - x
9826 1.25*(2**3) - x
9827 """
9828 [a, b] = _coerce_fp_expr_list([other, self], self.ctx)
9829 return fpSub(_dflt_rm(), a, b, self.ctx)
9830
9831 def __mul__(self, other):
9832 """Create the Z3 expression `self * other`.
9833
9834 >>> x = FP('x', FPSort(8, 24))
9835 >>> y = FP('y', FPSort(8, 24))
9836 >>> x * y
9837 x * y
9838 >>> (x * y).sort()
9839 FPSort(8, 24)
9840 >>> 10 * y
9841 1.25*(2**3) * y
9842 """
9843 [a, b] = _coerce_fp_expr_list([self, other], self.ctx)
9844 return fpMul(_dflt_rm(), a, b, self.ctx)
9845
9846 def __rmul__(self, other):
9847 """Create the Z3 expression `other * self`.
9848
9849 >>> x = FP('x', FPSort(8, 24))
9850 >>> y = FP('y', FPSort(8, 24))
9851 >>> x * y
9852 x * y
9853 >>> x * 10
9854 x * 1.25*(2**3)
9855 """
9856 [a, b] = _coerce_fp_expr_list([other, self], self.ctx)
9857 return fpMul(_dflt_rm(), a, b, self.ctx)
9858
9859 def __pos__(self):
9860 """Create the Z3 expression `+self`."""
9861 return self
9862
9863 def __neg__(self):
9864 """Create the Z3 expression `-self`.
9865
9866 >>> x = FP('x', Float32())
9867 >>> -x
9868 -x
9869 """
9870 return fpNeg(self)
9871
9872 def __div__(self, other):
9873 """Create the Z3 expression `self / other`.
9874
9875 >>> x = FP('x', FPSort(8, 24))
9876 >>> y = FP('y', FPSort(8, 24))
9877 >>> x / y
9878 x / y
9879 >>> (x / y).sort()
9880 FPSort(8, 24)
9881 >>> 10 / y
9882 1.25*(2**3) / y
9883 """
9884 [a, b] = _coerce_fp_expr_list([self, other], self.ctx)
9885 return fpDiv(_dflt_rm(), a, b, self.ctx)
9886
9887 def __rdiv__(self, other):
9888 """Create the Z3 expression `other / self`.
9889
9890 >>> x = FP('x', FPSort(8, 24))
9891 >>> y = FP('y', FPSort(8, 24))
9892 >>> x / y
9893 x / y
9894 >>> x / 10
9895 x / 1.25*(2**3)
9896 """
9897 [a, b] = _coerce_fp_expr_list([other, self], self.ctx)
9898 return fpDiv(_dflt_rm(), a, b, self.ctx)
9899
9900 def __truediv__(self, other):
9901 """Create the Z3 expression division `self / other`."""
9902 return self.__div__(other)
9903
9904 def __rtruediv__(self, other):
9905 """Create the Z3 expression division `other / self`."""
9906 return self.__rdiv__(other)
9907
9908 def __mod__(self, other):
9909 """Create the Z3 expression mod `self % other`."""
9910 return fpRem(self, other)
9911
9912 def __rmod__(self, other):
9913 """Create the Z3 expression mod `other % self`."""
9914 return fpRem(other, self)
9915
9916
9917class FPRMRef(ExprRef):
9918 """Floating-point rounding mode expressions"""
9919
9920 def as_string(self):
9921 """Return a Z3 floating point expression as a Python string."""
9922 return Z3_ast_to_string(self.ctx_ref(), self.as_ast())
9923
9924
9925def RoundNearestTiesToEven(ctx=None):
9926 ctx = _get_ctx(ctx)
9927 return FPRMRef(Z3_mk_fpa_round_nearest_ties_to_even(ctx.ref()), ctx)
9928
9929
9930def RNE(ctx=None):
9931 ctx = _get_ctx(ctx)
9932 return FPRMRef(Z3_mk_fpa_round_nearest_ties_to_even(ctx.ref()), ctx)
9933
9934
9935def RoundNearestTiesToAway(ctx=None):
9936 ctx = _get_ctx(ctx)
9937 return FPRMRef(Z3_mk_fpa_round_nearest_ties_to_away(ctx.ref()), ctx)
9938
9939
9940def RNA(ctx=None):
9941 ctx = _get_ctx(ctx)
9942 return FPRMRef(Z3_mk_fpa_round_nearest_ties_to_away(ctx.ref()), ctx)
9943
9944
9945def RoundTowardPositive(ctx=None):
9946 ctx = _get_ctx(ctx)
9947 return FPRMRef(Z3_mk_fpa_round_toward_positive(ctx.ref()), ctx)
9948
9949
9950def RTP(ctx=None):
9951 ctx = _get_ctx(ctx)
9952 return FPRMRef(Z3_mk_fpa_round_toward_positive(ctx.ref()), ctx)
9953
9954
9955def RoundTowardNegative(ctx=None):
9956 ctx = _get_ctx(ctx)
9957 return FPRMRef(Z3_mk_fpa_round_toward_negative(ctx.ref()), ctx)
9958
9959
9960def RTN(ctx=None):
9961 ctx = _get_ctx(ctx)
9962 return FPRMRef(Z3_mk_fpa_round_toward_negative(ctx.ref()), ctx)
9963
9964
9965def RoundTowardZero(ctx=None):
9966 ctx = _get_ctx(ctx)
9967 return FPRMRef(Z3_mk_fpa_round_toward_zero(ctx.ref()), ctx)
9968
9969
9970def RTZ(ctx=None):
9971 ctx = _get_ctx(ctx)
9972 return FPRMRef(Z3_mk_fpa_round_toward_zero(ctx.ref()), ctx)
9973
9974
9975def is_fprm(a):
9976 """Return `True` if `a` is a Z3 floating-point rounding mode expression.
9977
9978 >>> rm = RNE()
9979 >>> is_fprm(rm)
9980 True
9981 >>> rm = 1.0
9982 >>> is_fprm(rm)
9983 False
9984 """
9985 return isinstance(a, FPRMRef)
9986
9987
9988def is_fprm_value(a):
9989 """Return `True` if `a` is a Z3 floating-point rounding mode numeral value."""
9990 return is_fprm(a) and _is_numeral(a.ctx, a.ast)
9991
9992# FP Numerals
9993
9994
9995class FPNumRef(FPRef):
9996 """The sign of the numeral.
9997
9998 >>> x = FPVal(+1.0, FPSort(8, 24))
9999 >>> x.sign()
10000 False
10001 >>> x = FPVal(-1.0, FPSort(8, 24))
10002 >>> x.sign()
10003 True
10004 """
10005
10006 def sign(self):
10007 num = (ctypes.c_int)()
10008 nsign = Z3_fpa_get_numeral_sign(self.ctx.ref(), self.as_ast(), byref(num))
10009 if nsign is False:
10010 raise Z3Exception("error retrieving the sign of a numeral.")
10011 return num.value != 0
10012
10013 """The sign of a floating-point numeral as a bit-vector expression.
10014
10015 Remark: NaN's are invalid arguments.
10016 """
10017
10018 def sign_as_bv(self):
10019 return BitVecNumRef(Z3_fpa_get_numeral_sign_bv(self.ctx.ref(), self.as_ast()), self.ctx)
10020
10021 """The significand of the numeral.
10022
10023 >>> x = FPVal(2.5, FPSort(8, 24))
10024 >>> x.significand()
10025 1.25
10026 """
10027
10028 def significand(self):
10029 return Z3_fpa_get_numeral_significand_string(self.ctx.ref(), self.as_ast())
10030
10031 """The significand of the numeral as a long.
10032
10033 >>> x = FPVal(2.5, FPSort(8, 24))
10034 >>> x.significand_as_long()
10035 1.25
10036 """
10037
10038 def significand_as_long(self):
10039 ptr = (ctypes.c_ulonglong * 1)()
10040 if not Z3_fpa_get_numeral_significand_uint64(self.ctx.ref(), self.as_ast(), ptr):
10041 raise Z3Exception("error retrieving the significand of a numeral.")
10042 return ptr[0]
10043
10044 """The significand of the numeral as a bit-vector expression.
10045
10046 Remark: NaN are invalid arguments.
10047 """
10048
10049 def significand_as_bv(self):
10050 return BitVecNumRef(Z3_fpa_get_numeral_significand_bv(self.ctx.ref(), self.as_ast()), self.ctx)
10051
10052 """The exponent of the numeral.
10053
10054 >>> x = FPVal(2.5, FPSort(8, 24))
10055 >>> x.exponent()
10056 1
10057 """
10058
10059 def exponent(self, biased=True):
10060 return Z3_fpa_get_numeral_exponent_string(self.ctx.ref(), self.as_ast(), biased)
10061
10062 """The exponent of the numeral as a long.
10063
10064 >>> x = FPVal(2.5, FPSort(8, 24))
10065 >>> x.exponent_as_long()
10066 1
10067 """
10068
10069 def exponent_as_long(self, biased=True):
10070 ptr = (ctypes.c_longlong * 1)()
10071 if not Z3_fpa_get_numeral_exponent_int64(self.ctx.ref(), self.as_ast(), ptr, biased):
10072 raise Z3Exception("error retrieving the exponent of a numeral.")
10073 return ptr[0]
10074
10075 """The exponent of the numeral as a bit-vector expression.
10076
10077 Remark: NaNs are invalid arguments.
10078 """
10079
10080 def exponent_as_bv(self, biased=True):
10081 return BitVecNumRef(Z3_fpa_get_numeral_exponent_bv(self.ctx.ref(), self.as_ast(), biased), self.ctx)
10082
10083 """Indicates whether the numeral is a NaN."""
10084
10085 def isNaN(self):
10086 return Z3_fpa_is_numeral_nan(self.ctx.ref(), self.as_ast())
10087
10088 """Indicates whether the numeral is +oo or -oo."""
10089
10090 def isInf(self):
10091 return Z3_fpa_is_numeral_inf(self.ctx.ref(), self.as_ast())
10092
10093 """Indicates whether the numeral is +zero or -zero."""
10094
10095 def isZero(self):
10096 return Z3_fpa_is_numeral_zero(self.ctx.ref(), self.as_ast())
10097
10098 """Indicates whether the numeral is normal."""
10099
10100 def isNormal(self):
10101 return Z3_fpa_is_numeral_normal(self.ctx.ref(), self.as_ast())
10102
10103 """Indicates whether the numeral is subnormal."""
10104
10105 def isSubnormal(self):
10106 return Z3_fpa_is_numeral_subnormal(self.ctx.ref(), self.as_ast())
10107
10108 """Indicates whether the numeral is positive."""
10109
10110 def isPositive(self):
10111 return Z3_fpa_is_numeral_positive(self.ctx.ref(), self.as_ast())
10112
10113 """Indicates whether the numeral is negative."""
10114
10115 def isNegative(self):
10116 return Z3_fpa_is_numeral_negative(self.ctx.ref(), self.as_ast())
10117
10118 """
10119 The string representation of the numeral.
10120
10121 >>> x = FPVal(20, FPSort(8, 24))
10122 >>> x.as_string()
10123 1.25*(2**4)
10124 """
10125
10126 def as_string(self):
10127 s = Z3_get_numeral_string(self.ctx.ref(), self.as_ast())
10128 return ("FPVal(%s, %s)" % (s, self.sort()))
10129
10130 def py_value(self):
10131 bv = simplify(fpToIEEEBV(self))
10132 binary = bv.py_value()
10133 if not isinstance(binary, int):
10134 return None
10135 # Decode the IEEE 754 binary representation
10136 import struct
10137 bytes_rep = binary.to_bytes(8, byteorder='big')
10138 return struct.unpack('>d', bytes_rep)[0]
10139
10140
10141def is_fp(a):
10142 """Return `True` if `a` is a Z3 floating-point expression.
10143
10144 >>> b = FP('b', FPSort(8, 24))
10145 >>> is_fp(b)
10146 True
10147 >>> is_fp(b + 1.0)
10148 True
10149 >>> is_fp(Int('x'))
10150 False
10151 """
10152 return isinstance(a, FPRef)
10153
10154
10155def is_fp_value(a):
10156 """Return `True` if `a` is a Z3 floating-point numeral value.
10157
10158 >>> b = FP('b', FPSort(8, 24))
10159 >>> is_fp_value(b)
10160 False
10161 >>> b = FPVal(1.0, FPSort(8, 24))
10162 >>> b
10163 1
10164 >>> is_fp_value(b)
10165 True
10166 """
10167 return is_fp(a) and _is_numeral(a.ctx, a.ast)
10168
10169
10170def FPSort(ebits, sbits, ctx=None):
10171 """Return a Z3 floating-point sort of the given sizes. If `ctx=None`, then the global context is used.
10172
10173 >>> Single = FPSort(8, 24)
10174 >>> Double = FPSort(11, 53)
10175 >>> Single
10176 FPSort(8, 24)
10177 >>> x = Const('x', Single)
10178 >>> eq(x, FP('x', FPSort(8, 24)))
10179 True
10180 """
10181 ctx = _get_ctx(ctx)
10182 return FPSortRef(Z3_mk_fpa_sort(ctx.ref(), ebits, sbits), ctx)
10183
10184
10185def _to_float_str(val, exp=0):
10186 if isinstance(val, float):
10187 if math.isnan(val):
10188 res = "NaN"
10189 elif val == 0.0:
10190 sone = math.copysign(1.0, val)
10191 if sone < 0.0:
10192 return "-0.0"
10193 else:
10194 return "+0.0"
10195 elif val == float("+inf"):
10196 res = "+oo"
10197 elif val == float("-inf"):
10198 res = "-oo"
10199 else:
10200 v = val.as_integer_ratio()
10201 num = v[0]
10202 den = v[1]
10203 rvs = str(num) + "/" + str(den)
10204 res = rvs + "p" + _to_int_str(exp)
10205 elif isinstance(val, bool):
10206 if val:
10207 res = "1.0"
10208 else:
10209 res = "0.0"
10210 elif _is_int(val):
10211 res = str(val)
10212 elif isinstance(val, str):
10213 inx = val.find("*(2**")
10214 if inx == -1:
10215 res = val
10216 elif val[-1] == ")":
10217 res = val[0:inx]
10218 exp = str(int(val[inx + 5:-1]) + int(exp))
10219 else:
10220 _z3_assert(False, "String does not have floating-point numeral form.")
10221 elif z3_debug():
10222 _z3_assert(False, "Python value cannot be used to create floating-point numerals.")
10223 if exp == 0:
10224 return res
10225 else:
10226 return res + "p" + exp
10227
10228
10229def fpNaN(s):
10230 """Create a Z3 floating-point NaN term.
10231
10232 >>> s = FPSort(8, 24)
10233 >>> set_fpa_pretty(True)
10234 >>> fpNaN(s)
10235 NaN
10236 >>> pb = get_fpa_pretty()
10237 >>> set_fpa_pretty(False)
10238 >>> fpNaN(s)
10239 fpNaN(FPSort(8, 24))
10240 >>> set_fpa_pretty(pb)
10241 """
10242 _z3_assert(isinstance(s, FPSortRef), "sort mismatch")
10243 return FPNumRef(Z3_mk_fpa_nan(s.ctx_ref(), s.ast), s.ctx)
10244
10245
10246def fpPlusInfinity(s):
10247 """Create a Z3 floating-point +oo term.
10248
10249 >>> s = FPSort(8, 24)
10250 >>> pb = get_fpa_pretty()
10251 >>> set_fpa_pretty(True)
10252 >>> fpPlusInfinity(s)
10253 +oo
10254 >>> set_fpa_pretty(False)
10255 >>> fpPlusInfinity(s)
10256 fpPlusInfinity(FPSort(8, 24))
10257 >>> set_fpa_pretty(pb)
10258 """
10259 _z3_assert(isinstance(s, FPSortRef), "sort mismatch")
10260 return FPNumRef(Z3_mk_fpa_inf(s.ctx_ref(), s.ast, False), s.ctx)
10261
10262
10263def fpMinusInfinity(s):
10264 """Create a Z3 floating-point -oo term."""
10265 _z3_assert(isinstance(s, FPSortRef), "sort mismatch")
10266 return FPNumRef(Z3_mk_fpa_inf(s.ctx_ref(), s.ast, True), s.ctx)
10267
10268
10269def fpInfinity(s, negative):
10270 """Create a Z3 floating-point +oo or -oo term."""
10271 _z3_assert(isinstance(s, FPSortRef), "sort mismatch")
10272 _z3_assert(isinstance(negative, bool), "expected Boolean flag")
10273 return FPNumRef(Z3_mk_fpa_inf(s.ctx_ref(), s.ast, negative), s.ctx)
10274
10275
10276def fpPlusZero(s):
10277 """Create a Z3 floating-point +0.0 term."""
10278 _z3_assert(isinstance(s, FPSortRef), "sort mismatch")
10279 return FPNumRef(Z3_mk_fpa_zero(s.ctx_ref(), s.ast, False), s.ctx)
10280
10281
10282def fpMinusZero(s):
10283 """Create a Z3 floating-point -0.0 term."""
10284 _z3_assert(isinstance(s, FPSortRef), "sort mismatch")
10285 return FPNumRef(Z3_mk_fpa_zero(s.ctx_ref(), s.ast, True), s.ctx)
10286
10287
10288def fpZero(s, negative):
10289 """Create a Z3 floating-point +0.0 or -0.0 term."""
10290 _z3_assert(isinstance(s, FPSortRef), "sort mismatch")
10291 _z3_assert(isinstance(negative, bool), "expected Boolean flag")
10292 return FPNumRef(Z3_mk_fpa_zero(s.ctx_ref(), s.ast, negative), s.ctx)
10293
10294
10295def FPVal(sig, exp=None, fps=None, ctx=None):
10296 """Return a floating-point value of value `val` and sort `fps`.
10297 If `ctx=None`, then the global context is used.
10298
10299 >>> v = FPVal(20.0, FPSort(8, 24))
10300 >>> v
10301 1.25*(2**4)
10302 >>> print("0x%.8x" % v.exponent_as_long(False))
10303 0x00000004
10304 >>> v = FPVal(2.25, FPSort(8, 24))
10305 >>> v
10306 1.125*(2**1)
10307 >>> v = FPVal(-2.25, FPSort(8, 24))
10308 >>> v
10309 -1.125*(2**1)
10310 >>> FPVal(-0.0, FPSort(8, 24))
10311 -0.0
10312 >>> FPVal(0.0, FPSort(8, 24))
10313 +0.0
10314 >>> FPVal(+0.0, FPSort(8, 24))
10315 +0.0
10316 """
10317 ctx = _get_ctx(ctx)
10318 if is_fp_sort(exp):
10319 fps = exp
10320 exp = None
10321 elif fps is None:
10322 fps = _dflt_fps(ctx)
10323 _z3_assert(is_fp_sort(fps), "sort mismatch")
10324 if exp is None:
10325 exp = 0
10326 val = _to_float_str(sig)
10327 if val == "NaN" or val == "nan":
10328 return fpNaN(fps)
10329 elif val == "-0.0":
10330 return fpMinusZero(fps)
10331 elif val == "0.0" or val == "+0.0":
10332 return fpPlusZero(fps)
10333 elif val == "+oo" or val == "+inf" or val == "+Inf":
10334 return fpPlusInfinity(fps)
10335 elif val == "-oo" or val == "-inf" or val == "-Inf":
10336 return fpMinusInfinity(fps)
10337 else:
10338 return FPNumRef(Z3_mk_numeral(ctx.ref(), val, fps.ast), ctx)
10339
10340
10341def FP(name, fpsort, ctx=None):
10342 """Return a floating-point constant named `name`.
10343 `fpsort` is the floating-point sort.
10344 If `ctx=None`, then the global context is used.
10345
10346 >>> x = FP('x', FPSort(8, 24))
10347 >>> is_fp(x)
10348 True
10349 >>> x.ebits()
10350 8
10351 >>> x.sort()
10352 FPSort(8, 24)
10353 >>> word = FPSort(8, 24)
10354 >>> x2 = FP('x', word)
10355 >>> eq(x, x2)
10356 True
10357 """
10358 if isinstance(fpsort, FPSortRef) and ctx is None:
10359 ctx = fpsort.ctx
10360 else:
10361 ctx = _get_ctx(ctx)
10362 return FPRef(Z3_mk_const(ctx.ref(), to_symbol(name, ctx), fpsort.ast), ctx)
10363
10364
10365def FPs(names, fpsort, ctx=None):
10366 """Return an array of floating-point constants.
10367
10368 >>> x, y, z = FPs('x y z', FPSort(8, 24))
10369 >>> x.sort()
10370 FPSort(8, 24)
10371 >>> x.sbits()
10372 24
10373 >>> x.ebits()
10374 8
10375 >>> fpMul(RNE(), fpAdd(RNE(), x, y), z)
10376 (x + y) * z
10377 """
10378 ctx = _get_ctx(ctx)
10379 if isinstance(names, str):
10380 names = names.split(" ")
10381 return [FP(name, fpsort, ctx) for name in names]
10382
10383
10384def fpAbs(a, ctx=None):
10385 """Create a Z3 floating-point absolute value expression.
10386
10387 >>> s = FPSort(8, 24)
10388 >>> rm = RNE()
10389 >>> x = FPVal(1.0, s)
10390 >>> fpAbs(x)
10391 fpAbs(1)
10392 >>> y = FPVal(-20.0, s)
10393 >>> y
10394 -1.25*(2**4)
10395 >>> fpAbs(y)
10396 fpAbs(-1.25*(2**4))
10397 >>> fpAbs(-1.25*(2**4))
10398 fpAbs(-1.25*(2**4))
10399 >>> fpAbs(x).sort()
10400 FPSort(8, 24)
10401 """
10402 ctx = _get_ctx(ctx)
10403 [a] = _coerce_fp_expr_list([a], ctx)
10404 return FPRef(Z3_mk_fpa_abs(ctx.ref(), a.as_ast()), ctx)
10405
10406
10407def fpNeg(a, ctx=None):
10408 """Create a Z3 floating-point addition expression.
10409
10410 >>> s = FPSort(8, 24)
10411 >>> rm = RNE()
10412 >>> x = FP('x', s)
10413 >>> fpNeg(x)
10414 -x
10415 >>> fpNeg(x).sort()
10416 FPSort(8, 24)
10417 """
10418 ctx = _get_ctx(ctx)
10419 [a] = _coerce_fp_expr_list([a], ctx)
10420 return FPRef(Z3_mk_fpa_neg(ctx.ref(), a.as_ast()), ctx)
10421
10422
10423def _mk_fp_unary(f, rm, a, ctx):
10424 ctx = _get_ctx(ctx)
10425 [a] = _coerce_fp_expr_list([a], ctx)
10426 if z3_debug():
10427 _z3_assert(is_fprm(rm), "First argument must be a Z3 floating-point rounding mode expression")
10428 _z3_assert(is_fp(a), "Second argument must be a Z3 floating-point expression")
10429 return FPRef(f(ctx.ref(), rm.as_ast(), a.as_ast()), ctx)
10430
10431
10432def _mk_fp_unary_pred(f, a, ctx):
10433 ctx = _get_ctx(ctx)
10434 [a] = _coerce_fp_expr_list([a], ctx)
10435 if z3_debug():
10436 _z3_assert(is_fp(a), "First argument must be a Z3 floating-point expression")
10437 return BoolRef(f(ctx.ref(), a.as_ast()), ctx)
10438
10439
10440def _mk_fp_bin(f, rm, a, b, ctx):
10441 ctx = _get_ctx(ctx)
10442 [a, b] = _coerce_fp_expr_list([a, b], ctx)
10443 if z3_debug():
10444 _z3_assert(is_fprm(rm), "First argument must be a Z3 floating-point rounding mode expression")
10445 _z3_assert(is_fp(a) or is_fp(b), "Second or third argument must be a Z3 floating-point expression")
10446 return FPRef(f(ctx.ref(), rm.as_ast(), a.as_ast(), b.as_ast()), ctx)
10447
10448
10449def _mk_fp_bin_norm(f, a, b, ctx):
10450 ctx = _get_ctx(ctx)
10451 [a, b] = _coerce_fp_expr_list([a, b], ctx)
10452 if z3_debug():
10453 _z3_assert(is_fp(a) or is_fp(b), "First or second argument must be a Z3 floating-point expression")
10454 return FPRef(f(ctx.ref(), a.as_ast(), b.as_ast()), ctx)
10455
10456
10457def _mk_fp_bin_pred(f, a, b, ctx):
10458 ctx = _get_ctx(ctx)
10459 [a, b] = _coerce_fp_expr_list([a, b], ctx)
10460 if z3_debug():
10461 _z3_assert(is_fp(a) or is_fp(b), "First or second argument must be a Z3 floating-point expression")
10462 return BoolRef(f(ctx.ref(), a.as_ast(), b.as_ast()), ctx)
10463
10464
10465def _mk_fp_tern(f, rm, a, b, c, ctx):
10466 ctx = _get_ctx(ctx)
10467 [a, b, c] = _coerce_fp_expr_list([a, b, c], ctx)
10468 if z3_debug():
10469 _z3_assert(is_fprm(rm), "First argument must be a Z3 floating-point rounding mode expression")
10470 _z3_assert(is_fp(a) or is_fp(b) or is_fp(
10471 c), "Second, third or fourth argument must be a Z3 floating-point expression")
10472 return FPRef(f(ctx.ref(), rm.as_ast(), a.as_ast(), b.as_ast(), c.as_ast()), ctx)
10473
10474
10475def fpAdd(rm, a, b, ctx=None):
10476 """Create a Z3 floating-point addition expression.
10477
10478 >>> s = FPSort(8, 24)
10479 >>> rm = RNE()
10480 >>> x = FP('x', s)
10481 >>> y = FP('y', s)
10482 >>> fpAdd(rm, x, y)
10483 x + y
10484 >>> fpAdd(RTZ(), x, y) # default rounding mode is RTZ
10485 fpAdd(RTZ(), x, y)
10486 >>> fpAdd(rm, x, y).sort()
10487 FPSort(8, 24)
10488 """
10489 return _mk_fp_bin(Z3_mk_fpa_add, rm, a, b, ctx)
10490
10491
10492def fpSub(rm, a, b, ctx=None):
10493 """Create a Z3 floating-point subtraction expression.
10494
10495 >>> s = FPSort(8, 24)
10496 >>> rm = RNE()
10497 >>> x = FP('x', s)
10498 >>> y = FP('y', s)
10499 >>> fpSub(rm, x, y)
10500 x - y
10501 >>> fpSub(rm, x, y).sort()
10502 FPSort(8, 24)
10503 """
10504 return _mk_fp_bin(Z3_mk_fpa_sub, rm, a, b, ctx)
10505
10506
10507def fpMul(rm, a, b, ctx=None):
10508 """Create a Z3 floating-point multiplication expression.
10509
10510 >>> s = FPSort(8, 24)
10511 >>> rm = RNE()
10512 >>> x = FP('x', s)
10513 >>> y = FP('y', s)
10514 >>> fpMul(rm, x, y)
10515 x * y
10516 >>> fpMul(rm, x, y).sort()
10517 FPSort(8, 24)
10518 """
10519 return _mk_fp_bin(Z3_mk_fpa_mul, rm, a, b, ctx)
10520
10521
10522def fpDiv(rm, a, b, ctx=None):
10523 """Create a Z3 floating-point division expression.
10524
10525 >>> s = FPSort(8, 24)
10526 >>> rm = RNE()
10527 >>> x = FP('x', s)
10528 >>> y = FP('y', s)
10529 >>> fpDiv(rm, x, y)
10530 x / y
10531 >>> fpDiv(rm, x, y).sort()
10532 FPSort(8, 24)
10533 """
10534 return _mk_fp_bin(Z3_mk_fpa_div, rm, a, b, ctx)
10535
10536
10537def fpRem(a, b, ctx=None):
10538 """Create a Z3 floating-point remainder expression.
10539
10540 >>> s = FPSort(8, 24)
10541 >>> x = FP('x', s)
10542 >>> y = FP('y', s)
10543 >>> fpRem(x, y)
10544 fpRem(x, y)
10545 >>> fpRem(x, y).sort()
10546 FPSort(8, 24)
10547 """
10548 return _mk_fp_bin_norm(Z3_mk_fpa_rem, a, b, ctx)
10549
10550
10551def fpMin(a, b, ctx=None):
10552 """Create a Z3 floating-point minimum expression.
10553
10554 >>> s = FPSort(8, 24)
10555 >>> rm = RNE()
10556 >>> x = FP('x', s)
10557 >>> y = FP('y', s)
10558 >>> fpMin(x, y)
10559 fpMin(x, y)
10560 >>> fpMin(x, y).sort()
10561 FPSort(8, 24)
10562 """
10563 return _mk_fp_bin_norm(Z3_mk_fpa_min, a, b, ctx)
10564
10565
10566def fpMax(a, b, ctx=None):
10567 """Create a Z3 floating-point maximum expression.
10568
10569 >>> s = FPSort(8, 24)
10570 >>> rm = RNE()
10571 >>> x = FP('x', s)
10572 >>> y = FP('y', s)
10573 >>> fpMax(x, y)
10574 fpMax(x, y)
10575 >>> fpMax(x, y).sort()
10576 FPSort(8, 24)
10577 """
10578 return _mk_fp_bin_norm(Z3_mk_fpa_max, a, b, ctx)
10579
10580
10581def fpFMA(rm, a, b, c, ctx=None):
10582 """Create a Z3 floating-point fused multiply-add expression.
10583 """
10584 return _mk_fp_tern(Z3_mk_fpa_fma, rm, a, b, c, ctx)
10585
10586
10587def fpSqrt(rm, a, ctx=None):
10588 """Create a Z3 floating-point square root expression.
10589 """
10590 return _mk_fp_unary(Z3_mk_fpa_sqrt, rm, a, ctx)
10591
10592
10593def fpRoundToIntegral(rm, a, ctx=None):
10594 """Create a Z3 floating-point roundToIntegral expression.
10595 """
10596 return _mk_fp_unary(Z3_mk_fpa_round_to_integral, rm, a, ctx)
10597
10598
10599def fpIsNaN(a, ctx=None):
10600 """Create a Z3 floating-point isNaN expression.
10601
10602 >>> s = FPSort(8, 24)
10603 >>> x = FP('x', s)
10604 >>> y = FP('y', s)
10605 >>> fpIsNaN(x)
10606 fpIsNaN(x)
10607 """
10608 return _mk_fp_unary_pred(Z3_mk_fpa_is_nan, a, ctx)
10609
10610
10611def fpIsInf(a, ctx=None):
10612 """Create a Z3 floating-point isInfinite expression.
10613
10614 >>> s = FPSort(8, 24)
10615 >>> x = FP('x', s)
10616 >>> fpIsInf(x)
10617 fpIsInf(x)
10618 """
10619 return _mk_fp_unary_pred(Z3_mk_fpa_is_infinite, a, ctx)
10620
10621
10622def fpIsZero(a, ctx=None):
10623 """Create a Z3 floating-point isZero expression.
10624 """
10625 return _mk_fp_unary_pred(Z3_mk_fpa_is_zero, a, ctx)
10626
10627
10628def fpIsNormal(a, ctx=None):
10629 """Create a Z3 floating-point isNormal expression.
10630 """
10631 return _mk_fp_unary_pred(Z3_mk_fpa_is_normal, a, ctx)
10632
10633
10634def fpIsSubnormal(a, ctx=None):
10635 """Create a Z3 floating-point isSubnormal expression.
10636 """
10637 return _mk_fp_unary_pred(Z3_mk_fpa_is_subnormal, a, ctx)
10638
10639
10640def fpIsNegative(a, ctx=None):
10641 """Create a Z3 floating-point isNegative expression.
10642 """
10643 return _mk_fp_unary_pred(Z3_mk_fpa_is_negative, a, ctx)
10644
10645
10646def fpIsPositive(a, ctx=None):
10647 """Create a Z3 floating-point isPositive expression.
10648 """
10649 return _mk_fp_unary_pred(Z3_mk_fpa_is_positive, a, ctx)
10650
10651
10652def _check_fp_args(a, b):
10653 if z3_debug():
10654 _z3_assert(is_fp(a) or is_fp(b), "First or second argument must be a Z3 floating-point expression")
10655
10656
10657def fpLT(a, b, ctx=None):
10658 """Create the Z3 floating-point expression `other < self`.
10659
10660 >>> x, y = FPs('x y', FPSort(8, 24))
10661 >>> fpLT(x, y)
10662 x < y
10663 >>> (x < y).sexpr()
10664 '(fp.lt x y)'
10665 """
10666 return _mk_fp_bin_pred(Z3_mk_fpa_lt, a, b, ctx)
10667
10668
10669def fpLEQ(a, b, ctx=None):
10670 """Create the Z3 floating-point expression `other <= self`.
10671
10672 >>> x, y = FPs('x y', FPSort(8, 24))
10673 >>> fpLEQ(x, y)
10674 x <= y
10675 >>> (x <= y).sexpr()
10676 '(fp.leq x y)'
10677 """
10678 return _mk_fp_bin_pred(Z3_mk_fpa_leq, a, b, ctx)
10679
10680
10681def fpGT(a, b, ctx=None):
10682 """Create the Z3 floating-point expression `other > self`.
10683
10684 >>> x, y = FPs('x y', FPSort(8, 24))
10685 >>> fpGT(x, y)
10686 x > y
10687 >>> (x > y).sexpr()
10688 '(fp.gt x y)'
10689 """
10690 return _mk_fp_bin_pred(Z3_mk_fpa_gt, a, b, ctx)
10691
10692
10693def fpGEQ(a, b, ctx=None):
10694 """Create the Z3 floating-point expression `other >= self`.
10695
10696 >>> x, y = FPs('x y', FPSort(8, 24))
10697 >>> fpGEQ(x, y)
10698 x >= y
10699 >>> (x >= y).sexpr()
10700 '(fp.geq x y)'
10701 """
10702 return _mk_fp_bin_pred(Z3_mk_fpa_geq, a, b, ctx)
10703
10704
10705def fpEQ(a, b, ctx=None):
10706 """Create the Z3 floating-point expression `fpEQ(other, self)`.
10707
10708 >>> x, y = FPs('x y', FPSort(8, 24))
10709 >>> fpEQ(x, y)
10710 fpEQ(x, y)
10711 >>> fpEQ(x, y).sexpr()
10712 '(fp.eq x y)'
10713 """
10714 return _mk_fp_bin_pred(Z3_mk_fpa_eq, a, b, ctx)
10715
10716
10717def fpNEQ(a, b, ctx=None):
10718 """Create the Z3 floating-point expression `Not(fpEQ(other, self))`.
10719
10720 >>> x, y = FPs('x y', FPSort(8, 24))
10721 >>> fpNEQ(x, y)
10722 Not(fpEQ(x, y))
10723 >>> (x != y).sexpr()
10724 '(distinct x y)'
10725 """
10726 return Not(fpEQ(a, b, ctx))
10727
10728
10729def fpFP(sgn, exp, sig, ctx=None):
10730 """Create the Z3 floating-point value `fpFP(sgn, sig, exp)` from the three bit-vectors sgn, sig, and exp.
10731
10732 >>> s = FPSort(8, 24)
10733 >>> x = fpFP(BitVecVal(1, 1), BitVecVal(2**7-1, 8), BitVecVal(2**22, 23))
10734 >>> print(x)
10735 fpFP(1, 127, 4194304)
10736 >>> xv = FPVal(-1.5, s)
10737 >>> print(xv)
10738 -1.5
10739 >>> slvr = Solver()
10740 >>> slvr.add(fpEQ(x, xv))
10741 >>> slvr.check()
10742 sat
10743 >>> xv = FPVal(+1.5, s)
10744 >>> print(xv)
10745 1.5
10746 >>> slvr = Solver()
10747 >>> slvr.add(fpEQ(x, xv))
10748 >>> slvr.check()
10749 unsat
10750 """
10751 _z3_assert(is_bv(sgn) and is_bv(exp) and is_bv(sig), "sort mismatch")
10752 _z3_assert(sgn.sort().size() == 1, "sort mismatch")
10753 ctx = _get_ctx(ctx)
10754 _z3_assert(ctx == sgn.ctx == exp.ctx == sig.ctx, "context mismatch")
10755 return FPRef(Z3_mk_fpa_fp(ctx.ref(), sgn.ast, exp.ast, sig.ast), ctx)
10756
10757
10758def fpToFP(a1, a2=None, a3=None, ctx=None):
10759 """Create a Z3 floating-point conversion expression from other term sorts
10760 to floating-point.
10761
10762 From a bit-vector term in IEEE 754-2008 format:
10763 >>> x = FPVal(1.0, Float32())
10764 >>> x_bv = fpToIEEEBV(x)
10765 >>> simplify(fpToFP(x_bv, Float32()))
10766 1
10767
10768 From a floating-point term with different precision:
10769 >>> x = FPVal(1.0, Float32())
10770 >>> x_db = fpToFP(RNE(), x, Float64())
10771 >>> x_db.sort()
10772 FPSort(11, 53)
10773
10774 From a real term:
10775 >>> x_r = RealVal(1.5)
10776 >>> simplify(fpToFP(RNE(), x_r, Float32()))
10777 1.5
10778
10779 From a signed bit-vector term:
10780 >>> x_signed = BitVecVal(-5, BitVecSort(32))
10781 >>> simplify(fpToFP(RNE(), x_signed, Float32()))
10782 -1.25*(2**2)
10783 """
10784 ctx = _get_ctx(ctx)
10785 if is_bv(a1) and is_fp_sort(a2):
10786 return FPRef(Z3_mk_fpa_to_fp_bv(ctx.ref(), a1.ast, a2.ast), ctx)
10787 elif is_fprm(a1) and is_fp(a2) and is_fp_sort(a3):
10788 return FPRef(Z3_mk_fpa_to_fp_float(ctx.ref(), a1.ast, a2.ast, a3.ast), ctx)
10789 elif is_fprm(a1) and is_real(a2) and is_fp_sort(a3):
10790 return FPRef(Z3_mk_fpa_to_fp_real(ctx.ref(), a1.ast, a2.ast, a3.ast), ctx)
10791 elif is_fprm(a1) and is_bv(a2) and is_fp_sort(a3):
10792 return FPRef(Z3_mk_fpa_to_fp_signed(ctx.ref(), a1.ast, a2.ast, a3.ast), ctx)
10793 else:
10794 raise Z3Exception("Unsupported combination of arguments for conversion to floating-point term.")
10795
10796
10797def fpBVToFP(v, sort, ctx=None):
10798 """Create a Z3 floating-point conversion expression that represents the
10799 conversion from a bit-vector term to a floating-point term.
10800
10801 >>> x_bv = BitVecVal(0x3F800000, 32)
10802 >>> x_fp = fpBVToFP(x_bv, Float32())
10803 >>> x_fp
10804 fpToFP(1065353216)
10805 >>> simplify(x_fp)
10806 1
10807 """
10808 _z3_assert(is_bv(v), "First argument must be a Z3 bit-vector expression")
10809 _z3_assert(is_fp_sort(sort), "Second argument must be a Z3 floating-point sort.")
10810 ctx = _get_ctx(ctx)
10811 return FPRef(Z3_mk_fpa_to_fp_bv(ctx.ref(), v.ast, sort.ast), ctx)
10812
10813
10814def fpFPToFP(rm, v, sort, ctx=None):
10815 """Create a Z3 floating-point conversion expression that represents the
10816 conversion from a floating-point term to a floating-point term of different precision.
10817
10818 >>> x_sgl = FPVal(1.0, Float32())
10819 >>> x_dbl = fpFPToFP(RNE(), x_sgl, Float64())
10820 >>> x_dbl
10821 fpToFP(RNE(), 1)
10822 >>> simplify(x_dbl)
10823 1
10824 >>> x_dbl.sort()
10825 FPSort(11, 53)
10826 """
10827 _z3_assert(is_fprm(rm), "First argument must be a Z3 floating-point rounding mode expression.")
10828 _z3_assert(is_fp(v), "Second argument must be a Z3 floating-point expression.")
10829 _z3_assert(is_fp_sort(sort), "Third argument must be a Z3 floating-point sort.")
10830 ctx = _get_ctx(ctx)
10831 return FPRef(Z3_mk_fpa_to_fp_float(ctx.ref(), rm.ast, v.ast, sort.ast), ctx)
10832
10833
10834def fpRealToFP(rm, v, sort, ctx=None):
10835 """Create a Z3 floating-point conversion expression that represents the
10836 conversion from a real term to a floating-point term.
10837
10838 >>> x_r = RealVal(1.5)
10839 >>> x_fp = fpRealToFP(RNE(), x_r, Float32())
10840 >>> x_fp
10841 fpToFP(RNE(), 3/2)
10842 >>> simplify(x_fp)
10843 1.5
10844 """
10845 _z3_assert(is_fprm(rm), "First argument must be a Z3 floating-point rounding mode expression.")
10846 _z3_assert(is_real(v), "Second argument must be a Z3 expression or real sort.")
10847 _z3_assert(is_fp_sort(sort), "Third argument must be a Z3 floating-point sort.")
10848 ctx = _get_ctx(ctx)
10849 return FPRef(Z3_mk_fpa_to_fp_real(ctx.ref(), rm.ast, v.ast, sort.ast), ctx)
10850
10851
10852def fpSignedToFP(rm, v, sort, ctx=None):
10853 """Create a Z3 floating-point conversion expression that represents the
10854 conversion from a signed bit-vector term (encoding an integer) to a floating-point term.
10855
10856 >>> x_signed = BitVecVal(-5, BitVecSort(32))
10857 >>> x_fp = fpSignedToFP(RNE(), x_signed, Float32())
10858 >>> x_fp
10859 fpToFP(RNE(), 4294967291)
10860 >>> simplify(x_fp)
10861 -1.25*(2**2)
10862 """
10863 _z3_assert(is_fprm(rm), "First argument must be a Z3 floating-point rounding mode expression.")
10864 _z3_assert(is_bv(v), "Second argument must be a Z3 bit-vector expression")
10865 _z3_assert(is_fp_sort(sort), "Third argument must be a Z3 floating-point sort.")
10866 ctx = _get_ctx(ctx)
10867 return FPRef(Z3_mk_fpa_to_fp_signed(ctx.ref(), rm.ast, v.ast, sort.ast), ctx)
10868
10869
10870def fpUnsignedToFP(rm, v, sort, ctx=None):
10871 """Create a Z3 floating-point conversion expression that represents the
10872 conversion from an unsigned bit-vector term (encoding an integer) to a floating-point term.
10873
10874 >>> x_signed = BitVecVal(-5, BitVecSort(32))
10875 >>> x_fp = fpUnsignedToFP(RNE(), x_signed, Float32())
10876 >>> x_fp
10877 fpToFPUnsigned(RNE(), 4294967291)
10878 >>> simplify(x_fp)
10879 1*(2**32)
10880 """
10881 _z3_assert(is_fprm(rm), "First argument must be a Z3 floating-point rounding mode expression.")
10882 _z3_assert(is_bv(v), "Second argument must be a Z3 bit-vector expression")
10883 _z3_assert(is_fp_sort(sort), "Third argument must be a Z3 floating-point sort.")
10884 ctx = _get_ctx(ctx)
10885 return FPRef(Z3_mk_fpa_to_fp_unsigned(ctx.ref(), rm.ast, v.ast, sort.ast), ctx)
10886
10887
10888def fpToFPUnsigned(rm, x, s, ctx=None):
10889 """Create a Z3 floating-point conversion expression, from unsigned bit-vector to floating-point expression."""
10890 if z3_debug():
10891 _z3_assert(is_fprm(rm), "First argument must be a Z3 floating-point rounding mode expression")
10892 _z3_assert(is_bv(x), "Second argument must be a Z3 bit-vector expression")
10893 _z3_assert(is_fp_sort(s), "Third argument must be Z3 floating-point sort")
10894 ctx = _get_ctx(ctx)
10895 return FPRef(Z3_mk_fpa_to_fp_unsigned(ctx.ref(), rm.ast, x.ast, s.ast), ctx)
10896
10897
10898def fpToSBV(rm, x, s, ctx=None):
10899 """Create a Z3 floating-point conversion expression, from floating-point expression to signed bit-vector.
10900
10901 >>> x = FP('x', FPSort(8, 24))
10902 >>> y = fpToSBV(RTZ(), x, BitVecSort(32))
10903 >>> print(is_fp(x))
10904 True
10905 >>> print(is_bv(y))
10906 True
10907 >>> print(is_fp(y))
10908 False
10909 >>> print(is_bv(x))
10910 False
10911 """
10912 if z3_debug():
10913 _z3_assert(is_fprm(rm), "First argument must be a Z3 floating-point rounding mode expression")
10914 _z3_assert(is_fp(x), "Second argument must be a Z3 floating-point expression")
10915 _z3_assert(is_bv_sort(s), "Third argument must be Z3 bit-vector sort")
10916 ctx = _get_ctx(ctx)
10917 return BitVecRef(Z3_mk_fpa_to_sbv(ctx.ref(), rm.ast, x.ast, s.size()), ctx)
10918
10919
10920def fpToUBV(rm, x, s, ctx=None):
10921 """Create a Z3 floating-point conversion expression, from floating-point expression to unsigned bit-vector.
10922
10923 >>> x = FP('x', FPSort(8, 24))
10924 >>> y = fpToUBV(RTZ(), x, BitVecSort(32))
10925 >>> print(is_fp(x))
10926 True
10927 >>> print(is_bv(y))
10928 True
10929 >>> print(is_fp(y))
10930 False
10931 >>> print(is_bv(x))
10932 False
10933 """
10934 if z3_debug():
10935 _z3_assert(is_fprm(rm), "First argument must be a Z3 floating-point rounding mode expression")
10936 _z3_assert(is_fp(x), "Second argument must be a Z3 floating-point expression")
10937 _z3_assert(is_bv_sort(s), "Third argument must be Z3 bit-vector sort")
10938 ctx = _get_ctx(ctx)
10939 return BitVecRef(Z3_mk_fpa_to_ubv(ctx.ref(), rm.ast, x.ast, s.size()), ctx)
10940
10941
10942def fpToReal(x, ctx=None):
10943 """Create a Z3 floating-point conversion expression, from floating-point expression to real.
10944
10945 >>> x = FP('x', FPSort(8, 24))
10946 >>> y = fpToReal(x)
10947 >>> print(is_fp(x))
10948 True
10949 >>> print(is_real(y))
10950 True
10951 >>> print(is_fp(y))
10952 False
10953 >>> print(is_real(x))
10954 False
10955 """
10956 if z3_debug():
10957 _z3_assert(is_fp(x), "First argument must be a Z3 floating-point expression")
10958 ctx = _get_ctx(ctx)
10959 return ArithRef(Z3_mk_fpa_to_real(ctx.ref(), x.ast), ctx)
10960
10961
10962def fpToIEEEBV(x, ctx=None):
10963 """\brief Conversion of a floating-point term into a bit-vector term in IEEE 754-2008 format.
10964
10965 The size of the resulting bit-vector is automatically determined.
10966
10967 Note that IEEE 754-2008 allows multiple different representations of NaN. This conversion
10968 knows only one NaN and it will always produce the same bit-vector representation of
10969 that NaN.
10970
10971 >>> x = FP('x', FPSort(8, 24))
10972 >>> y = fpToIEEEBV(x)
10973 >>> print(is_fp(x))
10974 True
10975 >>> print(is_bv(y))
10976 True
10977 >>> print(is_fp(y))
10978 False
10979 >>> print(is_bv(x))
10980 False
10981 """
10982 if z3_debug():
10983 _z3_assert(is_fp(x), "First argument must be a Z3 floating-point expression")
10984 ctx = _get_ctx(ctx)
10985 return BitVecRef(Z3_mk_fpa_to_ieee_bv(ctx.ref(), x.ast), ctx)
10986
10987
10988#########################################
10989#
10990# Strings, Sequences and Regular expressions
10991#
10992#########################################
10993
10994class SeqSortRef(SortRef):
10995 """Sequence sort."""
10996
10997 def is_string(self):
10998 """Determine if sort is a string
10999 >>> s = StringSort()
11000 >>> s.is_string()
11001 True
11002 >>> s = SeqSort(IntSort())
11003 >>> s.is_string()
11004 False
11005 """
11006 return Z3_is_string_sort(self.ctx_ref(), self.ast)
11007
11008 def basis(self):
11009 return _to_sort_ref(Z3_get_seq_sort_basis(self.ctx_ref(), self.ast), self.ctx)
11010
11011class CharSortRef(SortRef):
11012 """Character sort."""
11013
11014
11015def StringSort(ctx=None):
11016 """Create a string sort
11017 >>> s = StringSort()
11018 >>> print(s)
11019 String
11020 """
11021 ctx = _get_ctx(ctx)
11022 return SeqSortRef(Z3_mk_string_sort(ctx.ref()), ctx)
11023
11024def CharSort(ctx=None):
11025 """Create a character sort
11026 >>> ch = CharSort()
11027 >>> print(ch)
11028 Char
11029 """
11030 ctx = _get_ctx(ctx)
11031 return CharSortRef(Z3_mk_char_sort(ctx.ref()), ctx)
11032
11033
11034def SeqSort(s):
11035 """Create a sequence sort over elements provided in the argument
11036 >>> s = SeqSort(IntSort())
11037 >>> s == Unit(IntVal(1)).sort()
11038 True
11039 """
11040 return SeqSortRef(Z3_mk_seq_sort(s.ctx_ref(), s.ast), s.ctx)
11041
11042
11043class SeqRef(ExprRef):
11044 """Sequence expression."""
11045
11046 def sort(self):
11047 return SeqSortRef(Z3_get_sort(self.ctx_ref(), self.as_ast()), self.ctx)
11048
11049 def __add__(self, other):
11050 return Concat(self, other)
11051
11052 def __radd__(self, other):
11053 return Concat(other, self)
11054
11055 def __getitem__(self, i):
11056 if _is_int(i):
11057 i = IntVal(i, self.ctx)
11058 return _to_expr_ref(Z3_mk_seq_nth(self.ctx_ref(), self.as_ast(), i.as_ast()), self.ctx)
11059
11060 def at(self, i):
11061 if _is_int(i):
11062 i = IntVal(i, self.ctx)
11063 return SeqRef(Z3_mk_seq_at(self.ctx_ref(), self.as_ast(), i.as_ast()), self.ctx)
11064
11065 def is_string(self):
11066 return Z3_is_string_sort(self.ctx_ref(), Z3_get_sort(self.ctx_ref(), self.as_ast()))
11067
11068 def is_string_value(self):
11069 return Z3_is_string(self.ctx_ref(), self.as_ast())
11070
11071 def as_string(self):
11072 """Return a string representation of sequence expression."""
11073 if self.is_string_value():
11074 string_length = ctypes.c_uint()
11075 chars = Z3_get_lstring(self.ctx_ref(), self.as_ast(), byref(string_length))
11076 return string_at(chars, size=string_length.value).decode("latin-1")
11077 return Z3_ast_to_string(self.ctx_ref(), self.as_ast())
11078
11079 def py_value(self):
11080 return self.as_string()
11081
11082 def __le__(self, other):
11083 return _to_expr_ref(Z3_mk_str_le(self.ctx_ref(), self.as_ast(), other.as_ast()), self.ctx)
11084
11085 def __lt__(self, other):
11086 return _to_expr_ref(Z3_mk_str_lt(self.ctx_ref(), self.as_ast(), other.as_ast()), self.ctx)
11087
11088 def __ge__(self, other):
11089 return _to_expr_ref(Z3_mk_str_le(self.ctx_ref(), other.as_ast(), self.as_ast()), self.ctx)
11090
11091 def __gt__(self, other):
11092 return _to_expr_ref(Z3_mk_str_lt(self.ctx_ref(), other.as_ast(), self.as_ast()), self.ctx)
11093
11094
11095def _coerce_char(ch, ctx=None):
11096 if isinstance(ch, str):
11097 ctx = _get_ctx(ctx)
11098 ch = CharVal(ch, ctx)
11099 if not is_expr(ch):
11100 raise Z3Exception("Character expression expected")
11101 return ch
11102
11103class CharRef(ExprRef):
11104 """Character expression."""
11105
11106 def __le__(self, other):
11107 other = _coerce_char(other, self.ctx)
11108 return _to_expr_ref(Z3_mk_char_le(self.ctx_ref(), self.as_ast(), other.as_ast()), self.ctx)
11109
11110 def to_int(self):
11111 return _to_expr_ref(Z3_mk_char_to_int(self.ctx_ref(), self.as_ast()), self.ctx)
11112
11113 def to_bv(self):
11114 return _to_expr_ref(Z3_mk_char_to_bv(self.ctx_ref(), self.as_ast()), self.ctx)
11115
11116 def is_digit(self):
11117 return _to_expr_ref(Z3_mk_char_is_digit(self.ctx_ref(), self.as_ast()), self.ctx)
11118
11119
11120def CharVal(ch, ctx=None):
11121 ctx = _get_ctx(ctx)
11122 if isinstance(ch, str):
11123 ch = ord(ch)
11124 if not isinstance(ch, int):
11125 raise Z3Exception("character value should be an ordinal")
11126 return _to_expr_ref(Z3_mk_char(ctx.ref(), ch), ctx)
11127
11128def CharFromBv(bv):
11129 if not is_expr(bv):
11130 raise Z3Exception("Bit-vector expression needed")
11131 return _to_expr_ref(Z3_mk_char_from_bv(bv.ctx_ref(), bv.as_ast()), bv.ctx)
11132
11133def CharToBv(ch, ctx=None):
11134 ch = _coerce_char(ch, ctx)
11135 return ch.to_bv()
11136
11137def CharToInt(ch, ctx=None):
11138 ch = _coerce_char(ch, ctx)
11139 return ch.to_int()
11140
11141def CharIsDigit(ch, ctx=None):
11142 ch = _coerce_char(ch, ctx)
11143 return ch.is_digit()
11144
11145def _coerce_seq(s, ctx=None):
11146 if isinstance(s, str):
11147 ctx = _get_ctx(ctx)
11148 s = StringVal(s, ctx)
11149 if not is_expr(s):
11150 raise Z3Exception("Non-expression passed as a sequence")
11151 if not is_seq(s):
11152 raise Z3Exception("Non-sequence passed as a sequence")
11153 return s
11154
11155
11156def _get_ctx2(a, b, ctx=None):
11157 if is_expr(a):
11158 return a.ctx
11159 if is_expr(b):
11160 return b.ctx
11161 if ctx is None:
11162 ctx = main_ctx()
11163 return ctx
11164
11165
11166def is_seq(a):
11167 """Return `True` if `a` is a Z3 sequence expression.
11168 >>> print (is_seq(Unit(IntVal(0))))
11169 True
11170 >>> print (is_seq(StringVal("abc")))
11171 True
11172 """
11173 return isinstance(a, SeqRef)
11174
11175
11176def is_string(a: Any) -> bool:
11177 """Return `True` if `a` is a Z3 string expression.
11178 >>> print (is_string(StringVal("ab")))
11179 True
11180 """
11181 return isinstance(a, SeqRef) and a.is_string()
11182
11183
11184def is_string_value(a: Any) -> bool:
11185 """return 'True' if 'a' is a Z3 string constant expression.
11186 >>> print (is_string_value(StringVal("a")))
11187 True
11188 >>> print (is_string_value(StringVal("a") + StringVal("b")))
11189 False
11190 """
11191 return isinstance(a, SeqRef) and a.is_string_value()
11192
11193def StringVal(s, ctx=None):
11194 """create a string expression"""
11195 s = "".join(str(ch) if 32 <= ord(ch) and ord(ch) < 127 else "\\u{%x}" % (ord(ch)) for ch in s)
11196 ctx = _get_ctx(ctx)
11197 return SeqRef(Z3_mk_string(ctx.ref(), s), ctx)
11198
11199
11200def String(name, ctx=None):
11201 """Return a string constant named `name`. If `ctx=None`, then the global context is used.
11202
11203 >>> x = String('x')
11204 """
11205 ctx = _get_ctx(ctx)
11206 return SeqRef(Z3_mk_const(ctx.ref(), to_symbol(name, ctx), StringSort(ctx).ast), ctx)
11207
11208
11209def Strings(names, ctx=None):
11210 """Return a tuple of String constants. """
11211 ctx = _get_ctx(ctx)
11212 if isinstance(names, str):
11213 names = names.split(" ")
11214 return [String(name, ctx) for name in names]
11215
11216
11217def SubString(s, offset, length):
11218 """Extract substring or subsequence starting at offset.
11219
11220 This is a convenience function that redirects to Extract(s, offset, length).
11221
11222 >>> s = StringVal("hello world")
11223 >>> SubString(s, 6, 5) # Extract "world"
11224 str.substr("hello world", 6, 5)
11225 >>> simplify(SubString(StringVal("hello"), 1, 3))
11226 "ell"
11227 """
11228 return Extract(s, offset, length)
11229
11230
11231def SubSeq(s, offset, length):
11232 """Extract substring or subsequence starting at offset.
11233
11234 This is a convenience function that redirects to Extract(s, offset, length).
11235
11236 >>> s = StringVal("hello world")
11237 >>> SubSeq(s, 0, 5) # Extract "hello"
11238 str.substr("hello world", 0, 5)
11239 >>> simplify(SubSeq(StringVal("testing"), 2, 4))
11240 "stin"
11241 """
11242 return Extract(s, offset, length)
11243
11244
11245def Empty(s):
11246 """Create the empty sequence of the given sort
11247 >>> e = Empty(StringSort())
11248 >>> e2 = StringVal("")
11249 >>> print(e.eq(e2))
11250 True
11251 >>> e3 = Empty(SeqSort(IntSort()))
11252 >>> print(e3)
11253 Empty(Seq(Int))
11254 >>> e4 = Empty(ReSort(SeqSort(IntSort())))
11255 >>> print(e4)
11256 Empty(ReSort(Seq(Int)))
11257 """
11258 if isinstance(s, SeqSortRef):
11259 return SeqRef(Z3_mk_seq_empty(s.ctx_ref(), s.ast), s.ctx)
11260 if isinstance(s, ReSortRef):
11261 return ReRef(Z3_mk_re_empty(s.ctx_ref(), s.ast), s.ctx)
11262 raise Z3Exception("Non-sequence, non-regular expression sort passed to Empty")
11263
11264
11265def Full(s):
11266 """Create the regular expression that accepts the universal language
11267 >>> e = Full(ReSort(SeqSort(IntSort())))
11268 >>> print(e)
11269 Full(ReSort(Seq(Int)))
11270 >>> e1 = Full(ReSort(StringSort()))
11271 >>> print(e1)
11272 Full(ReSort(String))
11273 """
11274 if isinstance(s, ReSortRef):
11275 return ReRef(Z3_mk_re_full(s.ctx_ref(), s.ast), s.ctx)
11276 raise Z3Exception("Non-sequence, non-regular expression sort passed to Full")
11277
11278
11279
11280def Unit(a):
11281 """Create a singleton sequence"""
11282 return SeqRef(Z3_mk_seq_unit(a.ctx_ref(), a.as_ast()), a.ctx)
11283
11284
11285def PrefixOf(a, b):
11286 """Check if 'a' is a prefix of 'b'
11287 >>> s1 = PrefixOf("ab", "abc")
11288 >>> simplify(s1)
11289 True
11290 >>> s2 = PrefixOf("bc", "abc")
11291 >>> simplify(s2)
11292 False
11293 """
11294 ctx = _get_ctx2(a, b)
11295 a = _coerce_seq(a, ctx)
11296 b = _coerce_seq(b, ctx)
11297 return BoolRef(Z3_mk_seq_prefix(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)
11298
11299
11300def SuffixOf(a, b):
11301 """Check if 'a' is a suffix of 'b'
11302 >>> s1 = SuffixOf("ab", "abc")
11303 >>> simplify(s1)
11304 False
11305 >>> s2 = SuffixOf("bc", "abc")
11306 >>> simplify(s2)
11307 True
11308 """
11309 ctx = _get_ctx2(a, b)
11310 a = _coerce_seq(a, ctx)
11311 b = _coerce_seq(b, ctx)
11312 return BoolRef(Z3_mk_seq_suffix(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)
11313
11314
11315def Contains(a, b):
11316 """Check if 'a' contains 'b'
11317 >>> s1 = Contains("abc", "ab")
11318 >>> simplify(s1)
11319 True
11320 >>> s2 = Contains("abc", "bc")
11321 >>> simplify(s2)
11322 True
11323 >>> x, y, z = Strings('x y z')
11324 >>> s3 = Contains(Concat(x,y,z), y)
11325 >>> simplify(s3)
11326 True
11327 """
11328 ctx = _get_ctx2(a, b)
11329 a = _coerce_seq(a, ctx)
11330 b = _coerce_seq(b, ctx)
11331 return BoolRef(Z3_mk_seq_contains(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)
11332
11333
11334def Replace(s, src, dst):
11335 """Replace the first occurrence of 'src' by 'dst' in 's'
11336 >>> r = Replace("aaa", "a", "b")
11337 >>> simplify(r)
11338 "baa"
11339 """
11340 ctx = _get_ctx2(dst, s)
11341 if ctx is None and is_expr(src):
11342 ctx = src.ctx
11343 src = _coerce_seq(src, ctx)
11344 dst = _coerce_seq(dst, ctx)
11345 s = _coerce_seq(s, ctx)
11346 return SeqRef(Z3_mk_seq_replace(src.ctx_ref(), s.as_ast(), src.as_ast(), dst.as_ast()), s.ctx)
11347
11348
11349def IndexOf(s, substr, offset=None):
11350 """Retrieve the index of substring within a string starting at a specified offset.
11351 >>> simplify(IndexOf("abcabc", "bc", 0))
11352 1
11353 >>> simplify(IndexOf("abcabc", "bc", 2))
11354 4
11355 """
11356 if offset is None:
11357 offset = IntVal(0)
11358 ctx = None
11359 if is_expr(offset):
11360 ctx = offset.ctx
11361 ctx = _get_ctx2(s, substr, ctx)
11362 s = _coerce_seq(s, ctx)
11363 substr = _coerce_seq(substr, ctx)
11364 if _is_int(offset):
11365 offset = IntVal(offset, ctx)
11366 return ArithRef(Z3_mk_seq_index(s.ctx_ref(), s.as_ast(), substr.as_ast(), offset.as_ast()), s.ctx)
11367
11368
11369def LastIndexOf(s, substr):
11370 """Retrieve the last index of substring within a string"""
11371 ctx = None
11372 ctx = _get_ctx2(s, substr, ctx)
11373 s = _coerce_seq(s, ctx)
11374 substr = _coerce_seq(substr, ctx)
11375 return ArithRef(Z3_mk_seq_last_index(s.ctx_ref(), s.as_ast(), substr.as_ast()), s.ctx)
11376
11377
11378def Length(s):
11379 """Obtain the length of a sequence 's'
11380 >>> l = Length(StringVal("abc"))
11381 >>> simplify(l)
11382 3
11383 """
11384 s = _coerce_seq(s)
11385 return ArithRef(Z3_mk_seq_length(s.ctx_ref(), s.as_ast()), s.ctx)
11386
11387def SeqMap(f, s):
11388 """Map function 'f' over sequence 's'"""
11389 ctx = _get_ctx2(f, s)
11390 s = _coerce_seq(s, ctx)
11391 return _to_expr_ref(Z3_mk_seq_map(s.ctx_ref(), f.as_ast(), s.as_ast()), ctx)
11392
11393def SeqMapI(f, i, s):
11394 """Map function 'f' over sequence 's' at index 'i'"""
11395 ctx = _get_ctx2(f, s)
11396 s = _coerce_seq(s, ctx)
11397 if not is_expr(i):
11398 i = _py2expr(i)
11399 return _to_expr_ref(Z3_mk_seq_mapi(s.ctx_ref(), f.as_ast(), i.as_ast(), s.as_ast()), ctx)
11400
11401def SeqFoldLeft(f, a, s):
11402 ctx = _get_ctx2(f, s)
11403 s = _coerce_seq(s, ctx)
11404 a = _py2expr(a)
11405 return _to_expr_ref(Z3_mk_seq_foldl(s.ctx_ref(), f.as_ast(), a.as_ast(), s.as_ast()), ctx)
11406
11407def SeqFoldLeftI(f, i, a, s):
11408 ctx = _get_ctx2(f, s)
11409 s = _coerce_seq(s, ctx)
11410 a = _py2expr(a)
11411 i = _py2expr(i)
11412 return _to_expr_ref(Z3_mk_seq_foldli(s.ctx_ref(), f.as_ast(), i.as_ast(), a.as_ast(), s.as_ast()), ctx)
11413
11414def StrToInt(s):
11415 """Convert string expression to integer
11416 >>> a = StrToInt("1")
11417 >>> simplify(1 == a)
11418 True
11419 >>> b = StrToInt("2")
11420 >>> simplify(1 == b)
11421 False
11422 >>> c = StrToInt(IntToStr(2))
11423 >>> simplify(1 == c)
11424 False
11425 """
11426 s = _coerce_seq(s)
11427 return ArithRef(Z3_mk_str_to_int(s.ctx_ref(), s.as_ast()), s.ctx)
11428
11429
11430def IntToStr(s):
11431 """Convert integer expression to string"""
11432 if not is_expr(s):
11433 s = _py2expr(s)
11434 return SeqRef(Z3_mk_int_to_str(s.ctx_ref(), s.as_ast()), s.ctx)
11435
11436
11437def StrToCode(s):
11438 """Convert a unit length string to integer code"""
11439 if not is_expr(s):
11440 s = _py2expr(s)
11441 return ArithRef(Z3_mk_string_to_code(s.ctx_ref(), s.as_ast()), s.ctx)
11442
11443def StrFromCode(c):
11444 """Convert code to a string"""
11445 if not is_expr(c):
11446 c = _py2expr(c)
11447 return SeqRef(Z3_mk_string_from_code(c.ctx_ref(), c.as_ast()), c.ctx)
11448
11449def Re(s, ctx=None):
11450 """The regular expression that accepts sequence 's'
11451 >>> s1 = Re("ab")
11452 >>> s2 = Re(StringVal("ab"))
11453 >>> s3 = Re(Unit(BoolVal(True)))
11454 """
11455 s = _coerce_seq(s, ctx)
11456 return ReRef(Z3_mk_seq_to_re(s.ctx_ref(), s.as_ast()), s.ctx)
11457
11458
11459# Regular expressions
11460
11461class ReSortRef(SortRef):
11462 """Regular expression sort."""
11463
11464 def basis(self):
11465 return _to_sort_ref(Z3_get_re_sort_basis(self.ctx_ref(), self.ast), self.ctx)
11466
11467
11468def ReSort(s):
11469 if is_ast(s):
11470 return ReSortRef(Z3_mk_re_sort(s.ctx.ref(), s.ast), s.ctx)
11471 if s is None or isinstance(s, Context):
11472 ctx = _get_ctx(s)
11473 return ReSortRef(Z3_mk_re_sort(ctx.ref(), Z3_mk_string_sort(ctx.ref())), s.ctx)
11474 raise Z3Exception("Regular expression sort constructor expects either a string or a context or no argument")
11475
11476
11477class ReRef(ExprRef):
11478 """Regular expressions."""
11479
11480 def __add__(self, other):
11481 return Union(self, other)
11482
11483
11484def is_re(s):
11485 return isinstance(s, ReRef)
11486
11487
11488def InRe(s, re):
11489 """Create regular expression membership test
11490 >>> re = Union(Re("a"),Re("b"))
11491 >>> print (simplify(InRe("a", re)))
11492 True
11493 >>> print (simplify(InRe("b", re)))
11494 True
11495 >>> print (simplify(InRe("c", re)))
11496 False
11497 """
11498 s = _coerce_seq(s, re.ctx)
11499 return BoolRef(Z3_mk_seq_in_re(s.ctx_ref(), s.as_ast(), re.as_ast()), s.ctx)
11500
11501
11502def Union(*args):
11503 """Create union of regular expressions.
11504 >>> re = Union(Re("a"), Re("b"), Re("c"))
11505 >>> print (simplify(InRe("d", re)))
11506 False
11507 """
11508 args = _get_args(args)
11509 sz = len(args)
11510 if z3_debug():
11511 _z3_assert(sz > 0, "At least one argument expected.")
11512 _z3_assert(all([is_re(a) for a in args]), "All arguments must be regular expressions.")
11513 if sz == 1:
11514 return args[0]
11515 ctx = args[0].ctx
11516 v = (Ast * sz)()
11517 for i in range(sz):
11518 v[i] = args[i].as_ast()
11519 return ReRef(Z3_mk_re_union(ctx.ref(), sz, v), ctx)
11520
11521
11522def Intersect(*args):
11523 """Create intersection of regular expressions.
11524 >>> re = Intersect(Re("a"), Re("b"), Re("c"))
11525 """
11526 args = _get_args(args)
11527 sz = len(args)
11528 if z3_debug():
11529 _z3_assert(sz > 0, "At least one argument expected.")
11530 _z3_assert(all([is_re(a) for a in args]), "All arguments must be regular expressions.")
11531 if sz == 1:
11532 return args[0]
11533 ctx = args[0].ctx
11534 v = (Ast * sz)()
11535 for i in range(sz):
11536 v[i] = args[i].as_ast()
11537 return ReRef(Z3_mk_re_intersect(ctx.ref(), sz, v), ctx)
11538
11539
11540def Plus(re):
11541 """Create the regular expression accepting one or more repetitions of argument.
11542 >>> re = Plus(Re("a"))
11543 >>> print(simplify(InRe("aa", re)))
11544 True
11545 >>> print(simplify(InRe("ab", re)))
11546 False
11547 >>> print(simplify(InRe("", re)))
11548 False
11549 """
11550 if z3_debug():
11551 _z3_assert(is_expr(re), "expression expected")
11552 return ReRef(Z3_mk_re_plus(re.ctx_ref(), re.as_ast()), re.ctx)
11553
11554
11555def Option(re):
11556 """Create the regular expression that optionally accepts the argument.
11557 >>> re = Option(Re("a"))
11558 >>> print(simplify(InRe("a", re)))
11559 True
11560 >>> print(simplify(InRe("", re)))
11561 True
11562 >>> print(simplify(InRe("aa", re)))
11563 False
11564 """
11565 if z3_debug():
11566 _z3_assert(is_expr(re), "expression expected")
11567 return ReRef(Z3_mk_re_option(re.ctx_ref(), re.as_ast()), re.ctx)
11568
11569
11570def Complement(re):
11571 """Create the complement regular expression."""
11572 return ReRef(Z3_mk_re_complement(re.ctx_ref(), re.as_ast()), re.ctx)
11573
11574
11575def Star(re):
11576 """Create the regular expression accepting zero or more repetitions of argument.
11577 >>> re = Star(Re("a"))
11578 >>> print(simplify(InRe("aa", re)))
11579 True
11580 >>> print(simplify(InRe("ab", re)))
11581 False
11582 >>> print(simplify(InRe("", re)))
11583 True
11584 """
11585 if z3_debug():
11586 _z3_assert(is_expr(re), "expression expected")
11587 return ReRef(Z3_mk_re_star(re.ctx_ref(), re.as_ast()), re.ctx)
11588
11589
11590def Loop(re, lo, hi=0):
11591 """Create the regular expression accepting between a lower and upper bound repetitions
11592 >>> re = Loop(Re("a"), 1, 3)
11593 >>> print(simplify(InRe("aa", re)))
11594 True
11595 >>> print(simplify(InRe("aaaa", re)))
11596 False
11597 >>> print(simplify(InRe("", re)))
11598 False
11599 """
11600 if z3_debug():
11601 _z3_assert(is_expr(re), "expression expected")
11602 return ReRef(Z3_mk_re_loop(re.ctx_ref(), re.as_ast(), lo, hi), re.ctx)
11603
11604
11605def Range(lo, hi, ctx=None):
11606 """Create the range regular expression over two sequences of length 1
11607 >>> range = Range("a","z")
11608 >>> print(simplify(InRe("b", range)))
11609 True
11610 >>> print(simplify(InRe("bb", range)))
11611 False
11612 """
11613 lo = _coerce_seq(lo, ctx)
11614 hi = _coerce_seq(hi, ctx)
11615 if z3_debug():
11616 _z3_assert(is_expr(lo), "expression expected")
11617 _z3_assert(is_expr(hi), "expression expected")
11618 return ReRef(Z3_mk_re_range(lo.ctx_ref(), lo.ast, hi.ast), lo.ctx)
11619
11620def Diff(a, b, ctx=None):
11621 """Create the difference regular expression
11622 """
11623 if z3_debug():
11624 _z3_assert(is_expr(a), "expression expected")
11625 _z3_assert(is_expr(b), "expression expected")
11626 return ReRef(Z3_mk_re_diff(a.ctx_ref(), a.ast, b.ast), a.ctx)
11627
11628def AllChar(regex_sort, ctx=None):
11629 """Create a regular expression that accepts all single character strings
11630 """
11631 return ReRef(Z3_mk_re_allchar(regex_sort.ctx_ref(), regex_sort.ast), regex_sort.ctx)
11632
11633# Special Relations
11634
11635
11636def PartialOrder(a, index):
11637 return FuncDeclRef(Z3_mk_partial_order(a.ctx_ref(), a.ast, index), a.ctx)
11638
11639
11640def LinearOrder(a, index):
11641 return FuncDeclRef(Z3_mk_linear_order(a.ctx_ref(), a.ast, index), a.ctx)
11642
11643
11644def TreeOrder(a, index):
11645 return FuncDeclRef(Z3_mk_tree_order(a.ctx_ref(), a.ast, index), a.ctx)
11646
11647
11648def PiecewiseLinearOrder(a, index):
11649 return FuncDeclRef(Z3_mk_piecewise_linear_order(a.ctx_ref(), a.ast, index), a.ctx)
11650
11651
11652def TransitiveClosure(f):
11653 """Given a binary relation R, such that the two arguments have the same sort
11654 create the transitive closure relation R+.
11655 The transitive closure R+ is a new relation.
11656 """
11657 return FuncDeclRef(Z3_mk_transitive_closure(f.ctx_ref(), f.ast), f.ctx)
11658
11659def to_Ast(ptr,):
11660 ast = Ast(ptr)
11661 super(ctypes.c_void_p, ast).__init__(ptr)
11662 return ast
11663
11664def to_ContextObj(ptr,):
11665 ctx = ContextObj(ptr)
11666 super(ctypes.c_void_p, ctx).__init__(ptr)
11667 return ctx
11668
11669def to_AstVectorObj(ptr,):
11670 v = AstVectorObj(ptr)
11671 super(ctypes.c_void_p, v).__init__(ptr)
11672 return v
11673
11674# NB. my-hacky-class only works for a single instance of OnClause
11675# it should be replaced with a proper correlation between OnClause
11676# and object references that can be passed over the FFI.
11677# for UserPropagator we use a global dictionary, which isn't great code.
11678
11679_my_hacky_class = None
11680def on_clause_eh(ctx, p, n, dep, clause):
11681 onc = _my_hacky_class
11682 p = _to_expr_ref(to_Ast(p), onc.ctx)
11683 clause = AstVector(to_AstVectorObj(clause), onc.ctx)
11684 deps = [dep[i] for i in range(n)]
11685 onc.on_clause(p, deps, clause)
11686
11687_on_clause_eh = Z3_on_clause_eh(on_clause_eh)
11688
11689class OnClause:
11690 def __init__(self, s, on_clause):
11691 self.s = s
11692 self.ctx = s.ctx
11693 self.on_clause = on_clause
11694 self.idx = 22
11695 global _my_hacky_class
11696 _my_hacky_class = self
11697 Z3_solver_register_on_clause(self.ctx.ref(), self.s.solver, self.idx, _on_clause_eh)
11698
11699
11700class PropClosures:
11701 def __init__(self):
11702 self.bases = {}
11703 self.lock = None
11704
11705 def set_threaded(self):
11706 if self.lock is None:
11707 import threading
11708 self.lock = threading.Lock()
11709
11710 def get(self, ctx):
11711 if self.lock:
11712 with self.lock:
11713 r = self.bases[ctx]
11714 else:
11715 r = self.bases[ctx]
11716 return r
11717
11718 def set(self, ctx, r):
11719 if self.lock:
11720 with self.lock:
11721 self.bases[ctx] = r
11722 else:
11723 self.bases[ctx] = r
11724
11725 def insert(self, r):
11726 if self.lock:
11727 with self.lock:
11728 id = len(self.bases) + 3
11729 self.bases[id] = r
11730 else:
11731 id = len(self.bases) + 3
11732 self.bases[id] = r
11733 return id
11734
11735
11736_prop_closures = None
11737
11738
11739def ensure_prop_closures():
11740 global _prop_closures
11741 if _prop_closures is None:
11742 _prop_closures = PropClosures()
11743
11744
11745def user_prop_push(ctx, cb):
11746 prop = _prop_closures.get(ctx)
11747 prop.cb = cb
11748 prop.push()
11749
11750
11751def user_prop_pop(ctx, cb, num_scopes):
11752 prop = _prop_closures.get(ctx)
11753 prop.cb = cb
11754 prop.pop(num_scopes)
11755
11756
11757def user_prop_fresh(ctx, _new_ctx):
11758 _prop_closures.set_threaded()
11759 prop = _prop_closures.get(ctx)
11760 nctx = Context()
11761 Z3_del_context(nctx.ctx)
11762 new_ctx = to_ContextObj(_new_ctx)
11763 nctx.ctx = new_ctx
11764 nctx.eh = Z3_set_error_handler(new_ctx, z3_error_handler)
11765 nctx.owner = False
11766 new_prop = prop.fresh(nctx)
11767 _prop_closures.set(new_prop.id, new_prop)
11768 return new_prop.id
11769
11770
11771def user_prop_fixed(ctx, cb, id, value):
11772 prop = _prop_closures.get(ctx)
11773 old_cb = prop.cb
11774 prop.cb = cb
11775 id = _to_expr_ref(to_Ast(id), prop.ctx())
11776 value = _to_expr_ref(to_Ast(value), prop.ctx())
11777 prop.fixed(id, value)
11778 prop.cb = old_cb
11779
11780def user_prop_created(ctx, cb, id):
11781 prop = _prop_closures.get(ctx)
11782 old_cb = prop.cb
11783 prop.cb = cb
11784 id = _to_expr_ref(to_Ast(id), prop.ctx())
11785 prop.created(id)
11786 prop.cb = old_cb
11787
11788
11789def user_prop_final(ctx, cb):
11790 prop = _prop_closures.get(ctx)
11791 old_cb = prop.cb
11792 prop.cb = cb
11793 prop.final()
11794 prop.cb = old_cb
11795
11796def user_prop_eq(ctx, cb, x, y):
11797 prop = _prop_closures.get(ctx)
11798 old_cb = prop.cb
11799 prop.cb = cb
11800 x = _to_expr_ref(to_Ast(x), prop.ctx())
11801 y = _to_expr_ref(to_Ast(y), prop.ctx())
11802 prop.eq(x, y)
11803 prop.cb = old_cb
11804
11805def user_prop_diseq(ctx, cb, x, y):
11806 prop = _prop_closures.get(ctx)
11807 old_cb = prop.cb
11808 prop.cb = cb
11809 x = _to_expr_ref(to_Ast(x), prop.ctx())
11810 y = _to_expr_ref(to_Ast(y), prop.ctx())
11811 prop.diseq(x, y)
11812 prop.cb = old_cb
11813
11814def user_prop_decide(ctx, cb, t_ref, idx, phase):
11815 prop = _prop_closures.get(ctx)
11816 old_cb = prop.cb
11817 prop.cb = cb
11818 t = _to_expr_ref(to_Ast(t_ref), prop.ctx())
11819 prop.decide(t, idx, phase)
11820 prop.cb = old_cb
11821
11822def user_prop_binding(ctx, cb, q_ref, inst_ref):
11823 prop = _prop_closures.get(ctx)
11824 old_cb = prop.cb
11825 prop.cb = cb
11826 q = _to_expr_ref(to_Ast(q_ref), prop.ctx())
11827 inst = _to_expr_ref(to_Ast(inst_ref), prop.ctx())
11828 r = prop.binding(q, inst)
11829 prop.cb = old_cb
11830 return r
11831
11832
11833_user_prop_push = Z3_push_eh(user_prop_push)
11834_user_prop_pop = Z3_pop_eh(user_prop_pop)
11835_user_prop_fresh = Z3_fresh_eh(user_prop_fresh)
11836_user_prop_fixed = Z3_fixed_eh(user_prop_fixed)
11837_user_prop_created = Z3_created_eh(user_prop_created)
11838_user_prop_final = Z3_final_eh(user_prop_final)
11839_user_prop_eq = Z3_eq_eh(user_prop_eq)
11840_user_prop_diseq = Z3_eq_eh(user_prop_diseq)
11841_user_prop_decide = Z3_decide_eh(user_prop_decide)
11842_user_prop_binding = Z3_on_binding_eh(user_prop_binding)
11843
11844
11845def PropagateFunction(name, *sig):
11846 """Create a function that gets tracked by user propagator.
11847 Every term headed by this function symbol is tracked.
11848 If a term is fixed and the fixed callback is registered a
11849 callback is invoked that the term headed by this function is fixed.
11850 """
11851 sig = _get_args(sig)
11852 if z3_debug():
11853 _z3_assert(len(sig) > 0, "At least two arguments expected")
11854 arity = len(sig) - 1
11855 rng = sig[arity]
11856 if z3_debug():
11857 _z3_assert(is_sort(rng), "Z3 sort expected")
11858 dom = (Sort * arity)()
11859 for i in range(arity):
11860 if z3_debug():
11861 _z3_assert(is_sort(sig[i]), "Z3 sort expected")
11862 dom[i] = sig[i].ast
11863 ctx = rng.ctx
11864 return FuncDeclRef(Z3_solver_propagate_declare(ctx.ref(), to_symbol(name, ctx), arity, dom, rng.ast), ctx)
11865
11866
11867
11868class UserPropagateBase:
11869
11870 #
11871 # Either solver is set or ctx is set.
11872 # Propagators that are created through callbacks
11873 # to "fresh" inherit the context of that is supplied
11874 # as argument to the callback.
11875 # This context should not be deleted. It is owned by the solver.
11876 #
11877 def __init__(self, s, ctx=None):
11878 assert s is None or ctx is None
11879 ensure_prop_closures()
11880 self.solver = s
11881 self._ctx = None
11882 self.fresh_ctx = None
11883 self.cb = None
11884 self.id = _prop_closures.insert(self)
11885 self.fixed = None
11886 self.final = None
11887 self.eq = None
11888 self.diseq = None
11889 self.decide = None
11890 self.created = None
11891 self.binding = None
11892 if ctx:
11893 self.fresh_ctx = ctx
11894 if s:
11895 Z3_solver_propagate_init(self.ctx_ref(),
11896 s.solver,
11897 ctypes.c_void_p(self.id),
11898 _user_prop_push,
11899 _user_prop_pop,
11900 _user_prop_fresh)
11901
11902 def __del__(self):
11903 if self._ctx:
11904 self._ctx.ctx = None
11905
11906 def ctx(self):
11907 if self.fresh_ctx:
11908 return self.fresh_ctx
11909 else:
11910 return self.solver.ctx
11911
11912 def ctx_ref(self):
11913 return self.ctx().ref()
11914
11915 def add_fixed(self, fixed):
11916 assert not self.fixed
11917 assert not self._ctx
11918 if self.solver:
11919 Z3_solver_propagate_fixed(self.ctx_ref(), self.solver.solver, _user_prop_fixed)
11920 self.fixed = fixed
11921
11922 def add_created(self, created):
11923 assert not self.created
11924 assert not self._ctx
11925 if self.solver:
11926 Z3_solver_propagate_created(self.ctx_ref(), self.solver.solver, _user_prop_created)
11927 self.created = created
11928
11929 def add_final(self, final):
11930 assert not self.final
11931 assert not self._ctx
11932 if self.solver:
11933 Z3_solver_propagate_final(self.ctx_ref(), self.solver.solver, _user_prop_final)
11934 self.final = final
11935
11936 def add_eq(self, eq):
11937 assert not self.eq
11938 assert not self._ctx
11939 if self.solver:
11940 Z3_solver_propagate_eq(self.ctx_ref(), self.solver.solver, _user_prop_eq)
11941 self.eq = eq
11942
11943 def add_diseq(self, diseq):
11944 assert not self.diseq
11945 assert not self._ctx
11946 if self.solver:
11947 Z3_solver_propagate_diseq(self.ctx_ref(), self.solver.solver, _user_prop_diseq)
11948 self.diseq = diseq
11949
11950 def add_decide(self, decide):
11951 assert not self.decide
11952 assert not self._ctx
11953 if self.solver:
11954 Z3_solver_propagate_decide(self.ctx_ref(), self.solver.solver, _user_prop_decide)
11955 self.decide = decide
11956
11957 def add_on_binding(self, binding):
11958 assert not self.binding
11959 assert not self._ctx
11960 if self.solver:
11961 Z3_solver_propagate_on_binding(self.ctx_ref(), self.solver.solver, _user_prop_binding)
11962 self.binding = binding
11963
11964 def push(self):
11965 raise Z3Exception("push needs to be overwritten")
11966
11967 def pop(self, num_scopes):
11968 raise Z3Exception("pop needs to be overwritten")
11969
11970 def fresh(self, new_ctx):
11971 raise Z3Exception("fresh needs to be overwritten")
11972
11973 def add(self, e):
11974 assert not self._ctx
11975 if self.solver:
11976 Z3_solver_propagate_register(self.ctx_ref(), self.solver.solver, e.ast)
11977 else:
11978 Z3_solver_propagate_register_cb(self.ctx_ref(), ctypes.c_void_p(self.cb), e.ast)
11979
11980 #
11981 # Tell the solver to perform the next split on a given term
11982 # If the term is a bit-vector the index idx specifies the index of the Boolean variable being
11983 # split on. A phase of true = 1/false = -1/undef = 0 = let solver decide is the last argument.
11984 #
11985 def next_split(self, t, idx, phase):
11986 return Z3_solver_next_split(self.ctx_ref(), ctypes.c_void_p(self.cb), t.ast, idx, phase)
11987
11988 #
11989 # Propagation can only be invoked as during a fixed or final callback.
11990 #
11991 def propagate(self, e, ids, eqs=[]):
11992 _ids, num_fixed = _to_ast_array(ids)
11993 num_eqs = len(eqs)
11994 _lhs, _num_lhs = _to_ast_array([x for x, y in eqs])
11995 _rhs, _num_rhs = _to_ast_array([y for x, y in eqs])
11996 return Z3_solver_propagate_consequence(e.ctx.ref(), ctypes.c_void_p(
11997 self.cb), num_fixed, _ids, num_eqs, _lhs, _rhs, e.ast)
11998
11999 def conflict(self, deps = [], eqs = []):
12000 self.propagate(BoolVal(False, self.ctx()), deps, eqs)
approx(self, precision=10)
Definition z3py.py:3185
as_decimal(self, prec)
Definition z3py.py:3197
__rmod__(self, other)
Definition z3py.py:2667
__mod__(self, other)
Definition z3py.py:2652
__pow__(self, other)
Definition z3py.py:2576
__gt__(self, other)
Definition z3py.py:2725
__lt__(self, other)
Definition z3py.py:2712
__rtruediv__(self, other)
Definition z3py.py:2648
__rmul__(self, other)
Definition z3py.py:2543
__rsub__(self, other)
Definition z3py.py:2566
__add__(self, other)
Definition z3py.py:2505
__sub__(self, other)
Definition z3py.py:2553
is_real(self)
Definition z3py.py:2494
is_int(self)
Definition z3py.py:2480
__radd__(self, other)
Definition z3py.py:2518
__truediv__(self, other)
Definition z3py.py:2627
__le__(self, other)
Definition z3py.py:2699
__rpow__(self, other)
Definition z3py.py:2590
__pos__(self)
Definition z3py.py:2690
sort(self)
Definition z3py.py:2470
__mul__(self, other)
Definition z3py.py:2528
__rdiv__(self, other)
Definition z3py.py:2631
__ge__(self, other)
Definition z3py.py:2738
__neg__(self)
Definition z3py.py:2679
__div__(self, other)
Definition z3py.py:2604
Arithmetic.
Definition z3py.py:2375
subsort(self, other)
Definition z3py.py:2409
cast(self, val)
Definition z3py.py:2413
domain(self)
Definition z3py.py:4679
domain_n(self, i)
Definition z3py.py:4688
__getitem__(self, arg)
Definition z3py.py:4701
range(self)
Definition z3py.py:4692
sort(self)
Definition z3py.py:4670
default(self)
Definition z3py.py:4713
domain_n(self, i)
Definition z3py.py:4652
erase(self, k)
Definition z3py.py:6211
__deepcopy__(self, memo={})
Definition z3py.py:6148
__init__(self, m=None, ctx=None)
Definition z3py.py:6137
__repr__(self)
Definition z3py.py:6208
__len__(self)
Definition z3py.py:6155
keys(self)
Definition z3py.py:6240
__setitem__(self, k, v)
Definition z3py.py:6192
__contains__(self, key)
Definition z3py.py:6168
__del__(self)
Definition z3py.py:6151
__getitem__(self, key)
Definition z3py.py:6181
reset(self)
Definition z3py.py:6225
__deepcopy__(self, memo={})
Definition z3py.py:365
__nonzero__(self)
Definition z3py.py:380
as_ast(self)
Definition z3py.py:402
translate(self, target)
Definition z3py.py:431
__hash__(self)
Definition z3py.py:377
__init__(self, ast, ctx=None)
Definition z3py.py:355
__str__(self)
Definition z3py.py:368
ctx_ref(self)
Definition z3py.py:410
py_value(self)
Definition z3py.py:460
__repr__(self)
Definition z3py.py:371
get_id(self)
Definition z3py.py:406
hash(self)
Definition z3py.py:450
__eq__(self, other)
Definition z3py.py:374
eq(self, other)
Definition z3py.py:414
sexpr(self)
Definition z3py.py:393
__del__(self)
Definition z3py.py:360
__bool__(self)
Definition z3py.py:383
__copy__(self)
Definition z3py.py:447
__deepcopy__(self, memo={})
Definition z3py.py:6117
translate(self, other_ctx)
Definition z3py.py:6098
__repr__(self)
Definition z3py.py:6120
__len__(self)
Definition z3py.py:5992
__init__(self, v=None, ctx=None)
Definition z3py.py:5977
push(self, v)
Definition z3py.py:6050
__getitem__(self, i)
Definition z3py.py:6005
sexpr(self)
Definition z3py.py:6123
__del__(self)
Definition z3py.py:5988
__setitem__(self, i, v)
Definition z3py.py:6034
__contains__(self, item)
Definition z3py.py:6075
__copy__(self)
Definition z3py.py:6114
resize(self, sz)
Definition z3py.py:6062
as_binary_string(self)
Definition z3py.py:4031
as_signed_long(self)
Definition z3py.py:4005
as_string(self)
Definition z3py.py:4028
__and__(self, other)
Definition z3py.py:3695
__rmod__(self, other)
Definition z3py.py:3836
__rrshift__(self, other)
Definition z3py.py:3962
__mod__(self, other)
Definition z3py.py:3815
__or__(self, other)
Definition z3py.py:3672
__rlshift__(self, other)
Definition z3py.py:3976
__gt__(self, other)
Definition z3py.py:3886
__lt__(self, other)
Definition z3py.py:3870
__invert__(self)
Definition z3py.py:3761
__rtruediv__(self, other)
Definition z3py.py:3811
__rmul__(self, other)
Definition z3py.py:3639
__rxor__(self, other)
Definition z3py.py:3731
__ror__(self, other)
Definition z3py.py:3685
__rsub__(self, other)
Definition z3py.py:3662
__add__(self, other)
Definition z3py.py:3603
__sub__(self, other)
Definition z3py.py:3649
__radd__(self, other)
Definition z3py.py:3616
size(self)
Definition z3py.py:3592
__rand__(self, other)
Definition z3py.py:3708
__truediv__(self, other)
Definition z3py.py:3791
__le__(self, other)
Definition z3py.py:3854
__xor__(self, other)
Definition z3py.py:3718
__lshift__(self, other)
Definition z3py.py:3948
__pos__(self)
Definition z3py.py:3741
sort(self)
Definition z3py.py:3581
__mul__(self, other)
Definition z3py.py:3626
__rdiv__(self, other)
Definition z3py.py:3795
__ge__(self, other)
Definition z3py.py:3902
__neg__(self)
Definition z3py.py:3750
__rshift__(self, other)
Definition z3py.py:3918
__div__(self, other)
Definition z3py.py:3772
Bit-Vectors.
Definition z3py.py:3534
subsort(self, other)
Definition z3py.py:3546
cast(self, val)
Definition z3py.py:3549
__and__(self, other)
Definition z3py.py:1626
__or__(self, other)
Definition z3py.py:1629
__invert__(self)
Definition z3py.py:1635
__rmul__(self, other)
Definition z3py.py:1612
__add__(self, other)
Definition z3py.py:1604
py_value(self)
Definition z3py.py:1638
__radd__(self, other)
Definition z3py.py:1609
__xor__(self, other)
Definition z3py.py:1632
sort(self)
Definition z3py.py:1601
__mul__(self, other)
Definition z3py.py:1615
Booleans.
Definition z3py.py:1562
subsort(self, other)
Definition z3py.py:1588
is_bool(self)
Definition z3py.py:1594
cast(self, val)
Definition z3py.py:1565
__deepcopy__(self, memo={})
Definition z3py.py:7005
__eq__(self, other)
Definition z3py.py:7008
__ne__(self, other)
Definition z3py.py:7011
__init__(self, r)
Definition z3py.py:7002
param_descrs(self)
Definition z3py.py:240
__init__(self, *args, **kws)
Definition z3py.py:202
interrupt(self)
Definition z3py.py:232
__del__(self)
Definition z3py.py:222
ref(self)
Definition z3py.py:228
bool owner
Definition z3py.py:217
__deepcopy__(self, memo={})
Definition z3py.py:5197
create(self)
Definition z3py.py:5236
__init__(self, name, ctx=None)
Definition z3py.py:5192
__repr__(self)
Definition z3py.py:5233
list constructors
Definition z3py.py:5195
declare(self, name, *args)
Definition z3py.py:5212
declare_core(self, name, rec_name, *args)
Definition z3py.py:5202
constructor(self, idx)
Definition z3py.py:5389
accessor(self, i, j)
Definition z3py.py:5436
num_constructors(self)
Definition z3py.py:5376
recognizer(self, idx)
Definition z3py.py:5408
Expressions.
Definition z3py.py:1001
as_ast(self)
Definition z3py.py:1012
__hash__(self)
Definition z3py.py:1058
kind(self)
Definition z3py.py:1098
children(self)
Definition z3py.py:1142
serialize(self)
Definition z3py.py:1160
get_id(self)
Definition z3py.py:1015
num_args(self)
Definition z3py.py:1105
__eq__(self, other)
Definition z3py.py:1041
__ne__(self, other)
Definition z3py.py:1062
from_string(self, s)
Definition z3py.py:1157
sort_kind(self)
Definition z3py.py:1030
arg(self, idx)
Definition z3py.py:1121
sort(self)
Definition z3py.py:1018
params(self)
Definition z3py.py:1080
decl(self)
Definition z3py.py:1083
Function Declarations.
Definition z3py.py:758
as_func_decl(self)
Definition z3py.py:772
domain(self, i)
Definition z3py.py:796
as_ast(self)
Definition z3py.py:766
__call__(self, *args)
Definition z3py.py:859
arity(self)
Definition z3py.py:786
get_id(self)
Definition z3py.py:769
range(self)
Definition z3py.py:808
params(self)
Definition z3py.py:831
Definition z3py.py:6259
__deepcopy__(self, memo={})
Definition z3py.py:6267
ctx
Definition z3py.py:6264
__repr__(self)
Definition z3py.py:6364
num_args(self)
Definition z3py.py:6274
entry
Definition z3py.py:6263
value(self)
Definition z3py.py:6323
__init__(self, entry, ctx)
Definition z3py.py:6262
__del__(self)
Definition z3py.py:6270
as_list(self)
Definition z3py.py:6345
arg_value(self, idx)
Definition z3py.py:6292
__deepcopy__(self, memo={})
Definition z3py.py:6462
translate(self, other_ctx)
Definition z3py.py:6454
arity(self)
Definition z3py.py:6420
__repr__(self)
Definition z3py.py:6482
num_entries(self)
Definition z3py.py:6404
__init__(self, f, ctx)
Definition z3py.py:6371
__del__(self)
Definition z3py.py:6377
as_list(self)
Definition z3py.py:6465
else_value(self)
Definition z3py.py:6381
entry(self, idx)
Definition z3py.py:6434
__copy__(self)
Definition z3py.py:6459
__deepcopy__(self, memo={})
Definition z3py.py:5922
get(self, i)
Definition z3py.py:5780
prec(self)
Definition z3py.py:5724
translate(self, target)
Definition z3py.py:5896
append(self, *args)
Definition z3py.py:5823
as_expr(self)
Definition z3py.py:5945
assert_exprs(self, *args)
Definition z3py.py:5808
__repr__(self)
Definition z3py.py:5885
__len__(self)
Definition z3py.py:5767
inconsistent(self)
Definition z3py.py:5706
dimacs(self, include_names=True)
Definition z3py.py:5892
__getitem__(self, arg)
Definition z3py.py:5793
size(self)
Definition z3py.py:5754
precision(self)
Definition z3py.py:5745
simplify(self, *arguments, **keywords)
Definition z3py.py:5925
sexpr(self)
Definition z3py.py:5888
add(self, *args)
Definition z3py.py:5845
__del__(self)
Definition z3py.py:5684
convert_model(self, model)
Definition z3py.py:5856
insert(self, *args)
Definition z3py.py:5834
depth(self)
Definition z3py.py:5688
__init__(self, models=True, unsat_cores=False, proofs=False, ctx=None, goal=None)
Definition z3py.py:5674
__copy__(self)
Definition z3py.py:5919
as_binary_string(self)
Definition z3py.py:3070
py_value(self)
Definition z3py.py:3078
as_long(self)
Definition z3py.py:3049
as_string(self)
Definition z3py.py:3062
__deepcopy__(self, memo={})
Definition z3py.py:6828
eval(self, t, model_completion=False)
Definition z3py.py:6506
translate(self, target)
Definition z3py.py:6793
__getitem__(self, idx)
Definition z3py.py:6707
num_sorts(self)
Definition z3py.py:6632
get_universe(self, s)
Definition z3py.py:6687
get_sort(self, idx)
Definition z3py.py:6647
project(self, vars, fml)
Definition z3py.py:6801
__repr__(self)
Definition z3py.py:6499
__len__(self)
Definition z3py.py:6563
get_interp(self, decl)
Definition z3py.py:6580
__init__(self, m, ctx)
Definition z3py.py:6489
sexpr(self)
Definition z3py.py:6502
sorts(self)
Definition z3py.py:6670
__del__(self)
Definition z3py.py:6495
decls(self)
Definition z3py.py:6752
project_with_witness(self, vars, fml)
Definition z3py.py:6813
update_value(self, x, value)
Definition z3py.py:6771
evaluate(self, t, model_completion=False)
Definition z3py.py:6537
__copy__(self)
Definition z3py.py:6825
__deepcopy__(self, memo={})
Definition z3py.py:5618
__init__(self, descr, ctx=None)
Definition z3py.py:5612
get_kind(self, n)
Definition z3py.py:5640
get_documentation(self, n)
Definition z3py.py:5645
__getitem__(self, arg)
Definition z3py.py:5650
get_name(self, i)
Definition z3py.py:5635
Parameter Sets.
Definition z3py.py:5539
__deepcopy__(self, memo={})
Definition z3py.py:5553
validate(self, ds)
Definition z3py.py:5580
__repr__(self)
Definition z3py.py:5577
__init__(self, ctx=None, params=None)
Definition z3py.py:5545
set(self, name, val)
Definition z3py.py:5560
__del__(self)
Definition z3py.py:5556
Patterns.
Definition z3py.py:1998
as_ast(self)
Definition z3py.py:2003
get_id(self)
Definition z3py.py:2006
Quantifiers.
Definition z3py.py:2065
num_no_patterns(self)
Definition z3py.py:2183
no_pattern(self, idx)
Definition z3py.py:2187
num_patterns(self)
Definition z3py.py:2153
var_name(self, idx)
Definition z3py.py:2216
__getitem__(self, arg)
Definition z3py.py:2122
var_sort(self, idx)
Definition z3py.py:2232
pattern(self, idx)
Definition z3py.py:2165
numerator_as_long(self)
Definition z3py.py:3111
is_int_value(self)
Definition z3py.py:3141
as_fraction(self)
Definition z3py.py:3169
py_value(self)
Definition z3py.py:3178
numerator(self)
Definition z3py.py:3085
is_real(self)
Definition z3py.py:3138
as_long(self)
Definition z3py.py:3144
is_int(self)
Definition z3py.py:3135
denominator_as_long(self)
Definition z3py.py:3124
as_string(self)
Definition z3py.py:3160
denominator(self)
Definition z3py.py:3100
as_decimal(self, prec)
Definition z3py.py:3148
__init__(self, c, ctx)
Definition z3py.py:5256
__init__(self, c, ctx)
Definition z3py.py:5268
Strings, Sequences and Regular expressions.
Definition z3py.py:10994
__init__(self, solver=None, ctx=None, logFile=None)
Definition z3py.py:7049
assert_and_track(self, a, p)
Definition z3py.py:7218
num_scopes(self)
Definition z3py.py:7130
append(self, *args)
Definition z3py.py:7196
__iadd__(self, fml)
Definition z3py.py:7192
pop(self, num=1)
Definition z3py.py:7108
import_model_converter(self, other)
Definition z3py.py:7296
assert_exprs(self, *args)
Definition z3py.py:7162
model(self)
Definition z3py.py:7277
set(self, *args, **keys)
Definition z3py.py:7073
__enter__(self)
Definition z3py.py:7066
add(self, *args)
Definition z3py.py:7181
__del__(self)
Definition z3py.py:7062
int backtrack_level
Definition z3py.py:7052
insert(self, *args)
Definition z3py.py:7207
check(self, *assumptions)
Definition z3py.py:7248
push(self)
Definition z3py.py:7086
__exit__(self, *exc_info)
Definition z3py.py:7070
reset(self)
Definition z3py.py:7148
subsort(self, other)
Definition z3py.py:599
as_ast(self)
Definition z3py.py:576
__hash__(self)
Definition z3py.py:660
kind(self)
Definition z3py.py:582
__gt__(self, other)
Definition z3py.py:656
get_id(self)
Definition z3py.py:579
__eq__(self, other)
Definition z3py.py:632
__ne__(self, other)
Definition z3py.py:645
cast(self, val)
Definition z3py.py:607
name(self)
Definition z3py.py:622
Statistics.
Definition z3py.py:6858
__deepcopy__(self, memo={})
Definition z3py.py:6866
__getattr__(self, name)
Definition z3py.py:6961
__getitem__(self, idx)
Definition z3py.py:6905
__init__(self, stats, ctx)
Definition z3py.py:6861
__repr__(self)
Definition z3py.py:6873
__len__(self)
Definition z3py.py:6891
__del__(self)
Definition z3py.py:6869
get_key_value(self, key)
Definition z3py.py:6941
subsort(self, other)
Definition z3py.py:734
cast(self, val)
Definition z3py.py:737
ASTs base class.
Definition z3py.py:338
_repr_html_(self)
Definition z3py.py:344
use_pp(self)
Definition z3py.py:341
Z3_ast Z3_API Z3_model_get_const_interp(Z3_context c, Z3_model m, Z3_func_decl a)
Return the interpretation (i.e., assignment) of constant a in the model m. Return NULL,...
Z3_sort Z3_API Z3_mk_int_sort(Z3_context c)
Create the integer type.
Z3_sort Z3_API Z3_mk_array_sort_n(Z3_context c, unsigned n, Z3_sort const *domain, Z3_sort range)
Create an array type with N arguments.
bool Z3_API Z3_open_log(Z3_string filename)
Log interaction to a file.
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_denominator(Z3_context c, Z3_ast a)
Return the denominator (as a numeral AST) of a numeral AST of sort Real.
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_decl_kind Z3_API Z3_get_decl_kind(Z3_context c, Z3_func_decl d)
Return declaration kind corresponding to declaration.
void Z3_API Z3_solver_assert_and_track(Z3_context c, Z3_solver s, Z3_ast a, Z3_ast p)
Assert a constraint a into the solver, and track it (in the unsat) core using the Boolean constant p.
Z3_ast Z3_API Z3_func_interp_get_else(Z3_context c, Z3_func_interp f)
Return the 'else' value of the given function interpretation.
Z3_ast Z3_API Z3_mk_bvsge(Z3_context c, Z3_ast t1, Z3_ast t2)
Two's complement signed greater than or equal to.
void Z3_API Z3_ast_map_inc_ref(Z3_context c, Z3_ast_map m)
Increment the reference counter of the given AST map.
Z3_ast Z3_API Z3_mk_const_array(Z3_context c, Z3_sort domain, Z3_ast v)
Create the constant array.
Z3_ast Z3_API Z3_mk_bvsle(Z3_context c, Z3_ast t1, Z3_ast t2)
Two's complement signed less than or equal to.
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_del_context(Z3_context c)
Delete the given logical context.
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_ast Z3_API Z3_ast_map_find(Z3_context c, Z3_ast_map m, Z3_ast k)
Return the value associated with the key k.
Z3_string Z3_API Z3_ast_map_to_string(Z3_context c, Z3_ast_map m)
Convert the given map into a string.
Z3_string Z3_API Z3_param_descrs_to_string(Z3_context c, Z3_param_descrs p)
Convert a parameter description set into a string. This function is mainly used for printing the cont...
Z3_ast Z3_API Z3_mk_zero_ext(Z3_context c, unsigned i, Z3_ast t1)
Extend the given bit-vector with zeros to the (unsigned) equivalent bit-vector of size m+i,...
void Z3_API Z3_solver_set_params(Z3_context c, Z3_solver s, Z3_params p)
Set the given solver using the given parameters.
Z3_ast Z3_API Z3_mk_set_intersect(Z3_context c, unsigned num_args, Z3_ast const args[])
Take the intersection of a list of sets.
Z3_params Z3_API Z3_mk_params(Z3_context c)
Create a Z3 (empty) parameter set. Starting at Z3 4.0, parameter sets are used to configure many comp...
unsigned Z3_API Z3_get_decl_num_parameters(Z3_context c, Z3_func_decl d)
Return the number of parameters associated with a declaration.
Z3_ast Z3_API Z3_mk_set_subset(Z3_context c, Z3_ast arg1, Z3_ast arg2)
Check for subsetness of sets.
Z3_ast Z3_API Z3_mk_bvule(Z3_context c, Z3_ast t1, Z3_ast t2)
Unsigned less than or equal to.
Z3_ast Z3_API Z3_mk_full_set(Z3_context c, Z3_sort domain)
Create the full set.
Z3_param_kind Z3_API Z3_param_descrs_get_kind(Z3_context c, Z3_param_descrs p, Z3_symbol n)
Return the kind associated with the given parameter name n.
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.
Z3_ast Z3_API Z3_mk_true(Z3_context c)
Create an AST node representing true.
Z3_ast Z3_API Z3_mk_set_union(Z3_context c, unsigned num_args, Z3_ast const args[])
Take the union of a list of sets.
Z3_func_interp Z3_API Z3_add_func_interp(Z3_context c, Z3_model m, Z3_func_decl f, Z3_ast default_value)
Create a fresh func_interp object, add it to a model for a specified function. It has reference count...
Z3_ast Z3_API Z3_mk_bvsdiv_no_overflow(Z3_context c, Z3_ast t1, Z3_ast t2)
Create a predicate that checks that the bit-wise signed division of t1 and t2 does not overflow.
unsigned Z3_API Z3_get_arity(Z3_context c, Z3_func_decl d)
Alias for Z3_get_domain_size.
void Z3_API Z3_ast_vector_set(Z3_context c, Z3_ast_vector v, unsigned i, Z3_ast a)
Update position i of the AST vector v with the AST a.
Z3_ast Z3_API Z3_mk_bvxor(Z3_context c, Z3_ast t1, Z3_ast t2)
Bitwise exclusive-or.
Z3_string Z3_API Z3_stats_to_string(Z3_context c, Z3_stats s)
Convert a statistics into a string.
Z3_sort Z3_API Z3_mk_real_sort(Z3_context c)
Create the real type.
Z3_ast Z3_API Z3_mk_le(Z3_context c, Z3_ast t1, Z3_ast t2)
Create less than or equal to.
bool Z3_API Z3_global_param_get(Z3_string param_id, Z3_string_ptr param_value)
Get a global (or module) parameter.
bool Z3_API Z3_goal_inconsistent(Z3_context c, Z3_goal g)
Return true if the given goal contains the formula false.
Z3_ast Z3_API Z3_mk_lambda_const(Z3_context c, unsigned num_bound, Z3_app const bound[], Z3_ast body)
Create a lambda expression using a list of constants that form the set of bound variables.
void Z3_API Z3_solver_dec_ref(Z3_context c, Z3_solver s)
Decrement the reference counter of the given solver.
Z3_ast Z3_API Z3_mk_bvslt(Z3_context c, Z3_ast t1, Z3_ast t2)
Two's complement signed less than.
Z3_func_decl Z3_API Z3_model_get_func_decl(Z3_context c, Z3_model m, unsigned i)
Return the declaration of the i-th function in the given model.
bool Z3_API Z3_ast_map_contains(Z3_context c, Z3_ast_map m, Z3_ast k)
Return true if the map m contains the AST key k.
Z3_ast Z3_API Z3_mk_numeral(Z3_context c, Z3_string numeral, Z3_sort ty)
Create a numeral of a given sort.
unsigned Z3_API Z3_func_entry_get_num_args(Z3_context c, Z3_func_entry e)
Return the number of arguments in a Z3_func_entry object.
Z3_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_symbol Z3_API Z3_get_quantifier_skolem_id(Z3_context c, Z3_ast a)
Obtain skolem id of quantifier.
Z3_ast Z3_API Z3_get_numerator(Z3_context c, Z3_ast a)
Return the numerator (as a numeral AST) of a numeral AST of sort Real.
Z3_ast Z3_API Z3_mk_unary_minus(Z3_context c, Z3_ast arg)
Create an AST node representing - arg.
Z3_ast Z3_API Z3_mk_and(Z3_context c, unsigned num_args, Z3_ast const args[])
Create an AST node representing args[0] and ... and args[num_args-1].
void Z3_API Z3_interrupt(Z3_context c)
Interrupt the execution of a Z3 procedure. This procedure can be used to interrupt: solvers,...
void Z3_API Z3_goal_assert(Z3_context c, Z3_goal g, Z3_ast a)
Add a new formula a to the given goal. The formula is split according to the following procedure that...
Z3_symbol Z3_API Z3_param_descrs_get_name(Z3_context c, Z3_param_descrs p, unsigned i)
Return the name of the parameter at given index i.
Z3_ast Z3_API Z3_func_entry_get_value(Z3_context c, Z3_func_entry e)
Return the value of this point.
bool Z3_API Z3_is_quantifier_exists(Z3_context c, Z3_ast a)
Determine if ast is an existential quantifier.
Z3_sort Z3_API Z3_mk_uninterpreted_sort(Z3_context c, Z3_symbol s)
Create a free (uninterpreted) type using the given name (symbol).
Z3_ast Z3_API Z3_mk_false(Z3_context c)
Create an AST node representing false.
Z3_ast_vector Z3_API Z3_ast_map_keys(Z3_context c, Z3_ast_map m)
Return the keys stored in the given map.
Z3_ast Z3_API Z3_mk_bvmul(Z3_context c, Z3_ast t1, Z3_ast t2)
Standard two's complement multiplication.
Z3_model Z3_API Z3_goal_convert_model(Z3_context c, Z3_goal g, Z3_model m)
Convert a model of the formulas of a goal to a model of an original goal. The model may be null,...
void Z3_API Z3_del_constructor(Z3_context c, Z3_constructor constr)
Reclaim memory allocated to constructor.
Z3_ast Z3_API Z3_mk_bvsgt(Z3_context c, Z3_ast t1, Z3_ast t2)
Two's complement signed greater than.
Z3_string Z3_API Z3_ast_to_string(Z3_context c, Z3_ast a)
Convert the given AST node into a string.
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....
Z3_string Z3_API Z3_get_full_version(void)
Return a string that fully describes the version of Z3 in use.
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.
Z3_ast Z3_API Z3_mk_set_complement(Z3_context c, Z3_ast arg)
Take the complement of a set.
unsigned Z3_API Z3_get_quantifier_num_patterns(Z3_context c, Z3_ast a)
Return number of patterns used in quantifier.
Z3_symbol Z3_API Z3_get_quantifier_bound_name(Z3_context c, Z3_ast a, unsigned i)
Return symbol of the i'th bound variable.
bool Z3_API Z3_stats_is_uint(Z3_context c, Z3_stats s, unsigned idx)
Return true if the given statistical data is a unsigned integer.
unsigned Z3_API Z3_model_get_num_consts(Z3_context c, Z3_model m)
Return the number of constants assigned by the given model.
Z3_ast Z3_API Z3_mk_extract(Z3_context c, unsigned high, unsigned low, Z3_ast t1)
Extract the bits high down to low from a bit-vector of size m to yield a new bit-vector of size n,...
Z3_ast Z3_API Z3_mk_mod(Z3_context c, Z3_ast arg1, Z3_ast arg2)
Create an AST node representing arg1 mod arg2.
Z3_ast Z3_API Z3_mk_bvredand(Z3_context c, Z3_ast t1)
Take conjunction of bits in vector, return vector of length 1.
Z3_ast Z3_API Z3_mk_set_add(Z3_context c, Z3_ast set, Z3_ast elem)
Add an element to a set.
Z3_ast Z3_API Z3_mk_ge(Z3_context c, Z3_ast t1, Z3_ast t2)
Create greater than or equal to.
Z3_ast Z3_API Z3_mk_bvadd_no_underflow(Z3_context c, Z3_ast t1, Z3_ast t2)
Create a predicate that checks that the bit-wise signed addition of t1 and t2 does not underflow.
Z3_ast Z3_API Z3_mk_bvadd_no_overflow(Z3_context c, Z3_ast t1, Z3_ast t2, bool is_signed)
Create a predicate that checks that the bit-wise addition of t1 and t2 does not overflow.
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.
Z3_ast Z3_API Z3_mk_array_default(Z3_context c, Z3_ast array)
Access the array default value. Produces the default range value, for arrays that can be represented ...
unsigned Z3_API Z3_model_get_num_sorts(Z3_context c, Z3_model m)
Return the number of uninterpreted sorts that m assigns an interpretation to.
Z3_ast_vector Z3_API Z3_ast_vector_translate(Z3_context s, Z3_ast_vector v, Z3_context t)
Translate the AST vector v from context s into an AST vector in context t.
void Z3_API Z3_func_entry_inc_ref(Z3_context c, Z3_func_entry e)
Increment the reference counter of the given Z3_func_entry object.
Z3_ast Z3_API Z3_mk_fresh_const(Z3_context c, Z3_string prefix, Z3_sort ty)
Declare and create a fresh constant.
Z3_ast Z3_API Z3_mk_bvsub_no_overflow(Z3_context c, Z3_ast t1, Z3_ast t2)
Create a predicate that checks that the bit-wise signed subtraction of t1 and t2 does not overflow.
void Z3_API Z3_solver_push(Z3_context c, Z3_solver s)
Create a backtracking point.
Z3_ast Z3_API Z3_mk_bvsub_no_underflow(Z3_context c, Z3_ast t1, Z3_ast t2, bool is_signed)
Create a predicate that checks that the bit-wise subtraction of t1 and t2 does not underflow.
Z3_goal Z3_API Z3_goal_translate(Z3_context source, Z3_goal g, Z3_context target)
Copy a goal g from the context source to the context target.
Z3_ast Z3_API Z3_mk_bvudiv(Z3_context c, Z3_ast t1, Z3_ast t2)
Unsigned division.
Z3_string Z3_API Z3_ast_vector_to_string(Z3_context c, Z3_ast_vector v)
Convert AST vector into a string.
Z3_ast Z3_API Z3_mk_bvshl(Z3_context c, Z3_ast t1, Z3_ast t2)
Shift left.
bool Z3_API Z3_is_numeral_ast(Z3_context c, Z3_ast a)
Z3_ast Z3_API Z3_mk_bvsrem(Z3_context c, Z3_ast t1, Z3_ast t2)
Two's complement signed remainder (sign follows dividend).
bool Z3_API Z3_is_as_array(Z3_context c, Z3_ast a)
The (_ as-array f) AST node is a construct for assigning interpretations for arrays in Z3....
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.
Z3_ast Z3_API Z3_mk_is_int(Z3_context c, Z3_ast t1)
Check if a real number is an integer.
void Z3_API Z3_params_set_bool(Z3_context c, Z3_params p, Z3_symbol k, bool v)
Add a Boolean parameter k with value v to the parameter set p.
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_ast Z3_API Z3_mk_select(Z3_context c, Z3_ast a, Z3_ast i)
Array read. The argument a is the array and i is the index of the array that gets read.
Z3_ast Z3_API Z3_mk_sign_ext(Z3_context c, unsigned i, Z3_ast t1)
Sign-extend of the given bit-vector to the (signed) equivalent bit-vector of size m+i,...
unsigned Z3_API Z3_goal_size(Z3_context c, Z3_goal g)
Return the number of formulas in the given goal.
void Z3_API Z3_stats_inc_ref(Z3_context c, Z3_stats s)
Increment the reference counter of the given statistics object.
Z3_ast Z3_API Z3_mk_select_n(Z3_context c, Z3_ast a, unsigned n, Z3_ast const *idxs)
n-ary Array read. The argument a is the array and idxs are the indices of the array that gets read.
Z3_ast_vector Z3_API Z3_algebraic_get_poly(Z3_context c, Z3_ast a)
Return the coefficients of the defining polynomial.
Z3_ast Z3_API Z3_mk_div(Z3_context c, Z3_ast arg1, Z3_ast arg2)
Create an AST node representing arg1 div arg2.
void Z3_API Z3_model_dec_ref(Z3_context c, Z3_model m)
Decrement the reference counter of the given model.
void Z3_API Z3_func_interp_inc_ref(Z3_context c, Z3_func_interp f)
Increment the reference counter of the given Z3_func_interp object.
void Z3_API Z3_params_set_double(Z3_context c, Z3_params p, Z3_symbol k, double v)
Add a double parameter k with value v to the parameter set p.
Z3_string Z3_API Z3_param_descrs_get_documentation(Z3_context c, Z3_param_descrs p, Z3_symbol s)
Retrieve documentation string corresponding to parameter name s.
Z3_sort Z3_API Z3_mk_datatype_sort(Z3_context c, Z3_symbol name)
create a forward reference to a recursive datatype being declared. The forward reference can be used ...
Z3_solver Z3_API Z3_mk_solver(Z3_context c)
Create a new solver. This solver is a "combined solver" (see combined_solver module) that internally ...
Z3_model Z3_API Z3_solver_get_model(Z3_context c, Z3_solver s)
Retrieve the model for the last Z3_solver_check or Z3_solver_check_assumptions.
int Z3_API Z3_get_symbol_int(Z3_context c, Z3_symbol s)
Return the symbol int value.
Z3_func_decl Z3_API Z3_get_as_array_func_decl(Z3_context c, Z3_ast a)
Return the function declaration f associated with a (_ as_array f) node.
Z3_ast Z3_API Z3_mk_ext_rotate_left(Z3_context c, Z3_ast t1, Z3_ast t2)
Rotate bits of t1 to the left t2 times.
void Z3_API Z3_goal_inc_ref(Z3_context c, Z3_goal g)
Increment the reference counter of the given goal.
Z3_ast Z3_API Z3_mk_implies(Z3_context c, Z3_ast t1, Z3_ast t2)
Create an AST node representing t1 implies t2.
unsigned Z3_API Z3_get_datatype_sort_num_constructors(Z3_context c, Z3_sort t)
Return number of constructors for datatype.
void Z3_API Z3_params_set_uint(Z3_context c, Z3_params p, Z3_symbol k, unsigned v)
Add a unsigned parameter k with value v to the parameter set p.
Z3_lbool Z3_API Z3_solver_check_assumptions(Z3_context c, Z3_solver s, unsigned num_assumptions, Z3_ast const assumptions[])
Check whether the assertions in the given solver and optional assumptions are consistent or not.
Z3_sort Z3_API Z3_model_get_sort(Z3_context c, Z3_model m, unsigned i)
Return a uninterpreted sort that m assigns an interpretation.
Z3_ast Z3_API Z3_mk_bvashr(Z3_context c, Z3_ast t1, Z3_ast t2)
Arithmetic shift right.
Z3_ast Z3_API Z3_mk_bv2int(Z3_context c, Z3_ast t1, bool is_signed)
Create an integer from the bit-vector argument t1. If is_signed is false, then the bit-vector t1 is t...
Z3_sort Z3_API Z3_get_array_sort_domain_n(Z3_context c, Z3_sort t, unsigned idx)
Return the i'th domain sort of an n-dimensional array.
Z3_ast Z3_API Z3_mk_set_del(Z3_context c, Z3_ast set, Z3_ast elem)
Remove an element to a set.
Z3_ast Z3_API Z3_mk_bvmul_no_overflow(Z3_context c, Z3_ast t1, Z3_ast t2, bool is_signed)
Create a predicate that checks that the bit-wise multiplication of t1 and t2 does not overflow.
Z3_ast Z3_API Z3_mk_bvor(Z3_context c, Z3_ast t1, Z3_ast t2)
Bitwise or.
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_quantifier_num_no_patterns(Z3_context c, Z3_ast a)
Return number of no_patterns used in quantifier.
Z3_func_decl Z3_API Z3_get_datatype_sort_constructor(Z3_context c, Z3_sort t, unsigned idx)
Return idx'th constructor.
void Z3_API Z3_ast_vector_resize(Z3_context c, Z3_ast_vector v, unsigned n)
Resize the AST vector v.
Z3_ast Z3_API Z3_mk_quantifier_const_ex(Z3_context c, bool is_forall, unsigned weight, Z3_symbol quantifier_id, Z3_symbol skolem_id, unsigned num_bound, Z3_app const bound[], unsigned num_patterns, Z3_pattern const patterns[], unsigned num_no_patterns, Z3_ast const no_patterns[], Z3_ast body)
Create a universal or existential quantifier using a list of constants that will form the set of boun...
Z3_pattern Z3_API Z3_mk_pattern(Z3_context c, unsigned num_patterns, Z3_ast const terms[])
Create a pattern for quantifier instantiation.
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...
bool Z3_API Z3_is_lambda(Z3_context c, Z3_ast a)
Determine if ast is a lambda expression.
unsigned Z3_API Z3_stats_get_uint_value(Z3_context c, Z3_stats s, unsigned idx)
Return the unsigned value of the given statistical data.
Z3_sort Z3_API Z3_get_array_sort_domain(Z3_context c, Z3_sort t)
Return the domain of the given array sort. In the case of a multi-dimensional array,...
Z3_ast Z3_API Z3_mk_bvmul_no_underflow(Z3_context c, Z3_ast t1, Z3_ast t2)
Create a predicate that checks that the bit-wise signed multiplication of t1 and t2 does not underflo...
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.
void Z3_API Z3_add_const_interp(Z3_context c, Z3_model m, Z3_func_decl f, Z3_ast a)
Add a constant interpretation.
Z3_ast Z3_API Z3_mk_bvadd(Z3_context c, Z3_ast t1, Z3_ast t2)
Standard two's complement addition.
unsigned Z3_API Z3_algebraic_get_i(Z3_context c, Z3_ast a)
Return which root of the polynomial the algebraic number represents.
void Z3_API Z3_params_dec_ref(Z3_context c, Z3_params p)
Decrement the reference counter of the given parameter set.
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_string Z3_API Z3_model_to_string(Z3_context c, Z3_model m)
Convert the given model into a string.
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.
unsigned Z3_API Z3_ast_map_size(Z3_context c, Z3_ast_map m)
Return the size of the given map.
unsigned Z3_API Z3_param_descrs_size(Z3_context c, Z3_param_descrs p)
Return the number of parameters in the given parameter description set.
Z3_string Z3_API Z3_goal_to_dimacs_string(Z3_context c, Z3_goal g, bool include_names)
Convert a goal into a DIMACS formatted string. The goal must be in CNF. You can convert a goal to CNF...
Z3_ast Z3_API Z3_mk_lt(Z3_context c, Z3_ast t1, Z3_ast t2)
Create less than.
Z3_ast Z3_API Z3_get_quantifier_no_pattern_ast(Z3_context c, Z3_ast a, unsigned i)
Return i'th no_pattern.
double Z3_API Z3_stats_get_double_value(Z3_context c, Z3_stats s, unsigned idx)
Return the double value of the given statistical data.
Z3_ast Z3_API Z3_mk_bvugt(Z3_context c, Z3_ast t1, Z3_ast t2)
Unsigned greater than.
unsigned Z3_API Z3_goal_depth(Z3_context c, Z3_goal g)
Return the depth of the given goal. It tracks how many transformations were applied to it.
Z3_string Z3_API Z3_get_symbol_string(Z3_context c, Z3_symbol s)
Return the symbol name.
Z3_ast Z3_API Z3_pattern_to_ast(Z3_context c, Z3_pattern p)
Convert a Z3_pattern into Z3_ast. This is just type casting.
Z3_ast Z3_API Z3_mk_bvnot(Z3_context c, Z3_ast t1)
Bitwise negation.
Z3_ast Z3_API Z3_mk_bvurem(Z3_context c, Z3_ast t1, Z3_ast t2)
Unsigned remainder.
void Z3_API Z3_mk_datatypes(Z3_context c, unsigned num_sorts, Z3_symbol const sort_names[], Z3_sort sorts[], Z3_constructor_list constructor_lists[])
Create mutually recursive datatypes.
unsigned Z3_API Z3_func_interp_get_arity(Z3_context c, Z3_func_interp f)
Return the arity (number of arguments) of the given function interpretation.
Z3_ast Z3_API Z3_mk_bvsub(Z3_context c, Z3_ast t1, Z3_ast t2)
Standard two's complement subtraction.
Z3_ast Z3_API Z3_get_algebraic_number_upper(Z3_context c, Z3_ast a, unsigned precision)
Return a upper bound for the given real algebraic number. The interval isolating the number is smalle...
Z3_ast Z3_API Z3_mk_power(Z3_context c, Z3_ast arg1, Z3_ast arg2)
Create an AST node representing arg1 ^ arg2.
Z3_ast Z3_API Z3_mk_seq_concat(Z3_context c, unsigned n, Z3_ast const args[])
Concatenate sequences.
Z3_sort Z3_API Z3_mk_enumeration_sort(Z3_context c, Z3_symbol name, unsigned n, Z3_symbol const enum_names[], Z3_func_decl enum_consts[], Z3_func_decl enum_testers[])
Create a enumeration sort.
unsigned Z3_API Z3_get_bv_sort_size(Z3_context c, Z3_sort t)
Return the size of the given bit-vector sort.
Z3_ast Z3_API Z3_mk_set_member(Z3_context c, Z3_ast elem, Z3_ast set)
Check for set membership.
void Z3_API Z3_ast_vector_dec_ref(Z3_context c, Z3_ast_vector v)
Decrement the reference counter of the given AST vector.
void Z3_API Z3_func_interp_dec_ref(Z3_context c, Z3_func_interp f)
Decrement the reference counter of the given Z3_func_interp object.
void Z3_API Z3_params_inc_ref(Z3_context c, Z3_params p)
Increment the reference counter of the given parameter set.
void Z3_API Z3_set_error_handler(Z3_context c, Z3_error_handler h)
Register a Z3 error handler.
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_config Z3_API Z3_mk_config(void)
Create a configuration object for the Z3 context object.
void Z3_API Z3_set_param_value(Z3_config c, Z3_string param_id, Z3_string param_value)
Set a configuration parameter.
Z3_sort Z3_API Z3_mk_bv_sort(Z3_context c, unsigned sz)
Create a bit-vector type of the given size.
Z3_ast Z3_API Z3_mk_bvult(Z3_context c, Z3_ast t1, Z3_ast t2)
Unsigned less than.
void Z3_API Z3_ast_map_dec_ref(Z3_context c, Z3_ast_map m)
Decrement the reference counter of the given AST map.
Z3_string Z3_API Z3_params_to_string(Z3_context c, Z3_params p)
Convert a parameter set into a string. This function is mainly used for printing the contents of a pa...
Z3_param_descrs Z3_API Z3_get_global_param_descrs(Z3_context c)
Retrieve description of global parameters.
Z3_func_decl Z3_API Z3_model_get_const_decl(Z3_context c, Z3_model m, unsigned i)
Return the i-th constant in the given model.
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...
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_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_ast_vector Z3_API Z3_model_get_sort_universe(Z3_context c, Z3_model m, Z3_sort s)
Return the finite set of distinct values that represent the interpretation for sort s.
void Z3_API Z3_func_entry_dec_ref(Z3_context c, Z3_func_entry e)
Decrement the reference counter of the given Z3_func_entry object.
unsigned Z3_API Z3_stats_size(Z3_context c, Z3_stats s)
Return the number of statistical data in s.
void Z3_API Z3_append_log(Z3_string string)
Append user-defined string to interaction log.
Z3_ast Z3_API Z3_get_quantifier_body(Z3_context c, Z3_ast a)
Return body of quantifier.
void Z3_API Z3_param_descrs_dec_ref(Z3_context c, Z3_param_descrs p)
Decrement the reference counter of the given parameter description set.
Z3_model Z3_API Z3_mk_model(Z3_context c)
Create a fresh model object. It has reference count 0.
Z3_symbol Z3_API Z3_get_decl_name(Z3_context c, Z3_func_decl d)
Return the constant declaration name as a symbol.
Z3_ast Z3_API Z3_mk_bvneg_no_overflow(Z3_context c, Z3_ast t1)
Check that bit-wise negation does not overflow when t1 is interpreted as a signed bit-vector.
Z3_string Z3_API Z3_stats_get_key(Z3_context c, Z3_stats s, unsigned idx)
Return the key (a string) for a particular statistical data.
Z3_ast Z3_API Z3_mk_bvand(Z3_context c, Z3_ast t1, Z3_ast t2)
Bitwise and.
Z3_ast_kind Z3_API Z3_get_ast_kind(Z3_context c, Z3_ast a)
Return the kind of the given AST.
Z3_ast Z3_API Z3_mk_bvsmod(Z3_context c, Z3_ast t1, Z3_ast t2)
Two's complement signed remainder (sign follows divisor).
Z3_model Z3_API Z3_model_translate(Z3_context c, Z3_model m, Z3_context dst)
translate model from context c to context dst.
void Z3_API Z3_get_version(unsigned *major, unsigned *minor, unsigned *build_number, unsigned *revision_number)
Return Z3 version number information.
Z3_ast Z3_API Z3_mk_int2bv(Z3_context c, unsigned n, Z3_ast t1)
Create an n bit bit-vector from the integer argument t1.
void Z3_API Z3_solver_assert(Z3_context c, Z3_solver s, Z3_ast a)
Assert a constraint into the solver.
unsigned Z3_API Z3_ast_vector_size(Z3_context c, Z3_ast_vector v)
Return the size of the given AST vector.
unsigned Z3_API Z3_get_quantifier_weight(Z3_context c, Z3_ast a)
Obtain weight of quantifier.
bool Z3_API Z3_model_eval(Z3_context c, Z3_model m, Z3_ast t, bool model_completion, Z3_ast *v)
Evaluate the AST node t in the given model. Return true if succeeded, and store the result in v.
unsigned Z3_API Z3_solver_get_num_scopes(Z3_context c, Z3_solver s)
Return the number of backtracking points.
Z3_sort Z3_API Z3_get_array_sort_range(Z3_context c, Z3_sort t)
Return the range of the given array sort.
void Z3_API Z3_del_constructor_list(Z3_context c, Z3_constructor_list clist)
Reclaim memory allocated for constructor list.
Z3_ast Z3_API Z3_mk_bound(Z3_context c, unsigned index, Z3_sort ty)
Create a variable.
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_ast Z3_API Z3_func_entry_get_arg(Z3_context c, Z3_func_entry e, unsigned i)
Return an argument of a Z3_func_entry object.
Z3_ast Z3_API Z3_mk_eq(Z3_context c, Z3_ast l, Z3_ast r)
Create an AST node representing l = r.
void Z3_API Z3_ast_vector_inc_ref(Z3_context c, Z3_ast_vector v)
Increment the reference counter of the given AST vector.
unsigned Z3_API Z3_model_get_num_funcs(Z3_context c, Z3_model m)
Return the number of function interpretations in the given model.
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_...
Z3_ast_vector Z3_API Z3_mk_ast_vector(Z3_context c)
Return an empty AST vector.
Z3_ast Z3_API Z3_mk_empty_set(Z3_context c, Z3_sort domain)
Create the empty set.
Z3_ast Z3_API Z3_mk_set_has_size(Z3_context c, Z3_ast set, Z3_ast k)
Create predicate that holds if Boolean array set has k elements set to true.
Z3_ast Z3_API Z3_mk_repeat(Z3_context c, unsigned i, Z3_ast t1)
Repeat the given bit-vector up length i.
Z3_goal_prec Z3_API Z3_goal_precision(Z3_context c, Z3_goal g)
Return the "precision" of the given goal. Goals can be transformed using over and under approximation...
void Z3_API Z3_solver_pop(Z3_context c, Z3_solver s, unsigned n)
Backtrack n backtracking points.
void Z3_API Z3_ast_map_erase(Z3_context c, Z3_ast_map m, Z3_ast k)
Erase a key from the map.
Z3_ast Z3_API Z3_mk_int2real(Z3_context c, Z3_ast t1)
Coerce an integer to a real.
unsigned Z3_API Z3_get_index_value(Z3_context c, Z3_ast a)
Return index of de-Bruijn bound variable.
Z3_goal Z3_API Z3_mk_goal(Z3_context c, bool models, bool unsat_cores, bool proofs)
Create a goal (aka problem). A goal is essentially a set of formulas, that can be solved and/or trans...
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...
Z3_symbol Z3_API Z3_get_sort_name(Z3_context c, Z3_sort d)
Return the sort name as a symbol.
void Z3_API Z3_params_validate(Z3_context c, Z3_params p, Z3_param_descrs d)
Validate the parameter set p against the parameter description set d.
Z3_func_decl Z3_API Z3_get_datatype_sort_recognizer(Z3_context c, Z3_sort t, unsigned idx)
Return idx'th recognizer.
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...
Z3_ast Z3_API Z3_mk_gt(Z3_context c, Z3_ast t1, Z3_ast t2)
Create greater than.
Z3_ast Z3_API Z3_mk_store(Z3_context c, Z3_ast a, Z3_ast i, Z3_ast v)
Array update.
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_ast_vector_push(Z3_context c, Z3_ast_vector v, Z3_ast a)
Add the AST a in the end of the AST vector v. The size of v is increased by one.
bool Z3_API Z3_is_eq_ast(Z3_context c, Z3_ast t1, Z3_ast t2)
Compare terms.
bool Z3_API Z3_is_quantifier_forall(Z3_context c, Z3_ast a)
Determine if an ast is a universal quantifier.
Z3_ast_map Z3_API Z3_mk_ast_map(Z3_context c)
Return an empty mapping from AST to AST.
Z3_ast Z3_API Z3_mk_xor(Z3_context c, Z3_ast t1, Z3_ast t2)
Create an AST node representing t1 xor t2.
Z3_ast Z3_API Z3_mk_map(Z3_context c, Z3_func_decl f, unsigned n, Z3_ast const *args)
Map f on the argument arrays.
Z3_ast Z3_API Z3_mk_const(Z3_context c, Z3_symbol s, Z3_sort ty)
Declare and create a constant.
Z3_symbol Z3_API Z3_mk_string_symbol(Z3_context c, Z3_string s)
Create a Z3 symbol using a C string.
void Z3_API Z3_param_descrs_inc_ref(Z3_context c, Z3_param_descrs p)
Increment the reference counter of the given parameter description set.
void Z3_API Z3_stats_dec_ref(Z3_context c, Z3_stats s)
Decrement the reference counter of the given statistics object.
Z3_ast Z3_API Z3_mk_array_ext(Z3_context c, Z3_ast arg1, Z3_ast arg2)
Create array extensionality index given two arrays with the same sort. The meaning is given by the ax...
Z3_ast Z3_API Z3_mk_re_concat(Z3_context c, unsigned n, Z3_ast const args[])
Create the concatenation of the regular languages.
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.
Z3_func_entry Z3_API Z3_func_interp_get_entry(Z3_context c, Z3_func_interp f, unsigned i)
Return a "point" of the given function interpretation. It represents the value of f in a particular p...
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.
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....
Z3_ast Z3_API Z3_mk_concat(Z3_context c, Z3_ast t1, Z3_ast t2)
Concatenate the given bit-vectors.
unsigned Z3_API Z3_get_quantifier_num_bound(Z3_context c, Z3_ast a)
Return number of bound variables of quantifier.
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.
Z3_constructor_list Z3_API Z3_mk_constructor_list(Z3_context c, unsigned num_constructors, Z3_constructor const constructors[])
Create list of constructors.
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.
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).
Z3_ast Z3_API Z3_mk_bvneg(Z3_context c, Z3_ast t1)
Standard two's complement unary minus.
Z3_ast Z3_API Z3_mk_store_n(Z3_context c, Z3_ast a, unsigned n, Z3_ast const *idxs, Z3_ast v)
n-ary Array update.
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.
Z3_sort Z3_API Z3_mk_bool_sort(Z3_context c)
Create the Boolean type.
void Z3_API Z3_params_set_symbol(Z3_context c, Z3_params p, Z3_symbol k, Z3_symbol v)
Add a symbol parameter k with value v to the parameter set p.
Z3_ast Z3_API Z3_ast_vector_get(Z3_context c, Z3_ast_vector v, unsigned i)
Return the AST at position i in the AST vector v.
Z3_func_decl Z3_API Z3_to_func_decl(Z3_context c, Z3_ast a)
Convert an AST into a FUNC_DECL_AST. This is just type casting.
Z3_ast Z3_API Z3_mk_set_difference(Z3_context c, Z3_ast arg1, Z3_ast arg2)
Take the set difference between two sets.
Z3_ast Z3_API Z3_mk_bvsdiv(Z3_context c, Z3_ast t1, Z3_ast t2)
Two's complement signed division.
Z3_ast Z3_API Z3_mk_bvlshr(Z3_context c, Z3_ast t1, Z3_ast t2)
Logical shift right.
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.
Z3_pattern Z3_API Z3_get_quantifier_pattern_ast(Z3_context c, Z3_ast a, unsigned i)
Return i'th pattern.
void Z3_API Z3_goal_dec_ref(Z3_context c, Z3_goal g)
Decrement the reference counter of the given goal.
Z3_ast Z3_API Z3_mk_not(Z3_context c, Z3_ast a)
Create an AST node representing not(a).
Z3_ast Z3_API Z3_mk_or(Z3_context c, unsigned num_args, Z3_ast const args[])
Create an AST node representing args[0] or ... or args[num_args-1].
Z3_sort Z3_API Z3_mk_array_sort(Z3_context c, Z3_sort domain, Z3_sort range)
Create an array type.
void Z3_API Z3_model_inc_ref(Z3_context c, Z3_model m)
Increment the reference counter of the given model.
Z3_ast Z3_API Z3_mk_seq_extract(Z3_context c, Z3_ast s, Z3_ast offset, Z3_ast length)
Extract subsequence starting at offset of length.
Z3_sort Z3_API Z3_mk_type_variable(Z3_context c, Z3_symbol s)
Create a type variable.
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.
void Z3_API Z3_func_interp_add_entry(Z3_context c, Z3_func_interp fi, Z3_ast_vector args, Z3_ast value)
add a function entry to a function interpretation.
Z3_ast Z3_API Z3_mk_bvuge(Z3_context c, Z3_ast t1, Z3_ast t2)
Unsigned greater than or equal to.
Z3_string Z3_API Z3_get_numeral_binary_string(Z3_context c, Z3_ast a)
Return numeral value, as a binary string of a numeric constant term.
Z3_sort Z3_API Z3_get_quantifier_bound_sort(Z3_context c, Z3_ast a, unsigned i)
Return sort of the i'th bound variable.
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_ast Z3_API Z3_goal_formula(Z3_context c, Z3_goal g, unsigned idx)
Return a formula from the given goal.
Z3_symbol Z3_API Z3_mk_int_symbol(Z3_context c, int i)
Create a Z3 symbol using an integer.
unsigned Z3_API Z3_func_interp_get_num_entries(Z3_context c, Z3_func_interp f)
Return the number of entries in the given function interpretation.
void Z3_API Z3_ast_map_insert(Z3_context c, Z3_ast_map m, Z3_ast k, Z3_ast v)
Store/Replace a new key, value pair in the given map.
Z3_constructor Z3_API Z3_mk_constructor(Z3_context c, Z3_symbol name, Z3_symbol recognizer, unsigned num_fields, Z3_symbol const field_names[], Z3_sort const sorts[], unsigned sort_refs[])
Create a constructor.
Z3_string Z3_API Z3_goal_to_string(Z3_context c, Z3_goal g)
Convert a goal into a string.
bool Z3_API Z3_is_eq_sort(Z3_context c, Z3_sort s1, Z3_sort s2)
compare sorts.
void Z3_API Z3_del_config(Z3_config c)
Delete the given configuration object.
double Z3_API Z3_get_numeral_double(Z3_context c, Z3_ast a)
Return numeral as a double.
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_real2int(Z3_context c, Z3_ast t1)
Coerce a real to an integer.
Z3_func_interp Z3_API Z3_model_get_func_interp(Z3_context c, Z3_model m, Z3_func_decl f)
Return the interpretation of the function f in the model m. Return NULL, if the model does not assign...
void Z3_API Z3_solver_inc_ref(Z3_context c, Z3_solver s)
Increment the reference counter of the given solver.
Z3_symbol Z3_API Z3_get_quantifier_id(Z3_context c, Z3_ast a)
Obtain id of quantifier.
Z3_ast Z3_API Z3_mk_ext_rotate_right(Z3_context c, Z3_ast t1, Z3_ast t2)
Rotate bits of t1 to the right t2 times.
Z3_string Z3_API Z3_get_numeral_decimal_string(Z3_context c, Z3_ast a, unsigned precision)
Return numeral as a string in decimal notation. The result has at most precision decimal places.
Z3_sort Z3_API Z3_get_sort(Z3_context c, Z3_ast a)
Return the sort of an AST node.
Z3_func_decl Z3_API Z3_get_datatype_sort_constructor_accessor(Z3_context c, Z3_sort t, unsigned idx_c, unsigned idx_a)
Return idx_a'th accessor for the idx_c'th constructor.
Z3_ast Z3_API Z3_mk_bvredor(Z3_context c, Z3_ast t1)
Take disjunction of bits in vector, return vector of length 1.
void Z3_API Z3_ast_map_reset(Z3_context c, Z3_ast_map m)
Remove all keys from the given map.
void Z3_API Z3_solver_reset(Z3_context c, Z3_solver s)
Remove all assertions from the solver.
bool Z3_API Z3_is_algebraic_number(Z3_context c, Z3_ast a)
Return true if the given AST is a real algebraic number.
_py2expr(a, ctx=None)
Definition z3py.py:3216
RotateRight(a, b)
Definition z3py.py:4466
_symbol2py(ctx, s)
Definition z3py.py:140
BitVecVal(val, bv, ctx=None)
Definition z3py.py:4116
BVSNegNoOverflow(a)
Definition z3py.py:4613
SetAdd(s, e)
Definition z3py.py:5085
SetSort(s)
Sets.
Definition z3py.py:5036
_coerce_exprs(a, b, ctx=None)
Definition z3py.py:1249
UGT(a, b)
Definition z3py.py:4337
is_probe(p)
Definition z3py.py:8896
SetDel(s, e)
Definition z3py.py:5096
bool is_le(Any a)
Definition z3py.py:2956
BoolSort(ctx=None)
Definition z3py.py:1768
is_bv_sort(s)
Definition z3py.py:3567
_ctx_from_ast_args(*args)
Definition z3py.py:525
RatVal(a, b, ctx=None)
Definition z3py.py:3308
_to_func_decl_ref(a, ctx)
Definition z3py.py:945
SetUnion(*args)
Definition z3py.py:5059
_valid_accessor(acc)
Datatypes.
Definition z3py.py:5156
BitVec(name, bv, ctx=None)
Definition z3py.py:4133
EmptySet(s)
Definition z3py.py:5041
BVMulNoUnderflow(a, b)
Definition z3py.py:4627
CreateDatatypes(*ds)
Definition z3py.py:5277
is_func_decl(a)
Definition z3py.py:890
get_as_array_func(n)
Definition z3py.py:6845
Distinct(*args)
Definition z3py.py:1451
RecAddDefinition(f, args, body)
Definition z3py.py:967
ToInt(a)
Definition z3py.py:3467
Implies(a, b, ctx=None)
Definition z3py.py:1862
UGE(a, b)
Definition z3py.py:4319
Ext(a, b)
Definition z3py.py:4987
_to_ast_array(args)
Definition z3py.py:537
bool is_sort(Any s)
Definition z3py.py:665
_check_bv_args(a, b)
Definition z3py.py:4278
RealSort(ctx=None)
Definition z3py.py:3248
IsSubset(a, b)
Definition z3py.py:5139
DeclareTypeVar(name, ctx=None)
Definition z3py.py:741
_get_args_ast_list(args)
Definition z3py.py:168
bool is_to_real(Any a)
Definition z3py.py:3016
_to_ref_array(ref, args)
Definition z3py.py:545
get_map_func(a)
Definition z3py.py:4795
_z3_check_cint_overflow(n, name)
Definition z3py.py:118
bool is_and(Any a)
Definition z3py.py:1698
TupleSort(name, sorts, ctx=None)
Definition z3py.py:5482
_coerce_expr_list(alist, ctx=None)
Definition z3py.py:1277
is_select(a)
Definition z3py.py:5005
SignExt(n, a)
Definition z3py.py:4482
Int(name, ctx=None)
Definition z3py.py:3337
Bools(names, ctx=None)
Definition z3py.py:1817
_probe_and(args, ctx)
Definition z3py.py:8963
Int2BV(a, num_bits)
Definition z3py.py:4092
Lambda(vs, body)
Definition z3py.py:2348
_to_param_value(val)
Definition z3py.py:178
FreshFunction(*sig)
Definition z3py.py:926
RealVector(prefix, sz, ctx=None)
Definition z3py.py:3418
BVRedOr(a)
Definition z3py.py:4571
SRem(a, b)
Definition z3py.py:4397
SortRef _sort(Context ctx, Any a)
Definition z3py.py:709
set_option(*args, **kws)
Definition z3py.py:311
ExprRef RealVar(int idx, ctx=None)
Definition z3py.py:1534
bool is_sub(Any a)
Definition z3py.py:2903
is_bv(a)
Definition z3py.py:4040
SetDifference(a, b)
Definition z3py.py:5117
bool is_arith_sort(Any s)
Definition z3py.py:2451
BitVecs(names, bv, ctx=None)
Definition z3py.py:4157
bool is_mod(Any a)
Definition z3py.py:2944
BoolVector(prefix, sz, ctx=None)
Definition z3py.py:1833
_has_probe(args)
Definition z3py.py:1918
IsMember(e, s)
Definition z3py.py:5128
get_param(name)
Definition z3py.py:317
BVAddNoUnderflow(a, b)
Definition z3py.py:4585
deserialize(st)
Definition z3py.py:1166
bool is_not(Any a)
Definition z3py.py:1734
Extract(high, low, a)
Definition z3py.py:4224
Function(name, *sig)
Definition z3py.py:903
get_version()
Definition z3py.py:100
FreshConst(sort, prefix="c")
Definition z3py.py:1511
ULT(a, b)
Definition z3py.py:4301
EnumSort(name, values, ctx=None)
Definition z3py.py:5506
bool is_is_int(Any a)
Definition z3py.py:3004
_to_int_str(val)
Definition z3py.py:3265
is_algebraic_value(a)
Definition z3py.py:2865
is_bv_value(a)
Definition z3py.py:4054
BVSDivNoOverflow(a, b)
Definition z3py.py:4606
bool is_eq(Any a)
Definition z3py.py:1746
Context main_ctx()
Definition z3py.py:249
SetIntersect(*args)
Definition z3py.py:5072
simplify(a, *arguments, **keywords)
Utils.
Definition z3py.py:9030
BV2Int(a, is_signed=False)
Definition z3py.py:4069
FreshInt(prefix="x", ctx=None)
Definition z3py.py:3376
_to_ast_ref(a, ctx)
Definition z3py.py:553
_to_func_decl_array(args)
Definition z3py.py:529
disable_trace(msg)
Definition z3py.py:87
bool is_to_int(Any a)
Definition z3py.py:3031
is_map(a)
Definition z3py.py:4770
Context _get_ctx(ctx)
Definition z3py.py:270
Or(*args)
Definition z3py.py:1959
is_re(s)
Definition z3py.py:11484
args2params(arguments, keywords, ctx=None)
Definition z3py.py:5585
bool is_idiv(Any a)
Definition z3py.py:2932
Consts(names, sort)
Definition z3py.py:1496
Cond(p, t1, t2, ctx=None)
Definition z3py.py:9013
_to_pattern(arg)
Definition z3py.py:2052
RealVarVector(int n, ctx=None)
Definition z3py.py:1544
is_arith(a)
Definition z3py.py:2752
bool is_true(Any a)
Definition z3py.py:1666
bool is_false(Any a)
Definition z3py.py:1684
bool is_int(a)
Definition z3py.py:2773
If(a, b, c, ctx=None)
Definition z3py.py:1428
bool eq(AstRef a, AstRef b)
Definition z3py.py:486
is_app_of(a, k)
Definition z3py.py:1415
is_app(a)
Definition z3py.py:1312
bool is_add(Any a)
Definition z3py.py:2879
z3_error_handler(c, e)
Definition z3py.py:184
None reset_params()
Definition z3py.py:305
Reals(names, ctx=None)
Definition z3py.py:3403
is_int_value(a)
Definition z3py.py:2819
set_param(*args, **kws)
Definition z3py.py:281
is_pattern(a)
Definition z3py.py:2010
_coerce_seq(s, ctx=None)
Definition z3py.py:11145
bool is_distinct(Any a)
Definition z3py.py:1756
bool is_lt(Any a)
Definition z3py.py:2968
ULE(a, b)
Definition z3py.py:4283
is_real(a)
Definition z3py.py:2792
FullSet(s)
Definition z3py.py:5050
to_symbol(s, ctx=None)
Definition z3py.py:132
bool is_mul(Any a)
Definition z3py.py:2891
bool is_ast(Any a)
Definition z3py.py:465
_get_args(args)
Definition z3py.py:152
And(*args)
Definition z3py.py:1926
RepeatBitVec(n, a)
Definition z3py.py:4540
SetHasSize(a, k)
Definition z3py.py:4999
get_version_string()
Definition z3py.py:91
FreshReal(prefix="b", ctx=None)
Definition z3py.py:3433
Array(name, *sorts)
Definition z3py.py:4852
Concat(*args)
Definition z3py.py:4178
_reduce(func, sequence, initial)
Definition z3py.py:1270
_is_algebraic(ctx, a)
Definition z3py.py:2815
Ints(names, ctx=None)
Definition z3py.py:3350
Select(a, *args)
Definition z3py.py:4926
Const(name, sort)
Definition z3py.py:1484
is_array_sort(a)
Definition z3py.py:4726
bool is_div(Any a)
Definition z3py.py:2915
ExprRef Var(int idx, SortRef s)
Definition z3py.py:1519
BVAddNoOverflow(a, b, signed)
Definition z3py.py:4578
Real(name, ctx=None)
Definition z3py.py:3390
FreshBool(prefix="b", ctx=None)
Definition z3py.py:1848
BitVecSort(sz, ctx=None)
Definition z3py.py:4101
open_log(fname)
Definition z3py.py:122
RecFunction(name, *sig)
Definition z3py.py:949
bool is_ge(Any a)
Definition z3py.py:2980
Model(ctx=None, eval={})
Definition z3py.py:6832
BVSubNoOverflow(a, b)
Definition z3py.py:4592
bool is_gt(Any a)
Definition z3py.py:2992
is_default(a)
Definition z3py.py:4786
is_K(a)
Definition z3py.py:4757
Bool(name, ctx=None)
Definition z3py.py:1805
_is_int(v)
Definition z3py.py:76
is_const_array(a)
Definition z3py.py:4744
Sqrt(a, ctx=None)
Definition z3py.py:3502
Default(a)
Definition z3py.py:4898
_ctx_from_ast_arg_list(args, default_ctx=None)
Definition z3py.py:511
SetComplement(s)
Definition z3py.py:5107
is_as_array(n)
Definition z3py.py:6840
is_store(a)
Definition z3py.py:5018
bool is_or(Any a)
Definition z3py.py:1710
is_quantifier(a)
Definition z3py.py:2260
_mk_bin(f, a, b)
Definition z3py.py:1475
K(dom, v)
Definition z3py.py:4965
Xor(a, b, ctx=None)
Definition z3py.py:1876
Store(a, *args)
Definition z3py.py:4909
bool is_array(Any a)
Definition z3py.py:4730
mk_not(a)
Definition z3py.py:1911
is_expr(a)
Definition z3py.py:1289
_array_select(ar, arg)
Definition z3py.py:4717
is_const(a)
Definition z3py.py:1338
BoolVal(val, ctx=None)
Definition z3py.py:1786
RealVal(val, ctx=None)
Definition z3py.py:3289
bool is_implies(Any a)
Definition z3py.py:1722
z3_debug()
Definition z3py.py:70
get_full_version()
Definition z3py.py:109
IntVector(prefix, sz, ctx=None)
Definition z3py.py:3363
_coerce_expr_merge(s, a)
Definition z3py.py:1230
Context get_ctx(ctx)
Definition z3py.py:277
LShR(a, b)
Definition z3py.py:4418
ArraySort(*sig)
Definition z3py.py:4819
Map(f, *args)
Definition z3py.py:4942
is_rational_value(a)
Definition z3py.py:2843
_probe_or(args, ctx)
Definition z3py.py:8967
BVRedAnd(a)
Definition z3py.py:4564
Cbrt(a, ctx=None)
Definition z3py.py:3515
_to_expr_ref(a, ctx)
Definition z3py.py:1180
DisjointSum(name, sorts, ctx=None)
Definition z3py.py:5494
IntSort(ctx=None)
Definition z3py.py:3231
is_seq(a)
Definition z3py.py:11166
Not(a, ctx=None)
Definition z3py.py:1892
_to_sort_ref(s, ctx)
Definition z3py.py:678
enable_trace(msg)
Definition z3py.py:83
Exists(vs, body, weight=1, qid="", skid="", patterns=[], no_patterns=[])
Definition z3py.py:2327
ToReal(a)
Definition z3py.py:3447
URem(a, b)
Definition z3py.py:4376
bool is_bool(Any a)
Definition z3py.py:1648
StringVal(s, ctx=None)
Definition z3py.py:11193
IsInt(a)
Definition z3py.py:3485
_is_numeral(ctx, a)
Definition z3py.py:2811
MultiPattern(*args)
Definition z3py.py:2028
ForAll(vs, body, weight=1, qid="", skid="", patterns=[], no_patterns=[])
Definition z3py.py:2309
ZeroExt(n, a)
Definition z3py.py:4512
_sort_kind(ctx, s)
Sorts.
Definition z3py.py:569
int _ast_kind(Context ctx, Any a)
Definition z3py.py:505
BVSubNoUnderflow(a, b, signed)
Definition z3py.py:4599
UDiv(a, b)
Definition z3py.py:4355
DatatypeSort(name, ctx=None)
Definition z3py.py:5477
Q(a, b, ctx=None)
Definition z3py.py:3324
Update(a, *args)
Definition z3py.py:4866
get_var_index(a)
Definition z3py.py:1382
append_log(s)
Definition z3py.py:127
is_var(a)
Definition z3py.py:1357
SortRef DeclareSort(name, ctx=None)
Definition z3py.py:713
IntVal(val, ctx=None)
Definition z3py.py:3277
BVMulNoOverflow(a, b, signed)
Definition z3py.py:4620
RotateLeft(a, b)
Definition z3py.py:4450
_mk_quantifier(is_forall, vs, body, weight=1, qid="", skid="", patterns=[], no_patterns=[])
Definition z3py.py:2274
_z3_assert(cond, msg)
Definition z3py.py:113