08-249r2 To: J3 From: Michael Ingrassia Subject: Public Comment J32035 Date: 2008 August 15 ---------------------------------------------------------------------- Commenter: James Giles Subject: "IMPURE ELEMENTALs" I don't see the value in having IMPURE ELEMENTAL procedures. There are no examples in the draft standard and the only example in the document describing new features can be rewritten without the procedure being ELEMENTAL at all. The example in the document is: impure elemental function accumulate (a,sum) real :: accumulate real, intent(in) :: a real, intent(inout) :: sum sum = sum + a accumulate = sum end function accumulate Where the call is: real a(n), sum : sum = 0.0 a = accumulate (a,sum) This seems simple enough. But suppose SUM was also declared to be an array. The procedure would no longer "accumulate". Nor would it generate any error messages or warnings. An elemental is simply not a good way of accompishing such things in the first place. You're trying to graft inappropriate functionality in a place it doesn't fit well. Now, if the procedure were declared: function accumulate (a,s) real, intent(inout) :: s real, intent(in) :: a(:) real :: accumulate(size(a)) s = sum(a) accumulate = s end function accumulate The caller could be the same. The SUM argument could even be made optional - it is certainly required to remain a scalar. ---------------------------------------------------------------------- J3 response: The case for impure elementals was made in J3 meeting paper 05-238, from meeting 173 in 2005. The feature had earlier been proposed by the UK delegation as item UK-011. The case for them was sufficiently convincing that they were added to the work plan for the current revision. One of the several reasons for their inclusion in the work plan was their applicability to the reverse mode of automatic differentiation. Another reason for impure elemental procedures is that a pure elemental procedure cannot contain a variable with the SAVE attribute, or assign a value to a variable in common or accessed by use or host association. An application where this is important is random number generators. The example cited in the comment does not appear in the current draft. One could accumulate into a scalar variable "SUM" by making "SUM" a variable in COMMON rather than a dummy argument (to avoid canonical promotion of scalar dummy arguments of ELEMENTAL procedures to arrays). This was part of the justification for impure elementals: a pure ELEMENTAL procedure is not permitted to update a variable in COMMON but an impure ELEMENTAL procedure can. Further, your example of writing the procedure without the procedure being elemental at all is somewhat misleading. The example handles rank=1 arrays. What is needed is a procedure which handles arrays of all ranks from 1 to 15. J3 decided not to remove impure elemental procedures, or change their description.