Fortran Abstract Types
An abstract type describes what a family of types can do without saying how. It can't be instantiated on its own; it exists to be extended. Its deferred type-bound procedures must be implemented by every non-abstract extension. This is Fortran's version of an interface or abstract base class, and it's the cleanest way to write code that works with "any solver", "any integrator" or "any shape".
Declaring an abstract type
Two ingredients are needed: type, abstract :: name and, for each required method, procedure(interface_name), deferred :: method together with an abstract interface describing the method's signature.
module shape_family
implicit none
private
public :: shape, circle, rectangle
type, abstract :: shape
contains
procedure(area_iface), deferred :: area
procedure(name_iface), deferred :: name
procedure :: summary ! a normal, shared method
end type shape
abstract interface
real function area_iface(self)
import :: shape
class(shape), intent(in) :: self
end function area_iface
function name_iface(self) result(s)
import :: shape
class(shape), intent(in) :: self
character(len=:), allocatable :: s
end function name_iface
end interface
type, extends(shape) :: circle
real :: r
contains
procedure :: area => circle_area
procedure :: name => circle_name
end type circle
type, extends(shape) :: rectangle
real :: w, h
contains
procedure :: area => rect_area
procedure :: name => rect_name
end type rectangle
contains
subroutine summary(self)
class(shape), intent(in) :: self
print '(a12, a, f9.3)', self%name(), ' area', self%area()
end subroutine summary
real function circle_area(self)
class(circle), intent(in) :: self
circle_area = 3.14159265 * self%r**2
end function circle_area
function circle_name(self) result(s)
class(circle), intent(in) :: self
character(len=:), allocatable :: s
s = 'circle'
end function circle_name
real function rect_area(self)
class(rectangle), intent(in) :: self
rect_area = self%w * self%h
end function rect_area
function rect_name(self) result(s)
class(rectangle), intent(in) :: self
character(len=:), allocatable :: s
s = 'rectangle'
end function rect_name
end module shape_family
program use_abstract
use shape_family
implicit none
call report(circle(r=2.0))
call report(rectangle(w=3.0, h=1.5))
contains
subroutine report(s)
class(shape), intent(in) :: s ! works for ANY concrete shape
call s%summary()
end subroutine report
end program use_abstract circle area 12.566
rectangle area 4.500
summary is written once in the abstract type. It calls the deferred methods name and area, which every concrete extension is forced to provide.
Abstract types can't be instantiated
Trying to create a variable of an abstract type is a compile-time error, and so is forgetting to implement a deferred method:
module incomplete
implicit none
type, abstract :: sensor
contains
procedure(read_iface), deferred :: read_value
end type sensor
abstract interface
real function read_iface(self)
import :: sensor
class(sensor), intent(in) :: self
end function read_iface
end interface
type, extends(sensor) :: thermometer ! forgot to implement read_value
end type thermometer
end module incomplete
program main
use incomplete
implicit none
type(thermometer) :: t
end program main/app/example.f90:15:76:
15 | type, extends(sensor) :: thermometer ! forgot to implement read_value
| 1
Error: Derived-type 'thermometer' declared at (1) must be ABSTRACT because 'read_value' is DEFERRED and not overridden
/app/example.f90:20:7:
20 | use incomplete
| 1
Fatal Error: Cannot open module file 'incomplete.mod' for reading at (1): No such file or directory
compilation terminated.
A practical example: pluggable integrators
Abstract types shine when an algorithm needs a user-supplied piece. Here, any ODE right-hand side can be plugged into the same time-stepping routine, and each implementation carries its own parameters as components:
module ode
use iso_fortran_env, only: real64
implicit none
integer, parameter :: dp = real64
type, abstract :: rhs
contains
procedure(rhs_iface), deferred :: eval ! dy/dt = f(t, y)
end type rhs
abstract interface
function rhs_iface(self, t, y) result(dydt)
import :: rhs, dp
class(rhs), intent(in) :: self
real(dp), intent(in) :: t, y
real(dp) :: dydt
end function rhs_iface
end interface
type, extends(rhs) :: decay ! dy/dt = -k y
real(dp) :: k
contains
procedure :: eval => decay_eval
end type decay
type, extends(rhs) :: logistic ! dy/dt = r y (1 - y/cap)
real(dp) :: r, cap
contains
procedure :: eval => logistic_eval
end type logistic
contains
function decay_eval(self, t, y) result(dydt)
class(decay), intent(in) :: self
real(dp), intent(in) :: t, y
real(dp) :: dydt
dydt = -self%k * y
end function decay_eval
function logistic_eval(self, t, y) result(dydt)
class(logistic), intent(in) :: self
real(dp), intent(in) :: t, y
real(dp) :: dydt
dydt = self%r * y * (1.0_dp - y / self%cap)
end function logistic_eval
function rk4(f, y0, t_end, n) result(y) ! classic Runge-Kutta, n steps
class(rhs), intent(in) :: f
real(dp), intent(in) :: y0, t_end
integer, intent(in) :: n
real(dp) :: y, t, h, k1, k2, k3, k4
integer :: i
h = t_end / n
t = 0.0_dp
y = y0
do i = 1, n
k1 = f%eval(t, y)
k2 = f%eval(t + h/2, y + h/2 * k1)
k3 = f%eval(t + h/2, y + h/2 * k2)
k4 = f%eval(t + h, y + h * k3)
y = y + h / 6 * (k1 + 2*k2 + 2*k3 + k4)
t = t + h
end do
end function rk4
end module ode
program solve_odes
use ode
implicit none
print '(a, f12.8)', 'decay y(1) = ', rk4(decay(k=1.0_dp), 1.0_dp, 1.0_dp, 20)
print '(a, f12.8)', 'exact e**-1 = ', exp(-1.0_dp)
print '(a, f12.6)', 'logistic y(10) = ', rk4(logistic(r=0.8_dp, cap=500.0_dp), 10.0_dp, 10.0_dp, 100)
end program solve_odesdecay y(1) = 0.36787946
exact e**-1 = 0.36787944
logistic y(10) = 491.914068
rk4 knows nothing about decay or population growth. Adding a new model means writing one small extension type, without touching the solver.
Test yourself
What does the deferred attribute on a type-bound procedure mean?
- It runs later, asynchronously
- Its implementation must be supplied by each non-abstract extension
- It is private
- It is called automatically at finalisation
Can you declare type(shape) :: s when shape is abstract?
- Yes
- No, abstract types cannot be instantiated
- Only inside a module
- Only with
allocatable
Why is import :: shape needed in the abstract interface?
- To load a file
- Because the interface body cannot see host entities like the type
shapewithout it - To make shape public
- It allocates a shape