You need to define a predicate is_leaf/1
that is a generator,
i.e. it instantiates the input variable with possible solutions.
Something like this:
% Directed graph
de(0,1).
de(0,2).
de(2,3).
de(2,4).
de(3,4).
de(4,5).
% If Node is ground,
% then test if it is a child node that is not a parent node.
% If Node is not ground,
% then bind it to a child node that is not a parent node.
is_leaf(Node) :-
de(_, Node),
\+ de(Node, _).
Usage examples:
?- is_leaf(Node).
Node = 1 ;
Node = 5.
?- is_leaf(Node), writeln(Node), fail ; true.
1
5
true.
?- findall(Node, is_leaf(Node), Leaf_Nodes).
Leaf_Nodes = [1, 5].
Your solution immediately calls not
. (Btw, SWI-Prolog recommends using \+
instead of not
.)
isLeaf(Node) :-
not(de(Node,_)).
This means that your isLeaf/2
is not a generator: it either fails or succeeds (once), and never binds the input argument if it happens to be a variable.
Also, it never tests that the input is a leaf, it just tests if it's not a parent node.
% Is it false that 1 is a parent? YES
?- isLeaf(1).
true.
% Is it false that blah is a parent? YES
?- isLeaf(blah).
true.
% Is it false that 2 is a parent? NO
?- isLeaf(2).
false.
% Basically just tests if the predicate de/2 is in the knowledge base,
% in this sense quite useless.
?- isLeaf(Node).
false.