Newsgroups: comp.lang.c++,comp.object,comp.theory,comp.lang.smalltalk
From: mfinney@lynchburg.net
Subject: Re: Opinions on Ellipse-Circle dilemma?
Reply-To: mfinney@lynchburg.net
References: <5d4fmf$7gk@bagan.srce.hr> <u8rvi86ntcx.fsf@miris10.leeds.ac.uk> <32FA2A1C.6B31@arscorp.com> <E5B2HH.8AH@ecf.toronto.edu> <E5EMwz.74K@undergrad.math.uwaterloo.ca>
X-Newsreader: IBM NewsReader/2 2.0
NNTP-Posting-Host: 208.197.56.56
Message-ID: <32ffb03c.0@news3.paonline.com>
Date: 10 Feb 97 23:33:16 GMT
Lines: 98
Path: cantaloupe.srv.cs.cmu.edu!rochester!cornellcs!newsstand.cit.cornell.edu!newstand.syr.edu!news.maxwell.syr.edu!cam-news-hub1.bbnplanet.com!news.bbnplanet.com!news.idt.net!enews.sgi.com!news.sgi.com!news-out.microserve.net!news-in.microserve.net!news.paonline.com!news3.paonline.com!208.197.56.56
Xref: glinda.oz.cs.cmu.edu comp.lang.c++:247035 comp.object:60983 comp.theory:17880 comp.lang.smalltalk:51372

In <E5EMwz.74K@undergrad.math.uwaterloo.ca>, eadengle@hoporoo.uwaterloo.ca (Ed "Cynwrig" Dengler) writes:

>CLASS Circle: SUBCLASS Ellipse


>A: Circle
>B: Ellipse

>A.Create
>B := A
>B.setAxesLength( 5, 10 )

>Hmmmm, doesn't work too well does it?

>The problem of matching subtypes is that you also match interfaces, which means
>that any subclass better support what its superclass can be told to do.

The problem is not interfaces, but rather a "first-order" definition.
The Liskov substitution principle only holds when the class can be
defined without using second-order predicates (which contain self
reference).  A second order example which works quite well is...


class Ellipse
   {
   private:
      double xAxis;
      double yAxis;
   public:
      virtual bool isValidAxesRatio(double x, double y) const
         {
         return true;
         }
      void setAxes(double x, double y)
         {
         if (not isValidAxesRatio(x, y))
            throw "Ellipse precondition exception";
         xAxis = x;
         yAxis = y;
         }
   };

class Circle : Ellipse
   {
   virtual bool isValidAxesRatio(double x, double y) const
      {
      return x == y;
      }
   };

class Oval : Ellipse
   {
   virtual bool isValidAxesRatio(double x, double y) const
      {
      return x != y;
      }
   };


This solution works because the setAxes() method has a precondition
which only permits it to be called for valid x and y ratios.  That
precondition can be separately checked by calling isValidAxesRatio();.

Essentially, the set of valid ratios is part of the class predicate and
the additional constraints placed on Circle do not violate any of the
constraints on Ellipse.  Membership in that set is checked by calling
the isValidAxesRatio() method and the precondition on setAxes() ensure
that a Circle will always be round.  But, notice that the similar
Oval class cannot inherit from Circle because it would violate the
constraints on Circle.  It can, however, inherit from Ellipse as a
sibling of Circle.

Now, if you have an Ellipse instance in your code which does not
violate the constraints on circle, then a Circle instance can be
substituted.  So, second order substition works correctly.  However,
the class predicate is second order and not first order, and so you
cannot substitute a Circle for an arbitrary Ellipse.

Some people may be uncomfortable with class designs which use
second order references.  However, they are essential in many cases.
Eiffel directly supports a limited second order constraint since it
allows covariant method parameters.  Also, parallel class heirarchies
are another example where second order references come into play.

The only thing about this example which is unusual is that the
"obvious" mathematical intuition and the "obvious" programming
intuition conflict.  I suggest that they are not in conflict when you
consider that the appropriate class definition contains a second
order predicate.

Liskov's Substitution Principle is widely quoted, but is limited to
first order constraints.  Where second order constraints are present,
it breaks down as stated.  It can, of course, be reformulated in a
second order form.


Michael Lee Finney

