3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-04-10 03:07:07 +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:
Nikolaj Bjorner 2022-04-19 07:21:22 +02:00
parent 09b0c4bc9d
commit 9b66d8600b

View file

@ -1101,6 +1101,28 @@ class ExprRef(AstRef):
else:
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):
if isinstance(a, Pattern):