mirror of
https://github.com/Z3Prover/z3
synced 2025-06-12 00:53:25 +00:00
add shortcut to serialize/deserialize based on question at FV hangout
use case ``` from z3 import * x, y = Ints('x y') s = (x + y).serialize() y = deserialize(s) print(y) ```
This commit is contained in:
parent
09b0c4bc9d
commit
9b66d8600b
1 changed files with 22 additions and 0 deletions
|
@ -1101,6 +1101,28 @@ class ExprRef(AstRef):
|
||||||
else:
|
else:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
def from_string(self, s):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def serialize(self):
|
||||||
|
s = Solver()
|
||||||
|
f = Function('F', self.sort(), BoolSort(self.ctx))
|
||||||
|
s.add(f(self))
|
||||||
|
return s.sexpr()
|
||||||
|
|
||||||
|
def deserialize(st):
|
||||||
|
"""inverse function to the serialize method on ExprRef.
|
||||||
|
It is made available to make it easier for users to serialize expressions back and forth between
|
||||||
|
strings. Solvers can be serialized using the 'sexpr()' method.
|
||||||
|
"""
|
||||||
|
s = Solver()
|
||||||
|
s.from_string(st)
|
||||||
|
if len(s.assertions()) != 1:
|
||||||
|
raise Z3Exception("single assertion expected")
|
||||||
|
fml = s.assertions()[0]
|
||||||
|
if fml.num_args() != 1:
|
||||||
|
raise Z3Exception("dummy function 'F' expected")
|
||||||
|
return fml.arg(0)
|
||||||
|
|
||||||
def _to_expr_ref(a, ctx):
|
def _to_expr_ref(a, ctx):
|
||||||
if isinstance(a, Pattern):
|
if isinstance(a, Pattern):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue