kallakafar
kallakafar

Reputation: 735

ECLiPSe Prolog - Unexpected behaviour of 'nonvar/1'

I was going through some ECLiPSe documentation and found this:

`nonvar/1` : Fails if Term is not instantiated

I tried doing this:

1. Query: nonvar(X). Result: No (AS EXPECTED)
2. Query: X=5, nonvar(X). Result: Yes (AS EXPECTED)
3. Query: X=5, nonvar(f(X)). Result: Yes, X=5. (AS EXPECTED)

Now this query result confused me:

4. Query: nonvar(f(X)). Result: Yes, X=X.

Queries 1,2,3 work as expected. To me, the result of 4 is weird. As per the documentation of nonvar/1 in eclipse, the argument can be ANY PROLOG TERM (so, f(X) is fine), and nonvar tests if the argument is INSTANTIATED or not.

In the case of 4, it is not instantiated so it should be NO and not YES (X=X). Please correct me if my understanding is incorrect, or is it a documentation issue in ECLiPSe prolog? I am using the latest version.

Thanks!

Upvotes: 3

Views: 225

Answers (1)

Fred Foo
Fred Foo

Reputation: 363627

In your query 4, f(X) contains the non-instantiated variable X but it is not itself an uninstantiated variable.

SWI-Prolog's help is perhaps a bit clearer on the meaning of nonvar:

nonvar(Term): True if Term currently is not a free variable.

You can check whether a term contains free variables with ground/1:

?- X = 5, ground(f(X)).
X = 5.

?- ground(f(X)).
false.

Upvotes: 4

Related Questions