Generics : instantiate type from template?

Hello all and thanks for the lively discussion of present & future Fortran.

This is my first post. I am a scientist at an atmospheric sciences lab (LMD/IPSL in France) involved in the present and future of our climate modelling software, which represent a large codebase of not-so-modern Fortran. I am concerned with the viability of Fortran for our work, and at the same time willing to advocate at my institution for more involvement in Fortran tooling and Fortran features.

I have looked with interest at the proposed generic features for Fortran. It seems very well thought-out with a lot of potential, thanks for the great work. For my own education I sketched a generic implementation of a Runge-Kutta time stepping scheme and tried compiling with lfortran. However I get :

“semantic error: Only functions can be instantiated”

In the current proposal, is it possible to define a derived type in a template, and then use it in a subroutine using or instantiating the template ? I think I have seen such examples in the Discourse, but unfortunately cannot find them anymore.

Thanks,
Thomas


module template_add_m

    requirement R(T)
        type :: T; end type
    end requirement

    template try_t(T)
        requires R(T)
        type my_type_t
            type(T):: x
        end type
    end template

contains

    subroutine test_template()
        instantiate try_t(real), only: my_type_t
    end subroutine
    
end module
2 Likes

This is a separate issue regarding the ability to write generic functions.

There are several fortran intrinsic functions where the programmer can specify the returned kind with an argument: REAL(X,KIND=RK), INT(X,KIND=IK), SIZE(X,KIND=IK), and so on. There are several of these functions that work like this because it is obviously a useful feature.

However, it is not possible for a programmer to write a function that works this way. When designing and writing generic functions, it seems like this would be a good feature for programmers to have too. Is there any way that this feature could be included along with, or within, the ongoing generic template proposal?

The general idea is that when the compiler sees F(X,KIND=RK), then it would check to see if it has already compiled the template F() with that particular argument combination, and if not, then it would compile it at that time, and then establish the appropriate reference and the argument association.

If I understand the present proposal correctly, then the code should be:

module template_add_m

    !public :: test_template    

    template try_t(T)
       type, deferred :: T
       !public :: my_type_t
       type my_type_t
           type(T):: x
       end type
    end template

contains

    subroutine test_template()
        instantiate try_t(real), only: my_type_t
    end subroutine
    
end module

Note: edited as per the suggestion below.

Thanks @AniruddhaDas . I tried your changes in the online lfortran and get the same error message.

The error is thrown from here

So it seems it is indeed forbidden in the present prototype. But maybe planned as a future feature ? It seems an essential feature to make templates fully usable.

It is really difficult to keep track of all the things that has been proposed for generic features some are:

  1. Keyword arguments for deferred procedures in templates: 23-163.txt
  2. Pushing the usability of templates: 23-166r1.txt
  3. Rank agnostic nested loops: 23-165r1.txt
  4. Specs for rank-agnostic intrinsics : 23-167.txt
  5. Generic Iterators and Accessors for F202Y : 23-181.txt
  6. Expanded Requirements : 23-182r1.txt
  7. More rank-independent functionality: 23-184r1.txt
  8. F202Y Use of keywords with deferred procedure argument : 23-185r1.txt
  9. Shorthands for Simple Templates : 23-187.txt
  10. Possible Solutions to Long Templates : 23-188.txt
  11. F202Y allow polymorphic deferred type : 23-189.txt
  12. Properties for requirements : 23-190.txt
  13. F202Y Further Refinements of Lower Bounds in Requirements : 23-191r1.txt
  14. Packaging long argument lists of templates : 23-202.txt

I might have missed some, so maybe we should wait and see what the feature set might be :grinning:

1 Like

This would be a valid template under the current proposal (although the public statements are unnecessary as written). LFortran is still just doing some early prototyping (albeit greatly appreciated), so unimplemented features and bugs are expected at this stage.

2 Likes

Thanks. We are working on instantiating derived types here: Support struct in template by ansharlubis · Pull Request #1784 · lfortran/lfortran · GitHub.

@dubos if you want to help with the LFortran implementation (I noticed you figured out where the error message is coming from), we would really appreciate it! The more hands on this, the better.

You can help by reporting bugs as well as sending PRs. I can help with answering any questions and help getting you up to speed.

Thanks everyone for trying this out!

2 Likes

Nice. The truth is, as soon as you taste the existing features, you wish you already had the missing ones. My skills will stop at bug reports, but I will ask a colleagues with compiler skills if he can spend some time on lfortran.
Beyond bugs I will be happy to contribute examples using template features such as the RK scheme that I had initially in mind.

If I have remarks on the proposed features for 202Y, is this discourse the appropriate channel ?

Cheers

2 Likes

This forum is a good channel. Some of the committee members (myself included) frequent this forum, and many of us like it for the purposes of having open/public discussions with the broader community.

2 Likes

When programmer uses a preprocessor to generate fortran code from a template, the output source code is available for inspection, and after it is compiled, it can be debugged in the usual ways (interactively, or with print statements, etc.).

Does the proposed fortran template facility allow any way to inspect and debug the output code (i.e. after all deferred types and operators have been expanded)?

Perfect! Yes, bug reports are great. Both for valid code that should compile but doesn’t, and for invalid code that shouldn’t compile but does (or the error message should be improved).

Yes, this forum is perfect.

If I have remarks on the proposed features for 202Y.

The last ISO-IEC/SC22/WG5 meeting that a French delegation attended was the one they hosted at Chateau de Cadarache in 1999.

(And the last meeting that a Finnish delegation attended was the one they hosted in Oulu in 2000.)

Even better than the Fortran discourse forum would be to re-form a French delegation (and a Finnish one) and resume participation in standard development and maintenance.

2 Likes