附加到表单如何工作? (SICP 的逻辑编程部分)

发布于 2024-10-06 19:53:57 字数 908 浏览 0 评论 0原文

我目前正在学习 SICP 的逻辑编程部分,但我陷入了有关逻辑推论的示例中,尤其是附加到表单规则。它们如何工作?我不太明白的是第二条规则 cdr-downs 第一个列表是如何的。例如,给定:

(rule (append-to-form () ?y ?y))

(rule (append-to-form (?u . ?v) ?y (?u . ?z)) (append-to-form ?v ?y ?z))

a) 我们如何到达:

;;; Query input:
(append-to-form (a b) (c d) ?z)

to

;;; Query results:
(append-to-form (a b) (c d) (a b c d))

b) 这个怎么样:

;;; Query input:
(append-to-form (a b) ?y (a b c d))

to

;;; Query results:
(append-to-form (a b) (c d) (a b c d))

c) 最后:

;;; Query input:
(append-to-form ?x ?y (a b c d))

to

;;; Query results:
(append-to-form () (a b c d) (a b c d))
(append-to-form (a) (b c d) (a b c d))
(append-to-form (a b) (c d) (a b c d))
(append-to-form (a b c) (d) (a b c d))
(append-to-form (a b c d) () (a b c d))

我对执行该规则所需的具体心理步骤感兴趣匹配。

先感谢您。

I am currently working through SICP's section on Logic Programming, but I got stuck in the examples regarding logical deductions, especially the append-to-form rules. How do they work? What I don't quite understand is how the second rule cdr-downs the first list. For example, given:

(rule (append-to-form () ?y ?y))

(rule (append-to-form (?u . ?v) ?y (?u . ?z))
(append-to-form ?v ?y ?z))

a) How do we reach from:

;;; Query input:
(append-to-form (a b) (c d) ?z)

to

;;; Query results:
(append-to-form (a b) (c d) (a b c d))

b) And what bout this one:

;;; Query input:
(append-to-form (a b) ?y (a b c d))

to

;;; Query results:
(append-to-form (a b) (c d) (a b c d))

c) And lastly:

;;; Query input:
(append-to-form ?x ?y (a b c d))

to

;;; Query results:
(append-to-form () (a b c d) (a b c d))
(append-to-form (a) (b c d) (a b c d))
(append-to-form (a b) (c d) (a b c d))
(append-to-form (a b c) (d) (a b c d))
(append-to-form (a b c d) () (a b c d))

I would be interested in the specific mental steps required to carry out the rule matching.

Thank you in advance.

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

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

发布评论

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

评论(1

原谅我要高飞 2024-10-13 19:53:57

拿一张纸并写下每一个步骤来扮演口译员的角色。对于每一步,您都写下哪个规则被触发/可以被触发以及哪个变量绑定到什么值。

例如:

(append-to-form (a b) (c d) ?z)

触发规则

(rule (append-to-form (?u . ?v) ?y (?u . ?z)) 
  (append-to-form ?v ?y ?z))

注意

?u = a, ?v = (b), ?y = (c d), ?z = (a . ?z_2)

:原始查询中的?z 应该是与规则主体中的?z 不同的变量,因此将规则的?z 重命名为?z_2。列表 (1 2 3) 与 (?a . ?b) 匹配时会生成 ?a = 1, ?b = (2 3),就像 car/cdr'ing 列表时一样。

这些绑定应用于规则的主体 (append-to-form ?v ?y ?z) 因此,我们

(append-to-form (b) (c d) ?z_2)

再次

(append-to-form () (c d) ?z_3)

得到并触发不同的规则: (rule (append- to-form () ?y ?y)) 将 ?z_3 绑定到 (cd)。
然后递归开始,?z_2 被定义为 (b . ?z_3),?z 被定义为 (a . ?z2)

原始查询 (append-to-form (ab) (cd) ?z)< /code> 应用于 ?z = (a . (b . (cd))) 的绑定并返回 (append-to-form (ab) (cd) (abcd))

其余的练习留给读者;)

这里的关键概念是模式匹配和统一,可以在 第 4.2.2 节。整个查询评估器确实是 SICP 中最困难的部分,所以不要灰心。这是非常值得的。尝试运行代码(在 R5RS 方案中)并修改它,例如添加跟踪。

Play interpreter by taking a piece of paper and writing down every single step. For every step you write down which rule was/can be triggered and what variable was bound to what value.

For example:

(append-to-form (a b) (c d) ?z)

triggers the rule

(rule (append-to-form (?u . ?v) ?y (?u . ?z)) 
  (append-to-form ?v ?y ?z))

with

?u = a, ?v = (b), ?y = (c d), ?z = (a . ?z_2)

Note: ?z in the original query is supposed to be a different variable from ?z in the rule body, therefor rename the rule's ?z into ?z_2. A list (1 2 3) when matched to (?a . ?b) produces ?a = 1, ?b = (2 3) like when car/cdr'ing a list.

These bindings are applied to the body of the rule (append-to-form ?v ?y ?z) So we get

(append-to-form (b) (c d) ?z_2)

which again becomes

(append-to-form () (c d) ?z_3)

and triggers a different rule: (rule (append-to-form () ?y ?y)) binding ?z_3 to (c d).
Then recursion kicks in, ?z_2 was defined as (b . ?z_3), ?z was defined as (a . ?z2)

The original query (append-to-form (a b) (c d) ?z) gets applied to the bindings in which ?z = (a . (b . (c d))) and returns (append-to-form (a b) (c d) (a b c d))

The rest of the exercises are left to the reader ;)

The crucial concepts here are pattern matching and unification which can be found at section 4.2.2. The whole query evaluator is really the most difficult piece in SICP, so don't be discourage. It is well worth the effort. Try to run the code (in an R5RS Scheme) and fiddle with it, such as adding tracing.

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