gpt4 book ai didi

clojure - 从 defrel 和事实到 core.logic.pldb

转载 作者:行者123 更新时间:2023-12-05 01:37:43 30 4
gpt4 key购买 nike

在弃用 defrel 事实 以及迁移到 pldb 方面,希望如何更改此代码?

这是代码:

(defrel parent x y)
(facts parent ’[[dave kaylen]
[frank dave]])

(defn grandparent
[x y]
(fresh [z]
(parent x z)
(parent z y)))

;; In the REPL
user> (run* [q]
(fresh [x y]
(grandparent x y)
(== q [x y])))
;; Result
([frank kaylen])

最佳答案

(ns your.ns.here
(:require [clojure.core.logic.pldb :as pldb]
[clojure.core.logic :refer :all]))

(pldb/db-rel parent p1 p2)

(def facts
(pldb/db
[parent 'dave 'kaylen]
[parent 'frank 'dave]))

(defn grandparent
[x y]
(fresh [z]
(parent x z)
(parent z y)))

(pldb/with-db facts
(doall (run* [q]
(fresh [x y]
(grandparent x y)
(== q [x y])))))
=> ([frank kaylen])

有关更多示例,请参阅 core.logic 源中的 pldb tests

关于clojure - 从 defrel 和事实到 core.logic.pldb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23811409/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com