comp.lang.ada
 help / color / mirror / Atom feed
From: "Robert I. Eachus" <eachus@mitre.org>
Subject: Re: Self-referential types
Date: 1999/10/15
Date: 1999-10-15T21:27:23+00:00	[thread overview]
Message-ID: <38079D6F.4F7C00FA@mitre.org> (raw)
In-Reply-To: 38078775.73450564@pwfl.com

Marin David Condic wrote:
 
> This debate has been around before and a case has been made on the other
> side. Similar to the Cobol "perform", nested procedures give you a way of
> grouping blobs of code under some sort of descriptive name which helps
> clarify the main body of the code - especially where the main body of the
> code might otherwise become too deeply nested in control sturctures.

  But this is the wrong way to do that in Ada. There are two similar
cases and several structurally different ways to organize them.  First
if there is no shared local data common to all of the subprograms, or if
tasking is not an issue, you can put the local data in a package body. 
The package exports the subprograms, and the driver just contains a few
calls and perhaps some logic:

package Payroll is
  procedure Read_In_Transaction_Trapping_Device_Errors (
     Trans : out Trans_Type); 
  function Transaction_Successfully_Read return Boolean;
  procedure Edit_Transaction_For_Keypunch_Errors (
     Trans : in out Trans_Type);
end package Payroll;

with Payroll; use Payroll;
procedure Process_Payroll is
begin
  --blahblahblah
  while (not End_Of_File (Trans_File)) loop
    Read_In_Transaction_Trapping_Device_Errors (Trans) ;
    if (Transaction_Successfully_Read) then
       Edit_Transaction_For_Keypunch_Errors (Trans) ;
    end if ;
     --yadayadayada
  end loop ;
     --blahblahblah
end Process_Payroll ;

   Why is this better?  Because it promotes reuse.  Other payroll
operations can be created which share some, but not all, of the
subroutines used by Process_Payroll.

   The other case, of course, is where you have many payroll objects and
need local data for each, whether or not tasking is involved.  You can
either create payroll objects and store the local data as part of their
state, create a task type to represent the instances, or (usually best)
create an instance of a generic package for each payroll.  The
instantiation of this package may be internal to Process_Payroll, or
Process_Payroll can be just another declaration in that package.

   But in any case, the "right" place to keep state in Ada is almost
always in packages or tasks, not subprogram bodies.

   I'll tell you a (now oft-told) story about this.  At Honeywell we
implemented an in house cross-compiler from Multics to the DPS-6
series.  During the implementation, we had no idea whether the best way
to find uplevel references in the stack would be via static links or a
display.  Multics being Multics, we put a counter in the compiler, with
a feature to squirrel away the source whenever an uplevel reference was
encountered.  It also sent mail to the Ada compiler group every morning
if it trapped a reference.  (The compiler was installed on several
Multics machines, but they were all connected to the ARPAnet.)  Oh, the
compiler also did a full call tree trace to allow the code generator to
combine the stack frame of a nested procedure with its parent if the
compiler could determine that there were no recursive calls, and no
stack objects whose size depended on the parameters to the inner
procedure.

   I finally set the trap off two and half years later, while we were
running the ACVC tests.

   Needless to say, we implemented neither static links or a display,
and just left in the code (in the run time) which walked down the call
stack until it found the statically containing frame.  Potentially very
slow if there was an uplevel reference, but as we had shown, wasting
even one clock per stack frame created would have been slower on the
average.  (Note that the 'dynamic' walk was needed for exceptions in any
case.)

   As a result, not only that compiler, but most Ada compilers, are
tailored to the expected feature usage.  Anyone who violates those
assumptions will be penalized by slow code.  (Another example is
exceptions, in Ada the compiler author will try to lower the cost of
creating exception handler scopes at the cost of extra overhead when an
exception occurs.  Therefore, you are much better off checking in the
code for conditions that are not all that rare.)
 

-- 

                                        Robert I. Eachus

with Standard_Disclaimer;
use  Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...




  reply	other threads:[~1999-10-15  0:00 UTC|newest]

