我在用core.logic玩弄周圍,並嘗試翻譯一些Prolog代碼並運行到insert
事實的無盡的遞歸(摘自RAO'Keefe的「Prolog of Prolog 「):將插入的事實從Prolog轉換爲core.logic
insert(L, X, [X|L]).
insert([H|T], X, [H|L]) :-
insert(T,X,L).
這是我想出迄今(請注意,前兩個參數是交換與conso
參數列表匹配):
(defn insert [x l nl]
(conde
[(conso x l nl)]
[(fresh [h t]
(conso h t l)
(insert x t l)
(conso h l nl))]))
我的問題是從這些midje測試的最後兩個事實將永遠不會r E打開。 第一個工作正常,因爲這隻需要第一個conso
條款。
(fact "Inserting works"
(run* [q] (insert 1 [2] q)) => '((1 2)))
(run* [q] (insert 1 [2 3] q)) => '((1 2 3)))
(fact "We can retrieve the inserted data"
(run* [q] (insert q [2] '(1 2))) => '(1))
(fact "We can retrieve the list data, too"
(run* [q] (insert 1 q '(1 2))) => '((2))))
我想我忽略了一些明顯的東西,但什麼?
編輯:事實並不能正確反映Prolog代碼的行爲。正確的做法是這樣的:
?- insert([2,3], 1, Q).
Q = [1, 2, 3] ;
Q = [2, 1, 3] ;
Q = [2, 3, 1].
因此,第二可檢查的實際上應該是
(fact "Inserting works"
(run* [q] (insert 1 [2 3] q)) => '((1 2 3) (2 1 3) (2 3 1)))
使用'NL = [H | R]'。在那裏不能使用'L'而不是'R','L'已經被用作參數。 –