comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: Operation can be dispatching in only one type
Date: Tue, 17 Nov 2009 14:10:54 -0800 (PST)
Date: 2009-11-17T14:10:54-08:00	[thread overview]
Message-ID: <691d6892-bc5e-4d81-8025-c36556bf2593@13g2000prl.googlegroups.com> (raw)
In-Reply-To: 39kf90ha60px$.d7746cf5cx6h.dlg@40tude.net

On Nov 16, 2:28 pm, "Dmitry A. Kazakov" <mail...@dmitry-kazakov.de>
wrote:

>
> No, what I have in mind is that each time a new type is derived, we could
> somehow ensure that the whole row M+1 (from N x M+1) is filled either by a
> overriding or per a reasonable inheritance. The major problem is that we
> cannot see all Ns when we do M+1.
>
> The idea of deriving both types in one package looks like an attempt to
> control the places where we can expand the table N x M. That is not enough
> to enforce completeness of the table in the above sense.
>
> There should be something more strong, but also more liberal in terms of
> modularization. Obviously to derive from all types related via a shared
> primitive operation in one package is awful (and sometimes just impossible
> within the given parent-child hierarchy of packages).

....

> Yep, this is what I meant. The compiler should require to override *all*
> combinations:
>
>    type T2 is new T1 with ...;
>    overriding
>       procedure Some_Operation (X : T2; Y : T2);
>         -- Error!
>
>    overriding
>       procedure Some_Operation (X : T2; Y : T2);
>    overriding
>       procedure Some_Operation (X : T1; Y : T2);
>    overriding
>       procedure Some_Operation (X : T2; Y : T1);
>          -- OK
>
> Yes, this is tedious, but the programmer should know what we does when he
> declares a multi-method. This is the semantics of - there are n**2
> combinations which do potentially different things. If there are only n
> combinations, that is another case, which should *not* look MD, because it
> is not. I think it should somehow use tuples instead. The error should
> happen upon tuple construction, not in the call. For the reader it should
> be clear that there is only one tag. I have no idea how to spell that
> syntactically or by other means.

It appears to me that we're talking at cross-purposes.
I was trying to solve one particular class of problems;
you seem to be trying to solve the entire Multiple
Dispatch problem.  I believe that when I first thought
about making this proposal, I was assuming that a fully
general multiple dispatch wasn't ever going to be part
of the language, so I was thinking about an improvement
for one particular case I had run into.

Here's an example of the sort of problem I'm thinking
about: Suppose you want to define an abstract type that
represents a document, which could be a plain ASCII
text file, a Word document, a PDF file, etc., that all
have in common that they consist of sequences of
"characters" and "other elements of some sort".  Some
of the documents may also have formatting information
or other good stuff, but these won't be common to all
documents.

You want to write a package that deals with documents
and particularly with the text (characters) in the
documents.  The type of document (plain text, Word,
etc.) won't be known until run time.  Some of the
operations you want to provide are: search for given
text in the document, and return "markers" that point
to the beginning and end of the text that is found; and
move text from one point to another in the document.

A natural way to do this, it seems, would be to define
a Document as an abstract type, and let other packages
(Plaintext_Documents, Word_Documents, PDF_Documents,
etc.) define concrete types as extensions of that type
for plain text, Word documents, PDF documents, and so
on.  You also want to define, say, a Text_Region type
that represents a region of text in a document, and a
Marker type that represents a particular point in the
document text, between two characters.  The Text_Region
and Marker types should be abstract also, since we
don't know how they'd be implemented for different
document types; for a plain text document, a Marker is
probably just a string index, in essence, but the
Word_Documents and PDF_Documents might unpack a file
into some complex internal data structure when it reads
it, so a Marker for those document types might contain
pointers into the internal data structure, or
something.

So it seems natural to write the package like this
(along with other operations):

package Documents is

    type Document is abstract tagged private;
    type Text_Region is abstract tagged private;
    type Marker is abstract tagged private;

    function Search_For_Text (Doc : Document; Text : String)
               return Text_Region is abstract;
       -- I'm assuming here that it would return some sort of
       -- "no region" value if the text is not found
    function Beginning_Of (Reg : Text_Region) return Marker
       is abstract;
    function End_Of (Reg : Text_Region) return Marker
       is abstract;

    procedure Move_Text (Doc  : in out Document;
                         From : in Text_Region;
                         To   : in Marker)
       is abstract;

end Documents;

The last would cut the text represented by the From
region and move it to the point specified by "To".  (It
would be up to the packages to figure out how to move
formatting information or other information in an
appropriate way.)

