Prolog - 合并符号(或术语?)
(如果我的术语有误,请原谅...我是 Prolog 新手。)
假设您有一系列符号出现在一些未知数量的谓词中。
f1(a, b, c, d).
f2(b, b, c).
...
fn(b, d, e).
稍后,在运行时,您意识到术语 a
和 b
是相同的,并且您希望合并它们或将其中一个替换为另一个。换句话说,我想要:
- 使
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
withb
- Replace
a
andb
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以大写字母开头的原子是变量。第一步是使用
A
和B
。如果在某个时刻您认为两个变量实际上相等,您只需说A = B
即可。从逻辑上陈述一件事=另一件事的过程就是“统一”。例如:
当然,统一并不总是有效。
a(X) = b(X)
将失败。这一切都意味着,当编写代码时,您知道您不确定 A=B。
您还可以在运行时动态断言子句。将子句声明为动态并使用assera 或assertz。
但如果你说:
然后想通过说“在我的宇宙中鱼=金鱼”来实现这一点,那么你就处于奇怪的境地了。
Atoms that start with upper case letters are variables. The first step then is to use
A
andB
. If at some point you decide two variables are actually equal, you just say itA = B
. The process of stating logically that one thing = another is "unification".e.g.
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:
and then want to make that work by saying "in my universe fish = goldfish", then you're in strange territory.