comp.lang.ada
 help / color / mirror / Atom feed
* Ada Interfaces and the Liskov Substitution Principle
@ 2007-05-23 19:47 Stefan Lucks
  2007-05-23 20:32 ` Ludovic Brenta
                   ` (2 more replies)
  0 siblings, 3 replies; 81+ messages in thread
From: Stefan Lucks @ 2007-05-23 19:47 UTC (permalink / raw)


Hi all,

to me, it seems as if Ada 2005 is bluntly violating the Liskov 
Substitution Prinicple. E.g., define

---Start---
package Parents is
    type Parent is Interface;

    -- primitive operation
   procedure Do_Something(Self: in out Parent) is abstract;

    -- class-wide operations
    procedure Do_Nothing(From: in Parent'Class;
                         To:  out Parent'Class);
    procedure Convert(From: in Parent'Class;
                      To:  out Parent'Class);
end Parents;
----End----

with the primitve operations defined in

---Start---
package body Parents is

    procedure Do_Nothing(From: in Parent'Class;
                         To:  out Parent'Class) is
    begin
       null;
       -- warning: "To" is never assigned a value.
    end Do_Nothing;

    procedure Convert(From: in Parent'Class;
                      To:  out Parent'Class) is
    begin
       To := From;
    end Convert;

end Parents;
----End----

Now, Do_Nothing is harmless (except for generating a compiler warning). 
But Convert uses the assignment ":=", which seems OK, as the interface
Parents.Parent is not limited.

Now there are two children to inherit from Parents. The first one is 
Child.Object:

---Start---
with Parents;

package Child is

    type Object is new Parents.Parent with private;

    procedure Do_Something (Self: in out Object);

private

    ... doesn't matter ...

end Child;
----End----

Child.Object gets everything there are no limits to using 
Parents.Parent'Class. But look at Stepchild.Object:

---Start---
with Parents, Ada.Finalization;

package Stepchild is

    type Object is
      new Ada.Finalization.Limited_Controlled
      and Parents.Parent
      with private;

    procedure Do_Something (Self: in out Object);

private

    ... doesn't really matter ...

end Stepchild;
----End----

Stepchild.Object is really a poor cousin, prohibited from using 
Parent.Convert:

---Start---
with Ada.Text_IO, Parents, Child, Stepchild;

procedure Family is

    procedure All_Well(X: in out Parents.Parent'Class) is
    begin
       X.Do_Something;
    end All_Well;

    Procedure Not_So_Well(X, Y: in out Parents.Parent'Class) is
    begin
       Parents.Convert (X,Y);
    end Not_So_Well;

    Alice, Charles: Child.Object;
    Bob, Eve: Stepchild.Object;

begin
    All_Well(Alice);
    All_Well(Bob);
    All_Well(Charles);
    All_Well(Eve);
    Ada.Text_IO.Put("first OK ");
    Not_So_Well(Alice, Charles);
    Ada.Text_IO.Put("second OK ");
    Not_So_Well(Bob, Eve); -- this raises Eception Constraint_Error
                           -- with "... tag check failed".
    Ada.Text_IO.Put("will we ever get here?"); -- no, nay, never!!!
end Family;
----End----

My understanding of the Liskov substitution principle, see
   http://en.wikipedia.org/wiki/Liskov_substitution_principle
is that as Partens.Parent implicitely (by not being limited) provides 
certain primitve operations, such as ":=" and "=", and Stepchild.Object 
takes away these primitive operations, Stepchild.Object should not be in
Parents.Parent'Class, i.e.,
    Not_So_Well(Bob, Eve);
and even
    All_Well(Bob);
and
    All_Well(Eve);
ought to be a syntax error.

I think, it is a flaw that when calling Not_So_Well(X,Y) you need to 
know
   (a) of what type X and Y actually are (instead of just knowing that
       these are of type Parents.Parent'Class) and
   (b) and the implementation details of Not_So_Well (here the fact that
       it uses the assignment over Parents.Parent'Class).

What do you guys think about this?






-- 
Stefan Lucks      (moved to Bauhaus-University Weimar, Germany)
------  I  love  the  taste  of  Cryptanalysis  in  the  morning!  ------




^ permalink raw reply	[flat|nested] 81+ messages in thread

end of thread, other threads:[~2007-07-01  1:00 UTC | newest]

Thread overview: 81+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-05-23 19:47 Ada Interfaces and the Liskov Substitution Principle Stefan Lucks
2007-05-23 20:32 ` Ludovic Brenta
2007-05-23 22:00   ` Randy Brukardt
2007-05-24  0:56     ` Anh Vo
2007-05-24 18:27     ` Pascal Obry
2007-05-24 18:39       ` Dmitry A. Kazakov
2007-05-24 18:51         ` Pascal Obry
2007-05-24 22:44         ` Randy Brukardt
2007-05-24  6:57   ` Stefan Lucks
2007-05-23 20:54 ` Maciej Sobczak
2007-05-23 21:58   ` Randy Brukardt
2007-05-24  7:29     ` Maciej Sobczak
2007-05-24  8:02       ` Dmitry A. Kazakov
2007-05-24 12:58         ` Maciej Sobczak
2007-05-24 13:42           ` Dmitry A. Kazakov
2007-05-24 22:08           ` Robert A Duff
2007-07-01  1:00             ` David Thompson
2007-05-24 22:58           ` Randy Brukardt
2007-05-25  7:52             ` Maciej Sobczak
2007-05-25  8:21               ` Dmitry A. Kazakov
2007-05-25 20:27                 ` Maciej Sobczak
2007-05-26  7:48                   ` Dmitry A. Kazakov
2007-05-27  8:30                     ` Maciej Sobczak
2007-05-27 10:04                       ` Dmitry A. Kazakov
2007-05-29  8:03                         ` Maciej Sobczak
2007-05-29 13:18                           ` Dmitry A. Kazakov
2007-05-29 13:32                             ` Dmitry A. Kazakov
2007-05-29 15:34                             ` Maciej Sobczak
2007-05-29 17:07                               ` Dmitry A. Kazakov
2007-05-30  7:40                                 ` Maciej Sobczak
2007-05-30  8:43                                   ` Dmitry A. Kazakov
2007-05-30 12:54                                     ` Maciej Sobczak
2007-05-30 13:56                                       ` Dmitry A. Kazakov
2007-05-30 16:49                                         ` vgodunko
2007-05-30 20:52                                         ` Maciej Sobczak
2007-05-31  8:15                                           ` Dmitry A. Kazakov
2007-05-31 13:46                                             ` Maciej Sobczak
2007-06-01  7:29                                               ` Dmitry A. Kazakov
2007-06-01 13:32                                                 ` Maciej Sobczak
2007-06-01 14:53                                                   ` Dmitry A. Kazakov
2007-06-01 20:31                                                     ` Maciej Sobczak
2007-06-02  8:19                                                       ` Dmitry A. Kazakov
2007-06-02 16:49                                                         ` Maciej Sobczak
2007-06-03  7:09                                                           ` Dmitry A. Kazakov
2007-06-03 22:04                                                             ` Maciej Sobczak
2007-06-04  8:08                                                               ` Dmitry A. Kazakov
2007-06-04 17:02                                                                 ` Maciej Sobczak
2007-06-05  8:35                                                                   ` Dmitry A. Kazakov
2007-06-05 22:12                                                                     ` Maciej Sobczak
2007-06-06  8:21                                                                       ` Dmitry A. Kazakov
2007-06-06 14:46                                                                         ` Maciej Sobczak
2007-06-06 15:11                                                                           ` Maciej Sobczak
2007-06-06 15:32                                                                       ` Markus E Leypold
2007-05-24 10:42       ` Georg Bauhaus
2007-05-24 13:41         ` Dmitry A. Kazakov
2007-05-25 16:59         ` Markus E Leypold
2007-05-28  9:52           ` Georg Bauhaus
2007-05-28 11:50             ` Dmitry A. Kazakov
2007-05-28 23:32               ` Georg Bauhaus
2007-05-29 12:05                 ` Dmitry A. Kazakov
2007-05-29 13:33                 ` Georg Bauhaus
2007-05-29 17:29                   ` Dmitry A. Kazakov
2007-05-29 20:46                     ` Georg Bauhaus
2007-05-30  7:53                       ` Dmitry A. Kazakov
2007-05-30 13:18                       ` Georg Bauhaus
2007-05-31 10:27                         ` Dmitry A. Kazakov
2007-05-31 11:44                         ` Georg Bauhaus
2007-06-01  7:37                           ` Dmitry A. Kazakov
2007-06-01 10:07                             ` Markus E Leypold
2007-06-01 11:41                             ` Georg Bauhaus
2007-06-01 13:07                               ` Dmitry A. Kazakov
2007-05-28 13:47             ` Markus E Leypold
2007-05-28 23:12               ` Georg Bauhaus
2007-05-28 13:56             ` Markus E Leypold
2007-05-28 23:00               ` Georg Bauhaus
2007-05-24  7:39 ` Dmitry A. Kazakov
2007-05-24 11:12   ` Stefan Lucks
2007-05-24 13:56     ` Dmitry A. Kazakov
2007-05-24 14:41       ` Stefan Lucks
2007-05-24 15:46         ` Dmitry A. Kazakov
2007-05-24 15:00       ` Georg Bauhaus

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox