Skip to content

Commit df28e0f

Browse files
authored
Merge pull request #8 from certik/use
Initial proposal for namespace for modules
2 parents 3149810 + 017d863 commit df28e0f

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

proposals/19-246.txt

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
Proposal for Fortran Standard: 202y (NOT 202x)
2+
3+
To: J3 J3/19-246
4+
From: Ondřej Čertík
5+
Subject: Namespace For Modules
6+
Date: 2019-October-15
7+
8+
1. Introduction
9+
10+
The proposal is to allow import a module as a namespace and access its members
11+
using the % operator. Example:
12+
13+
use, namespace :: utils
14+
...
15+
call utils%savetxt(...)
16+
17+
Where `utils` is the only name that is imported in the local namespace.
18+
`savetxt` is not accesible directly, only via `utils%`.
19+
20+
2. Motivation
21+
22+
Fortran module usage is equivalent to Python:
23+
24+
Python Fortran
25+
26+
from A import foo use A, only: foo
27+
from A import foo as Afoo use A, only: Afoo => foo
28+
from A import * use A
29+
30+
Except:
31+
32+
Python Fortran
33+
34+
import A N/A
35+
import A as B N/A
36+
37+
This proposal proposes to fill in the missing functionality as follows:
38+
39+
Python Fortran
40+
41+
import A use, namespace :: A
42+
import A as B use, namespace :: B => A
43+
44+
3. Use Cases
45+
46+
3.1 Same function names in multiple modules
47+
48+
In Python a very common idiom is:
49+
50+
import math
51+
import numpy as np
52+
import sympy as sym
53+
...
54+
e1 = np.sin(np.pi) # NumPy expression
55+
e2 = math.sin(math.pi) # Built-in Python math expression
56+
e3 = sym.sin(sym.pi) # SymPy expression
57+
58+
In Fortran currently one has to do:
59+
60+
use math, only: math_sin => sin, math_pi => pi
61+
use numpy, only: np_sin => sin, np_pi => pi
62+
use sympy, only: sym_sin => sin, sym_pi => pi
63+
...
64+
e1 = np_sin(np_pi) ! NumPy expression
65+
e2 = math_sin(math_pi) ! Built-in Python math expression
66+
e3 = sym_sin(sym_pi) ! SymPy expression
67+
68+
With this proposal one could also do:
69+
70+
use, namespace :: math
71+
use, namespace :: np => numpy
72+
use, namespace :: sym => sympy
73+
...
74+
e1 = np%sin(np%pi) ! NumPy expression
75+
e2 = math%sin(math%pi) ! Built-in Python math expression
76+
e3 = sym%sin(sym%pi) ! SymPy expression
77+
78+
79+
3.2 Need to import lots of functions from a module
80+
81+
Exisintg code (https://github.com/certik/fortran-utils/blob/b43bd24cd421509a5bc6d3b9c3eeae8ce856ed88/src/linalg.f90):
82+
83+
use lapack, only: dsyevd, dsygvd, ilaenv, zgetri, zgetrf, zheevd, &
84+
dgeev, zgeev, zhegvd, dgesv, zgesv, dgetrf, dgetri, dgelsy, zgelsy, &
85+
dgesvd, zgesvd, dgeqrf, dorgqr, dpotrf, dtrtrs
86+
...
87+
call dgeev('N', 'V', n, At, lda, wr, wi, vl, ldvl, vr, ldvr, &
88+
work, lwork, info)
89+
...
90+
call dgetrf(n, n, Amt, lda, ipiv, info)
91+
...
92+
93+
Instead, one can write it as:
94+
95+
use, namespace :: lapack
96+
...
97+
call lapack%dgeev('N', 'V', n, At, lda, wr, wi, vl, ldvl, vr, ldvr, &
98+
work, lwork, info)
99+
...
100+
call lapack%dgetrf(n, n, Amt, lda, ipiv, info)
101+
...
102+
103+
Then when another subroutine must be called from the `lapack` module, one can
104+
just call it, without having to modify the `use` statement.

0 commit comments

Comments
 (0)