From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,fa18fb47ddd229a7 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-12-19 02:46:50 PST Path: archiver1.google.com!news2.google.com!fu-berlin.de!uni-berlin.de!dialin-145-254-037-189.arcor-ip.NET!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: Proposed change to BC iterator parameters Date: Fri, 19 Dec 2003 11:53:02 +0100 Organization: At home Message-ID: References: Reply-To: mailbox@dmitry-kazakov.de NNTP-Posting-Host: dialin-145-254-037-189.arcor-ip.net (145.254.37.189) Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7Bit X-Trace: news.uni-berlin.de 1071830809 7951594 145.254.37.189 ([77047]) User-Agent: KNode/0.7.2 Xref: archiver1.google.com comp.lang.ada:3549 Date: 2003-12-19T11:53:02+01:00 List-Id: Robert A Duff wrote: > "Dmitry A. Kazakov" 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; ); <> 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