首页 > 解决方案 > 为什么 `f(a)=a` 失败但 `f(X)=X` 在 prolog 中成功?

问题描述

我遇到以下情况:

?- f(X) = X.
X = f(X).

?- f(a) = a.
false.

为什么统一适用f(X) = X,但不适用f(a) = a?是不是因为第一个简单地说是f(X)as的名称返回值X,而第二个试图检查返回值是否f(a)a?但是f()这里没有定义!!另外,我想,序言中没有“返回值”这样的概念。那么,这里发生了什么?

标签: prologoccurs-check

解决方案


In your first example, X is a variable (identifier starts with capital letter, look it up). A free variable unifies with anything. (almost anything. you are creating a cyclic term, this will not work if you try to "unify with occurs check", look it up).

In your second example, a is an atom. It only unifies with a free variable or with itself. Since f(a) is not a the unification fails.

You are correct that there is no such thing as "return value". You might treat the success or failure of a goal as the "return value", but I don't know how much this helps.

Either way, there is no f() in Prolog. This is not a function. You don't need to define it. It is just a compound term (look it up). It is a data structure, in a way.


推荐阅读