Of course, Move_Text can't be declared because it is a
primitive operation of more than one tagged type.
Actually, none of the functions can be declared,
either, but I want to focus on the Move_Text procedure.
You might want to call Move_Text with a
Word_Documents.Document, Word_Documents.Text_Region,
and Word_Documents.Marker, or similarly with three
objects all with types from the PDF_Documents or
Plaintext_Documents package, but you would never want
to mix objects from two different packages.  And that
was the purpose of the proposal I was thinking of
making: to allow for restricted use of multiple
dispatch for cases like this (and this is a situation I
believe I've run into more than once), in which
Move_Text could be declared but could only be called if
the parameters' specific types all come from the same
package, checked at runtime if necessary.  The
Move_Text procedure would be inherited in each of the
other packages (Plaintext_Documents, etc.), with types
from that package, but there would not be any inherited
Move_Text that would involve types from different
packages.  (This was an idea I first thought about a
long time ago, but eventually abandoned; I really
hadn't thought about how to deal with the functions.)

There are workarounds, of course.  As I responded to
the original poster in this thread, you can make some
of the parameters to Move_Text class-wide, and the
concrete procedures would check to make sure the actual
tags for the class-wide parameters are correct---or
just doing a type conversion will do the check:

package body Word_Documents is

    overriding
    procedure Move_Text (Doc  : in out Document;
                         From : in Documents.Text_Region'Class;
                         To   : in Documents.Marker'Class) is
       X_From : Text_Region renames Text_Region(From);
       X_To   : Marker renames Marker(To);
       ...

I think that's correct.  Anyway, those will raise
Constraint_Error if the parameters are from the wrong
package.

However, I consider that to be a workaround (maybe
others don't think it's that bad), which is why I was
thinking about making this proposal---to make things
work more "naturally", without a workaround, in this
sort of situation that I've run into multiple times.
I'm not sure what your idea is trying to solve (other
than "making multiple dispatch perfect"); but even if
it does make general multiple dispatch available, your
way of doing things would not solve anything in this
sort of case, because you would require Move_Text to be
overridden for every possible combination of children
of the types, which makes it unusable and means that
even with your Grand Solution To Everything, those of
us who want to implement something like the above would
have to go back to using the workaround.

That's why I think we're not on the same page---we're
trying to solve two different and unrelated problems
that seem only superficially related.  Maybe we can't
solve them both.

                                  -- Adam





  reply	other threads:[~2009-11-17 22:10 UTC|newest]

Thread overview: 132+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-11-13 20:12 Operation can be dispatching in only one type xorque
2009-11-13 20:34 ` Dmitry A. Kazakov
2009-11-13 20:43   ` xorque
2009-11-13 21:14     ` Dmitry A. Kazakov
2009-11-13 20:44   ` xorque
2009-11-16 17:43 ` Adam Beneschan
2009-11-16 20:28   ` Dmitry A. Kazakov
2009-11-16 20:32     ` Dmitry A. Kazakov
2009-11-16 21:35     ` Adam Beneschan
2009-11-16 22:28       ` Dmitry A. Kazakov
2009-11-17 22:10         ` Adam Beneschan [this message]
2009-11-18  9:46           ` Dmitry A. Kazakov
2009-11-18 16:39             ` Adam Beneschan
2009-11-18 19:21               ` Dmitry A. Kazakov
2009-11-19  0:27                 ` Randy Brukardt
2009-11-19  2:11                   ` Robert A Duff
2009-11-19 15:57                     ` Adam Beneschan
2009-11-19 19:39                       ` Robert A Duff
2009-11-19 23:43                         ` Randy Brukardt
2009-11-19  8:50                   ` Dmitry A. Kazakov
2009-11-19 23:54                     ` Randy Brukardt
2009-11-20  8:34                       ` Dmitry A. Kazakov
2009-11-20 10:58                         ` Jean-Pierre Rosen
2009-11-21  6:02                         ` Randy Brukardt
2009-11-21 13:07                           ` Dmitry A. Kazakov
2009-11-22  5:45                         ` xorque
2009-11-22 11:25                           ` Georg Bauhaus
2009-11-22 11:30                             ` xorque
2009-11-22 16:25                             ` Dmitry A. Kazakov
2009-11-22 16:27                               ` xorque
2009-11-22 16:42                                 ` Dmitry A. Kazakov
2009-11-22 16:52                                   ` xorque
2009-11-22 17:41                                     ` Dmitry A. Kazakov
2009-11-22 18:03                                       ` xorque
2009-11-22 18:08                                         ` xorque
2009-11-22 18:28                                         ` Dmitry A. Kazakov
2009-11-22 18:41                                           ` xorque
2009-11-22 21:47                                           ` Robert A Duff
2009-11-23  3:42                                             ` stefan-lucks
2009-11-30 20:36                                               ` Robert A Duff
2009-11-30 23:54                                                 ` (see below)
2009-12-01 12:13                                                 ` Georg Bauhaus
2009-12-01 12:23                                                   ` Georg Bauhaus
2009-12-01 12:44                                                     ` Georg Bauhaus
2009-12-01 13:48                                                   ` Dmitry A. Kazakov
2009-12-01 15:02                                                     ` Georg Bauhaus
2009-12-01 16:18                                                       ` Dmitry A. Kazakov
2009-12-01 17:52                                                         ` Georg Bauhaus
2009-12-01 18:47                                                           ` Dmitry A. Kazakov
2009-12-01 21:53                                                             ` John B. Matthews
2009-12-02  0:32                                                               ` Georg Bauhaus
2009-12-02 11:18                                                                 ` John B. Matthews
2009-12-02 14:29                                                                   ` Jean-Pierre Rosen
2009-12-02 15:35                                                                     ` Georg Bauhaus
2009-12-02  1:13                                                             ` Georg Bauhaus
2009-12-02  9:07                                                               ` Dmitry A. Kazakov
2009-12-02 12:35                                                                 ` John B. Matthews
2009-12-02 13:35                                                                   ` Dmitry A. Kazakov
2009-12-03  5:23                                                                   ` Randy Brukardt
2009-12-03 20:21                                                                     ` John B. Matthews
2009-12-03  5:29                                                                 ` Randy Brukardt
2009-12-03 11:24                                                                   ` Georg Bauhaus
2009-12-03 23:08                                                                     ` Randy Brukardt
2009-12-04  8:52                                                                       ` Dmitry A. Kazakov
2009-12-05  2:45                                                                         ` Randy Brukardt
2009-12-05 10:32                                                                           ` Dmitry A. Kazakov
2009-12-08  0:19                                                                             ` Randy Brukardt
2009-12-08  4:30                                                                               ` stefan-lucks
2009-12-08  9:12                                                                                 ` Dmitry A. Kazakov
2009-12-10  4:09                                                                                   ` Randy Brukardt
2009-12-11  0:10                                                                                 ` Robert A Duff
2009-12-08  9:22                                                                               ` Dmitry A. Kazakov
2009-12-08 10:06                                                                                 ` Georg Bauhaus
2009-12-08 10:23                                                                                   ` Dmitry A. Kazakov
2009-12-08 10:33                                                                                     ` Georg Bauhaus
2009-12-08 10:49                                                                                       ` Dmitry A. Kazakov
2009-12-01 23:51                                                   ` Randy Brukardt
2009-11-23  8:52                                             ` Dmitry A. Kazakov
2009-11-30 20:43                                               ` Robert A Duff
2009-12-01  9:00                                                 ` Dmitry A. Kazakov
2009-12-01  5:45                                                   ` stefan-lucks
2009-12-01 11:12                                                     ` Dmitry A. Kazakov
2009-12-01  8:01                                                       ` stefan-lucks
2009-12-01 13:37                                                         ` Dmitry A. Kazakov
2009-12-15 23:54                                                         ` Robert A Duff
2009-11-23  7:48                                         ` Georg Bauhaus
2009-11-23  7:58                                           ` Georg Bauhaus
2009-11-19 16:04                 ` Adam Beneschan
2009-11-19  2:23           ` tmoran
2009-11-19  8:32             ` Dmitry A. Kazakov
  -- strict thread matches above, loose matches on Subject: below --
2015-11-23 10:23 operation " Serge Robyns
2015-11-23 11:29 ` Dmitry A. Kazakov
2015-11-23 13:05   ` Serge Robyns
2015-11-23 13:48     ` Dmitry A. Kazakov
2015-11-23 14:16       ` Serge Robyns
2015-11-23 14:59         ` G.B.
2015-11-23 15:52         ` Dmitry A. Kazakov
2015-11-23 17:40 ` Jeffrey R. Carter
2015-11-24  9:08   ` Serge Robyns
2015-11-24 16:44     ` AdaMagica
2015-11-24 17:09     ` Jeffrey R. Carter
2015-11-24 18:37       ` Serge Robyns
2015-11-24 20:18         ` Jeffrey R. Carter
2015-11-24 20:40           ` Serge Robyns
2015-11-24 20:25       ` Niklas Holsti
2015-11-24 21:48         ` Jeffrey R. Carter
2015-11-25  8:24           ` Dmitry A. Kazakov
2015-11-25 11:22             ` Serge Robyns
2015-11-25 17:38               ` Dmitry A. Kazakov
2015-11-26 11:30                 ` Serge Robyns
2015-11-26 13:14                   ` Dmitry A. Kazakov
2015-11-26 14:27                     ` Serge Robyns
2015-11-26 15:16                       ` J-P. Rosen
2015-11-26 18:27                         ` Serge Robyns
2015-11-26 21:20                           ` J-P. Rosen
2015-11-27  8:37                             ` Dmitry A. Kazakov
2015-11-27 12:58                               ` J-P. Rosen
2015-11-27 13:39                                 ` Dmitry A. Kazakov
2015-11-30 22:22                                   ` Randy Brukardt
2015-12-01  8:46                                     ` Dmitry A. Kazakov
2015-12-01 11:19                                       ` G.B.
2015-12-01 13:56                                         ` Dmitry A. Kazakov
2015-12-01 16:05                                           ` G.B.
2015-12-01 17:58                                             ` Dmitry A. Kazakov
2015-12-02 13:06                                               ` G.B.
2015-12-02 13:31                                                 ` Dmitry A. Kazakov
2015-12-02 19:33                                           ` Randy Brukardt
2015-12-02 19:27                                       ` Randy Brukardt
2015-11-29 17:59                     ` Jacob Sparre Andersen
2015-11-30 22:29                       ` Randy Brukardt
2015-11-25 12:27             ` G.B.
2015-11-25 17:25               ` Dmitry A. Kazakov
replies disabled

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