Skip to content

Initial draft of conditional expression intrinsic paper #213

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

Merged
merged 13 commits into from
Jun 27, 2021
56 changes: 56 additions & 0 deletions proposals/conditional_expressions/21-165.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
To: J3 J3/21-165
From: Ondrej Certik, Milan Curcic, Zach Jibben
Subject: Conditional expressions using intrinsic function
Date: 2021-June-27
Reference: 21-157, 21-159

Paper 21-157 proposes two syntactic forms for conditional expressions. The
paper 21-159 proposes an alternative form. This paper proposes to implement
conditional expressions using an intrinsic function.

The syntactic forms in 21-157 are too verbose, as argued in 21-159. The
form in 21-159 is concise, but based on the poll [1], it is not the most
popular either.

We propose the following syntactic form

<cond-expr> is ifthen( predicate, consequent, alternative )

where the `predicate` is a scalar logical expression and the `consequent`
and `alternative` are compatible expressions. The `alternative` can be
another `ifthen` as shown in an example below.

Examples:

res = ifthen(x >= 0.0, sqrt(x), -0.0)

res = ifthen(present(x), a, ifthen(present(b), b, 0))

Objections raised in the past:

1) It is harder to do chaining due to the extra parenthesis at the end.

Answer: it seems in practice it is not too bad, as in the second example
above. Here is a longer chain:

res = ifthen(present(x), x, ifthen(present(y), y,
ifthen(present(z), z, 0)))

2) `ifthen` does not behave like a regular function, it is effectively new
syntax, so we might as well actually introduce new syntax, to make it less
confusing

There is a precedent in optional arguments, where the argument is not
evaluated but passed through through nested functions until it hits the
function `present`. In a similar way, the arguments to `ifthen` are not
evaluated ahead of time, but rather passed into the function `ifthen`
itself, similar to how `present(x)` works. Both `ifthen` and `present` must
be implemented by the compiler itself, they cannot be implemented in
standard Fortran code.

References:

[1] https://fortran-lang.discourse.group/t/
poll-fortran-202x-conditional-expressions-syntax/1425