Home > Archive > Prolog > July 2006 > Testing for a negation
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Testing for a negation
|
|
| Jérémie Lumbroso 2006-07-11, 9:58 pm |
| 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 =3D a and X =3D b, AND also for Node =3D a and X =3D =
c=2E
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=E9r=E9mie Lumbroso
| |
| Advait 2006-07-12, 7:59 am |
| Hi
J=E9r=E9mie Lumbroso wrote:
> 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 =3D a and X =3D b, AND also for Node =3D a and X =
=3D 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=E9r=E9mie Lumbroso
You want to make a predicate 'is_root' which prints *all*
the roots; you mean atoms (which is impossible to make).
While modelling this system you should think on one more component
that is is_node() or node().
I have made little modifications in your program.
You can add more facts into node() and is_parent to get required
result.
%%%%%%%%% start %%%%%%%%%
:- op(500,yfx,is_parent).
node(a).
node(b).
node(c).
a is_parent b.
a is_parent c.
is_root(N):-
node(N),
not( _ is_parent N).
has_no_parents(X):-
is_node(X).
%%%%%%%%% end %%%%%%%%%%%
yours
Advait
|
|
|
|
|