Thread overview: 62+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <7ttb4a$8mq$1@nnrp1.deja.com>
1999-10-12  0:00 ` Self-referential types Vladimir Olensky
1999-10-12  0:00   ` Matthew Heaney
1999-10-12  0:00     ` Richard D Riehle
1999-10-12  0:00     ` news.oxy.com
1999-10-12  0:00       ` Ted Dennison
1999-10-12  0:00         ` Stanley R. Allen
1999-10-13  0:00           ` Ted Dennison
1999-10-13  0:00         ` Vladimir Olensky
1999-10-14  0:00         ` Multiple Inheritance in Ada 95 [was Re: Self-referential types] Tucker Taft
1999-10-12  0:00       ` Self-referential types Matthew Heaney
1999-10-12  0:00     ` Robert I. Eachus
1999-10-12  0:00       ` Matthew Heaney
1999-10-13  0:00         ` Vladimir Olensky
1999-10-13  0:00           ` Vladimir Olensky
1999-10-18  0:00           ` Robert Dewar
1999-10-18  0:00             ` Vladimir Olensky
1999-10-18  0:00             ` Laurent Guerby
1999-10-13  0:00         ` Ted Dennison
1999-10-13  0:00           ` Matthew Heaney
1999-10-13  0:00         ` Robert I. Eachus
1999-10-13  0:00           ` Brian Rogoff
1999-10-15  0:00             ` Robert I. Eachus
1999-10-15  0:00               ` Marin David Condic
1999-10-15  0:00                 ` Robert I. Eachus [this message]
1999-10-18  0:00                   ` Robert Dewar
1999-10-19  0:00                     ` Robert I. Eachus
1999-10-18  0:00               ` Robert Dewar
1999-10-18  0:00                 ` Ed Falis
1999-10-19  0:00                   ` Robert Dewar
1999-10-18  0:00                 ` Brian Rogoff
     [not found]               ` <7u86su$o5v$1@nntp8.atl.mindspring.net>
1999-10-18  0:00                 ` Robert I. Eachus
1999-10-22  0:00                   ` Richard D Riehle
1999-10-22  0:00                     ` Robert I. Eachus
     [not found]               ` <slrn80fl9f.68j.aidan@skinner.demon.co.uk>
1999-10-19  0:00                 ` Wes Groleau
1999-10-21  0:00                   ` Robert Dewar
1999-10-21  0:00                     ` Jean-Pierre Rosen
1999-10-21  0:00                       ` Robert Dewar
1999-10-21  0:00                     ` Larry Kilgallen
1999-10-21  0:00                     ` Comments (was: Self-referential types) Wes Groleau
1999-10-21  0:00                       ` Ehud Lamm
1999-10-22  0:00                         ` Ted Dennison
1999-10-23  0:00                           ` Ehud Lamm
1999-10-23  0:00                         ` Robert Dewar
1999-10-23  0:00                           ` Ehud Lamm
1999-10-23  0:00                             ` Comments Georg Bauhaus
1999-10-24  0:00                               ` Comments Ehud Lamm
1999-10-26  0:00                                 ` Comments Robert I. Eachus
1999-10-28  0:00                                   ` Comments Jerry van Dijk
1999-10-28  0:00                                     ` Comments Ted Dennison
1999-10-25  0:00                             ` Comments (was: Self-referential types) Wes Groleau
1999-10-23  0:00                       ` M.
     [not found]                       ` <Pine.A41.3.96-heb-2.07.991021191504.30582K-100000@pluto.mscc.huji. <381477c9.e1388ff3@ftw.rsc.raytheon.com>
1999-10-25  0:00                         ` Larry Kilgallen
1999-10-22  0:00                     ` Self-referential types Richard D Riehle
1999-10-23  0:00                       ` Robert A Duff
1999-10-23  0:00                         ` Richard D Riehle
1999-10-24  0:00                       ` Michel DELARCHE
1999-10-12  0:00     ` Ted Dennison
1999-10-12  0:00       ` Matthew Heaney
1999-10-12  0:00 ` Robert A Duff
     [not found] ` <3802597B.9205AEE8@averstar.com>
1999-10-12  0:00   ` Ted Dennison
1999-10-12  0:00     ` Matthew Heaney
1999-10-13  0:00       ` Ted Dennison
replies disabled

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