[net.lang.ada] Procedure parameters and their specification

cjh@petsd.UUCP (Chris Henrich) (06/12/86)

[]
The discussion of procedure parameters, in this group and in
correspondence, has been murkier than necessary because of
misunderstandings about what level of specification was required
of the programmer.  I would distinguish the following levels:

0. No specification of parameters or return types.
1. Specification of return types.
2. Full specification of types of parameters and return values,
   but the "subprogram type" defined by these is anonymous.
3. Subprogram types can be named and thus can be recursively
   defined.

FORTRAN has always had level 0.

Algol-60 has level 1.  The first definition of Pascal followed
Algol-60 in this respect.  As Donn Milton pointed out, there are 
programs written at this level where the compiler cannot tell
at compile time if the functions passed as arguments match the
formal parameters.

The current ISO/ANSI standard for Pascal has level 2.  Thus a
quadrature routine written in Pascal might have this header:

     function simpson( function integrand(x:real):real;
                       a, b : real ; npoints: integer ): real;

The type of "integrand" is fully specified, but is anonymous: it
doesn't have a name of its own.  Likewise, the type of "simpson",
though fully specified, is anonymous.  If I had several quadrature
functions, and several integrands, and wanted to write a procedure
which would test one quadrature method against one integrand, I
would have to do this:
     
     procedure testQuadrature (
          function method ( 
               function intg(x:real):real; a,b:real; n:integer):real;
          function integrand(x:real):real; a,b:real; n:integer):real;
          a, b: real; npoints: integer);

The compiler has no particular difficulty parsing declarations like
these, or checking that the subprogram name passed to one of these
parameters matches the type of the parameter.  Even references to
non-local variables get handled correctly.

The ANSI/ISO standard for Pascal mandates this treatment of
parameter subprograms.  It was adopted in 1981.  Such is the headlong
pace of technological progress that there are a lot of Pascal
compilers around which do not comply.  The current compiler of 
Concurrent Computer Corporation (rah, rah, rah) *does* 
comply with the ANSI standard.

Level 3 is exeplified by Algol-68.  In it, the type (oops, they call
it the "mode") of "integrand" could be given a name.  So could that
of "simpson".  As a result, it would become much more convenient to
specify something like the mode of "testQuadrature".  On the other
hand, the compiler has more work to do in checking that modes match.

Which level is better? I say, "Yes!"  

That doesn't answer the question? Well, I think Level 2 is
satisfactory, and I would be glad to see it adopted in Ada.  The
technical problems which it poses are all known, and tidy solutions
are available. The drawback is that specifications like that of
"testQuadrature" become verbose. Anything with more than two levels
of parameter-subprogram interfaces would be pretty ugly.  

At level 2, one cannot do recursive definitions of subprogram modes.
An example, in Algol 68 (apologies if I haven't got the surface
syntax right):
    
     mode A = proc ( A ) bool ;

so that a function of mode A can take one parameter, which is also a
function of mode A.  FORTRAN and Pascal programmers have been doing
without this possibility. Are there interesting cases in which a
recursive definition like this is helpful?

Regards,
Chris

--
Full-Name:  Christopher J. Henrich
UUCP:       ...!hjuxa!petsd!cjh
US Mail:    MS 313; Concurrent Computer Corporation;
            106 Apple St; Tinton Falls, NJ 07724
Phone:      (201) 758-7288
Concurrent Computer Corporation is a Perkin-Elmer company.