Interpreter syntax to make real64 the default

In a Fortran interpreter, it is clunky to type

implicit none
integer, parameter :: dp = kind(1.0d0)
real(kind=dp) :: x, y
y = 2.0_dp*x

so I want ofort to behave as follows when emitting source code from an interactive session:

  • --default-real64=dp means default real is exported as real(kind=dp).
  • Unsuffixed real literals are exported with _dp.
  • Existing explicit kinds are preserved.
  • If the program already defines dp, ofort should either reuse it if compatible or emit an error/warning.
  • If the program already imports real64, do not duplicate the import.
  • insert implicit none
  • insert :: in declarations

transforming

real x, y
y = 2*x

to

use, intrinsic :: iso_fortran_env, only: real64
implicit none
integer, parameter :: dp = real64
real(kind=dp) :: x, y
y = 2.0_dp*x

I wonder what people think of these conventions. Probably @certik has thought about this issue for LFortran.

1 Like

We now have --infer which infers the type from the RHS:

/tmp$ lfortran --infer
Interactive Fortran. Experimental prototype, not ready for end users.
LFortran version: 0.63.0-287-g47dbbe6d8e
  * Use Ctrl-D to exit
  * Use Enter to submit
  * Use Alt-Enter or Ctrl-N to make a new line
    - Editing (Keys: Left, Right, Home, End, Backspace, Delete)
    - History (Keys: Up, Down)
>>> a = 5                                                                1,6   ]
>>> b = 6.3                                                              1,8   ]
>>> a                                                                    1,2   ]
5
>>> b                                                                    1,2   ]
6.3000002

And currently we treat 6.3 as single precision, but possibly should switch to double. The question becomes how to handle this with existing codes, because I have a feeling it will be confusing if the interpreted behavior is different to ahead-of-time-compiled behavior. Right now LFotran’s behavior in interpreted mode (especially without --infer) is identical to how it behaves when compiling your existing code.

I want to warn by default when the precision is not correctly specified, and this is related to it. I think there is a way to achieve a robust behavior with warnings.

2 Likes

In LFortran, is there an option to specify the default real kind? In gfortran, one can specify that with -fdefault-real-8 (and -fdefault-double-8 to prevent the promotion of 8 byte to 16 byte reals).

Syntax to specify default kinds is included as a work item for the next revision (F2028). See j3-fortran.org/doc/year/26/26-108r1.txt and j3-fortran.org/doc/year/26/26-109r2.txt

3 Likes