使用 ATS 求解所有有效输入
假设您有一个纯表达式系统,例如,
(bi0, bi1, bi2, ai0, ai1, ai2) := inputs
b0 := bi0 && bi1
a1 := b0 ? ai0 : cbrt(ai0)
a2 := bi2 ? a1 : ai1
output := a2 > ai2
# prove:
output == True
可以对自动定理证明器进行编程吗?不仅要查找 output
为 true 的一些 input
,还要查找所有 output
可能的输入
对于哪些情况是正确的?
Let's say you have a system of pure expressions, like,
(bi0, bi1, bi2, ai0, ai1, ai2) := inputs
b0 := bi0 && bi1
a1 := b0 ? ai0 : cbrt(ai0)
a2 := bi2 ? a1 : ai1
output := a2 > ai2
# prove:
output == True
Can an automated theorem prover be programmed, not just to find some inputs
for which output
is true, but to find all possible inputs
for which it is true?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SMT 求解器非常擅长解决此类约束,并且有很多可供选择。请参阅 https://smtlib.cs.uiowa.edu 了解概述。
一种实现是 Microsoft 的 z3,它可以轻松处理您提出的查询: https://github.com/Z3Prover /z3
SMT 求解器可以使用所谓的 SMTLib 语法进行编程(请参阅上面的第一个链接),或者它们提供多种语言的 API 供您进行编程;包括 C/C++/Java/Python/Haskell 等。此外,现在大多数定理证明器(包括 Isabelle/HOL)都采用了在幕后使用这些证明器的策略;如此多的集成是可能的。
正如我提到的,z3 可以用多种语言进行编程,值得回顾的一篇很好的论文是 https:/ /theory.stanford.edu/~nikolaj/programmingz3.html,它使用 Python 来执行此操作。
在 Haskell 中使用 Z3 的解决方案
对于您的特定问题,以下是如何在 Haskell 中使用 z3 解决它,使用 SBV 层(http://leventerkok.github.io/sbv/):
当我运行这个时,我得到:
我中断了输出,因为似乎有很多作业(通过查看问题的结构可能是无限的)。
在 Python 中使用 Z3 的解决方案
另一种流行的选择是使用 Python 进行编程。在这种情况下,代码比 Haskell 更冗长一些,有一些样板来获取所有解决方案和其他 Python 陷阱,但在其他方面遵循类似的路径:
再次,当运行时不断地吐出解决方案。
SMT solvers are quite good at solving these sorts of constraints and there are many to choose from. See https://smtlib.cs.uiowa.edu for an overview.
One implementation is Microsoft's z3, which can easily handle queries like the one you posed: https://github.com/Z3Prover/z3
SMT solvers can be programmed in the so called SMTLib syntax (see first link above), or they provide APIs in many languages for you to program them in; including C/C++/Java/Python/Haskell etc. Furthermore, most theorem provers these days (including Isabelle/HOL) come with tactics that use these provers behind the scenes; so many integrations are possible.
As I mentioned, z3 can be programmed in various languages, a good paper to review is https://theory.stanford.edu/~nikolaj/programmingz3.html, which uses Python to do so.
A Solution using Z3 in Haskell
For your particular problem, here's how it'd be solved using z3 in Haskell, using the SBV layer (http://leventerkok.github.io/sbv/):
When I run this, I get:
I interrupted the output since it seems there's a lot of assignments (possibly infinite by looking at the structure of your problem).
A solution using Z3 in Python
Another popular choice is to use Python to program the same. The code is a bit more verbose than Haskell in this case, there's some boiler-plate to get all-solutions and other Python gotchas, but otherwise follow a similar path:
Again, when run this continuously spits out solutions.