smtlib.py 21 KB

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