-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Short-circuiting logical expressions #19
Comments
Another option to consider is adding an optional keyphrase like Personally I'd be happy to go as far as enforcing left-to-right short-circuiting in all logical expressions, which would adopt the behavior of other programming languages like C, C++, Python, Java, Lisp, Perl, JavaScript, Julia, R, Haskell, etc etc. My argument would be for more predictable codes and better programmer control. And, this wouldn't break existing programs because there currently isn't any standards-defined behavior for those programs to rely on (that I know of). Of course, as you mentioned this is controversial. Some Fortran programmers like that some compilers might rearrange their logical expressions and short-circuit in some optimal-ish order, though other compilers might evaluate the entire statement regardless. And, breaking troublesome if-statements into multiple lines as below is usually not very disruptive. So there is the question of whether this is really worth rocking the boat over. if (i < size(a)) then
if (a(i) == 0) ...
end if |
The way to move this forward would be to gather use cases from real codes, and see how many times the compiler can optimize the logical expression well, versus let the programmer do it explicitly. There might perhaps be a compiler option already to turn this on and off --- in which case one can measure the speed of the code / benchmark it. That would answer the question whether compilers should optimize the logical expression or not. |
As an implementor, I think that it would increase portability to our new compiler if we were to always implement short-circuiting in specific cases, viz.: scalar left operand to If people want to invent explicit short-circuiting logical operators, I should point out that you don't have to necessarily give them their own new levels in the operator precedence hierarchy; e.g., |
I think introducing more and more switches like "shortcircuting on", "implicit save off" etc is a very dangerous practice and should be avoided. There should be one set of rules. Is the short circuting only a matter of introducing a new operator/function? From what I remember, this issue was brought up like 20 times on c.l.f, but problem was that the language does not guarantee the order or even fact of evaluation of any expression, therefore this one new operator would have to be one exception. Which makes me think that an entire new construct with defined execution order must be introduced to not rewrite half of existing standard. I don't think it should be an operator like I suggest something along the lines of: ! what we want to do
if (present(x) .and. x > 10) x = 10
! warning: requires changing the standard
if (provided(present(x), x > 10)) x = 10
! will crash or not?
if (present(x).and.present(z).and_then.x>z+3) ... Other option is, since 95% of examples I see are with |
I'd also prefer, not to have an additional
I think the message such lines send are disastrous: 'You need to do a lot of gimmics to make sure, that Fortran feels like a modern language'. When teaching Fortran, I feel already ashamed for having to explain, why the |
The committee has discussed short-circuiting many times. The sentiment is generally against implicit short-circuiting, as the standard currently allows evaluation of any equivalent expression to any degree of completeness. Requiring short-circuiting would hinder some optimizations. Instead, WG5 has already approved for the 202X worklist explicit short-circuiting. The current proposal is 18-239. |
How about a SIF statement? like IF, but impliments short-circuiting. |
I have recently just fixed many short-circuit issues in our codebase which were dormant for years and were only discovered because we recently enabled more aggressive initialization. I mention this because people are already writing code as if it short circuits, and default behaviour often does not catch it as an issue. |
Here are the papers that were planned for the February Meeting #155. https://j3-fortran.org/doc/year/18/18-274.txt |
Note the above papers also discuss conditional expressions, such as a ternary operator which does not evaluate the arguments like |
It might become problematic. Enabling short circuiting on the top of a subroutine or module pollutes the whole scope, and potentially corrupts the behavior in the whole scope. I like the proposals of |
Currently one cannot write things like:
Because the compiler is free to evaluate
a(i)
first even ifi
is out of bounds. This proposal is to introduce.andthen.
and.orelse.
operators which will allow the above code to be written as:An alternative proposal to this is conditional expressions (#12) which got rejected.
There is also an option to make
.andthen.
just.and.
, the argument pro is that it does not introduce any new syntax, but an argument against it is that it would change the current freedom for compilers to re-arrange logical expressions.The text was updated successfully, but these errors were encountered: