Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
801 views
in Technique[技术] by (71.8m points)

prolog - Order of unification in lists when using pipe(|)

I am having trouble trying to figure out in what order unification is done. I have the following query:

[X, like | Z] = [I, Y, Prolog, language].

This gives me the following result:

X = I,
Z = [Prolog, language],
Y = like.

However, what I was expecting is:

X = I,
Y = like,
Z = [Prolog, language].

Is there any specific order in which Prolog unifies terms?

EDIT: I have a feeling that Prolog is giving higher priority to unification of an atom with a variable over that of a variable with an atom. Is that so?

question from:https://stackoverflow.com/questions/65873598/order-of-unification-in-lists-when-using-pipe

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is no order. Here is the mapping that is performed:

[I,  Y   , Prolog, language].
 |   |     +--------------+
 |   |           |
[X, like |       Z] 

if that mapping is possible (i.e. if the variables I ,Y, X, Z are bound to terms that allow unification to succeed)

In this case, assuming that all variables are unbound, then:

  • Variables I and X will become "the same variable" (more clearly, the variable name I and X will designate the same empty storage cell)

  • Variable Y will become the atom like' (more clearly, the storage cell designated by variable name Ywill be set tolike`)

  • Variable Z takes up the "rest (or suffix) of the list" (everything behind the |) and will become the complex term `[Prolog, language]'.

  • Variable Prolog is not changed and stays what it was.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...