To: J3 J3/25-163 From: John Reid & Hidetoshi Iwashita Subject: Auto-generic subprograms: requirements Date: 2025-August-26 References: 24-147r1, 25-150, 25-156r1 1. Introduction =============== Formal requirements for auto-generic subprograms were approved by J3 in 24-147r1. When the corresponding edits paper 25-156r1 was being discussed by J3, a desire was expressed to allow a declaration to reference the type, type parameters, and/or rank, of a generic dummy argument. Here is an example generic subroutine sub(a,b) real, allocatable, rank(1:2) :: a real, allocatable, rank(rank(a)+1) :: b There are further examples in 25-150. There are two requirements that make this problematical: d. An auto-generic procedure may have zero, one, or more "generic" dummy arguments; a generic dummy argument is one that might have more than one value for a kind type parameter, more than one rank, or more than one type. and f. A local variable of an auto-generic procedure can have the same type, type parameters, and/or rank, as a generic dummy argument. Function result variables similarly. The definition in requirement d says that both a and b are generic dummy arguments. However, b has a single type and kind, and its rank depends on the rank of a. It plays no role in determining which specific versions are generated. There just two, which could have been written as the specific procedures subroutine sub1(a,b) real, allocatable, rank(1) :: a real, allocatable, rank(rank(a)+1) :: b and subroutine sub2(a,b) real, allocatable, rank(2) :: a real, allocatable, rank(rank(a)+1) :: b We propose that requirement d be changed so that a dummy argument is not regarded as a generic argument unless its type, kind type parameter, or rank, is specified with special syntax such as rank(1:2) in the example above. Requirement f does not disallow a local variable having a type, kind type parameter, or rank that depends on the type, kind type parameter, or rank of a generic dummy argument without being the same, but appears to do so. We propose that requirement f be changed to allow this. Requirement e states "a generic procedure implicitly defines an anonymous set of specific procedures, one for every combination of type, kind type parameter value, and rank" of the generic arguments. This implies that in each specific procedure each generic argument would be treated as if it were declared with explicit type, kind type parameters, and rank. Here is an example generic subroutine good(a,b) type(real, double precision) :: a integer :: p p = precison(a) Requirement f provides another example so could be deleted. However, it appears to state a restriction, so we prefer to rewrite it. 2. Proposal =========== We propose that requirement d be changed to d. An auto-generic procedure may have zero, one, or more "generic" dummy arguments; a generic dummy argument is declared with special syntax for its type, kind, or rank. The special syntax usually specifies alternative values. We propose that requirement f be changed to f. In each specific procedure, each appearance of a generic dummy argument is treated as if its type, kind type parameters, and rank had been declared specifically. 3. Formal requirements with these changes made ============================================== a. There shall be a mechanism for defining an auto-generic procedure, with a single procedure body rather than a set of specific procedures. b. Such a generic procedure will have a generic name, but no specific name. c. There shall be a mechanism for defining a generic procedure with a generic identifier that is an operator name, assignment, or input/output. d. An auto-generic procedure may have zero, one, or more "generic" dummy arguments; a generic dummy argument is declared with special syntax for its type, kind, or rank. The special syntax usually specifies alternative values. NOTE: The proposed template facility has extensive features for handling different types; this is not meant to duplicate those. The motivating examples for different type are: (ex1) algorithms which work the same for real and complex; (ex2) algorithms which work the same for an intrinsic type such as real, and also for a user-defined type which provides a variation on such an intrinsic type. e. As implied by the above, such a generic procedure implicitly defines an anonymous set of specific procedures, one for every combination of type, kind type parameter value, and rank. We will call each such combination a "generic combination". f. In each specific procedure, each appearance of a generic dummy argument is treated as if its type, kind type parameters, and rank had been declared specifically. g. There shall be a construct for ad-hoc specialization of executable code for the type and/or kind type parameter values of a dummy argument that is generic in type and/or kind. NOTE: We already have such a construct for runtime polymorphism, but this requirement means compile-time polymorphism. h. There shall be a construct for ad-hoc specialization of executable code for the rank of a dummy argument that is generic in rank. NOTE: We already have such a construct for runtime rank variability, but this requirement is for compile-time rank variability. i. If possible, this generic procedure facility should be available for generic type-bound procedure definition. NOTE: This should be straightforward, except perhaps for coming up with convincing syntax. j. If possible, this generic procedure facility should be available within a template. k. If possible, this generic procedure facility should be available as a separate module subprogram, i.e. using a submodule. NOTE: All of the "if possible" requirements depend on feasibility in the language design, standard wording, and implementation. If any of those aspects are difficult, they should be reconsidered.