Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.compilers > #1156

Re: Is multi-level function return possible?

From glen herrmannsfeldt <gah@ugcs.caltech.edu>
Newsgroups comp.compilers
Subject Re: Is multi-level function return possible?
Date 2014-03-14 20:27 +0000
Organization Aioe.org NNTP Server
Message-ID <14-03-034@comp.compilers> (permalink)
References <14-03-020@comp.compilers> <14-03-022@comp.compilers> <14-03-025@comp.compilers> <14-03-027@comp.compilers> <14-03-029@comp.compilers>

Show all headers | View raw


Ivan Godard <ivan@ootbcomp.com> wrote:
> On 3/13/2014 10:05 AM, Anton Ertl wrote:
<snip>

>> But thinking about it makes me wonder: Can you come up with an
>> example that shows the difference?  And can you come up with a useful
>> example where there is a difference?

> The standard practical example is an integration package which takes
> an argument function to integrate over the range.

Integration routines and minimization routines go back to at least
Fortran II, when FUNCTION and SUBROUTINE were added. (Fortran I used
ASSIGN and assigned GOTO as a sort-of internal subroutine call, along
with "extended range of the DO" so you could use it inside DO loops.)

> The argument > function is not in the scope of the integration
> package. It may not even be in the scope of the caller of the
> integration routine.  Consequently it must use either a closure (so
> it can be a lambda) or the function-pointer format must contain a
> static link pointer as well as a code pointer. A display doesn't
> help this.

Reasonably often you don't need to pass any other data to the called
routine, but in Fortran you use COMMON (or, in newer Fortran, module
variables).

The other possiblity is for the integration (or other) routine to pass
data, such as a pointer, through to the called routine.  That seems to
be rare.

