Piano_traslato
Piano_traslato

Reputation: 21

How to test binary trees in prolog

I want to test this prolog program:

#
binary_tree(void).

binary_tree(tree(_Element,Left,Right)):-binary_tree(Left),binary_tree(Right).

test(tree(a,tree(b,tree(d,void,void),void),tree(c,void,void))).
#

When I execute it with:

test(X), binary_tree(X).

I get

X = tree(a, tree(b, tree(d, void, void), void), tree(c, void, void)).

(and I should get "true" instead)

What am I doing wrong?

Thanks

Upvotes: 1

Views: 197

Answers (1)

Isabelle Newbie
Isabelle Newbie

Reputation: 9378

Who says you should just get true instead? Calls to Prolog predicates may succeed or fail. On success, they answer with a variable substitution, as in your case. Your Prolog system's answer says that your test succeeded, as you expected.

You will usually only get true if no variable substitution can be given because the query does not contain any variables. If the query fails, you will get false or fail but no variable substitution.

Upvotes: 2

Related Questions