Prolog - Merging Symbols (or Terms?)
(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 wh开发者_Go百科ich predicates use these terms.
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.
精彩评论