core.logic lvar 上的算术和 clojure 函数
两个相关问题合二为一:
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 LVar
s, 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过
project
可以进行非关系算术。我相信给出的 Prolog 示例也是非关系型的。
您问题的后半部分也可以通过
project
解决,但您必须小心,始终输入地面值。PS:请屏住呼吸,等待约束逻辑编程来到 core.logic!
Non-relational arithmetic is possible via
project
.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.PS: Hold your breath for Constraint Logic Programming to come to core.logic!
我相信您必须先将逻辑变量“投影”(非相关/投影)到其绑定,然后才能将函数应用于它:
请注意,exist 替代了原始代码片段中的 fresh 以及事实声明的附加参数。
I believe you have to "project" (nonrel/project) a logic variable to its binding before you can apply a function to it:
Note that exist substitutes for fresh in the original snippet and the additional argument for the fact declarations.