-
Notifications
You must be signed in to change notification settings - Fork 21
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
Add short-circuit evaluation mode to expression evaluator #271
Comments
Good idea. Worth noting that test/EvalSpec.hs in fortran-vars has tests relating to this that show the shortcircuting behaviour is more powerful in an analysis setting. |
Although it seems that short-circuiting behaviour is the exception rather than the norm when it comes to compilers: https://www.scivision.dev/fortran-short-circuit-logic/ |
A concise example from @dorchard that demonstrates short-circuiting changing behaviour: program ShortCircuit
implicit none
logical :: k
if (.true. .OR. effects()) then
end if
if (.false. .AND. effects()) then
end if
contains
function effects()
logical :: effects
write(*,*) "HELLO"
effects = .false.
end function effects
end program ShortCircuit
|
Just a comment from a standards dweeb: Here's the relevant text from the Fortran 2018 standard:
So, a programmer shouldn't rely on side effects in the RHS of The referenced link at scivision.dev has a statement that I believe is not correct.
Short-circuiting is indeed standard-conforming behavior. Just not common enough for programmers to avoid the pitfalls. |
This was present in fortran-vars' evaluator. Currently, all arguments for functions and operators are eagerly evaluated. (I picked this because it was easier to write, since I couldn't find any relevant standards/expectations for compilers.) It would be straightforward to add a switch for selecting between short-circuit and eager-evaluation.
We would add the switch by extending the evaluator monad to include some
Reader
environment -- this was always the plan, we simply didn't have any config yet. Then the binary operator and function call evaluation code would be changed to inspect the current mode and provide the appropriate behaviour.The text was updated successfully, but these errors were encountered: