comp.lang.ada
 help / color / mirror / Atom feed
* An improved Ada?
@ 2004-09-27 19:38 jn
  2004-09-27 20:40 ` Matthew Heaney
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: jn @ 2004-09-27 19:38 UTC (permalink / raw)


Hi,

after spending numerous hours to do a simple double linked list
package - yes, I have seen charles - a 5 hours tedious car drive came
up with the following example of an improved Ada language:
  - prefix notation (as for tasks and protected types)
  - Ada OO syntax in line with tasks and protected objects
  - reference types 
  - controlled, allocates from Storage_Pools.Garbage_Collected :)
  - implicit up- and down-conversions

Here is my small example, based on the simset-library from Simula, not
complete but it should be easy for you to fill out the rest:)

package Simset is
   tagged type List_Type is private
   begin
      function First return Link_Type;
   end List_Type;

   tagged type Link_Type is private
   begin
      function Next return Link_Type;
   end Link_Type;
private
   tagged type Linkage_Type is
      Suc, Pred : Linkage;
   begin
      function Next return Linkage_Type;
      procedure Into (List : List_Type);
      procedure Initialize;
   end Linkage_Type;

   tagged type List_Type is new Linkage_Type with
   begin
      null;
   end List_Type;

   tagged type Link_Type is new Linkage_Type with
   begin
      null;
   end Link_Type;
end Simset;

package body Simset is
   tagged body Linkage_Type is
      function Next return Linkage_Type is
      begin
	if not Suc in List_Type then
           return Suc;
        else
           return null;
      end Suc;

      procedure Initialize is
      begin
         Suc  := This;
         Pred := This;
      end Initialize;
   end List_Type; 
 
   tagged body List_Type is
      function First return Link_Type is
      begin
	return Next;
      end First;
   end List_Type;
  
   tagged body Link_Type is
      function Next return Link_Type is
      begin
	return Linkage_Type(This).Next;
      end Suc;

      procedure Into (List : List_Type) is
      begin
          -- Unlink; -- remove from whichever list This is in
          -- manipulate pointers to put This last into List
      end Into;
   end List_Type;
end Simset;

-- Simset example usage

with Simset and use;
with Book_Intrinsics rename as BI;

procedure Example is
   tagged Book_Shelf is new List_Type;
   tagged Shopping_Cart is new List_Type;

   tagged Book_In_Shelf is new Link_Type with
      �uthor : BI.Author_Type;
      Title  : BI.Title_Type;
   end Book_In_Shelf;

   function Contains_Ada_In_Title (Title : BI.Title_Type) return
Boolean is
   begin ... end; 
   -- not part of example
   
   Shelf      : Book_Shelf := new Book_Shelf;
   Book, Buy  : Book_In_Shelf;
   Cart       : Shopping_Cart := new Shopping_Cart;
begin
   -- populate Shelf with books, not part of example

   Book := Shelf.First,
   while Book /= null loop
      if Ada_In_Title (Book.Title) then
         Buy := Book;
         Book := Book.Next
         Buy.Into (Cart);
      else
         Book := Book.Next;
      end if;
   end loop;
   -- pay for books in cart
end Example;



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

* Re: An improved Ada?
  2004-09-27 19:38 An improved Ada? jn
@ 2004-09-27 20:40 ` Matthew Heaney
  2004-09-28 20:41   ` Wojtek Narczynski
  2004-09-28  5:45 ` Matthew Heaney
  2004-09-29 17:05 ` jn
  2 siblings, 1 reply; 10+ messages in thread
From: Matthew Heaney @ 2004-09-27 20:40 UTC (permalink / raw)



"jn" <jonas.nygren@telia.com> wrote in message
news:311c6b78.0409271138.1795d07c@posting.google.com...
>
> after spending numerous hours to do a simple double linked list
> package - yes, I have seen charles - a 5 hours tedious car drive came
> up with the following example of an improved Ada language:
>   - prefix notation (as for tasks and protected types)
>   - Ada OO syntax in line with tasks and protected objects
>   - reference types
>   - controlled, allocates from Storage_Pools.Garbage_Collected :)
>   - implicit up- and down-conversions

Distinguished-receiver syntax is being added to the language, for tagged
types.

  L : List;
begin
  L.Append (42);

This is largely the reason why we made the AI-302 container types publicly
tagged.

Tagged types and limited types are already pass-by-reference, so usually you
don't need explicit reference types (a la C++), since the pass-by-reference
happens automatically.

The fact that the type is publicly tagged means that you're allowed to alias
parameters:

procedure Op (L : in out List) is
begin
   ... L'Access ...; --ok
end;

For class-wide types, the up-conversion happens automatically, since the
ancester class-wide type "covers" its descendent, e.g.

type T is tagged null record;
type NT is new T with null record;

procedure Op (O : T'Class);

declare
   O : NT;
begin
   Op (O);  --ok
end;

All the AI-302 containers (including the list) are controlled, so memory
management ("garbage collection") is automatic.

Not sure what your issue is, I guess...





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

* Re: An improved Ada?
  2004-09-27 19:38 An improved Ada? jn
  2004-09-27 20:40 ` Matthew Heaney
