@certik, I guess what I was really asking above is WHY is implementing the LEN PDT so hard and, in hindsight, are there any constraints that could have been imposed on the LEN parameter PDT that could have made its implementation easier and less buggy.
@rwmsu I am implementing it now, here are some tests to start with: test: add a test suite for len type parameters of PDTs by certik · Pull Request #19461 · lfortran/lfortran · GitHub and I’ll see what it takes to pass all tests and error tests. In general nothing is complicated in compilers, as long as you have the right design. If you have a bad design, you will be in a lot of pain later on.
Thanks, that makes a lot of sense and reinforces what I’ve suspected for a while now. Ie, most of the problems experienced with a lot of the newer Fortran features in some compilers are due to the underlying structure/framework of the compiler being fragile, poorly designed or maybe just poorly implemented and not a problem with the way a feature is defined in the Standard. Even experienced programmers can sometimes program themselves into a box that is very hard to get out of.
I tried writing a Fortran interpreter in Fortran using an OpenAI dot, but the design is poor, so it takes a lot more code to cover less of Fortran than ofort, written in C, does, as explained here by Opus 5.5, which also said what could be salvaged.
Is this an argument for writing the interpreter in C?
…
Where the language does matter
Points against Fortran:
- No tagged unions or cheap variant types. This is what slowed ffort’s evaluator: every value is a record with allocatable fields, so values get allocated and copied all the time. In C a value is a small struct with a union and costs almost nothing.
- No templates or standard containers yet. There are no generic hash maps, vectors or trees in the standard library. Fortran 2023’s templates aren’t available in compilers, so each container gets written again for each type.
- String handling is clumsy. Variable-length strings and string building take more code.
- Builds are slow and tooling is thinner. A release build of version 0.49 took about 16 minutes. There are fewer profilers and sanitizers, and compilers are less reliable with heavily nested derived types.
Points for Fortran:
- The host compiler does the hard numeric work. Arrays, intrinsics like
sumandmatmul, and kinds and numeric semantics all come built in.- Formatting comes free. An internal
WRITEgives exact Fortran output formatting, which C would have to reimplement. That matters for matching gfortran byte for byte.- It is self-hosting. Contributors who know Fortran can work on it, and extension modules can be ordinary Fortran.
A rough estimate, not measured: the language accounts for perhaps 1.5–2× in code size and a large factor in speed, the latter mainly through the value representation, which C makes much easier to get right. The remaining several-fold gap in coverage per line comes from the development approach.
I know very little about writing compilers, but I do know that this statement is very true also for application programming. If you have the foresight to start off with the right algorithms and the right data structures, things are much easier both in the present and in the future.
One thing I’ve noticed often is that algorithm choice is more or less a top-down kind of issue, while data structure is more or less a bottom-up kind of issue, so sometimes the best choice is for the two to meet somewhere in the middle levels of the program.
I Agree. There’s also the tight coupling assumptions that were just fine up to a certain standard, but conflict with the new one. Fortran 2008’s submodules is good at providing examples:
gforrtran’s propagation of the PUBLIC|PRIVATE attribute to the object file level (i.e., t versus T in *.obj and *.o) was okay for Fortran 2003, but caused issues with submodules, and it was seemingly too hard to fix properly/quickly.
ifort/fix’s global naming scheme for the CONTAINed procedures of a separate-module-subprogram breaks if it’s too large, since they never took into account that a submodule symbol might need more than 3x64-1 characters —the currently implemented hack to make it work consists of removing characters from the beginning of the global name and emitting a warning about the name having been truncated.
@rwmsu here is the fairly complete PDT implementation in LFortran that passes all the 77 tests above: Implement length type parameters of parameterized derived types by certik · Pull Request #19506 · lfortran/lfortran · GitHub, the ASR->LLVM backend is not too bad. But it takes thousands of lines in the frontend, about 2.5k lines in AST->ASR, plus 1.5k in two new ASR passes just for len PDT. That seems like a heavy price to pay for a feature that is rarely used. It does include about 900 lines of error checking. I’ll clean it up over the coming days and get it in. So there doesn’t seem to be any blocker or hard problem to solve.