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.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,d74d61525ce986e9 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-10-22 08:22:47 PST Path: archiver1.google.com!news2.google.com!fu-berlin.de!uni-berlin.de!tar-alcarin.cbb-automation.DE!not-for-mail From: Dmitry A. Kazakov Newsgroups: comp.lang.ada Subject: Re: Implementing Memorize Date: Wed, 22 Oct 2003 17:23:44 +0200 Message-ID: References: NNTP-Posting-Host: tar-alcarin.cbb-automation.de (212.79.194.111) Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de 1066836166 31358094 212.79.194.111 (16 [77047]) X-Newsreader: Forte Agent 1.8/32.548 Xref: archiver1.google.com comp.lang.ada:1428 Date: 2003-10-22T17:23:44+02:00 List-Id: On Wed, 22 Oct 2003 12:10:07 +0000 (UTC), Lutz Donnerhacke wrote: >* Dmitry A Kazakov wrote: >> On Wed, 22 Oct 2003 11:07:43 +0000 (UTC), Lutz Donnerhacke >>>Calculating Fibbonacci numbers recursivly is easy but braindead. Languages >>>with access to the symbol table at execution time are able to change the the >>>reccuring call with a memorize wrapper which returns already computed values >>>immediatly instead of recalculating them. In the referenced article I wrote >>>an C-Implementation of such a wrapper. Now I'm looking for a more elegant >>>version in Ada. >> >> Solution depends on how exposed should be the context used to store >> results: >> >> 1. Completely hidden (allocated upon elaboration of the body) >> 2. Partially exposed as a generic parameter of the compilation unit >> 3. Fully exposed as a parameter of the subprogram > >It's more or less irrelevant. You have to choose an abstraction first. For instance for (1), a generic variant could be: generic type Argument_Type is private; type Parameter_Type is private; with function Evaluate (X : Argument_Type; N : Parameter_Type) return Argument_Type; function Evaluate_With_Side_Effects (X : Argument_Type; N : Parameter_Type) return Argument_Type; function Evaluate_With_Side_Effects (X : Argument_Type; N : Parameter_Type) return Argument_Type is begin if not then Evaluate (X, N); end if; return ; end Evaluate_With_Side_Effects; What you wrote is also (1), but on the basis of a tagged type. Theoretically one could also use a discriminated type, to complete the list of opportunities. Note that (1) has problems with multi-tasking. Other problems appear when Evaluate is not pure. For example, in a some real project, there were values dependent from other values. When an equivalent of Evaluate was calculated over dependent values, it could be made more precise if the original values were known. The solution here was (3) + keeping track of original value changes. --- Regards, Dmitry Kazakov www.dmitry-kazakov.de