[J3] surprisingly PURE
Malcolm Cohen
malcolm at nag-j.co.jp
Wed Apr 22 03:16:27 EDT 2020
> Pointers allow one to do things with pure procedures that one might think violate the "principle" of PURE.
For Fortran 95/2003, I do not agree. It’s straightforward to use pointers safely, and a pure procedure cannot easily violate the no-side-effect rule by using pointers – that’s why the rules constraining what you can do with a pointer are there for.
Your example does not have any pure procedures, I guess that’s an oversight. It doesn’t do anything unexpected either. It’s actually possible to use that routine correctly.
Of course there is a BIG difference between the user passing in a pointer (naturally to an outside variable), and the routine magicking up a pointer to a global variable itself.
Of course it’s also possible to avoid the problems with polymorphism via constraint, just forbid MOLD= to be polymorphic or of a type with a bad pointer definit. I’d think that would not be very popular, *but it is possible*. For that matter, we could declare that default initialisation to a target was a mistake from the start, and delete it from the language; that is likely to be even less popular!
Of course there is value in PURE providing constraints that stop the user from (usually carelessly, rarely maliciously) writing a PURE procedure with side-effects and indeterminate results on parallel execution. In this case we’d be balancing the safety that we used to have in Fortran 95/2003 against the very minor convenience of default initialisation of pointer targets to a global variable.
I really do think that separating the polymorphic issue from the basic one is necessary for the initial discussion. It’s going to be complicated enough working through the basic cases, without dumping in any extra complication. Polymorphism just seems to confuse enough people that those issues should be discussed later.
And going into Despair Mode (that’s how I read “is there really anything [we] can do”) because of polymorphism sort-of underlines the need to do the basic case first. And carefully.
Cheers,
--
..............Malcolm Cohen, NAG Oxford/Tokyo.
From: J3 <j3-bounces at mailman.j3-fortran.org> On Behalf Of Van Snyder via J3
Sent: Wednesday, April 22, 2020 3:13 PM
To: j3 at mailman.j3-fortran.org
Cc: Van Snyder <van.snyder at jpl.nasa.gov>
Subject: Re: [J3] surprisingly PURE
Pointers allow one to do things with pure procedures that one might think violate the "principle" of PURE.
One can pass an object of a type that has a pointer component, with the component associated with a variable that's not mentioned in the argument list, and then the procedure can assign a valueto that variable (or undefine it).
real, target :: X = 1.0
type :: T
real, pointer :: X
end type T
type(t) :: V
print *, 'Before call, X =', x
v%x => x
call s ( v )
print *, 'Afer call, X =', x
contains
subroutine S ( A )
type(t), intent(inout) :: A
a%x = 42.0
end subroutine S
end
This has been with us since the beginning of PURE, but some might say "that's not pure!"
Is this really different from changing X by way of a pointer component with default initialization that associates it with X?
Bob's example is a bit diffent because it doesn't involve a side effect using a dummy argument that some might argue violates the spirit of "pure."
But, taking these together, and the potential problems with polymorphism that appear not to be addressable with constraints, is there really anything the standard can do to plug every loophole?
Disclaimer
The Numerical Algorithms Group Ltd is a company registered in England and Wales with company number 1249803. The registered office is: Wilkinson House, Jordan Hill Road, Oxford OX2 8DR, United Kingdom. Please see our Privacy Notice <https://www.nag.co.uk/content/privacy-notice> for information on how we process personal data and for details of how to stop or limit communications from us.
This e-mail has been scanned for all viruses and malware, and may have been automatically archived by Mimecast Ltd, an innovator in Software as a Service (SaaS) for business.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.j3-fortran.org/pipermail/j3/attachments/20200422/feac9b99/attachment.htm>
More information about the J3
mailing list