(j3.2006) PURE function vs subroutine
Damian Rouson
damian
Wed Mar 4 13:51:56 EST 2015
> On Mar 4, 2015, at 9:14 AM, Daniel C Chen <cdchen at ca.ibm.com> wrote:
>
> Hello,
>
> Can someone shed light on why a nonpointer dummy argument of a pure function that is not VALUE must be INTENT(IN)
>
Disallowing side effects, including modifying dummy arguments, is an central to the notion of functional purity (cf. http://en.wikipedia.org/wiki/Pure_function <http://en.wikipedia.org/wiki/Pure_function>). My understanding is that prohibiting side effects enables several compiler optimizations. Apparently the optimizations are sufficiently attractive that gfortran, for example, will attempt to detect if a procedure could have been declared pure even if it wasn?t declared so. If so, gfortran will then mark the procedure as "implicitly pure? and take advantage of all the optimizations that purity affords.
Fortran?s definition of PURE, however, falls short of the second central notion of functional purity as described at the above link: deterministic output. Fortran PURE procedures are allowed to read from global state (e.g., module variables and common blocks), which a non-PURE procedure might modify between two calls to a declared PURE procedure. For this reason, Tom recently suggested we add an attribute that ensures determinism ? possibly VIRTUOUS. :) Determinism facilitates additional optimizations. For example, repeated invocations with arguments known to have the same values can be implemented as one invocation followed by substituting the result of the first invocation at the location of all subsequent invocations. When the floor is open for adding new features, I?d second such a motion (or sooner if this can be considered wart removal).
> where such a dummy argument of a pure subroutine can be INTENT(OUT)/INTENT(INOUT)?
>
Note 12.50 of the Fortran 2008 standard indicates that the reason for allowing PURE subroutines is to facilitate calling subroutines inside PURE functions. The only way to get output from a subroutine is to allow some form of side effect and I?d imagine that the simplest side effect to allow is via INTENT(OUT) or INTENT(INOUT) dummy arguments ? although I?d prefer that INTENT(INOUT) had been disallowed because then PURE subroutines would be closer to being like PURE functions if one views the aggregate INTENT(OUT) variables as analogous to the function RESULT.
Damian
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.j3-fortran.org/pipermail/j3/attachments/20150304/3cfe1079/attachment-0001.html
More information about the J3
mailing list