[J3] Evaluation of arguments to MERGE
Robert Corbett
rpcorbett at att.net
Thu Mar 28 06:36:02 UTC 2024
I read a paper by John McCarthy in which he asserted that he had added an intrinsic function XIF to a FORTRAN compiler. His XIF function was equivalent to the scalar version of MERGE. He further asserted that experience with XIF led him to propose adding conditional expressions to the International Algorithmic Language IAL. IAL was later renamed Algol.
Bob Corbett
> On Mar 27, 2024, at 2:34 PM, Van Snyder via J3 <j3 at mailman.j3-fortran.org> wrote:
>
>
>>
>> On Wed, 2024-03-27 at 17:32 +0000, Clune, Thomas L. (GSFC-6101) via J3 wrote:
>> I have a colleague that is asking about the behavior of MERGE in the program below. The intent of the statement is to avoid divide-by-zero when the FLAG argument is false. This appears to work as intended with most compilers, but under certain flags at least one compiler produces a floating invalid.
>>
>> I had thought maybe there was special language in the standard about evaluation of the arguments to MERGE, but I did not find any. Therefore, I believe a standard conforming compiler is permitted to evaluate the TSOURCE argument even when FLAG evaluates to false. If I’m wrong about this, please point me to where in the standard that explains this, otherwise I will relay this as a quality-of-implementation concern to my colleague.
>
> Subclause 15.5.3 Function Reference says "When it is invoked, all actual argument expressions are evaluated, then the arguments are associated, and then the function is executed."
>
> When Fortran 90 was being developed, I advocated for what we finally did, that is, conditional expressions. I was told we didn't need them because of the existence of the MERGE intrinsic function. Back then, I didn't appreciate the distinction between eager and lazy evaluation of procedure arguments. All Fortran procedure invocations, including intrinsic ones, use eager evaluation, as explained in 15.5.3.
>
>>
>> program mergetest
>> implicit none
>> real*8 :: a,b,c
>> call random_number(a)
>> b = 1d0
>> a = a - a
>> c = merge(b/a, 0d0, a > 0d0)
>> write(6,*) c
>> end program mergetest
>>
>> Thanks,
>>
>> Tom
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.j3-fortran.org/pipermail/j3/attachments/20240327/430134b3/attachment.htm>
More information about the J3
mailing list