(j3.2006) PURE function vs subroutine

Daniel C Chen cdchen
Thu Mar 5 10:37:37 EST 2015


Thanks all for the response!

Daniel

XL Fortran Development - IBM Toronto Software Lab
Phone: 905-413-3056
Tie: 969-3056
Email: cdchen at ca.ibm.com
http://www.ibm.com/software/awdtools/fortran/xlfortran



From:	Damian Rouson <damian at sourceryinstitute.org>
To:	fortran standards email list for J3 <j3 at mailman.j3-fortran.org>
Date:	03/04/2015 13:52
Subject:	Re: (j3.2006) PURE function vs subroutine
Sent by:	j3-bounces at mailman.j3-fortran.org




      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).  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_______________________________________________
J3 mailing list
J3 at mailman.j3-fortran.org
http://mailman.j3-fortran.org/mailman/listinfo/j3
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.j3-fortran.org/pipermail/j3/attachments/20150305/5128f92f/attachment.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: graycol.gif
Type: image/gif
Size: 105 bytes
Desc: not available
Url : http://mailman.j3-fortran.org/pipermail/j3/attachments/20150305/5128f92f/attachment.gif 



More information about the J3 mailing list