| FortranFan
February 10 |
TL;DR: Start small with exception handling in Fortran 202Y will be my recommendation.
For whatever it’s worth, I do not at all like the working approach with this paper by @vsnyder :
- It immediately jumps into a “solution mode” with very little to poor treatment of the desired use cases,
The history of the world, or of Fortran, does not start anew with each new revision of the standard.
The history of exception handling is so long that we probably don’t need remedial tutoring about its fundamental principles. John Reid described block-structured exception handling for Fortran 2003, using integers instead of enumerators to identify exceptions, at the 1998 meeting in Trollhättan. Unfortunately, this was an oral presentation, and there was no meeting paper. Discussion of it had certainly preceded his proposal (e.g., see WG5 papers N1052 and N1257).
The lists of “use cases” are the lists of enumerators in Sections 5.1 and 5.2 in 23-106, each of which identifies an error condition specified by the standard. If one wants to understand the details of error conditions, one can read 23-007. Further explication in 23-106 would be needlessly duplicative.
- Besides, the solution approach laid down in the paper appears rather outdated. A lot of “water” has flown since the paper was first put together and slapping a recent date on it does not make it any current or relevant for future.
An explanation of a newer alternative methodology, along with a detailed description how to integrate it compatibly into Fortran, would be interesting to see, in a different paper.
We already have the older methodology of status variables, but they can’t catch things like failing to elaborate the specification part of a procedure or BLOCK construct because some automatic variables are too big.
- A lot of effort has gone into the standard with IEEE floating-point exceptions that at least for now, floating-point exceptions need not be covered in the above-mentioned use cases, at least during first revision of exception handling in the standard,
There are many use cases listed in Sections 5.1 and 5.2 in 23-106, in addition to IEEE floating point exceptions. Just last week, I had a code that failed at the SUBROUTINE statement of an internal subroutine. It took significant time to work out that an error in the specification of an array had resulted in failure to materialize all the automatic variables. Printing an exception would have been more informative than SEG FAULT. In many applications, being able to catch this particular failure would allow one to use a slower but less memory-intensive algorithm, instead of printing a mysterious SEG FAULT message.
- That is, any initial treatment of exception handling in Fortran 202Y (hopefully this is the revision that will introduce something; if not, then Fortran 203X) should study all the use cases closely and consider working toward a few simpler cases only based on guidance from compiler implementations re: run-time cost and also the possibilities of prototyping such as with LFortran based on feedback from @certik and team.
Many other languages have had block structured exception handling, some for several decades. As Kenneth Louden noted in Programming Languages: Principles and Practice, design questions were largely resolved about thirty years ago. The experience with Ada compilers, and perhaps compilers for other languages, is that block-structured exception handling can be implemented with very small execution-time overhead unless and until an exception occurs. Indeed, the GNU Ada Translator developers claim that with their processor, there is zero execution-time overhead unless and until an exception occurs.
Surely Fortran processor developers are able to learn from the developers of processors for other languages. I can provide contact information for members of WG9.
In terms of use cases, my suggestion(s)
- for the first case is when an
ERROR STOP
statement instruction takes place in a library method i.e., when “error termination” is initiated by a Fortran processor. Here the use case will be for the caller to be able to supply a “handler” method, perhaps with certain defined interfaces (a la defined IO), and which then gets executed with some defined semantics during error termination. The caller can then complete needed actions in this handler. A loose analogy might be the FINAL
procedure with finalizable types.
Starting small and promising to finish the job in ten or twenty years was the reason that people fled from Fortran to C and C++ and Python and … starting about 1988. PLEASE, NO MORE HALF MEASURES IN FORTRAN! And no more kludges. If we had done templates before 2003, we wouldn’t have provided for derived types with kind type parameters, which invite one to write almost-perfectly reasonable code that cannot be compiled. (Simply declare an object using a kind type parameter for which there is a missing type-bound procedure with the required spectrum of kinds of its dummy arguments. Richard Maine warned of this before 2003). If we had provided for decisions embedded in expressions, with the ability to calculate whether an actual argument is present, we wouldn’t have done the kludge of a disassociated pointer or deallocated allocatable variable corresponding to an optional nonpointer nonallocatable dummy argument being considered to be absent. (I proposed embedded decisions before 1990, but MERGE existed and I didn’t at the time appreciate the difference between eager and lazy function argument evaluation so I let it go.)
But if you insist on not doing block-structured exception handling now, print material about alternate returns in full-size type, because that’s the only exception handler that Fortran currently has, other than ERR= and END= in input/output statements.
- a companion to case 1 i.e., when a Fortran subprogram is invoked by a processor other than a Fortran main program, then to extend the handler scheme toward some procedure interoperable with a
C
companion processor.
As far as I am concerned, if Fortran 202Y can cover just these 2 use cases, it will more than satisfy the 80-20 rule (Pareto principle) with the needs we have noticed in industry with Fortran codes (primarily libraries aka DLLs).
A handler as described in 23-106 can invoke a procedure defined by the companion processor. There is no need to be able to specify that a handler is provided by a companion processor. Whether it is possible to raise an exception that one expects to be handled by a main program or calling procedure defined by a companion processor should be processor dependent, not mandated by the standard.