Simulating inheritance with a metainterpreter

From: seguso (look_at_in.signature)
Date: 04/20/04


Date: Tue, 20 Apr 2004 18:12:25 GMT

Hi there you gurus. This is more difficult. :-)

Having made sure that prolog does support polymorphism, I would like to
decrease the amount of code to be written in the following typical
situation:

Suppose I have a semantic domain made of "boxes" and "activeBoxes". A "box"
has 4 predicates defined over it (say size/2, color/2, setSize/2 and
setColor/2). An "activeBox" is a "box" with a speed. So it has an
additional predicate speed/2. But ANY predicate which works when passed a
box must continue to work if passed and activeBox. And work as intended :-)

In C++ and similar I would use inheritance. So I would define the four
predicates only once. In prolog I must define all the predicates twice
(once for box and once for abox):

 size(box(Siz, _), Siz).
 color(box(_, C), C).
 setSize(box(_, C), Siz, box(Siz, C)).
 setColor(box(S, _), C, box(S, C)).

 size(abox(Siz, _, _), Siz).
 color(abox(_, C, _), C).
 setSize(abox(_, C, Sp), Siz, abox(Siz, C, Sp)).
 setColor(abox(S, _, Sp), C, abox(S, C, Sp)).

 speed(abox(_, _, Sp), Sp). %unique to abox

It is quite boring to redefine all predicates that can be applied to box for
activeBox. In C++ I just need to say

 class ActiveBox : public Box

and this is done almost automatically (with some effort in the constructor).

I would like to simplify the above prolog code with something like:

 size(box(Siz, _), Siz).
 color(box(_, C), C).
 setSize(box(_, C), Siz, box(Siz, C)).
 setColor(box(S, _), C, box(S, C)).

 speed(abox(_, _, Sp), Sp). % I define only this one for abox.

 inherits(abox, box).

and write a metainterpreter that, when it encounters inherits, behaves as if
the four predicates were defined for abox too.

Of course writing inherits(abox, box) is not enough: it doesn't tell the
arity, and it doesn't define the correspondence between the size/color of a
box and the size/color of an abox. But how can we tell those info to the
metainterpreter?

PS: I would also be interested in the performance loss of using such a
metainterpreter.

Thank you very much for any idea!

-- 
Best Regards,
Maurizio Colucci
Please remove the uppercase letters "S,P,A,M":
seSgPuAsMo.forever@tin.it


Relevant Pages

  • Re: Simulating inheritance with a metainterpreter
    ... In prolog I must define all the predicates twice ... > size(box(Siz, _), Siz). ... > and write a metainterpreter that, when it encounters inherits, behaves as ... > the four predicates were defined for abox too. ...
    (comp.lang.prolog)
  • Re: Simulating inheritance with a metainterpreter
    ... Stephan Lehmke wrote: ... > pattern for these predicates, then prolog can go a long way ... > setproperty(size,box(_, C), Siz, box, C)). ...
    (comp.lang.prolog)
  • Re: OO versus RDB
    ... There is a very simple mapping from RM to PROLOG. ... a representation based on predicates, ... 'connection' comes from the fact that PROLOG stores rules about facts ...
    (comp.object)
  • Re: OO versus RDB
    ... There is a very simple mapping from RM to PROLOG. ... store knowledge using predicates. ... applied to a physical representation of a relational model. ...
    (comp.object)
  • Re: Prolog, memory management and memory leaks
    ... It depends on what you mean by a memory leak. ... Did the programmer mess up, ... the Prolog implementation you are working with). ... non-deterministic predicates in not always perfect. ...
    (comp.lang.prolog)