Prototype implementation of Fortran generics in LFortran

@certik ,

Can you and the contributors working on this prototype implementation in LFortran please take a look and consider the Fortran source below which is based on current standard i.e, Fortran 2018 with a rather, simple-minded implementation of a poor Fortranner version i.e., using unlimited polymorphism toward a generic 1-D sort that employs the Cormen’s algorithm?

Using this code, two Fortran compilers help generate the following output from the program:

C:\temp>gfortran kinds.f90 base.f90 string.f90 utils.f90 Qsort.f90 p.f90 -o p.exe

C:\temp>p.exe
Block 1
initial array is  42 999 -1 0 50 12
sorted array is  -1 0 12 42 50 999

Block 2
initial array is  32.0 30.0 75.0 54.0 64.0 66.0
sorted array is  30.0 32.0 54.0 64.0 66.0 75.0

Block 3
Initial array is  q w e r t y
sorted array is  e q r t w y
Block 4
Initial array is  Mary   had    a      little lamb
sorted array is  M      aar    lady   hittle lamb
Block 5
Initial array is
Mary   had    a      little lamb
sorted array is
Mary   a      had    lamb   little

So you will notice the above “generic” code supports 4 kinds of integers, 3 different floating-point kinds, 1 kind of Fortran’s CHARACTER type, and any user-defined type which extends an arbitrary base type has deferred procedures for comparer functions. The calling example program, you will notice, sorts 1-D arrays of 5 different types.

The Generics subcommittee might perhaps be able to help putting together an equivalent example of the above using the new, proposed TEMPLATE facility for Fortran 202X? If so, can it be an example you are looking for?

The reason I suggest something like this is because it is very simple, it does not involve any math or computer science concepts such semigroups, monoids, rings, fields, etc. and so a reader can thus focus on Fortran TEMPLATE facility and its syntax and semantics.

sort.f90 (26.2 KB)

2 Likes