Skip to content

Commit

Permalink
add shortcut to serialize/deserialize based on question at FV hangout
Browse files Browse the repository at this point in the history
use case
```
from z3 import *

x, y = Ints('x y')
s = (x + y).serialize()

y = deserialize(s)

print(y)
```
  • Loading branch information
NikolajBjorner committed Apr 19, 2022
1 parent 09b0c4b commit 9b66d86
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/api/python/z3/z3.py
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,28 @@ def children(self):
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):
Expand Down

0 comments on commit 9b66d86

Please sign in to comment.