To: J3 J3/23-244 From: Malcolm Cohen Subject: Formal specifications for generic procedures Date: 2023-October-24 References: 23-223r2, N2217 1. Introduction =============== At its meeting Jun 12-16, 2023, WG5 decided to approve generic procedures as described in N2217 for Fortran 202Y. Use cases for this are set out in N2217. Formal requirements were in 23-223r2. Here we have the formal specifications. The illustrative syntax and example are not being formally proposed at this time. 2. Formal specifications ======================== Generic procedure definition and identification: s01. A "generic procedure" shall be defined by a "generic subprogram". s02. A generic subprogram shall be a module subprogram, and shall not have multiple entry points (i.e. no ENTRY statement). NOTE: Although N2217 envisaged generic external subprograms, external subprograms need to have global names to be referenced. That would be undesirable - putting them in a module is easier to understand and safer to use. NOTE: In principle, this includes the possibility of being a separate module subprogram defined in a submodule, in which case there would be a generic module procedure interface in the ancestor module. NOTE: This specification may be revisited if sufficiently compelling reasons for needing generic external subprograms are found, and that are not satisfied by other means such as submodules. s03. A generic subprogram can have internal subprograms. s04. Every generic subprogram shall have a generic name. s05. The name of a generic procedure/subprogram shall be able to be added to the generic set of a generic identifier that is not a name, i.e. operator, assignment, input/output. s06. There is no specification regarding the ability or inability (as now), for adding a generic name that is a traditional generic set to the generic set of another generic identifier. Consistency may suggest doing this. Within a generic subprogram: s07. A generic procedure shall have at least one generic dummy argument. s08. There shall be a syntax that specifies all valid kinds of an intrinsic type for a generic dummy argument. The dummy argument is a generic dummy argument even if a compiler supports only one kind for the type. s09. There shall be a syntax that specifies all valid ranks for a generic dummy argument. s10. There shall be a syntax that specifies a set of kind type parameter values (as constant expressions) for a generic dummy argument. This syntax shall be usable for parameterized derived types as well as for intrinsic types. The dummy argument is still generic even if it only evaluates to one kind (assuming the syntax is different from normal non-generic kind specification, which seems likely). s11. There should be a hopefully-similar syntax for a list of ranks for a dummy argument to be generic over. s12. Assuming that generic-by-type is feasible and we do it, syntax is needed to supply a list of type-specs similarly, which may be intrinsic types or user-defined types. s13. A rank-generic dummy argument shall be a pointer, allocatable, or assumed-shape. It may have the CONTIGUOUS attribute, even if rank zero is included in the set of ranks. s14. A generic dummy argument may be polymorphic, if and only if every type over which it is generic is an extensible type. None of these types may be an extension of another. s15. The declarations within a generic subprogram shall be consistent with every generic combination. s16. The executable code within a generic subprogram, other than that within an ad-hoc specialization construct, shall be consistent with every generic combination. s17. Not only local variables (and the function result) of a generic subprogram may be declared to have the same attributes as a generic dummy argument, but also local variables (and function results) of nested scoping units viz. BLOCK constructs and internal subprograms. s18. The executable code within a block of an ad-hoc specialization need only be consistent with the specified generic combination. NOTE: This is exactly how our existing runtime ad-hoc polymorphism constructs work. s19. Ad-hoc specialization shall be performed at compile time, that is, there will be no trace of the non-chosen specializations in the generated anonymous specific. NOTE: This is aspirational, as there is no way at present to express it normatively. 3. Illustrative syntax ====================== i01. A generic (module) subprogram shall have the GENERIC keyword in its procedure heading (the SUBROUTINE or FUNCTION statement). i02. A generic procedure name may be added to the generic set of any other generic name by specifying it as a "procedure" in an interface block, using the PROCEDURE statement, or by listing it as a procedure-name in a GENERIC statement. i03. Genericity over all valid kind type parameters of an intrinsic type is specified by KIND=*. Similarly, genericity over all valid ranks is specified by RANK(*). i04. Genericity over a set of kind type parameter values is specified by KIND=array-expr, e.g. KIND=[int32,int64]. OPTIONS: (a) No duplicate values allowed. (b) Duplicates values allowed and ignored; e.g. to facilitate making lists with SELECTED_REAL_KIND et al. This would inhibit error detectability of typos like SELECTED_INT_KIND(8) for SELECTED_KIND_KIND(18). (c) No duplicate values allowed, but add an intrinsic function UNIQUE that returns the unique values in a vector (as a vector). Most of the time the user is likely to be writing a list of things she wants, as above (int32,int64), so this is probably not worth expending a lot of effort on. i05. Genericity over a set of valid ranks is specified by RANK(array-expr), e.g. RANK([1,2,3]). Duplicated values are not allowed. i06. Genericity over a set of types is specified by TYPE(type-spec-list), e.g. TYPE(INTEGER(int64),REAL(real64),COMPLEX). i07. For non-polymorphic dummy arguments generic in type, a SELECT TYPE construct can be used; in this case no "CLASS IS(...)" block will be allowed. Maybe "CLASS DEFAULT" should be "TYPE DEFAULT"? i08. For polymorphic dummy arguments, care needs to be taken in the design to establish which blocks are resolved at compile time, and which at runtime, but given no generic type will be an extension of another, this should be straightforward. i09. For dummy arguments generic in rank, the SELECT RANK construct can be used to select the rank. For more illustrative syntax, refer to N2217. 4. Example ========== Module example1 Interface Operator(.myop.) Procedure s ! All of the specific procedures of s. End Interface Contains Generic Subroutine s(a,b) Type(Real,Complex), Intent(InOut), Rank(*) :: a, b Type(Typeof(b)), Rank(Rank(b)) :: temp ... Select Type (b) Type Is (Real) temp = temp * (1-b) Type Is (Complex) ! Just this once, we want the conjugate. temp = temp * (1-Conjg(b)) End Select ... End Subroutine End Module ===END===