Testing for a negation



Hello,

I have defined a tree in terms of the "is_parent" relation. For
instance, the following code defines a tree with a root 'a', and two
leaves 'b' and 'c'.

a is_parent b.
a is_parent c.

Now, I would like to create a predicate that tells me if a given node
is the root of a tree or not.

is_root(Node) :-
not(D is_parent Node).

While

is_root(a).

returns the desired 'Yes.', typing

is_root(X).

returns 'No.'


I have somewhat understood why that is the case, it is because of the
unification, and so, to force Prolog to "pick" an atom to use in the
not evaluation, I've redefined my predicate:

is_root(Node) :-
Node is_parent X,
not(D is_parent Node).

This works, but not completely. Indeed

is_root(X).

returns 'a' twice (for every 'is_parent' fact that refers to it as the
parent!). I know why that is so. It is Prolog matches the 'Node
is_parent X' for Node = a and X = b, AND also for Node = a and X = c.

Oh. I just got what the "!" operator is for while writing this message:
I just have to add at the end of the "is_root" definition so it stops
matching. (I'm sorry! This is my second day writing Prolog code!!)


Well then, for this not to go to waste, I would like to ask an
additional question. Currently, the is_root(Node) predicate *requires*
Node from being the parent of something. Whereas a technically, any
atom is a root. A root just has no parents. How can I translate that in
Prolog?


Regards,

Jérémie Lumbroso

.



Relevant Pages

  • Re: Lowest common ancestor in binary tree
    ... You are given a binary tree where the only links are from a child ... to its parent, and the parent of the root is null. ... Suppose for two node pointers P1 and P2, ... What the OP calls height is measured from the root of the tree-- perhaps it would be more conventionally called depth. ...
    (comp.programming)
  • Re: Lowest common ancestor in binary tree
    ... You are given a binary tree where the only links are from a child ... to its parent, and the parent of the root is null. ... Suppose for two node pointers P1 and P2, ... to two nodes along a single non-branching path from the root. ...
    (comp.programming)
  • Re: How to create a self-referring datastructure in one line
    ... child => { ... root => \$$_ ... } for \my %tree; ...
    (comp.lang.perl.misc)
  • Re: Testing for a negation
    ... I have defined a tree in terms of the "is_parent" relation. ... I would like to create a predicate that tells me if a given node ... is the root of a tree or not. ... It is Prolog matches the 'Node ...
    (comp.lang.prolog)
  • Re: Treeview (Basic question)
    ... a child node and then a child of a child. ... The Tree itself has a Nodes member that is a collection of all the top level ... If you want a single root node in the tree, then just add 1 TreeNode to the ... and the children of the parent. ...
    (microsoft.public.dotnet.general)