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=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,e5eb8ca5dcea2827 X-Google-Attributes: gid103376,public From: Brian Rogoff Subject: Re: Ada OO Mechanism Date: 1999/05/28 Message-ID: #1/1 X-Deja-AN: 483357654 References: <7i05aq$rgl$1@news.orbitworld.net> <7i17gj$1u1k@news2.newsguy.com> <7icgkg$k4q$1@nnrp1.deja.com> <3749E9EC.2842436A@aasaa.ofe.org> <7id2eo$fag@drn.newsguy.com> <3749FF7D.F17CE16A@aasaa.ofe.org> <374AC676.F7AE0772@lmco.com> <374F1DD3.64070C3E@mitre.org> Content-Type: TEXT/PLAIN; charset=US-ASCII X-Trace: nntp1.ba.best.com 927952326 213 bpr@206.184.139.136 MIME-Version: 1.0 Newsgroups: comp.lang.ada Date: 1999-05-28T00:00:00+00:00 List-Id: On Fri, 28 May 1999, Robert I. Eachus wrote: > Hyman Rosen wrote: > > Once again, I will ask for an example demonstrating this, where > > something is more complicated to use in C++ generics than in Ada. > > This will make you the fourth person I have asked for this type > > of example, the others being Richard D. Riehle, Samuel Mize, and > > Robert Dewar. None of the others have chosen to post such an > > example. > > I've done one example (probably with a few typos and thinkos since > it is Friday afternoon), I'm not going to do this one too. The reason > no one is anxious to demonstrate this is that reasonable examples take > several pages of code in Ada, and of course lots more in C++. I think that there are a few examples of reasonably short length, going in each direction (Ada > C++ and C++ > Ada). I thought one of the original preconditions was OO, but if its just generics, then the example Richard O'Keefe presented a few years ago during one of the downward closure discussions where he used Ada generics to simulate downward closures is probably a good "pro-Ada" one, though I think the intent of the original example was to show how bad Ada came off against Pascal and Scheme. Note that using generics this way doesn't even really do a good job compared to what you could do with downward closures; your Integrate_2D wouldn't have to be tied to a particular Integrate_1D, you could just pass it in as an argument. Any nice way around that problem, besides using GNAT specific features? Anyways, I don't think this translates easily to C++, but if Hyman or someone else can surprise me I'd be delighted. -- First we write a simple 1-D integrator using the trapezoid rule generic type Float_Type is digits; with function F(X : Float_Type) return Float_Type; function Integrate_1D(Lower, Upper: Float_Type; Steps : Positive) return Float_Type; -- Tail recursive trapezoid rule, a tip o' the hat to Schemers! function Integrate_1D(L1, U1: Float_Type; Steps : Positive) return Float_Type is Dx : constant Float_Type := (Upper - Lower) / Float_Type(Steps); function Iterate( Step : Positive; Value : Float_Type ) return Float_Type is begin if Step = Steps then return DX * Current_Value; else return Iterate( Step + 1, Value + F(Lower + Float_Type(Step) * Dx)); end if; end Iterate; begin Iterate(1, .5 * F(Lower) + F(Upper)); end; -- The 2-D integration spec generic with function F(X, Y : Float_Type) return Float_Type; function Integrate_2D(L1, U1, L2, U2: Float_Type; S1, S2 : Positive) return Float_Type; -- How I wish Ada 95 had real downward closures!!! function Integrate_2D(L1, U1, L2, U2: Float_Type) return Float_Type is function Outer_Integrand(Y: Float_Type) return Float_Type is function Inner_Integrand(X: Float_Type) return Float_Type is begin return F(X, Y); end Inner_Integrand; function Inner_Integrator is new Integrate_1D(F => Inner_Integrand, Steps => S1); begin return Inner_Integrator(L1, U1); end Outer_Integrand; function Outer_Integrator is new Integrate_1D(F => Outer_Integrand, Steps => S2); begin return Outer_Integrator(L2, U2); end Integrate_2D; -- Brian