Re: function : question on argument for output
- From: "James Giles" <jamesgiles@xxxxxxxxxxxxxxxx>
- Date: Mon, 08 May 2006 20:54:27 GMT
Jan Vorbrüggen wrote:
....
I'm firmly in the "functions should be pure" camp. There certainly are
languages in productive use around that have this feature; one I have
used is occam2.
I havent even mentioned which camp I'm in on that issue. Fortran
permits functions to be written with side-effects. To claim that
those need not be performed when the function is evaluated is
to deliberately set a trap. That's the worst thing a language designer
can do.
Given the way that a compiler is already allowed to short-circuit
logical expressions as it sees fit - and one of my first programming
jobs about a quarter-century ago was to remove invalid code that
relied on side- effects to happen in such cases, so this isn't just
academic - I see it difficult to argue why this shouldn't be the case
for an agressively optimizing compiler in expressions. [...]
That's a different case. If the optimizer removes the function
reference *entirely* (because of short-circuited operators
or other permitted optimizations), clearly the function's side-effects
can't happen. Among the many things that can't happen if the
function is not referenced at all is that the function can't generate
an error that needs reporting.
The position of the other side in this discussion is that an
implementation may decide not to do function side-effects
*at* *all*. That is, even when such issues as short-circuit
operators and "mathematically equivalent expression
"transformations *can't* apply, and the function very
clearly *is* referenced, the side-effects might not occur.
I've never found a compiler vendor that wouldn't agree
that such a thing was just a bug.
To be sure, the vague rules of "mathematically equivalent
expressions" and such are themselves a trap for the unwary.
Back when we didn't have PURE, compilers couldn't (or
didn't) do data flow analysis, and other such failings, that
kind of rule served a purpose. Now, there's even a feature
being added which does the short-circuiting of logicals
*explicitly*. (Most other languages that allow short-circuit
of logicals do so explicitly. They specify the the order of
the evaluation of an expression when such operations are
in use. Fortran should follow suit.) We've reached a time
when the vague rules of §7.1.8 no longer serve a useful purpose.
OTOH, I do believe Giles has a case here that the implementors
mightn't follow suit because it breaks too much code.
They'll never decide to skip the side-effects in legally written
impure functions. I've never seen a compiler that did so. And
I've never talked to an implementor that didn't agree that it would
simply be a broken compiler if it did.
There are also vendors that have mostly ceased short-circuiting
and other applications of §7.1.8 because there are better uses of
their time. Such "optimizations" often don't. These also sometimes
break people's code, but that's not the usual reason that the
implementors have ceased doing them.
--
J. Giles
"I conclude that there are two ways of constructing a software
design: One way is to make it so simple that there are obviously
no deficiencies and the other way is to make it so complicated
that there are no obvious deficiencies." -- C. A. R. Hoare
.
- Follow-Ups:
- Re: function : question on argument for output
- From: Jan Vorbrüggen
- Re: function : question on argument for output
- References:
- function : question on argument for output
- From: Jamming
- Re: function : question on argument for output
- From: Richard Maine
- Re: function : question on argument for output
- From: Gary L. Scott
- Re: function : question on argument for output
- From: Jan Vorbrüggen
- function : question on argument for output
- Prev by Date: Re: Fortran/C string interop?
- Next by Date: Re: function : question on argument for output
- Previous by thread: Re: function : question on argument for output
- Next by thread: Re: function : question on argument for output
- Index(es):
Relevant Pages
|
Loading