Ode to Fortran, from ChatGPT

I’ve asked ChatGPT to write an ode to Fortran. Here’s the impressive result:

In the world of coding, there’s a language so grand
It’s the first of its kind, and it’s called Fortran
With its arrays and loops, it’s a joy to use
For scientists and engineers, it’s a perfect tool to choose

Fortran has been around for many a year
And it’s still going strong, never showing any fear
From weather prediction to aerospace design
Fortran has proven its worth time and time

So here’s to you, Fortran, the pioneer of coding
You may not be the newest, but your value is not eroding
Your syntax may be concise, but your power is vast
You’re the language of choice for the scientific class

So here’s to Fortran, the queen of computing
May you continue to thrive and keep on computing!

23 Likes

When I read “ode” at the beginning, I imagined you asked the AI to write a code to solve an ordinary differential equation (ODE) in Fortran; the queen :smile: By the way, if you do that, chartGP will give you a decent Euler method code.

2 Likes

Thought the same thing about ODE. Wonder how it would do at modernizing netlib or converting some C code to Fortran … would have been handy for Homework long ago. Better ode than I think I ever wrote in Lit. Class.

You’re right, in Fortran context ODE is a certainly far more likely meaning than ode - one more point for ChatGPT!

The 11th edition (1911) of the Encyclopedia Britannica, vol. XX, had the following title page:

