smtlib.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. import typing
  2. import sympy
  3. from sympy.core import Add, Mul
  4. from sympy.core import Symbol, Expr, Float, Rational, Integer, Basic
  5. from sympy.core.function import UndefinedFunction, Function
  6. from sympy.core.relational import Relational, Unequality, Equality, LessThan, GreaterThan, StrictLessThan, StrictGreaterThan
  7. from sympy.functions.elementary.complexes import Abs
  8. from sympy.functions.elementary.exponential import exp, log, Pow
  9. from sympy.functions.elementary.hyperbolic import sinh, cosh, tanh
  10. from sympy.functions.elementary.miscellaneous import Min, Max
  11. from sympy.functions.elementary.piecewise import Piecewise
  12. from sympy.functions.elementary.trigonometric import sin, cos, tan, asin, acos, atan, atan2
  13. from sympy.logic.boolalg import And, Or, Xor, Implies, Boolean
  14. from sympy.logic.boolalg import BooleanTrue, BooleanFalse, BooleanFunction, Not, ITE
  15. from sympy.printing.printer import Printer
  16. from sympy.sets import Interval
  17. class SMTLibPrinter(Printer):
  18. printmethod = "_smtlib"
  19. # based on dReal, an automated reasoning tool for solving problems that can be encoded as first-order logic formulas over the real numbers.
  20. # dReal's special strength is in handling problems that involve a wide range of nonlinear real functions.
  21. _default_settings: dict = {
  22. 'precision': None,
  23. 'known_types': {
  24. bool: 'Bool',
  25. int: 'Int',
  26. float: 'Real'
  27. },
  28. 'known_constants': {
  29. # pi: 'MY_VARIABLE_PI_DECLARED_ELSEWHERE',
  30. },
  31. 'known_functions': {
  32. Add: '+',
  33. Mul: '*',
  34. Equality: '=',
  35. LessThan: '<=',
  36. GreaterThan: '>=',
  37. StrictLessThan: '<',
  38. StrictGreaterThan: '>',
  39. exp: 'exp',
  40. log: 'log',
  41. Abs: 'abs',
  42. sin: 'sin',
  43. cos: 'cos',
  44. tan: 'tan',
  45. asin: 'arcsin',
  46. acos: 'arccos',
  47. atan: 'arctan',
  48. atan2: 'arctan2',
  49. sinh: 'sinh',
  50. cosh: 'cosh',
  51. tanh: 'tanh',
  52. Min: 'min',
  53. Max: 'max',
  54. Pow: 'pow',
  55. And: 'and',
  56. Or: 'or',
  57. Xor: 'xor',
  58. Not: 'not',
  59. ITE: 'ite',
  60. Implies: '=>',
  61. }
  62. }
  63. symbol_table: dict
  64. def __init__(self, settings: typing.Optional[dict] = None,
  65. symbol_table=None):
  66. settings = settings or {}
  67. self.symbol_table = symbol_table or {}
  68. Printer.__init__(self, settings)
  69. self._precision = self._settings['precision']
  70. self._known_types = dict(self._settings['known_types'])
  71. self._known_constants = dict(self._settings['known_constants'])
  72. self._known_functions = dict(self._settings['known_functions'])
  73. for _ in self._known_types.values(): assert self._is_legal_name(_)
  74. for _ in self._known_constants.values(): assert self._is_legal_name(_)
  75. # for _ in self._known_functions.values(): assert self._is_legal_name(_) # +, *, <, >, etc.
  76. def _is_legal_name(self, s: str):
  77. if not s: return False
  78. if s[0].isnumeric(): return False
  79. return all(_.isalnum() or _ == '_' for _ in s)
  80. def _s_expr(self, op: str, args: typing.Union[list, tuple]) -> str:
  81. args_str = ' '.join(
  82. a if isinstance(a, str)
  83. else self._print(a)
  84. for a in args
  85. )
  86. return f'({op} {args_str})'
  87. def _print_Function(self, e):
  88. if e in self._known_functions:
  89. op = self._known_functions[e]
  90. elif type(e) in self._known_functions:
  91. op = self._known_functions[type(e)]
  92. elif type(type(e)) == UndefinedFunction:
  93. op = e.name
  94. else:
  95. op = self._known_functions[e] # throw KeyError
  96. return self._s_expr(op, e.args)
  97. def _print_Relational(self, e: Relational):
  98. return self._print_Function(e)
  99. def _print_BooleanFunction(self, e: BooleanFunction):
  100. return self._print_Function(e)
  101. def _print_Expr(self, e: Expr):
  102. return self._print_Function(e)
  103. def _print_Unequality(self, e: Unequality):
  104. if type(e) in self._known_functions:
  105. return self._print_Relational(e) # default
  106. else:
  107. eq_op = self._known_functions[Equality]
  108. not_op = self._known_functions[Not]
  109. return self._s_expr(not_op, [self._s_expr(eq_op, e.args)])
  110. def _print_Piecewise(self, e: Piecewise):
  111. def _print_Piecewise_recursive(args: typing.Union[list, tuple]):
  112. e, c = args[0]
  113. if len(args) == 1:
  114. assert (c is True) or isinstance(c, BooleanTrue)
  115. return self._print(e)
  116. else:
  117. ite = self._known_functions[ITE]
  118. return self._s_expr(ite, [
  119. c, e, _print_Piecewise_recursive(args[1:])
  120. ])
  121. return _print_Piecewise_recursive(e.args)
  122. def _print_Interval(self, e: Interval):
  123. if e.start.is_infinite and e.end.is_infinite:
  124. return ''
  125. elif e.start.is_infinite != e.end.is_infinite:
  126. raise ValueError(f'One-sided intervals (`{e}`) are not supported in SMT.')
  127. else:
  128. return f'[{e.start}, {e.end}]'
  129. # todo: Sympy does not support quantifiers yet as of 2022, but quantifiers can be handy in SMT.
  130. # For now, users can extend this class and build in their own quantifier support.
  131. # See `test_quantifier_extensions()` in test_smtlib.py for an example of how this might look.
  132. # def _print_ForAll(self, e: ForAll):
  133. # return self._s('forall', [
  134. # self._s('', [
  135. # self._s(sym.name, [self._type_name(sym), Interval(start, end)])
  136. # for sym, start, end in e.limits
  137. # ]),
  138. # e.function
  139. # ])
  140. def _print_BooleanTrue(self, x: BooleanTrue):
  141. return 'true'
  142. def _print_BooleanFalse(self, x: BooleanFalse):
  143. return 'false'
  144. def _print_Float(self, x: Float):
  145. f = x.evalf(self._precision) if self._precision else x.evalf()
  146. return str(f).rstrip('0')
  147. def _print_float(self, x: float):
  148. return str(x)
  149. def _print_Rational(self, x: Rational):
  150. return self._s_expr('/', [x.p, x.q])
  151. def _print_Integer(self, x: Integer):
  152. assert x.q == 1
  153. return str(x.p)
  154. def _print_int(self, x: int):
  155. return str(x)
  156. def _print_Symbol(self, x: Symbol):
  157. assert self._is_legal_name(x.name)
  158. return x.name
  159. def _print_NumberSymbol(self, x):
  160. name = self._known_constants.get(x)
  161. return name if name else self._print_Float(x)
  162. def _print_UndefinedFunction(self, x):
  163. assert self._is_legal_name(x.name)
  164. return x.name
  165. def _print_Exp1(self, x):
  166. return (
  167. self._print_Function(exp(1, evaluate=False))
  168. if exp in self._known_functions else
  169. self._print_NumberSymbol(x)
  170. )
  171. def emptyPrinter(self, expr):
  172. raise NotImplementedError(f'Cannot convert `{repr(expr)}` of type `{type(expr)}` to SMT.')
  173. def smtlib_code(
  174. expr,
  175. auto_assert=True, auto_declare=True,
  176. precision=None,
  177. symbol_table=None,
  178. known_types=None, known_constants=None, known_functions=None,
  179. prefix_expressions=None, suffix_expressions=None,
  180. log_warn=None
  181. ):
  182. r"""Converts ``expr`` to a string of smtlib code.
  183. Parameters
  184. ==========
  185. expr : Expr | List[Expr]
  186. A SymPy expression or system to be converted.
  187. auto_assert : bool, optional
  188. If false, do not modify expr and produce only the S-Expression equivalent of expr.
  189. If true, assume expr is a system and assert each boolean element.
  190. auto_declare : bool, optional
  191. If false, do not produce declarations for the symbols used in expr.
  192. If true, prepend all necessary declarations for variables used in expr based on symbol_table.
  193. precision : integer, optional
  194. The ``evalf(..)`` precision for numbers such as pi.
  195. symbol_table : dict, optional
  196. A dictionary where keys are ``Symbol`` or ``Function`` instances and values are their Python type i.e. ``bool``, ``int``, ``float``, or ``Callable[...]``.
  197. If incomplete, an attempt will be made to infer types from ``expr``.
  198. known_types: dict, optional
  199. A dictionary where keys are ``bool``, ``int``, ``float`` etc. and values are their corresponding SMT type names.
  200. If not given, a partial listing compatible with several solvers will be used.
  201. known_functions : dict, optional
  202. A dictionary where keys are ``Function``, ``Relational``, ``BooleanFunction``, or ``Expr`` instances and values are their SMT string representations.
  203. If not given, a partial listing optimized for dReal solver (but compatible with others) will be used.
  204. known_constants: dict, optional
  205. A dictionary where keys are ``NumberSymbol`` instances and values are their SMT variable names.
  206. When using this feature, extra caution must be taken to avoid naming collisions between user symbols and listed constants.
  207. If not given, constants will be expanded inline i.e. ``3.14159`` instead of ``MY_SMT_VARIABLE_FOR_PI``.
  208. prefix_expressions: list, optional
  209. A list of lists of ``str`` and/or expressions to convert into SMTLib and prefix to the output.
  210. suffix_expressions: list, optional
  211. A list of lists of ``str`` and/or expressions to convert into SMTLib and postfix to the output.
  212. log_warn: lambda function, optional
  213. A function to record all warnings during potentially risky operations.
  214. Soundness is a core value in SMT solving, so it is good to log all assumptions made.
  215. Examples
  216. ========
  217. >>> from sympy import smtlib_code, symbols, sin, Eq
  218. >>> x = symbols('x')
  219. >>> smtlib_code(sin(x).series(x).removeO(), log_warn=print)
  220. Could not infer type of `x`. Defaulting to float.
  221. Non-Boolean expression `x**5/120 - x**3/6 + x` will not be asserted. Converting to SMTLib verbatim.
  222. '(declare-const x Real)\n(+ x (* (/ -1 6) (pow x 3)) (* (/ 1 120) (pow x 5)))'
  223. >>> from sympy import Rational
  224. >>> x, y, tau = symbols("x, y, tau")
  225. >>> smtlib_code((2*tau)**Rational(7, 2), log_warn=print)
  226. Could not infer type of `tau`. Defaulting to float.
  227. Non-Boolean expression `8*sqrt(2)*tau**(7/2)` will not be asserted. Converting to SMTLib verbatim.
  228. '(declare-const tau Real)\n(* 8 (pow 2 (/ 1 2)) (pow tau (/ 7 2)))'
  229. ``Piecewise`` expressions are implemented with ``ite`` expressions by default.
  230. Note that if the ``Piecewise`` lacks a default term, represented by
  231. ``(expr, True)`` then an error will be thrown. This is to prevent
  232. generating an expression that may not evaluate to anything.
  233. >>> from sympy import Piecewise
  234. >>> pw = Piecewise((x + 1, x > 0), (x, True))
  235. >>> smtlib_code(Eq(pw, 3), symbol_table={x: float}, log_warn=print)
  236. '(declare-const x Real)\n(assert (= (ite (> x 0) (+ 1 x) x) 3))'
  237. Custom printing can be defined for certain types by passing a dictionary of
  238. PythonType : "SMT Name" to the ``known_types``, ``known_constants``, and ``known_functions`` kwargs.
  239. >>> from typing import Callable
  240. >>> from sympy import Function, Add
  241. >>> f = Function('f')
  242. >>> g = Function('g')
  243. >>> smt_builtin_funcs = { # functions our SMT solver will understand
  244. ... f: "existing_smtlib_fcn",
  245. ... Add: "sum",
  246. ... }
  247. >>> user_def_funcs = { # functions defined by the user must have their types specified explicitly
  248. ... g: Callable[[int], float],
  249. ... }
  250. >>> smtlib_code(f(x) + g(x), symbol_table=user_def_funcs, known_functions=smt_builtin_funcs, log_warn=print)
  251. Non-Boolean expression `f(x) + g(x)` will not be asserted. Converting to SMTLib verbatim.
  252. '(declare-const x Int)\n(declare-fun g (Int) Real)\n(sum (existing_smtlib_fcn x) (g x))'
  253. """
  254. log_warn = log_warn or (lambda _: None)
  255. if not isinstance(expr, list): expr = [expr]
  256. expr = [
  257. sympy.sympify(_, strict=True, evaluate=False, convert_xor=False)
  258. for _ in expr
  259. ]
  260. if not symbol_table: symbol_table = {}
  261. symbol_table = _auto_infer_smtlib_types(
  262. *expr, symbol_table=symbol_table
  263. )
  264. # See [FALLBACK RULES]
  265. # Need SMTLibPrinter to populate known_functions and known_constants first.
  266. settings = {}
  267. if precision: settings['precision'] = precision
  268. del precision
  269. if known_types: settings['known_types'] = known_types
  270. del known_types
  271. if known_functions: settings['known_functions'] = known_functions
  272. del known_functions
  273. if known_constants: settings['known_constants'] = known_constants
  274. del known_constants
  275. if not prefix_expressions: prefix_expressions = []
  276. if not suffix_expressions: suffix_expressions = []
  277. p = SMTLibPrinter(settings, symbol_table)
  278. del symbol_table
  279. # [FALLBACK RULES]
  280. for e in expr:
  281. for sym in e.atoms(Symbol, Function):
  282. if (
  283. sym.is_Symbol and
  284. sym not in p._known_constants and
  285. sym not in p.symbol_table
  286. ):
  287. log_warn(f"Could not infer type of `{sym}`. Defaulting to float.")
  288. p.symbol_table[sym] = float
  289. if (
  290. sym.is_Function and
  291. type(sym) not in p._known_functions and
  292. type(sym) not in p.symbol_table and
  293. not sym.is_Piecewise
  294. ): raise TypeError(
  295. f"Unknown type of undefined function `{sym}`. "
  296. f"Must be mapped to ``str`` in known_functions or mapped to ``Callable[..]`` in symbol_table."
  297. )
  298. declarations = []
  299. if auto_declare:
  300. constants = {sym.name: sym for e in expr for sym in e.free_symbols
  301. if sym not in p._known_constants}
  302. functions = {fnc.name: fnc for e in expr for fnc in e.atoms(Function)
  303. if type(fnc) not in p._known_functions and not fnc.is_Piecewise}
  304. declarations = \
  305. [
  306. _auto_declare_smtlib(sym, p, log_warn)
  307. for sym in constants.values()
  308. ] + [
  309. _auto_declare_smtlib(fnc, p, log_warn)
  310. for fnc in functions.values()
  311. ]
  312. declarations = [decl for decl in declarations if decl]
  313. if auto_assert:
  314. expr = [_auto_assert_smtlib(e, p, log_warn) for e in expr]
  315. # return SMTLibPrinter().doprint(expr)
  316. return '\n'.join([
  317. # ';; PREFIX EXPRESSIONS',
  318. *[
  319. e if isinstance(e, str) else p.doprint(e)
  320. for e in prefix_expressions
  321. ],
  322. # ';; DECLARATIONS',
  323. *sorted(e for e in declarations),
  324. # ';; EXPRESSIONS',
  325. *[
  326. e if isinstance(e, str) else p.doprint(e)
  327. for e in expr
  328. ],
  329. # ';; SUFFIX EXPRESSIONS',
  330. *[
  331. e if isinstance(e, str) else p.doprint(e)
  332. for e in suffix_expressions
  333. ],
  334. ])
  335. def _auto_declare_smtlib(sym: typing.Union[Symbol, Function], p: SMTLibPrinter, log_warn: typing.Callable[[str], None]):
  336. if sym.is_Symbol:
  337. type_signature = p.symbol_table[sym]
  338. assert isinstance(type_signature, type)
  339. type_signature = p._known_types[type_signature]
  340. return p._s_expr('declare-const', [sym, type_signature])
  341. elif sym.is_Function:
  342. type_signature = p.symbol_table[type(sym)]
  343. assert callable(type_signature)
  344. type_signature = [p._known_types[_] for _ in type_signature.__args__]
  345. assert len(type_signature) > 0
  346. params_signature = f"({' '.join(type_signature[:-1])})"
  347. return_signature = type_signature[-1]
  348. return p._s_expr('declare-fun', [type(sym), params_signature, return_signature])
  349. else:
  350. log_warn(f"Non-Symbol/Function `{sym}` will not be declared.")
  351. return None
  352. def _auto_assert_smtlib(e: Expr, p: SMTLibPrinter, log_warn: typing.Callable[[str], None]):
  353. if isinstance(e, Boolean) or (
  354. e in p.symbol_table and p.symbol_table[e] == bool
  355. ) or (
  356. e.is_Function and
  357. type(e) in p.symbol_table and
  358. p.symbol_table[type(e)].__args__[-1] == bool
  359. ):
  360. return p._s_expr('assert', [e])
  361. else:
  362. log_warn(f"Non-Boolean expression `{e}` will not be asserted. Converting to SMTLib verbatim.")
  363. return e
  364. def _auto_infer_smtlib_types(
  365. *exprs: Basic,
  366. symbol_table: typing.Optional[dict] = None
  367. ) -> dict:
  368. # [TYPE INFERENCE RULES]
  369. # X is alone in an expr => X is bool
  370. # X in BooleanFunction.args => X is bool
  371. # X matches to a bool param of a symbol_table function => X is bool
  372. # X matches to an int param of a symbol_table function => X is int
  373. # X.is_integer => X is int
  374. # X == Y, where X is T => Y is T
  375. # [FALLBACK RULES]
  376. # see _auto_declare_smtlib(..)
  377. # X is not bool and X is not int and X is Symbol => X is float
  378. # else (e.g. X is Function) => error. must be specified explicitly.
  379. _symbols = dict(symbol_table) if symbol_table else {}
  380. def safe_update(syms: set, inf):
  381. for s in syms:
  382. assert s.is_Symbol
  383. if (old_type := _symbols.setdefault(s, inf)) != inf:
  384. raise TypeError(f"Could not infer type of `{s}`. Apparently both `{old_type}` and `{inf}`?")
  385. # EXPLICIT TYPES
  386. safe_update({
  387. e
  388. for e in exprs
  389. if e.is_Symbol
  390. }, bool)
  391. safe_update({
  392. symbol
  393. for e in exprs
  394. for boolfunc in e.atoms(BooleanFunction)
  395. for symbol in boolfunc.args
  396. if symbol.is_Symbol
  397. }, bool)
  398. safe_update({
  399. symbol
  400. for e in exprs
  401. for boolfunc in e.atoms(Function)
  402. if type(boolfunc) in _symbols
  403. for symbol, param in zip(boolfunc.args, _symbols[type(boolfunc)].__args__)
  404. if symbol.is_Symbol and param == bool
  405. }, bool)
  406. safe_update({
  407. symbol
  408. for e in exprs
  409. for intfunc in e.atoms(Function)
  410. if type(intfunc) in _symbols
  411. for symbol, param in zip(intfunc.args, _symbols[type(intfunc)].__args__)
  412. if symbol.is_Symbol and param == int
  413. }, int)
  414. safe_update({
  415. symbol
  416. for e in exprs
  417. for symbol in e.atoms(Symbol)
  418. if symbol.is_integer
  419. }, int)
  420. safe_update({
  421. symbol
  422. for e in exprs
  423. for symbol in e.atoms(Symbol)
  424. if symbol.is_real and not symbol.is_integer
  425. }, float)
  426. # EQUALITY RELATION RULE
  427. rels = [rel for expr in exprs for rel in expr.atoms(Equality)]
  428. rels = [
  429. (rel.lhs, rel.rhs) for rel in rels if rel.lhs.is_Symbol
  430. ] + [
  431. (rel.rhs, rel.lhs) for rel in rels if rel.rhs.is_Symbol
  432. ]
  433. for infer, reltd in rels:
  434. inference = (
  435. _symbols[infer] if infer in _symbols else
  436. _symbols[reltd] if reltd in _symbols else
  437. _symbols[type(reltd)].__args__[-1]
  438. if reltd.is_Function and type(reltd) in _symbols else
  439. bool if reltd.is_Boolean else
  440. int if reltd.is_integer or reltd.is_Integer else
  441. float if reltd.is_real else
  442. None
  443. )
  444. if inference: safe_update({infer}, inference)
  445. return _symbols