comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: Defining a binary operator between function access types: Is it possible?
Date: Thu, 16 Oct 2008 18:18:59 -0400
Date: 2008-10-16T18:18:59-04:00	[thread overview]
Message-ID: <wccej2gqj3g.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: e027c16b-3da9-4c85-a20d-f1468ac2649d@b31g2000prf.googlegroups.com

Adam Beneschan <adam@irvine.com> 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



  reply	other threads:[~2008-10-16 22:18 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-10-16 13:34 Defining a binary operator between function access types: Is it possible? soychangoman
2008-10-16 14:26 ` Dmitry A. Kazakov
2008-10-16 14:29 ` Adam Beneschan
2008-10-16 22:18   ` Robert A Duff [this message]
2008-10-16 15:14 ` Jacob Sparre Andersen
2008-10-17  7:10 ` Defining a binary operator between function access types: Is it anon
2008-10-21  6:46 ` Defining a binary operator between function access types: Is it possible? Ivan Levashew
2008-10-21 13:17   ` soychangoman
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox