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-23 07:35:23 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: Thu, 23 Oct 2003 16:36:24 +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 1066919722 30653601 212.79.194.111 (16 [77047]) X-Newsreader: Forte Agent 1.8/32.548 Xref: archiver1.google.com comp.lang.ada:1515 Date: 2003-10-23T16:36:24+02:00 List-Id: On Wed, 22 Oct 2003 19:41:56 +0000 (UTC), Lutz Donnerhacke wrote: >In this case I have reference Evaluate_With_Side_Effects from Evaluate. How >can I implement this? You cannot without modifying Evaluate if it has to be recursive and should take an advantage of knowing cached values at nested steps. >> 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. > >This does not solve the problem of recursion. Yep, you have to expose the context and to make Fib aware of it. >> precise if the original values were known. The solution here was (3) + >> keeping track of original value changes. > >Understood, but I didn't grasp how to prepare the Evaluate function for this >usage. You already did it. It could be sort of: package Fibonacci is type Context_Type is tagged limited private; function Fib (N : Natural; Context : access Context_Type'Class) return Natural; function Is_In (N : Natural; Context : access Context_Type) return Boolean; function Load (N : Natural; Context : access Context_Type) return Natural; procedure Store (N, Result : Natural; Context : access Context_Type); private type Context_Type is tagged limited null record; end Fibonacci; ------------------- package Fibonacci.Cached is type Memory is new Context_Type with private; function Is_In (N : Natural; Context : access Memory) return Boolean; function Load (N : Natural; Context : access Memory) return Natural; procedure Store (N, Result : Natural; Context : access Memory); private type Results is array (Natural range <>) of Natural; type Memory is new Context_Type with record Data : Results (1..100) := (others => 0); end record; end Fibonacci.Cached; ----------------- package body Fibonacci is function Is_In (N : Natural; Context : access Context_Type) return Boolean is begin return False; end Is_In; function Load (N : Natural; Context : access Context_Type) return Natural is begin raise Program_Error; return 0; end Load; procedure Store (N, Result : Natural; Context : access Context_Type) is begin null; end Store; function Fib (N : Natural; Context : access Context_Type'Class) return Natural is begin if N < 2 then return 1; end if; if Is_In (N, Context) then return Load (N, Context); else declare Result : constant Natural := Fib (N - 1, Context) + Fib (N - 2, Context); begin Store (N, Result, Context); return Result; end; end if; end Fib; end Fibonacci; --------------- package body Fibonacci.Cached is function Is_In (N : Natural; Context : access Memory) return Boolean is begin return N in Context.Data'Range and then 0 /= Context.Data (N); end Is_In; function Load (N : Natural; Context : access Memory) return Natural is begin return Context.Data (N); end Load; procedure Store (N, Result : Natural; Context : access Memory) is begin if N in Context.Data'Range then Context.Data (N) := Result; end if; end Store; end Fibonacci.Cached; --- Regards, Dmitry Kazakov www.dmitry-kazakov.de