comp.lang.ada
 help / color / mirror / Atom feed
From: Dmitry A. Kazakov <mailbox@dmitry-kazakov.de>
Subject: Re: The "()" operator revisited.
Date: Mon, 19 Jan 2004 11:25:32 +0100
Date: 2004-01-19T11:25:32+01:00	[thread overview]
Message-ID: <ov7n00hpliaebfdrqp8mqb0j7os6daqhp6@4ax.com> (raw)
In-Reply-To: bu9urc$k4t$1@shell.monmouth.com

On 16 Jan 2004 19:15:40 -0500, ka@sorry.no.email (Kenneth Almquist)
wrote:

>Dmitry A. Kazakov wrote:
>> The problem is that without notion of abstract array as a class-wide
>> or generic parameter type, there will be no way to write
>> class-wide/generic subroutines taking advantage of indexing. Let I
>> have all containers of different kind having () indexing. This will
>> not help me. Because I will still be unable as little as just to write
>> a universal program searching max element in *a* container.
>
>You can write it.  The problem is that it's awkward to instantiate.
>
>    generic
>        type Collection is limited private;
>        type Index is (<>);
>        type Element is private;
>        with function "()"(C : Collection; I : Index) return Element is <>;
>        with function First(C : Collection) return Index is <>;
>        with function Last(C : Collection) return Index is <>;
>        with function ">"(Left, Right : Element) return Boolean is <>;
>        Element_First : Element;   -- Value of Element'first
>    function Max(C : Collection) return Element;

It is not an abstract array, it is an explicit specification of some
arbitrary interface. It is awkward because array interface is not a
simple thing. But what is worse, there is no way to ensure that thing
being designed is an array until an attempt to instantiate Max with
it. So your example just illustrates the starting point:

to have ()-operator /= to have abstract array

>    function Max(C : Collection) return Element is
>        Result : Element := First;
>    begin
>        for I in First(C) .. Last(C) loop
>            if C(I) > Result then
>                Result := C(I);
>            end if;
>        end loop;
>        return Result;
>    end Max;
>
>>> To get something that could match a generic array parameter would require
>>> a lot of restrictions on abstract arrays, in particular that the indice
>>> types would have to be abstract discrete types.
>>
>> No. We should simply go on and introduce abstract index type as a
>> counterpart of the formal discrete type:
>>
>> type X is (<>);
>>
>> One should be consequent. We need abstract discrete, integer, index,
>> numeric, string, array, access types. Have I forgot something? (:-))
>
>The problem with extending the Ada class system to cover user
>defined types is that the language specification is never going
>to include all the classes that user defined types might fall
>into.

Are you talking about classes in the sense of 'Class or generic formal
parameter classes?

My position, many disagree with, is that 'Class could suffice if all
types would have it + MI [at least in the form of abstract
interfaces.]

>Languages like Haskell get around this by allowing the
>programmer to define new classes.
>
>Ada already has a work-around for the lack of user-defined classes:
>default values for generic subprogram parameters.

Ada also have a direct and better way to specify interface conformity:

generic
   type Implementation is new Interface with private;

The problem is that this is for tagged types only.

>I that that the
>way to support user defined types which act like arrays is to
>(1) make array indexing be an operator, as was proposed at the start
>of this thread, (2) allow user defined types to have attributes such
>as 'first and 'last, and (3) allow the generic to specify that it's
>parameters must support specific attributes.  I'd also define
>attributes for array types which give the index and component types,
>and allow generic type parameters to have defaults.  With these
>changes, the Max generic becomes:
>
>    generic
>        type Collection is limited private;
>        type Index is (<>) := Collection'Index_Type;

You need no explicit Index if there is an attribute for that. Actually
Index is "renaming":

subtype Index is Collection'Index_Type;

>        type Element is private := Collection'Element_Type;
>        with function "()"(C : Collection; I : Index) return Element is <>;
>        with Collection'Object'First;

Attributes should be [primitive] operations as any other, so

with function 'First (C : Collection)
   return Collection'Index_Type is <>;

>        with Collection'Object'Last;
>        with function ">"(Left, Right : Element) return Boolean is <>;
>        with Element'First;

Unfortunately this does not tell what is the type of the result. Let
Element be an array? This is why abstract interfaces are needed. To
write Max one should *require* that Element is an ordered type having
the minimal value [ to be precise, Max can be written without
Element'First ] which differs from having Element with operations
having good-looking names and profiles.

>    function Max(C : Collection) return Element;
>
>The directive "wtih Element'First;" means that type Element provides
>the 'First attribute.  The directive "with Collection'Object'First;"
>means that objects of type Collection provide the 'First attribute.
>With these changes, the function Max can be sensibly instantiated
>by providing a single parameter specifying an array type.

--
Regards,
Dmitry A. Kazakov
www.dmitry-kazakov.de



  parent reply	other threads:[~2004-01-19 10:25 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-01-12 17:53 The "()" operator revisited Frank J. Lhota
2004-01-12 18:38 ` Frank J. Lhota
2004-01-12 22:26 ` Robert A Duff
2004-01-13 16:29   ` Frank J. Lhota
2004-01-13  9:24 ` Dmitry A. Kazakov
2004-01-13 16:44   ` Frank J. Lhota
2004-01-13 17:13     ` Hyman Rosen
2004-01-13 22:27     ` Randy Brukardt
2004-01-14  2:30     ` Stephen Leake
2004-01-14  9:04     ` Dmitry A. Kazakov
2004-01-17  0:15       ` Kenneth Almquist
2004-01-17 21:15         ` Robert A Duff
2004-01-19 10:25         ` Dmitry A. Kazakov [this message]
2004-01-13 13:13 ` Marin David Condic
2004-01-13 17:38   ` Warren W. Gay VE3WWG
2004-01-13 19:09     ` Robert A Duff
2004-01-15 17:30       ` Warren W. Gay VE3WWG
2004-01-15 18:11         ` Robert A Duff
2004-01-15 19:36           ` tmoran
2004-01-15 20:35             ` Robert A Duff
2004-01-17  5:48               ` Robert I. Eachus
2004-01-16  1:52           ` Redefining := (was: The "()" operator revisited.) Jeffrey Carter
2004-01-16 21:37             ` Randy Brukardt
2004-01-19 11:33               ` Dmitry A. Kazakov
2004-01-16  3:11           ` The "()" operator revisited Mark A. Biggar
2004-01-16 13:28             ` Hyman Rosen
2004-01-16 16:19             ` Robert A Duff
2004-01-16 18:09             ` Warren W. Gay VE3WWG
2004-01-16 13:56           ` Frank J. Lhota
2004-01-16 16:14             ` Robert A Duff
2004-01-16 21:29               ` Frank J. Lhota
  -- strict thread matches above, loose matches on Subject: below --
2004-01-13 17:46 amado.alves
2004-01-13 22:21 ` Randy Brukardt
2004-01-13 17:53 amado.alves
2004-01-14  9:09 ` Dmitry A. Kazakov
2004-01-14 12:55   ` Georg Bauhaus
2004-01-14 15:05     ` Dmitry A. Kazakov
2004-01-15  1:21       ` Georg Bauhaus
2004-01-15  8:50         ` Dmitry A. Kazakov
2004-01-15 11:09           ` Georg Bauhaus
2004-01-15 13:23             ` Dmitry A. Kazakov
2004-01-17  6:26               ` Robert I. Eachus
2004-01-14 13:04   ` Hyman Rosen
2004-01-14 15:22 amado.alves
2004-01-14 16:16 ` 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