[英]Converting `appendo` relation from smt2 to python
最近我在學習 SMT 求解器。 雖然 SMT 求解器對我來說是一個新概念,但它讓我想起了邏輯編程,例如 Prolog 和 minikanren。 所以我在 SMT 求解器中嘗試了一個經典的邏輯編程示例。
例子是appendo
關系,我們可以向后執行它。 即給定一個輸出列表,返回所有可能的兩個輸入,當連接這兩個輸入列表時返回輸出列表。
以下是appendo
關系,我在z3/smt2 求解器中實現:
(define-fun-rec appendo ((l (List Int)) (s (List Int))) (List Int)
(ite (= nil l)
s
(insert (head l) (appendo (tail l) s))
))
(declare-const a (List Int))
(declare-const b (List Int))
(assert (= (appendo a b) (insert 1 (insert 2 nil))))
(check-sat)
(get-model)
(echo "solution 1:")
(eval a)
(eval b)
;; nil
;; (insert 1 (insert 2 nil))
(assert (not (= a nil)))
(assert (not (= b (insert 1 (insert 2 nil)))))
(check-sat)
(get-model)
(echo "solution 2:")
(eval a)
(eval b)
;; (insert 1 nil)
;; (insert 2 nil)
(assert (not (= a (insert 1 nil))))
(assert (not (= b (insert 2 nil))))
(check-sat)
(get-model)
(echo "solution 3:")
(eval a)
(eval b)
;; (insert 1 (insert 2 nil))
;; nil
(assert (not (= a (insert 1 (insert 2 nil)))))
(assert (not (= b nil)))
(check-sat)
;; unsat
雖然它有效,但這種實現的缺點是它不能自動獲得所有令人滿意的模型。
根據這個問題,在純smt2(?)中自動獲得所有令人滿意的模型似乎是不可能的。 我們必須使用一些 API 綁定。
我嘗試了 z3 python API 幾個小時,但失敗了。
有人可以幫我將上面的 smt2 代碼轉換為 z3py 嗎? (z3py 的文檔很簡短,讀起來很困難,尤其是關於如何定義遞歸函數,見諒...)
很感謝。
以下是我未完成的代碼:
from z3 import *
## Define List
def DeclareList(sort):
List = Datatype('List_of_%s' % sort.name())
List.declare('cons', ('car', sort), ('cdr', List))
List.declare('nil')
return List.create()
IntList = DeclareList(IntSort())
## Define Rec Function
appendo = RecFunction('appendo', IntList, IntList, IntList)
RecAddDefinition(appendo, [l, s], If(IntList.nil == l, s, IntList.cons(IntList.car(l), appendo(IntList.cdr(l), s)))) ## <== NameError: name 'l' is not defined
a = Const('a', IntList)
b = Const('b', IntList)
## ...
事實上,在 SMTLib 中獲取所有模型是不可能的,因為 SMTLib 語言不允許任何控制結構。 來自 Python、C、Java、Haskell 等的高級 API 更適合於此。
以下是您在 Python 中編寫問題的方法:
from z3 import *
## Define List
def DeclareList(sort):
List = Datatype('List_of_%s' % sort.name())
List.declare('cons', ('car', sort), ('cdr', List))
List.declare('nil')
return List.create()
IntList = DeclareList(IntSort())
## Define Rec Function
appendo = RecFunction('appendo', IntList, IntList, IntList)
l = FreshConst(IntList)
s = FreshConst(IntList)
RecAddDefinition( appendo
, [l, s]
, If(IntList.nil == l,
s,
IntList.cons(IntList.car(l), appendo(IntList.cdr(l), s)))
)
a = Const('a', IntList)
b = Const('b', IntList)
solver = Solver()
solver.add(appendo(a, b) == IntList.cons(1, IntList.cons(0, IntList.nil)))
while solver.check() == sat:
m = solver.model()
v_a = m.eval(a, model_completion=True)
v_b = m.eval(b, model_completion=True)
print("Solution:")
print(" a = " + str(v_a))
print(" b = " + str(v_b))
block = Or(a != v_a, b != v_b)
solver.add(block)
當我運行這個時,我得到:
Solution:
a = nil
b = cons(1, cons(0, nil))
Solution:
a = cons(1, nil)
b = cons(0, nil)
Solution:
a = cons(1, cons(0, nil))
b = nil
這就是我相信你在尋找什么。 請注意使用FreshConst
來避免您遇到的錯誤,而while
循環確保我們迭代所有可能的解決方案。
請注意,雖然 z3 支持遞歸函數,但它相當挑剔; 如果你有復雜的約束,你最終可能會得到unknown
答案,或者你可能會得到很長的執行時間甚至無限的電子匹配循環。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.