@ 2004-09-28  5:45 ` Matthew Heaney
  2004-09-29 17:05 ` jn
  2 siblings, 0 replies; 10+ messages in thread
From: Matthew Heaney @ 2004-09-28  5:45 UTC (permalink / raw)


jonas.nygren@telia.com (jn) writes:

> Here is my small example, based on the simset-library from Simula, not
> complete but it should be easy for you to fill out the rest:)

I don't understand what your problem is.  It looks like you need a list
whose elements have type Book_In_Shelf.  What's wrong with:

   package Book_Lists is
     new Ada.Containers.Doubly_Linked_Lists (Book_In_Shelf); 

If you need to move an element from one list onto another, then just use
the Splice operation for lists.

If you need a list whose elements have a class-wide type, then just use
the indefinite list.





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

* Re: An improved Ada?
  2004-09-27 20:40 ` Matthew Heaney
@ 2004-09-28 20:41   ` Wojtek Narczynski
  2004-09-28 22:54     ` Randy Brukardt
  0 siblings, 1 reply; 10+ messages in thread
From: Wojtek Narczynski @ 2004-09-28 20:41 UTC (permalink / raw)


On Mon, 27 Sep 2004 16:40:40 -0400, Matthew Heaney wrote:

> This is largely the reason why we made the AI-302 container types publicly
> tagged.

In that case, let me ask. If they are publicly tagged now, how about
interfaces as of AI-00251 on them?

Regards,
Wojtek



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

* Re: An improved Ada?
  2004-09-28 20:41   ` Wojtek Narczynski
@ 2004-09-28 22:54     ` Randy Brukardt
  0 siblings, 0 replies; 10+ messages in thread
From: Randy Brukardt @ 2004-09-28 22:54 UTC (permalink / raw)


"Wojtek Narczynski" <wojtek@power.com.pl> wrote in message
news:pan.2004.09.28.20.41.14.508631@power.com.pl...
> On Mon, 27 Sep 2004 16:40:40 -0400, Matthew Heaney wrote:
>
> > This is largely the reason why we made the AI-302 container types
publicly
> > tagged.
>
> In that case, let me ask. If they are publicly tagged now, how about
> interfaces as of AI-00251 on them?

We didn't do that, because they would have to be declared inside the generic
units and be specific to the element type. That would make them pretty much
useless; you can get the same functionality by deriving from the container
type, and it would be much easier to write as well (you wouldn't have to
define dozens of wrappers).

For interfaces to really work, the element type would have to be an
interface. But that would be too weird for most users (requiring the
understanding of a new feature before being able to use containers), would
have suboptimal performance (because of all of the dispatching for trivial
operations) and it wouldn't work well for making containers of existing
types (especially elementary types).

                                      Randy.








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

* Re: An improved Ada?
  2004-09-27 19:38 An improved Ada? jn
  2004-09-27 20:40 ` Matthew Heaney
  2004-09-28  5:45 ` Matthew Heaney
@ 2004-09-29 17:05 ` jn
  2004-09-29 18:26   ` Matthew Heaney
  2004-09-29 21:33   ` Randy Brukardt
  2 siblings, 2 replies; 10+ messages in thread
From: jn @ 2004-09-29 17:05 UTC (permalink / raw)


I got into a container discussion which was not the intention.

I want improved OO capabilities in Ada. The way things are results
in having to 'with' base class packages because not all methods seems
to be inherited and also a lot of meaningless safe type conversions that
should have been done by the compiler (my opinion) instead of the programmer.

I have made a streamlined example to show my point - it gets complicated
and difficult to understand even in these few lines of code, in large
code bases it must be a nightmare. Are there really any codebases that
applies 'tagged' types and type extension on a larger scale. Not just
wrapping it up in generics.

My small example plus detailed questions:

package A is
   type X is tagged private; 
   type Y is new X with private; 
   type Z is new X with private; 

   type X_Ref is access all X'Class; 
   type Y_Ref is access all Y'Class; 
   type Z_Ref is access all Z'Class; 

   function X_A (Xa : access X) return Y_Ref; 
   function Z_A (Zv : Z) return Y_Ref; 
private
   type X is tagged 
      record 
         Xr : X_Ref;  
      end record; 
   type Y is new X with null record; 
   type Z is new X with null record; 
end A;

package body A is
   function X_A (Xa : access X) return Y_Ref is
      begin return null; end; 
   function Z_A (Zv : Z) return Y_Ref is
      begin return null; end; 
end A;

with A;
package B is
   type Y is new A.Y with private; 
   type Y_Ref is access all Y'Class; 
   type Z is new A.Z with private; 
   
   function Y_A (
         Ya : access Y ) 
     return Integer; 
private
   type Y is new A.Y with 
      record 
         Value : Integer;  
      end record; 
   type Z is new A.Z with null record; 
end B;

with A;
with B;
procedure T is 
   Z  : B.Z;  
   Yr : B.Y_Ref;  
begin
   Yr := B.Y_Ref ( B.Z_A (Z) );
   Yr := B.Y_Ref (A.X_A (A.X_Ref (Yr)));
   -- why not:
   --    Yr := B.Z_A (Z);
   -- B.Y is a descendant of A.Y that is a descendant of A.X
   -- ret value 'tag' could be checked against Yr tag
   -- why don't the compiler do it, why do I have to do it explicitly
   --
   -- why not:
   --    Yr := B.X_A (Yr);
   -- again, B.Y is a descendant of A.Y that is a descendant of A.X
   -- why is not B.X_A visible, X_A should have been inherited
   -- when A.X_A (Yr) is called, why is not the called dispatched?
   --     I have to convert B.Y_Ref to A.X_Ref explicitly
   -- and again I explicitly have to convert return type
   --     from A.X_Ref to B.Y_Ref, a completely safe conversion

end T;



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

