comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Proposed change to BC iterator parameters
Date: Fri, 19 Dec 2003 11:53:02 +0100
Date: 2003-12-19T11:53:02+01:00	[thread overview]
Message-ID: <brukuo$7il7a$1@ID-77047.news.uni-berlin.de> (raw)
In-Reply-To: wcczndqc7t8.fsf@shell01.TheWorld.com

Robert A Duff wrote:

> "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> writes:
> 
>> Robert A Duff wrote:
>> 
>> > I'm still pushing for downward closures to be added to the language,
>> > which would add at least a minimal amount of support for iterators,
>> > and greatly simplify this and other data-structure proposals.
>> 
>> Wouldn't subroutine types do the trick? I wonder why Ada does not have
>> them.
> 
> By "downward closures", I mean the ability to pass more-nested
> procedures as parameters to less-nested procedures.  If that's what
> you mean by "subroutine types", then yes.

Yes. A subroutine object will be of course passed in "IN" mode, so there
will be no way to get an access object from it and store that non-locally,
which was the main concern caused the accessibility rules.

type Func is function (X : Float) return Float;
procedure Integrate (F : Func; X1, X2 : Float) return Float;

> Access-to-subprogram types don't do the trick because of the
> accessibility rules.

Right. However, there was an idea to have limited access types, which would
probably do it. But I think that to have subroutine types is more cleaner
and in Ada spirit.

> Hence the extra "context" parameters in
> Jeff Carter's iterators.
> 
> I also would like the ability to have anonymous procedures, like lambda
> in Lisp, or blocks in Smalltalk.  The idea is to pass the body of a loop
> as a parameter to the iterator, and I don't want to have to give a
> *name* to every loop body, and I want to write it *inside* the loop
> (i.e. inside the call to the iterator).

This should work with subroutine types. We only need to invent some syntax
sugar for "subroutine literals".

type Item is ...;
type Container is ...;
type Action is procedure (I : Item);
procedure Enumerate (X : Container; For_All : Action);
procedure Print (I : Item; Output : in out Device);
...
procedure Print (X : Container; Output : in out Device) is
begin
   Enumerate (X, Action'(Print (I, Device);));
-- or maybe
   Enumerate (X, procedure (I : Item) is begin Print (I, Device); end;);
end Print;

However mixing statements and expressions looks awful to my taste.

> I also want the ability to jump out of those anonymous procedures, to
> exit the loop, without passing extra parameters around.  The extra
> parameter is bad because it forces loops that *don't* want to exit
> prematurely to worry about whether they might want to exit prematurely.

If you have an ability to put a subroutine body in an expression then gotos
out of it are for free:

procedure Print
          (  X : Container;
             Output : in out Device;
             Count : Natural
          )  is
   To_Print : Natural := Count;
begin
   Enumerate
   (  X,
      procedure (I : Item) is
      begin
         if To_Print > 0 then
            Print (I, Device);
            To_Print := To_Print - 1;
         else
            goto Done; -- Done is visible here
         end if;
      end;
   );
<<Done>> null;
end Print;

But you will probably need a return-from-nested-subprogram statement (like
we have for exit):

function Search (X : Container; Key : Item_Key) return Item is
begin
   Enumerate
   (  X,
      procedure (I : Item) is
      begin
         if Get_Key (I) = Key then
            return I out Search; -- Return through all nested contexts
         end if;
      end;
   );
   raise Nothing_Found;
end Search;

> I don't expect to see any of this in Ada, ever, except perhaps for
> downward closures.  There is an AI on downward closures, but I don't
> remember it's status.  I hope it gets in.  It's the only case where Ada
> is inferior in power to Pascal.

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



  reply	other threads:[~2003-12-19 10:53 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-12-10 13:36 Proposed change to BC iterator parameters amado.alves
2003-12-10 16:47 ` Proposed change to BC iterator parameters [limitedness] Georg Bauhaus
2003-12-10 17:39 ` Proposed change to BC iterator parameters Martin Krischik
2003-12-10 18:22 ` Jeffrey Carter
2003-12-10 23:00   ` Robert A Duff
2003-12-11  1:00     ` Jeffrey Carter
2003-12-11 15:09       ` Robert A Duff
2003-12-11  8:33     ` Dmitry A. Kazakov
2003-12-10 20:50 ` Simon Wright
2003-12-10 23:12 ` Robert A Duff
2003-12-11  5:07   ` Steve
2003-12-11 15:24     ` Robert A Duff
2003-12-11 17:39       ` Jeffrey Carter
2003-12-12 22:22         ` Robert A Duff
2003-12-13  0:57           ` Jeffrey Carter
2003-12-17 20:59             ` Robert A Duff
2003-12-18 10:05               ` Dmitry A. Kazakov
2003-12-18 18:14                 ` Robert A Duff
2003-12-19 10:53                   ` Dmitry A. Kazakov [this message]
2003-12-19 16:17                     ` Georg Bauhaus
2003-12-19 17:19                       ` Dmitry A. Kazakov
2003-12-19 22:51                         ` Robert A Duff
2003-12-20 12:20                           ` Dmitry A. Kazakov
2003-12-19 22:47                       ` Robert A Duff
2003-12-20  2:11                         ` Stephen Leake
2003-12-20 19:08                         ` Robert I. Eachus
2003-12-21 11:39                           ` Simon Wright
2003-12-21 18:13                             ` Robert I. Eachus
2003-12-21 13:58                           ` Dmitry A. Kazakov
2003-12-22  1:25                             ` Robert I. Eachus
     [not found]         ` <916oa1-c93.ln1@beastie.ix.netcom.com>
2003-12-13 16:57           ` Simon Wright
2003-12-12  5:29     ` Simon Wright
2003-12-12 22:26       ` Robert A Duff
2003-12-13 16:55         ` Simon Wright
2003-12-13 17:27           ` Dmitry A. Kazakov
2003-12-13  2:44       ` Steve
  -- strict thread matches above, loose matches on Subject: below --
2003-12-23 10:40 amado.alves
2003-12-19 15:53 amado.alves
2003-12-19 23:05 ` Robert A Duff
2003-12-11 16:02 amado.alves
2003-12-11 15:05 ada_wizard
2003-12-11 16:45 ` Robert A Duff
2003-12-11 12:56 amado.alves
2003-12-17 20:25 ` Robert A Duff
2003-12-11 12:43 amado.alves
2003-12-11 12:33 amado.alves
2003-12-10 14:39 amado.alves
2003-12-10  5:46 Simon Wright
2003-12-10 18:12 ` Jeffrey Carter
2003-12-11 16:10   ` Martin Krischik
2003-12-10 20:59 ` Simon Wright
replies disabled

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