[J3] Conditional expressions using intrinsic function

Ondřej Čertík ondrej at certik.us
Sun Jun 27 16:47:36 UTC 2021


Hi Robert,

On Sun, Jun 27, 2021, at 1:33 AM, Robert Corbett via J3 wrote:
> The intrinsic function MERGE
> almost works as a conditional
> expression.  If the MASK
> argument is scalar, most
> implementations evaluate
> only one of TSOURCE and
> FSOURCE.
> 
> I proposed adding a function
> COND that acted as a
> conditional expression.  COND
> had the form
> 
> COND(predicate, tsource, fsource)
> 
> My proposal did not get much
> support.

One idea is to use `?` instead of `ifthen`, then it is new syntax and not a new "function". The examples from the paper would look like:

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

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

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

> 
> I would like to see how
> short-circuit evaluation would
> be implemented using your
> scheme.  How would the
> statements
> 
> IF ( p1 .AND. p2 .AND. p3 ) THEN
> 
> and
> 
> IF ( p1 .OR. p2 .OR. p3 ) THEN
> 
> where .AND. and .OR. are
> short-circuit operators be
> translated into your
> scheme.

I think it would look like this for AND:

IF ( p1 .AND. p2 ) THEN
IF ( ifthen(p1, p2, .false.) ) THEN
IF ( ?(p1, p2, .false.) ) THEN

IF ( p1 .AND. p2 .AND. p3 ) THEN
IF ( ifthen(p1, ifthen(p2, p3, .false.), .false.) ) THEN
IF ( ?(p1, ?(p2, p3, .false.), .false.) ) THEN

and this for OR:

IF ( p1 .OR. p2 ) THEN
IF ( ifthen(p1, .true., p2) ) THEN
IF ( ?(p1, .true., p2) ) THEN

IF ( p1 .OR. p2 .OR. p3) THEN
IF ( ifthen(p1, .true., ifthen(p2, .true., p3) ) THEN
IF ( ?(p1, .true., ?(p2, .true., p3) ) THEN

Hopefully I didn't make a mistake.

> 
> Which of the three forms
> in papers 21-157 and 21-159
> was the most popular?

73 people voted so far, it was a multiple choice vote, the keyword form and Other (do not add this feature, or, pursue alternative forms) is about equal at 43-45%. The -> form got 24% and the ? form got 6%.

Ondrej


More information about the J3 mailing list