PL/I internal procedures can be passed in this way, and reference data
from the procedure that they are internal to.  This ability has, I
believe been added to Fortran in the 2008 standard. (Internal
procedures were added earlier, but couldn't be used this way.)  As I
understand it, this isn't quite what closure allows, but works most of
the time in practical use.

The other place this is commonly needed is in sort programs, such
as C's qsort(). I did it once using a C file scope (static) variable.

> True closures are more powerful than static nesting, because a closure
> can be invoked even when the scope in which it was created has exited,
> which cannot be done with static nesting. However this means that the
> representation of a closure has unbounded size (and so must use the
> heap), whereas static nesting has bounded size, e.g. two pointers. It
> also has unbounded lifetime, so it requires garbage collection. As an
> alternative, some languages define a poor-man's-lambda that cannot
> escape its scope of creation. That lets the PML be allocated on the
> stack (an alloca) and requires neither heap nor garbage collection, but
> is less powerful than true closures.

I am not so sure what you mean by "static" here. The Fortran COMMON or
module variable are static allocated, and so will fail when reentrant
code is needed, such as recursion.  The PL/I version is designed to
work properly in the case of recursion, such that data from the
appropriate instance is seen by the called routine.

As I understand it, most of the PL/I language was defined before
anyone tried writing a compiler for it. Getting this right may have
complicated the implementation. Otherwise, is hard to see why it took
until 2008 for Fortran to allow it.  (and still maybe not implemented
in any compilers.)

> In addition, one can create an unbounded number of closures with
> different bindings, whereas the number of statically nested functions is
> statically bound. In effect, a statically bound function receives its
> bindings as global (to it) variables passed by reference, whereas a
> closure receives its bindings as arguments passed by value. Thus
> statically nested functions have all the well-known issues of passing
> arguments via global data, while closures avoid that.

I am still not sure how this works in the case of recursion.

> The C++ approach of creating a temporary class to hold the bindings and
> then using operator() to invoke the result is a syntactic variant of a
> closure rather than of statically nested functions. However, the
> temporary class is a nuisance to write, so recent C++ has provided more
> convenient syntax to create lambda/closures.

-- glen
[Closures are dandy, but they need a garbage collected heap of stack
frames.  In many environments, e.g., embedded devices, that's too
slow or takes too much space. -John]

Back to comp.compilers | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Is multi-level function return possible? noitalmost <noitalmost@cox.net> - 2014-03-10 15:46 -0400
  Re: Is multi-level function return possible? Kaz Kylheku <kaz@kylheku.com> - 2014-03-11 05:20 +0000
  Re: Is multi-level function return possible? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2014-03-11 13:04 +0000
    Re: Is multi-level function return possible? Kaz Kylheku <kaz@kylheku.com> - 2014-03-11 22:15 +0000
      Re: Is multi-level function return possible? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2014-03-13 17:05 +0000
        Re: Is multi-level function return possible? Bill Findlay <yaldnif.w@blueyonder.co.uk> - 2014-03-14 01:28 +0000
        Re: Is multi-level function return possible? Ivan Godard <ivan@ootbcomp.com> - 2014-03-13 18:32 -0700
          Re: Is multi-level function return possible? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-03-14 20:27 +0000
      Re: Is multi-level function return possible? George Neuner <gneuner2@comcast.net> - 2014-03-14 02:39 -0400
        Re: Is multi-level function return possible? Kaz Kylheku <kaz@kylheku.com> - 2014-03-14 21:34 +0000
          Re: Is multi-level function return possible? Marco van de Voort <marcov@toad.stack.nl> - 2014-03-14 22:59 +0000
        Re: Is multi-level function return possible? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2014-03-16 17:19 +0000
          Re: Is multi-level function return possible? Marco van de Voort <marcov@toad.stack.nl> - 2014-03-16 18:39 +0000
          Re: Is multi-level function return possible? Andy Walker <news@cuboid.co.uk> - 2014-03-16 19:45 +0000
            Re: Is multi-level function return possible? Bill Findlay <yaldnif.w@blueyonder.co.uk> - 2014-03-17 16:15 +0000
              Re: Is multi-level function return possible? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2014-03-18 17:36 +0000
                Re: Is multi-level function return possible? Andy Walker <news@cuboid.co.uk> - 2014-03-21 02:04 +0000
                Re: Is multi-level function return possible? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-03-21 02:50 +0000
                Re: design of PL/I, was Is multi-level function return possible? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-03-21 06:43 +0000
                Re: design of PL/I, was Is multi-level function return possible? noitalmost <noitalmost@cox.net> - 2014-03-26 14:16 -0400
                Re: exception catching in PL/I, was Is multi-level function return possible? "Robin Vowels" <robin51@dodo.com.au> - 2014-03-27 21:08 +1100
                Re: Is multi-level function return possible? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2014-03-24 17:06 +0000
                Re: Is multi-level function return possible? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-03-24 20:01 +0000
                Re: Is multi-level function return possible? George Neuner <gneuner2@comcast.net> - 2014-03-26 04:00 -0400
                Re: Is multi-level function return possible? Hans-Peter Diettrich <DrDiettrich1@aol.com> - 2014-03-27 01:58 +0100
                Re: Is multi-level function return possible? Stefan Monnier <monnier@iro.umontreal.ca> - 2014-04-07 14:48 -0400
                Re: Is multi-level function return possible? Andy Walker <news@cuboid.co.uk> - 2014-03-26 12:16 +0000
                Re: Is multi-level function return possible? Andy Walker <news@cuboid.co.uk> - 2014-03-26 17:34 +0000
                Re: history of C, was Is multi-level function return possible? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-03-27 00:05 +0000
      Re: Is multi-level function return possible? Bakul Shah <usenet@bitblocks.com> - 2014-03-15 10:28 -0700
    Re: Is multi-level function return possible? George Neuner <gneuner2@comcast.net> - 2014-03-14 02:44 -0400
      Re: Is multi-level function return possible? Marco van de Voort <marcov@toad.stack.nl> - 2014-03-14 19:38 +0000
      Re: Is multi-level function return possible? Kaz Kylheku <kaz@kylheku.com> - 2014-03-14 21:36 +0000
      Re: Is multi-level function return possible? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-03-14 21:47 +0000
        Re: Is multi-level function return possible? Hans-Peter Diettrich <DrDiettrich1@aol.com> - 2014-03-15 13:34 +0100
      Re: Is multi-level function return possible? Ian Lance Taylor <ian@airs.com> - 2014-03-14 16:55 -0700
  Re: Is multi-level function return possible? George Neuner <gneuner2@comcast.net> - 2014-03-11 13:56 -0400
    Re: Is multi-level function return possible? federation2005@netzero.com - 2014-03-26 17:39 -0700
      Re: catch and throw, was Is multi-level function return possible? rpw3@rpw3.org (Rob Warnock) - 2014-03-27 04:23 +0000
        Re: catch and throw, was Is multi-level function return possible? Kaz Kylheku <kaz@kylheku.com> - 2014-03-27 05:44 +0000
        Re: catch and throw, was Is multi-level function return possible? Bakul Shah <usenet@bitblocks.com> - 2014-03-26 23:20 -0700
          Re: catch and throw, was Is multi-level function return possible? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-03-27 19:21 +0000
          Re: exception catching in PL/I "Robin Vowels" <robin51@dodo.com.au> - 2014-03-28 10:50 +1100
            Re: exception catching in PL/I Martin Ward <martin@gkc.org.uk> - 2014-04-16 14:32 +0100
          Re: catch and throw, was Is multi-level function return possible? mac <acolvin@efunct.com> - 2014-03-28 23:08 +0000
            Re: catch and throw, was Is multi-level function return possible? Ivan Godard <ivan@ootbcomp.com> - 2014-03-28 18:24 -0700
              Re: catch and throw, was Is multi-level function return possible? Kaz Kylheku <kaz@kylheku.com> - 2014-03-29 02:47 +0000
                Re: catch and throw, was Is multi-level function return possible? Ivan Godard <ivan@ootbcomp.com> - 2014-03-28 21:32 -0700
              Re: catch and throw, was Is multi-level function return possible? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-03-29 03:38 +0000
                Re: precise and imprecise, was catch and throw, was Is multi-level function return possible? Ivan Godard <ivan@ootbcomp.com> - 2014-03-28 22:12 -0700
            Re: catch and throw, was Is multi-level function return possible? Kaz Kylheku <kaz@kylheku.com> - 2014-03-29 02:07 +0000
              Re: catch and throw, was Is multi-level function return possible? Alan Bawden <alan@scooby-doo.csail.mit.edu> - 2014-03-29 02:44 -0400
            Re: PL/I exceptions, was catch and throw, was Is multi-level function return possible? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-03-29 02:40 +0000
              Re: PL/I exceptions "Robin Vowels" <robin51@dodo.com.au> - 2014-03-30 01:58 +1100
      Re: catch and throw, was Is multi-level function return possible? George Neuner <gneuner2@comcast.net> - 2014-03-28 03:52 -0400
  Re: Is multi-level function return possible? Louis Krupp <lkrupp@pssw.com> - 2014-03-11 15:16 -0600
  Re: Is multi-level function return possible? noitalmost <noitalmost@cox.net> - 2014-03-15 12:21 -0400

csiph-web