forked from pysmt/pysmt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fxp2smt.py
67 lines (54 loc) · 1.91 KB
/
fxp2smt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import sys
from pysmt.smtlib.parser import SmtLibParser
from six.moves import cStringIO # Py2-Py3 Compatibility
from pysmt.shortcuts import get_model
import time
BV = True
if BV:
from pysmt.rewritings import get_fp_bv_converter
else:
from pysmt.rewritings import get_fp_real_converter
with open(sys.argv[1], 'r') as f:
QUERY=f.read()
# We read the SMT-LIB Script by creating a Parser.
# From here we can get the SMT-LIB script.
parser = SmtLibParser()
# The method SmtLibParser.get_script takes a buffer in input. We use
# cStringIO to simulate an open file.
# See SmtLibParser.get_script_fname() if to pass the path of a file.
script = parser.get_script(cStringIO(QUERY))
# The SmtLibScript provides an iterable representation of the commands
# that are present in the SMT-LIB file.
#
# Printing a summary of the issued commands
f = script.get_last_formula()
if BV:
conv = get_fp_bv_converter()
else:
conv = get_fp_real_converter()
F = conv.convert(f)
if BV:
print("(set-logic QF_UFBV)")
else:
print("(set-logic QF_NIRA)")
sm = conv.symbol_map
inv_sm = {v:k for k, v in sm.items()}
for old_n, new_n in conv.symbol_map.items():
print("(declare-fun {} {})".format(old_n.to_smtlib(), new_n._content.payload[1].as_smtlib(funstyle=True)))
for x in F.get_free_variables():
if x in inv_sm.keys():
old_n = inv_sm[x]
print("(define-fun {} {} {})".format(x.to_smtlib(), x.get_type().as_smtlib(), old_n.to_smtlib()))
else:
print("(declare-fun {} {})".format(x.to_smtlib(), x._content.payload[1].as_smtlib(funstyle=True)))
print("(assert {})".format(F.to_smtlib()))
print("(check-sat)")
# start = time.clock()
# model = get_model(F, solver_name='cvc4', logic='QF_NIRA')
# end = time.clock()
# with open(sys.argv[1]+'-out', 'w') as out:
# out.write("TIME: " + str(end - start) + '\n')
# if model:
# out.write(str(model) + '\n')
# else:
# out.write("UNSAT\n")