Prolog - 合并符号(或术语?)

发布于 2024-11-26 17:46:33 字数 461 浏览 0 评论 0原文

(如果我的术语有误,请原谅...我是 Prolog 新手。)

假设您有一系列符号出现在一些未知数量的谓词中。

f1(a, b, c, d).
f2(b, b, c).
...
fn(b, d, e).

稍后,在运行时,您意识到术语 ab 是相同的,并且您希望合并它们或将其中一个替换为另一个。换句话说,我想要:

  • 使 a = b
  • a 的所有实例替换为 b
  • 替换 a > 和 b 带有一个新符号(通过 gensym/2 创建)
  • ...或任何其他可以实现此目的的

...我不知道哪些谓词使用这些术语。

(Pardon if my terminology is wrong... I'm new to Prolog.)

Suppose you have a series of symbols appearing in some unknown number of predicates.

f1(a, b, c, d).
f2(b, b, c).
...
fn(b, d, e).

Later--at runtime--you realize that terms a and b are the same, and you wish to merge them or replace one of them with the other. In other words, I would like to either:

  • Make a = b
  • Replace all instances of a with b
  • Replace a and b with a new symbol (made through gensym/2)
  • ...or anything else that accomplishes this

... where I do not know which predicates use these terms.

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

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

发布评论

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

评论(1

诗笺 2024-12-03 17:46:33

以大写字母开头的原子是变量。第一步是使用 AB。如果在某个时刻您认为两个变量实际上相等,您只需说A = B即可。从逻辑上陈述一件事=另一件事的过程就是“统一”。

例如:

veryDifferentOrTheSame(A,B) :- veryDifferent(A,B).
veryDifferentOrTheSame(A,B) :- A = B.

当然,统一并不总是有效。 a(X) = b(X) 将失败。

这一切都意味着,当编写代码时,您知道您不确定 A=B。

您还可以在运行时动态断言子句。将子句声明为动态并使用assera 或assertz。

但如果你说:

iOwn(goldfish).
iOwnFish :- iOwn(fish).

然后想通过说“在我的宇宙中鱼=金鱼”来实现这一点,那么你就处于奇怪的境地了。

Atoms that start with upper case letters are variables. The first step then is to use A and B. If at some point you decide two variables are actually equal, you just say it A = B. The process of stating logically that one thing = another is "unification".

e.g.

veryDifferentOrTheSame(A,B) :- veryDifferent(A,B).
veryDifferentOrTheSame(A,B) :- A = B.

Of course, unification won't always work. a(X) = b(X) will fail.

This all implies that when the code was written, you knew that you weren't sure A=B.

You can also dynamically assert clauses at runtime. Declaring a clause as dynamic and using assera or assertz.

But if you state:

iOwn(goldfish).
iOwnFish :- iOwn(fish).

and then want to make that work by saying "in my universe fish = goldfish", then you're in strange territory.

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