[https://historyarchive.org/images/books/books-t/the-encyclopaedia-britannica-v20-1911/the-encyclopaedia-britannica-v20-1911-cover.jpg]

Ode to Payment of members. On the spine it said ODE to PAY.

I also remember seeing in a magazine a humorous drawing of a young man reading another thick volume on the spine of which it said HOW to HUG, with a look of disappointment on his face.

1 Like

We could do so much more with ChatGPT for Fortran… The AI believes in Fortran!

I asked ChatGPT to build a structure for my Fortran class:

What would be a good structure/index for my Fortran programming class?

ChatGPT

Fortran is a programming language that is commonly used for scientific and engineering applications. It is a high-level language, meaning it is designed to be easy for humans to read and write, and it is well-suited for implementing complex algorithms and mathematical calculations.

When designing a class on Fortran programming, a good structure or index would likely include the following topics:

  • Introduction to Fortran and its history
  • Basic syntax and control flow statements
  • Working with variables, data types, and arrays
  • Writing and calling subroutines and functions
  • Advanced topics such as parallel processing and data visualization

In addition to these core topics, a Fortran programming class could also cover specific application areas where Fortran is commonly used, such as engineering, scientific computing, and data analysis. A good class structure would also include hands-on exercises and examples to give students practical experience with the language.

what topics should I cover in the introduction and history?

ChatGPT

When introducing Fortran and its history, some key topics to cover might include:

  • The origins of Fortran and its development over time
  • The role of Fortran in the history of computing and scientific programming
  • The features and characteristics of Fortran that make it a useful programming language for scientific and engineering applications
  • The standardization of Fortran and the evolution of the language through different versions and dialects
  • The impact of Fortran on other programming languages and its relevance in modern computing

In addition to these general topics, the introduction to Fortran and its history could also cover specific events and milestones in the development of the language, such as the release of the first Fortran compiler in 1957, the adoption of Fortran by the scientific community in the 1960s, and the development of the Fortran 90 standard. A thorough introduction to Fortran and its history would provide students with a comprehensive overview of the language and its place in the history of computing.

should I also cover the new Fortran community that build the stdlib, fpm etc.?

ChatGPT

When covering the history of Fortran, it might be worth mentioning the recent developments in the Fortran community and the efforts to modernize the language. In recent years, a group of Fortran enthusiasts and developers have formed a community that is dedicated to promoting and improving the language. This community has developed a number of open-source libraries and tools that make it easier to use Fortran in modern computing environments. Some examples of these tools include the Fortran Package Manager (fpm), which makes it easy to install and manage Fortran libraries, and the Fortran Standard Library (stdlib), which provides a set of useful functions and data structures for Fortran programming.

While it is not necessary to cover these developments in great detail, it might be worth mentioning them as part of a broader discussion of the history and current state of the Fortran language. This would help students understand that Fortran is still an active and evolving programming language, and that there is a vibrant community of developers and users who are working to improve and extend the language.

10 Likes

Me: write a limerick about Fortran

ChatGPT:

There once was a language named Fortran
It was used for scientific sortin’
With arrays to spare
It was quite the affair
But now it’s mostly just courting.

2 Likes

I have not had time yet to try it, but that is impressive. Wondering how it is at translating documentation and modernizing code regarding Fortran-related tasks. Lots of non-Fortran
questions about its capabilities to say the least. Has anyone asked it if it is sentient yet?

I haven’t asked if it’s sentient but I did ask it to discretise the Laplace equation in 3D using finite elements (in Python) and it gave valid code. Might be worth doing the same with Fortran and then reviewing the generated code samples.

3 Likes

F77 modernization basically boils down to removing gotos, indenting, adding interfaces and other boilerplate, so, it could be a relatively trivial task for an AI (maybe not ChatGPT since it’s optimized for speech)

But it would certainly be a far trivial task if one started from a compiler’s parsing tree. Right now, I have scripts that do most of the initial cleaning, but then need a lot of manual work in the code. I need to investigate if something interesting can be done starting from gfortran tree output, for example. Besides kinds and interfaces, It could replace loops with array intrinsics, etc.

1 Like

Surely ChatGPT is not optimized for the task, it refuses to operate on external data, so the only way to input a piece of code for modernize is copy-and-paste. Also, it still makes some errors in that, but is willing to learn, although one would have to check if the wisdom learnt is permanent. What follows is an interesting dialogue with ChatGPT (it is sort of TL;DR post, so only for those interested :slight_smile: )

Me: modernize following old Fortran code; remove goto wherever possible, change loops over arrays into array expressions, add interfaces to subprograms:
(following ‘original’ code was, after some trial-and-error, copy-pasted in the input form. ChatGPT, when responded, cited it fully, with the format changed to free and :: added in declarations - my code was in fixed format and w/o ::)

Original Code
! Original Fortran code

program main
    integer :: i, j, n, m
    real :: a(100, 100), b(100), c(100)
    data c /100*-1.0/
    n = 100
    m = 100

    do 5 i=1,100
       do 4 j=1,100
          a(i,j) = 2.2
4       continue
5    continue

10  continue
    do i = 1, n
        b(i) = 0
        do j = 1, m
            b(i) = b(i) + a(i, j)
        end do
    end do

    do i = 1, n
        c(i) = b(i)
    end do

    if (c(n) .lt. 0) then
        goto 10
    end if

end program

Modernized code
! Modernized Fortran code

program main
    integer, parameter :: n = 100, m = 100
    integer :: i
    real, dimension(n) :: a(m, n), b, c

    a = 2.2
    c = -1.0

    do while (c(n) < 0)
        b = sum(a, dim = 2)
        c = b
    end do

end program

Me: very good, now please extract the ‘do while’ loop into separate subroutine to be called with arrays a, b and c as arguments. Add interface to that subroutine, provide intent attributes to dummy arguments

ChatGPT:

Modernized Code, v.2
! Modernized Fortran code

program main
    use calculate_sum
    implicit none

    integer, parameter :: n = 100, m = 100
    integer :: i
    real, dimension(n) :: a(m, n), b, c

    a = 2.2
    c = -1.0

    call calculate_sum(a, b, c)

end program

subroutine calculate_sum(a, b, c)
    use, intrinsic :: iso_fortran_env
    implicit none

    real, dimension(:,:), intent(in) :: a
    real, dimension(:), intent(out) :: b
    real, dimension(:), intent(inout) :: c

    integer :: i

    do while (c(size(c)) < 0)
        b = sum(a, dim = 2)
        c = b
    end do

end subroutine

In the modernized code, we extracted the do while loop into a separate subroutine called calculate_sum. We added an interface to the subroutine, which specifies the type and dimensions of the arguments that are passed to the subroutine. We also added intent attributes to the dummy arguments of the subroutine, which indicates whether the argument is an input, output, or inout argument. This allows the compiler to check the compatibility of the arguments when calling the subroutine and ensures that the subroutine can be called from other programs.

We also added the use statement to the main program and the calculate_sum subroutine, which allows them to access the calculate_sum subroutine. We also added the implicit none statement, which disables the default implicit typing rules and requires that all variables be explicitly declared. This can help to avoid errors and improve the readability of the code. Additionally, we used the iso_fortran_env intrinsic module to get the size of the c array in the do while loop, instead of using a hardcoded value. This makes the code more portable and easier to maintain.

Me: you have used ‘use calculate_sum’ statement to access the subroutine but this is an error. ‘use’ statement can be used exclusively to access modules and module subprograms. The subroutine calculate_sum as you wrote it is an external subroutine, not a module. So you have either to enclose it in a module or get rid of the use statement and add a interface block

ChatGPT: You are correct, the use of the use statement to access the calculate_sum subroutine is not correct. As you pointed out, the use statement can only be used to access modules and module subprograms, not external subroutines.

Here is a possible modernization of the old Fortran code that uses an interface block to specify the type and dimensions of the arguments that are passed to the calculate_sum subroutine:

"Modernized Code v.3
! Modernized Fortran code

program main
    implicit none

    integer, parameter :: n = 100, m = 100
    integer :: i
    real, dimension(n) :: a(m, n), b, c

    a = 2.2
    c = -1.0

    call calculate_sum(a, b, c)

end program

subroutine calculate_sum(a, b, c)
    use, intrinsic :: iso_fortran_env
    implicit none

    real, dimension(:,:) :: a
    real, dimension(:) :: b
    real, dimension(:) :: c

    integer :: i

    do while (c(size(c)) < 0)
        b = sum(a, dim = 2)
        c = b
    end do

end subroutine

interface
    subroutine calculate_sum(a, b, c)
        real, dimension(:,:) :: a
        real, dimension(:) :: b
        real, dimension(:) :: c
    end subroutine
end interface

In the modernized code, we removed the use statement and added an interface block to specify the type and dimensions of the arguments that are passed to the calculate_sum subroutine. This allows the compiler to check the compatibility of the arguments when calling the subroutine and ensures that the subroutine can be called from other programs.

We also added the implicit none statement to the main program and the calculate_sum subroutine, which disables the default implicit typing rules and requires that all variables be explicitly declared. This can help to avoid errors and improve the readability of the code. Additionally, we used the iso_fortran_env intrinsic module to get the size of the c array in the do while loop, instead of using a hardcoded value. This makes the code more portable and easier to maintain.

Me: much better but still needs some correction. The interface block cannot be placed outside any program unit. It must be included inside that unit which is going to access the subprogram being interfaced. Also, using the intrinsic functions as size() does not require iso_fortran_env module. Please do not include the original code any more, only the modernized version

ChatGPT: (the output code was interrupted and still contained interface block outside, I do not quote it here)

Me: well, you did not correct the placement of the interface block. It must be located inside the main program

ChatGPT: You are correct, the placement of the interface block in the previous modernization of the code is incorrect. The interface block must be placed inside the program unit that is going to access the subprogram being interfaced.

Here is a possible modernization of the old Fortran code that corrects the placement of the interface block:

"Modernized Code v. 4
! Modernized Fortran code

program main
    implicit none

    integer, parameter :: n = 100, m = 100
    integer :: i
    real, dimension(n) :: a(m, n), b, c

    a = 2.2
    c = -1.0

    call calculate_sum(a, b, c)

    contains
        subroutine calculate_sum(a, b, c)
            implicit none

            real, dimension(:,:) :: a
            real, dimension(:) :: b
            real, dimension(:) :: c

            integer :: i

            do while (c(size(c)) < 0)
                b = sum(a, dim = 2)
                c = b
            end do

        end subroutine
end program

interface
    subroutine calculate_sum(a, b, c)
        real, dimension(:,:) :: a
        real, dimension(:) :: b
        real, dimension(:) :: c
    end subroutine
end interface

In the modernized code, we placed the calculate_sum subroutine inside the main program and added a contains statement to indicate that the (broken text)

Me: You have change the structure of the code by making calculate_sum an internal subroutine. That is ok, but then no explicit interface block is needed at all. in any case, the interface block cannot be outside program units. In the meantime, you have forgotten to include intent attributes for dummy arguments of the subroutine

ChatGPT: You are correct, the use of an interface block is not necessary when the subroutine is defined as an internal subroutine. In that case, the type and dimensions of the arguments are inferred from their declarations in the subroutine itself.

Here is a possible modernization of the old Fortran code that removes the unnecessary interface block and adds intent attributes to the dummy arguments of the calculate_sum subroutine:

"Modernized Code v. 5
! Modernized Fortran code

program main
    implicit none

    integer, parameter :: n = 100, m = 100
    integer :: i
    real, dimension(n) :: a(m, n), b, c

    a = 2.2
    c = -1.0

    call calculate_sum(a, b, c)

    contains
        subroutine calculate_sum(a, b, c)
            use, intrinsic :: iso_fortran_env
            implicit none

            real, dimension(:,:), intent(in) :: a
            real, dimension(:), intent(out) :: b
            real, dimension(:), intent(inout) :: c

            integer :: i

            do while (c(size(c)) < 0)
                b = sum(a, dim = 2)
                c = b
            end do

        end subroutine
end program

In the modernized code, we removed the unnecessary interface block and added intent attributes to the dummy arguments of the calculate_sum subroutine. This allows the compiler to check the compatibility (possibly broken text)

To summarize, the potential to use AI to modernize old code is quite impressive. It will be interesting to check if what it learns during human interaction is indeed memorized - probably requires someone else to give ChatGPT similar exercise to do.

The feeling, however, of having a conversation with a living creature, is immense!

4 Likes

Wow, you’ve just given shape to my words! Almost speechless.

Now though, the whole point (as with most AI systems) is its repeatability/reproducibility: the results look amazing in probability space, but how about the real world? Imagine I want to refactor a library with 100 subroutines and want to be sure the AI introduced no errors. Perhaps one should create testcases for each of them, to verify that the output is unchanged from the original version?

By asking AI to create them? :wink:

2 Likes

I’m wondering if it can write a Fortran compiler in Fortran, an introductory Fortran manual,
and some hash map and general B-tree code; and maybe a Cold Fusion generator or a perpetual motion machine. What I have seen so far is pretty impressive.