Testing for a negation
- From: "Jérémie Lumbroso" <jeremie.lumbroso@xxxxxxxxx>
- Date: 11 Jul 2006 19:39:14 -0700
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
.
- Follow-Ups:
- Re: Testing for a negation
- From: Advait
- Re: Testing for a negation
- Prev by Date: mathematical integration
- Next by Date: Re: Testing for a negation
- Previous by thread: mathematical integration
- Next by thread: Re: Testing for a negation
- Index(es):
Relevant Pages
|
|