Inherit – Define IS_A predicates in prolog?

I am trying to define the inheritance check predicate is_a/2 in Prolog, but all my experiments so far have failed.

As long as Y is For the superclass of X, the is_a(X,Y) predicate should return true. For example:

object(bare).
object(mammal).
object(animal).
object(bird).
is_a(bare, mammal).
is_a(mammal, animal).
is_a(bird, animal).
is_a(X, Y):- .

The definition should make the following query return true:

?- is_a( bare, animal).
true.

I tried to define it in an obvious way, but I got stuck in an infinite loop:

is_a(X , Y):- X\==Y, object(X), object(Y), object(Z), is_a(X, Z), is_a(Z, Y).

Any suggestions ?

One way to avoid infinite loops is to add a predicate that shows “direct” inheritance (not Pass), that is, direct / 2. Then you can write:

object(bare).
object(mammal).
object (animal).
object(bird).

direct(bare, mammal).
direct(mammal, animal).
direct(bird, animal).

isa(X, Y) :- object(X), object(Y), direct(X, Y).
isa(X, Y) :- object(X), object(Y), object(Z), direct(X, Z), isa(Z, Y).

Then you get:

? -findall(X, isa(X, animal), L).
L = [mammal,bird,bare]? ;
no

I’m not sure what you asked for .

I am trying to define the inheritance check predicate is_a/2 in Prolog, but all my experiments have failed so far.

As long as Y is the superclass of X, the is_a(X,Y) predicate should return true. For example:

object(bare).
object( mammal).
object(animal).
object(bird).
is_a(bare, mammal).
is_a(mammal, animal).
is_a(bird , animal).
is_a(X, Y):- .

The definition should make the following query return true:

< pre>?- is_a(bare, animal).
true.

I try Try to define it in an obvious way, but I am stuck in an infinite loop:

is_a(X, Y):- X\==Y, object(X), object(Y ), object(Z), is_a(X, Z), is_a(Z, Y).

Any suggestions?

One way to avoid infinite loops is to add a predicate, which shows “direct” inheritance (not passable), i.e. direct/2. Then you can do this Write:

object(bare).
object(mammal).
object(animal).
object(bird) .

direct(bare, mammal).
direct(mammal, animal).
direct(bird, animal).

isa(X, Y) :- object(X), object(Y), direct(X, Y).
isa(X, Y) :- object(X), object(Y), object(Z), direct( X, Z), isa(Z, Y).

Then you get:

?- findall(X, isa(X, animal), L).
L = [mammal,bird,bare]? ;
no

I’m not sure what you asked for.

Leave a Comment

Your email address will not be published.