From J.L.Schonfelder@liverpool.ac.uk Wed Jun 22 03:29:12 1994
Received: from mailhub.liverpool.ac.uk (mail.liv.ac.uk) by dkuug.dk with SMTP id AA04508
  (5.65c8/IDA-1.4.4j for <SC22WG5@dkuug.dk>); Wed, 22 Jun 1994 11:29:15 +0200
Received: from liverpool.ac.uk by mailhub.liverpool.ac.uk with SMTP (PP) 
          id <11611-0@mailhub.liverpool.ac.uk>; Wed, 22 Jun 1994 10:29:05 +0100
Date: Wed, 22 Jun 1994 10:29:12 PDT
From: Lawrie Schonfelder <J.L.Schonfelder@liverpool.ac.uk>
Subject: Duplicate Interface Declaration
To: SC22/WG5 members <SC22WG5@dkuug.dk>
Message-Id: <ECS9406221012A@liv.ac.uk>
Priority: Normal
Mime-Version: 1.0
Content-Type: TEXT/PLAIN; CHARSET=US-ASCII
X-Charset: ASCII
X-Char-Esc: 29

 To: X3J3 and WG5 (2-Jun-94)
 From: Lawrie Schonfelder 
 References: none 
                     Duplicate Interface Specifications 
 
 1 Introduction     
 The description of Fortran 90 contains a substantial amount of
 often rather arcane text so as to make duplicate declaration of
 the same entity illegal. However, in one case at least there are
 positive advantages in allowing, even of encouraging duplicate
 declaration. This is for procedure interfaces. It would greatly
 assist in both the initial development of a large module,
 particularly one defining a data-abstraction, if the full
 interface to the generic procedures could be defined as part of
 the specification part and also that a full duplicate and
 identical interface could be defined as part of the specific
 module procedure definition. Provided the identity of these
 duplicate interface definitions is checked this allows support
 for top-down design methodology and at the same time provides
 long term maintenance support.
     The current situation is highly unsatisfactory. The
 insistence on only allowing the names of the specific procedures
 to appear on the MODULE PROCEDURE statement when defining the
 generic overload based on specific procedures contained in the
 module, makes design of this interface non-transparent. Of course
 it is possible to write the full interface definitions at this
 point and retain them as comments, or simply to move them down
 the module to their eventual location after the CONTAINS using
 cut/paste methods in the source code editor. Unfortunately in a
 large module these two locations will be widely separated in the
 source code. As a result there is a major maintenance problem. 
 When as inevitably happens a change is needed to the overload
 structure or the interface for a specific procedure, this change
 is quite likely to affect both locations. The language as now
 designed provides no support for ensuring that these changes are
 kept in step. If commentary is used to provide interface
 information near the MODULE PROCEDURE statement there can be no
 checking to ensure that this is actually accurate in reflecting
 the true interface definitions of the specific procedures. If it
 is not it could be all too easy to introduce a change in
 interface resulting in ambiguous or invalid overloads.
     It is in fact ironic that the one place where the language
 not only allows duplication but requires it is the one place
 where duplication cannot be checked, viz., when the interface
 body is declaring the interface for an external procedure. For
 obvious reasons this declaration must be the full definition of
 the interface but for equally obvious reasons there can be no
 checking that the duplicate information is consistent with that 
 included in the actual definition of the procedure. In situations
 where both the interface block and the procedure definition are
 directly visible to the compiler and hence checkable we have gone
 to considerable lengths to rule out the possibility of
 duplication. This is rightly seen some users as somewhat
 perverse.
     In what follows a proposal is made to remove this perverse
 restriction so as to allow duplicate interface declaration,
 constrained only that if duplication occurs the interfaces
 defined must be identical. 
     The argument against duplication is usually given is that
 duplicate declarations usually indicate a programming error and
 hence should be illegal so that they can be diagnosed by the
 compiler. I find this argument distinctly uncompelling. Even if
 it is accepted that in many cases such duplicate declarations may
 have been produced by mistake, mistakes that produce a valid
 identical declaration are likely to be relatively infrequent and
 it would be perfectly possible for a conforming processor to
 issue a warning in such identity cases as remain. If these were
 the result of an error in programming, the user is alerted but if
 they were deliberate the program does not fail. I personally get
 much more irritated by compilers that fail my programs for
 reasons that I find spurious that those that merely warn me that
 I might have done something wrong.
 
 2 Technical Description 
 The only technical obstacle that I can see against allowing
 duplicate interface declarations relates to the fact that where
 an interface body is intended to be for an external procedure it
 should not access the data environment of the host program.
 However, for an interface body defining a duplicate definition of
 a procedure that is contained within the host, the inheritance of
 the data environment is necessary. The external procedure does
 not have access to the host data environment and so to preserve
 the approximate possibility of simple textual copying the
 specification part from the external to create the interface,
 this insulation from the host data environment is required. For
 the contained procedure a similar argument requires the interface
 body to access the same data environment as the procedure
 definition.
     To deal with this issue I am proposing the addition of
 another attribute that can be declared when defining the
 interface of a procedure whose complete definition is contained
 in the same host. I suggest this attribute be spelt CONTAINED.
 With this attribute I am proposing that the following should be
 valid
 
 MODULE EXMP
  TYPE RAT
   INTEGER :: num,den
  ENDTYPE RAT 
  INTERFACE CLEAR
   SUBROUTINE real_clear(a) ! external procedure
    REAL :: a(:)
   ENDSUBROUTINE real_clear
   SUBROUTINE int_clear(a)  ! external procedure
    INTEGER :: a(:)
   ENDSUBROUTINE int_clear
   CONTAINED SUBROUTINE rat_clear(a) ! contained procedure
    type(RAT) :: a(:)
   ENDSUBROUTINE rat_clear 
  ENDINTERFACE
 ...
 CONTAINS
 ...
  SUBROUTINE rat_clear(a) ! contained procedure
   type(RAT) :: a(:)
   a%num = 0
   a%den = 0
  ENDSUBROUTINE rat_clear 
  ... 
 ENDMODULE EXMP
 
 Where I am assuming that the two procedures real_clear and
 int_clear are defined externally to the module but that the
 procedure depending on the type RAT is defined within the module
 where it inherits the type definition by host association both
 into the interface body declaration and the contained procedure
 definition.
     The rule should be that duplicate specifications should be
 permitted provided the interfaces defined are identical, and if
 an interface body is specified for a procedure that is defined
 contained within the same host this must be indicated by the
 CONTAINED prefix on the interface body header. The interface body
 specification for a contained procedure inherits the data
 environment of containing program unit by host association. If
 the CONTAINED prefix is not specified for an interface body it
 defines the interface for an procedure defined externally to the
 host program unit. 
     Note also, a side effect of this proposal which is both
 significant and useful is that it allows internal procedures to
 be included in generic overload sets. This removes a further
 strange anomaly currently restricting the use of internal
 procedures.
 
 3 Proposed Edits to IS 1539 : 1991     
 These edits are suggested as guidance for the editorial
 committee. They are written against IS 1539 :1991 and are
 believed to be sufficient to implement this proposal.
 
 3.1 12.3.2.1  [167/19]
     Before "function-stmt" add "[CONTAINED]"
          [167/22]
     Before "subroutine-stmt" add "[CONTAINED]"
 
 3.2 12.3.2.1  [167/35]
     Replace "for a" by  "for the"
 
 3.3 12.3.2.1  [167/36+]
     Add constraint
 Constraint:   If the CONTAINED prefix is specified for an
                interface body the procedure whose interface is
                specified by that body must be defined by a
                procedure contained with the same host.
 
 3.4 12.3.2.1  [167/37]
     After "external" add ",internal"
          [167/38]
     Before "module" add "internal and"
          [167/39]
     After "body" add ",without the CONTAINED prefix,"
          [167/40]
     Before "If" add sentence "An interface body with a CONTAINED
 prefix in an interface block specifies the interface for a
 procedure whose definition is contained in the same host, either
 by an internal subprogram or a module subprogram."
 
 3.5 12.3.2.1  [168/2]
     After "that"add "external procedures"
     After "names" add "as specified in the interface body"
     After "different." add sentence "For a contained procedure
 all characteristics including dummy argument names must be
 identical in both interface body and procedure definition."
 
 3.6 12.3.2.1  [168/6-7]
     Delete sentence "A procedure...scoping unit."
 
 4 Proposal    
 I would like to propose that WG5 consider this as a proposal for
 an extension to be included either in F95 or in F2000, preferably
 the former. 
--
Dr.J.L.Schonfelder
Director, Computing Services Dept.
The University of Liverpool, UK
e-mail J.L.Schonfelder@liv.ac.uk
phone: +44(51)794-3716
fax:   +44(51)794-3759



