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-Thread: 103376,bd50112c03f1f521 X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news1.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!nx02.iad.newshosting.com!newshosting.com!198.186.194.249.MISMATCH!transit3.readnews.com!news-xxxfer.readnews.com!news-out.readnews.com!transit4.readnews.com!panix!newsfeed-00.mathworks.com!nntp.TheWorld.com!not-for-mail From: Robert A Duff Newsgroups: comp.lang.ada Subject: Re: Defining a binary operator between function access types: Is it possible? Date: Thu, 16 Oct 2008 18:18:59 -0400 Organization: The World Public Access UNIX, Brookline, MA Message-ID: References: NNTP-Posting-Host: shell01.theworld.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: pcls6.std.com 1224195539 20701 192.74.137.71 (16 Oct 2008 22:18:59 GMT) X-Complaints-To: abuse@TheWorld.com NNTP-Posting-Date: Thu, 16 Oct 2008 22:18:59 +0000 (UTC) User-Agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/21.3 (irix) Cancel-Lock: sha1:Qk3U3Q+09n0NWmbPx4QVdAeZ86Q= Xref: g2news2.google.com comp.lang.ada:8158 Date: 2008-10-16T18:18:59-04:00 List-Id: Adam Beneschan writes: > On Oct 16, 6:34 am, soychango...@gmail.com wrote: >> I'm trying to define a binary operator between function access types >> that returns the access to the function that is the binary operator >> acting on both functions. Let's put it a bit clearer: >> >> ----------- >> procedure operating_on_access is >> >> type function_access is access function ( x : Real) return Real; >> >> function "+" (f1 : function_access; f2 : function_access) return >> function_access is >> begin >> -- The question is what to put here >> return null; >> end "+" >> >> funcion square (x : Real) return Real is >> begin >> return x**2; >> end square; >> >> funcion cubic (x : Real) return Real is >> begin >> return x**3; >> end square; >> >> f1_acc, f2_acc, f3_acc : function_access; >> >> begin >> >> f1_acc := square'Access; >> f2_acc := cubic'Access; >> >> -- f3_acc := f1_acc + f2_acc; >> -- the idea is that ' f3_acc.all ( x ) ' and ' square (x) + cubic(x) >> ' are equivalent >> >> end operating_on_access; > > You must be used to Lisp or something, right? You can't do that sort of thing in Ada. What the OP is asking for is "full closures". Ada only has "downward closures" -- you can pass procedures into procedures, but not out. >...This isn't going to > work at all. A Function_Access is basically a pointer to a function > that has already been written. You're asking the program to build a > *new* function on the fly and return a pointer to that. That pretty > much works only in interpreted languages, and Ada isn't one of those. Full closures are typically supported by functional languages, such as Lisp (which Adam mentioned), Scheme, SML, OCaml, Haskell, etc. They do not need to be "interpreted languages" -- it is reasonable to compile to machine code in many such languages, and it's done in practice. They do normally need garbage collection. Jacob Sparre Andersen outlined how to simulate full closures using tagged and class-wide types. > If you really need something like this, you'll have to build your own > interpreter somehow. It could be a very simple one depending on what > your needs are. - Bob