Re: function : question on argument for output



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


.



Relevant Pages

  • Re: Why C?
    ... > C programs can be as fast as a compiler can make them, ... > programs in other languages. ... > lurking bugs and inappropriate data types in any existing C code. ... With only a different declaration syntax it would be much ...
    (comp.os.linux.development.apps)
  • Re: Why do folks implement statically typed languages?
    ... The languages discussed are extremes. ... It allows the compiler to prove that your data-manipulations are ... an IDE for a dynamically-typed language can ... to IDEs in dynamically-typed languages. ...
    (comp.lang.scheme)
  • Re: OO compilers and efficiency
    ... > will be difficult for a compiler to do a good job of spotting when the OO ... "simpler" languages, ... I agree that Java ... (including adding lexical closures) ...
    (comp.programming)
  • Re: Delphi 2008 native?
    ... That's a function of the particular compiler you use. ... Once upon a time there were a number of languages you could use to write ... Very impressive I'm sure, all that hardware grunt. ... but society places value on humans remaining in touch with reality. ...
    (borland.public.delphi.non-technical)
  • Re: Why is C good??? any experts?
    ... it to maintain someone else's code is a completely different matter. ... > hodge-podge of exported subroutines and messy global variables. ... Compilers and, to a lesser extent, languages ... For example, to compute any algebraic expression, a compiler will ...
    (comp.lang.c)

Loading