* Re: An improved Ada?
  2004-09-29 17:05 ` jn
@ 2004-09-29 18:26   ` Matthew Heaney
  2004-09-29 21:33   ` Randy Brukardt
  1 sibling, 0 replies; 10+ messages in thread
From: Matthew Heaney @ 2004-09-29 18:26 UTC (permalink / raw)



"jn" <jonas.nygren@telia.com> wrote in message
news:311c6b78.0409290905.15ae255d@posting.google.com...
>
>
> package A is
>    type X is tagged private;
>    type Y is new X with private;
>    type Z is new X with private;
>
>    type X_Ref is access all X'Class;
>    type Y_Ref is access all Y'Class;
>    type Z_Ref is access all Z'Class;
>
>    function X_A (Xa : access X) return Y_Ref;
>    function Z_A (Zv : Z) return Y_Ref;
> private
>    type X is tagged
>       record
>          Xr : X_Ref;
>       end record;
>    type Y is new X with null record;
>    type Z is new X with null record;
> end A;
>
> package body A is
>    function X_A (Xa : access X) return Y_Ref is
>       begin return null; end;
>    function Z_A (Zv : Z) return Y_Ref is
>       begin return null; end;
> end A;
>
> with A;
> package B is
>    type Y is new A.Y with private;
>    type Y_Ref is access all Y'Class;
>    type Z is new A.Z with private;
>
>    function Y_A (
>          Ya : access Y )
>      return Integer;
> private
>    type Y is new A.Y with
>       record
>          Value : Integer;
>       end record;
>    type Z is new A.Z with null record;
> end B;
>
> with A;
> with B;
> procedure T is
>    Z  : B.Z;
>    Yr : B.Y_Ref;
> begin
>    Yr := B.Y_Ref ( B.Z_A (Z) );
>    Yr := B.Y_Ref (A.X_A (A.X_Ref (Yr)));
>    -- why not:
>    --    Yr := B.Z_A (Z);
>    -- B.Y is a descendant of A.Y that is a descendant of A.X
>    -- ret value 'tag' could be checked against Yr tag
>    -- why don't the compiler do it, why do I have to do it explicitly

Well, obviously this is wrong.  Operation B.Z_A returns type A.Y_Ref, not
B.Y_Ref.



>    --
>    -- why not:
>    --    Yr := B.X_A (Yr);
>    -- again, B.Y is a descendant of A.Y that is a descendant of A.X
>    -- why is not B.X_A visible, X_A should have been inherited
>    -- when A.X_A (Yr) is called, why is not the called dispatched?
>    --     I have to convert B.Y_Ref to A.X_Ref explicitly
>    -- and again I explicitly have to convert return type
>    --     from A.X_Ref to B.Y_Ref, a completely safe conversion

Well, obviously this is wrong.  There is no operation X_A that returns type
B.Y_Ref declared in package B.  You must be confusing this with the
inherited operation X_A that returns type A.Y_Ref.






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

* Re: An improved Ada?
  2004-09-29 17:05 ` jn
  2004-09-29 18:26   ` Matthew Heaney
@ 2004-09-29 21:33   ` Randy Brukardt
  2004-09-29 22:15     ` tmoran
  1 sibling, 1 reply; 10+ messages in thread
From: Randy Brukardt @ 2004-09-29 21:33 UTC (permalink / raw)


"jn" <jonas.nygren@telia.com> wrote in message
news:311c6b78.0409290905.15ae255d@posting.google.com...
> I got into a container discussion which was not the intention.
>
> I want improved OO capabilities in Ada. The way things are results
> in having to 'with' base class packages because not all methods seems
> to be inherited and also a lot of meaningless safe type conversions that
> should have been done by the compiler (my opinion) instead of the
programmer.

All methods that are in the base package are inherited. You seem to be
expecting the named types that happen to use the type to be automatically
inherited as well, but these are separate types. To do that in general would
be a complete nightmare.

Ada 95 does have a problem with use clauses. Ada 2005 solves this with the
prefix call notation:

   Obj.Subprog (...)

I'd suggest that your example be written the way real programs are (one
tagged type per package), because you can cause problems simply by putting
multiple types in one package. I'd also ask that you add comments about what
you would expect to be inherited. Otherwise, we can't quite tell what you
have in mind.

> Are there really any codebases that
> applies 'tagged' types and type extension on a larger scale. Not just
> wrapping it up in generics.

Claw has about 75 tagged types derived in various chains from
Root_Window_Type (a tagged abstract type); each of those types resides in
its own package.

The Claw Builder has a similar structure mirroring that sort of derivation.

Virtually all type conversions that we had to write in both of those are not
"safe"; they include a check of some sort that we do not expect to fail. But
converting from Root_Window_Type'Class to some specific type clearly should
be marked in the code.

                       Randy.






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

* Re: An improved Ada?
  2004-09-29 21:33   ` Randy Brukardt
@ 2004-09-29 22:15     ` tmoran
  2004-09-30 16:55       ` tmoran
  0 siblings, 1 reply; 10+ messages in thread
From: tmoran @ 2004-09-29 22:15 UTC (permalink / raw)


>Claw has about 75 tagged types derived in various chains from
>Root_Window_Type (a tagged abstract type); each of those types resides in
>its own package.
1) For examples, download the $0 version of Claw at www.rrsoftware.com
2) A nit: Some tagged types are derived from Controlled, eg Socket_Type.



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

* Re: An improved Ada?
  2004-09-29 22:15     ` tmoran
@ 2004-09-30 16:55       ` tmoran
  0 siblings, 0 replies; 10+ messages in thread
From: tmoran @ 2004-09-30 16:55 UTC (permalink / raw)


>>Claw has about 75 tagged types derived in various chains from
>>Root_Window_Type (a tagged abstract type); each of those types resides in
>>its own package.
>2) A nit: Some tagged types are derived from Controlled, eg Socket_Type.
There are also tagged types other than Controlled or Root_Window_Type.



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

end of thread, other threads:[~2004-09-30 16:55 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-09-27 19:38 An improved Ada? jn
2004-09-27 20:40 ` Matthew Heaney
2004-09-28 20:41   ` Wojtek Narczynski
2004-09-28 22:54     ` Randy Brukardt
2004-09-28  5:45 ` Matthew Heaney
2004-09-29 17:05 ` jn
2004-09-29 18:26   ` Matthew Heaney
2004-09-29 21:33   ` Randy Brukardt
2004-09-29 22:15     ` tmoran
2004-09-30 16:55       ` tmoran

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