From: Marcus Andrews
Subject: Query: inheritance
Date: 
Message-ID: <914612718.28828.0.nnrp-06.c2deb5bb@news.demon.co.uk>
Can anyone offer me assistance?

I am new to Lisp, having only ever coded in Prolog.  I am trying to
represent a hierachy of concepts obout objects such that my program can
substitute on kind of object with it's superclass.

eg.  triangle, rectangle and trapezium are all types of stable_poly while
hexagon is a type of unstable_poly.  Stable_poly and unstable_poly are bot
types of convex poly,  which in turn is a type of polygon.

The only way i can think of to represent this knowledge, so it may be called
by a recursive function designed to replace an object name with it's parent
name, is to place them all in a property list.  Can you use a value for one
key as the name of another key? Can you even use the same value for two
different keys? (I cant see why not).

eg.  (setf subclasses '(triangle stable_poly rectangle stable_poly
stable_poly convex_poly hexagon unstable_poly stable_poly convex_poly)

rather confusing huh?

Thanks in advance,

Marcus
From: Stig Hemmer
Subject: Re: Query: inheritance
Date: 
Message-ID: <ekvyanvajoy.fsf@gnoll.pvv.ntnu.no>
"Marcus Andrews" <······@thelocal.demon.co.uk> writes:
> I am new to Lisp, having only ever coded in Prolog.  I am trying to
> represent a hierachy of concepts obout objects such that my program can
> substitute on kind of object with it's superclass.

There are many Lisps out there... This answer is based on ANSI Common
Lisp.  Most of it should be relevant to your Lisp, whichever that
might be.

CL has an built-in class/object system called CLOS (CL Object System).

From your description it seems to me that you will not benefit
directly from using this.  However, it is possible that you could
reformulate your problem so that CLOS becomes the right answer.  My
guess is that you will simplify it in the process, but that is just a
guess.

> eg.  triangle, rectangle and trapezium are all types of stable_poly while
> hexagon is a type of unstable_poly.  Stable_poly and unstable_poly are bot
> types of convex poly,  which in turn is a type of polygon.

So, a class has one and only one superclass?  In that case you
basically have a set of key-value pairs.

You mention property lists as a possible implementation of this, and
indeed it it so.  In no less than two ways: (Remember that property
lists are always attached to a symbol)

- you can use the classname as the symbol and 'SUPERCLASS as the
  property.  (A good solution, for some problems)

- you can use 'SUPERCLASSES as the symbol and the class name as the
  property.  (Probably not a good solution for any problem)

The standard function (GET) handles property lists.

But there is more!  ANSI Common Lisp offers two more data structures:
Alists and hash tables.  Your Lisp is sure to handle alists, but might
not have hash tables.

Alist is short of association list and can look like this:

(setf superclasses '((triangle . stable_poly) 
                     (rectangle . stable_poly)
                     (stable_poly . convex_poly)
                     (hexagon . unstable_poly)
                     (unstable_poly . convex_poly)))

Note the spaces around the dots.

This data structure is easy to maintain and use, but doesn't scale
very well.  Don't use it for more than a few thousand elements.

The standard function (ASSOC) as well as all the standard list
functions handles alists.

Hash tables are a tad more cumbersome to use, but handles any number
of key-value pairs up to what you can fit in memory. (A million, give
or take a digit or two)

Giving an introduction to hash tables would be beyond the scope of
this article, but look them up in your Lisp book.

> Can you use a value for one key as the name of another key?
> Can you even use the same value for two different keys?

Yes to both of these, for all the above data structures.

In the SETF above, I use symbols for classnames.  You might also
consider using strings as classnames.  They are easier to handle in
some contexts.  (But harder in others, of course)

Good Luck!

Stig Hemmer,
Jack of a Few Trades.

PS: Can't an unstable polygon be concave?  
PPS: In the example given, you will always end up with convex_polygon.
    I trust this isn't the case in your real problem?