core.logic lvar 上的算术和 clojure 函数

发布于 2024-12-08 10:09:09 字数 1129 浏览 0 评论 0原文

两个相关问题合二为一:

Clojure 的 core.logic 模块可以像普通 Prolog 一样执行算术、逻辑比较等吗?我设想如下:

(defrel points person n) 
(fact :bob 2)
(fact :charlie 3)
(run* [q] (fresh [x y] 
   (points :bob x) 
   (points :charlie y) 
   (< x y) 
   (== q (+ x y))))

=> (5)

在这个例子中,逻辑比较 (< xy) 和 q 到 (+ xy) 的尝试绑定都不起作用。我想这是因为我现在正在使用 LVar,而不是整数,并且我无法进行这些比较,因为符号尚未绑定。但它在序言中起作用:

points(bob, 2).
points(charlie, 3).
?- points(bob, X), points(charlie, Y), Result is X + Y.

=> Result = 5.

以类似的方式,我可以以某种方式使用 Clojure 函数(返回布尔值或其他“真实”值)作为逻辑谓词吗?换句话说,用函数来告诉 Minikanren 哪些术语统一或不统一。大概是这样的:

(defmagic startswithhi-o [v]
  (.startsWith v "hi"))
(defrel person n)
(fact person "bob")
(fact person "hillary")
(run* [q] 
   (fresh [n]
     (person n)
     (startswithhi-o n)
     (== q n)))

=> ("hillary")

如果我尝试这样的事情,我也会收到错误,抱怨 Lvar 没有绑​​定。有办法做到这一点吗?

最后,如果有人读到这里,我不妨问:是否有计划将概率逻辑合并到 core.logic 中,大致如下:

http://dtai.cs.kuleuven.be/problog/

我没有屏住呼吸,但这太棒了!

Two related questions in one:

Can Clojure's core.logic module perform arithmetic, logical comparison, etc, like ordinary Prolog? I am envisioning something like the following:

(defrel points person n) 
(fact :bob 2)
(fact :charlie 3)
(run* [q] (fresh [x y] 
   (points :bob x) 
   (points :charlie y) 
   (< x y) 
   (== q (+ x y))))

=> (5)

In this example, neither the logical comparison (< x y) nor the attempted binding of q to (+ x y) works. I suppose that this is because I'm working with LVars, not integers at this point, and I can't make these comparisons because the symbols aren't yet bound. But it works in prolog:

points(bob, 2).
points(charlie, 3).
?- points(bob, X), points(charlie, Y), Result is X + Y.

=> Result = 5.

In a similar vein, can I somehow use Clojure functions (which return booleans or other "truthy" values) as logic predicates? In other words, to use functions to tell Minikanren which terms unify or not. Something along the lines of:

(defmagic startswithhi-o [v]
  (.startsWith v "hi"))
(defrel person n)
(fact person "bob")
(fact person "hillary")
(run* [q] 
   (fresh [n]
     (person n)
     (startswithhi-o n)
     (== q n)))

=> ("hillary")

If I try things like this I get errors also complaining that the LVars aren't bound. Is there a way to do this?

Lastly if anyone has read this far, I might as well ask: are there plans to incorporate probabilistic logic into core.logic, along the lines of:

http://dtai.cs.kuleuven.be/problog/ ?

I'm not holding my breath but it would be awesome!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝海似她心 2024-12-15 10:09:09

通过project可以进行非关系算术。

(run 1 [q]
   (fresh [x y]
        (== x 1)
        (== y 2)
        (project [x y]
                 (== q (+ x y)))))
(3)

我相信给出的 Prolog 示例也是非关系型的。

您问题的后半部分也可以通过 project 解决,但您必须小心,始终输入地面值。

(defn startwith [x]
    (project [x]
         (== true (.startsWith x "hi"))))

PS:请屏住呼吸,等待约束逻辑编程来到 core.logic!

Non-relational arithmetic is possible via project.

(run 1 [q]
   (fresh [x y]
        (== x 1)
        (== y 2)
        (project [x y]
                 (== q (+ x y)))))
(3)

I believe the Prolog example given is also non-relational.

The second half of your question can also be solved via project, but you must be careful that you always input a ground value.

(defn startwith [x]
    (project [x]
         (== true (.startsWith x "hi"))))

PS: Hold your breath for Constraint Logic Programming to come to core.logic!

帥小哥 2024-12-15 10:09:09

我相信您必须先将逻辑变量“投影”(非相关/投影)到其绑定,然后才能将函数应用于它:

(defrel points person n) 
(fact points :bob 2)
(fact points :charlie 3)
(run* [q] 
  (exist [x y] 
    (points :bob x) 
    (points :charlie y) 
    (project [x y]
      (== true (< x y)) 
      (== q (+ x y)))))

请注意,exist 替代了原始代码片段中的 fresh 以及事实声明的附加参数。

I believe you have to "project" (nonrel/project) a logic variable to its binding before you can apply a function to it:

(defrel points person n) 
(fact points :bob 2)
(fact points :charlie 3)
(run* [q] 
  (exist [x y] 
    (points :bob x) 
    (points :charlie y) 
    (project [x y]
      (== true (< x y)) 
      (== q (+ x y)))))

Note that exist substitutes for fresh in the original snippet and the additional argument for the fact declarations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文