Skip to content

Commit e3bcbfa

Browse files
authored
Merge pull request #143 from difference-scheme/master
First draft of run-time polymorphism proposal
2 parents 746e751 + 20abe46 commit e3bcbfa

File tree

8 files changed

+1751
-0
lines changed

8 files changed

+1751
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
! Fortran implementation of the Taylor series example program based on
2+
! subclassing given in Appendix A of the proposal "Improved run-time
3+
! polymorphism for Fortran".
4+
!
5+
6+
module derivs
7+
8+
type, abstract :: Deriv
9+
contains
10+
procedure(pderiv), deferred, nopass :: deriv1
11+
end type Deriv
12+
13+
abstract interface
14+
subroutine pderiv()
15+
end subroutine pderiv
16+
end interface
17+
18+
type, extends(Deriv) :: DerivF
19+
contains
20+
procedure, nopass :: deriv1 => deriv1f
21+
end type DerivF
22+
23+
type, extends(DerivF) :: HDerivF
24+
contains
25+
procedure, nopass :: deriv2 => deriv2f
26+
end type HDerivF
27+
28+
type, extends(Deriv) :: DerivG
29+
contains
30+
procedure, nopass :: deriv1 => deriv1g
31+
end type DerivG
32+
33+
type, extends(DerivG) :: HDerivG
34+
contains
35+
procedure, nopass :: deriv2 => deriv2g
36+
end type HDerivG
37+
38+
contains
39+
40+
subroutine deriv1f()
41+
write(*,*) ' 1st derivative of function F!'
42+
end subroutine deriv1f
43+
44+
subroutine deriv2f()
45+
write(*,*) ' 2nd derivative of function F!'
46+
end subroutine deriv2f
47+
48+
subroutine deriv1g()
49+
write(*,*) ' 1st derivative of function G!'
50+
end subroutine deriv1g
51+
52+
subroutine deriv2g()
53+
write(*,*) ' 2nd derivative of function G!'
54+
end subroutine deriv2g
55+
56+
end module derivs
57+
58+
module series
59+
60+
use derivs, only: Deriv, HDerivF, HDerivG
61+
62+
type :: Taylor
63+
class(Deriv), allocatable :: calc
64+
contains
65+
procedure :: term1
66+
procedure :: evaluate
67+
end type Taylor
68+
69+
type, extends(Taylor) :: HTaylor
70+
contains
71+
procedure :: term2 => hterm2
72+
procedure :: evaluate => hevaluate
73+
end type HTaylor
74+
75+
contains
76+
77+
subroutine term1(self)
78+
class(Taylor), intent(in) :: self
79+
call self%calc%deriv1()
80+
end subroutine term1
81+
82+
subroutine evaluate(self)
83+
class(Taylor), intent(in) :: self
84+
write(*,*) 'Evaluating Taylor series using'
85+
call self%term1()
86+
end subroutine evaluate
87+
88+
subroutine hterm2(self)
89+
class(HTaylor), intent(in) :: self
90+
select type ( calc => self%calc )
91+
class is ( HDerivF )
92+
call calc%deriv2()
93+
class is ( HDerivG )
94+
call calc%deriv2()
95+
class default
96+
write(*,*) 'Unknown type!'
97+
end select
98+
end subroutine hterm2
99+
100+
subroutine hevaluate(self)
101+
class(HTaylor), intent(in) :: self
102+
write(*,*) 'Evaluating Taylor series using'
103+
call self%term1()
104+
call self%term2()
105+
end subroutine hevaluate
106+
107+
end module series
108+
109+
program client
110+
111+
use derivs, only: DerivG, HDerivG, Deriv
112+
use series, only: Taylor, HTaylor
113+
114+
class(Deriv), allocatable :: derv
115+
class(Taylor), allocatable :: teval
116+
117+
derv = DerivG()
118+
teval = Taylor(derv)
119+
call teval%evaluate()
120+
121+
write(*,*)
122+
123+
derv = HDerivG()
124+
teval = HTaylor(derv)
125+
call teval%evaluate()
126+
127+
end program client
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
! Fortran implementation of the Taylor series example program based on
2+
! subtyping given in Appendix B of the proposal "Improved run-time
3+
! polymorphism for Fortran".
4+
!
5+
6+
module interfaces
7+
8+
abstract interface :: IDeriv
9+
subroutine deriv1()
10+
end subroutine deriv1
11+
end interface IDeriv
12+
13+
abstract interface, extends(IDeriv) :: IHDeriv
14+
subroutine deriv2()
15+
end subroutine deriv2
16+
end interface IHDeriv
17+
18+
end module interfaces
19+
20+
module derivs
21+
22+
use interfaces, only: IDeriv, IHDeriv
23+
24+
type, implements(IDeriv) :: DerivF
25+
contains
26+
procedure, nopass :: deriv1 => deriv1f
27+
end type DerivF
28+
29+
type, implements(IHDeriv) :: HDerivF
30+
contains
31+
procedure, nopass :: deriv1 => deriv1f
32+
procedure, nopass :: deriv2 => deriv2f
33+
end type HDerivF
34+
35+
type, implements(IDeriv) :: DerivG
36+
contains
37+
procedure, nopass :: deriv1 => deriv1g
38+
end type DerivG
39+
40+
type, implements(IHDeriv) :: HDerivG
41+
contains
42+
procedure, nopass :: deriv1 => deriv1g
43+
procedure, nopass :: deriv2 => deriv2g
44+
end type HDerivG
45+
46+
contains
47+
48+
subroutine deriv1f()
49+
write(*,*) " 1st derivative of function F!"
50+
end subroutine deriv1f
51+
52+
subroutine deriv2f()
53+
write(*,*) " 2nd derivative of function F!"
54+
end subroutine deriv2f
55+
56+
subroutine deriv1g()
57+
write(*,*) " 1st derivative of function G!"
58+
end subroutine deriv1g
59+
60+
subroutine deriv2g()
61+
write(*,*) " 2nd derivative of function G!"
62+
end subroutine deriv2g
63+
64+
end module derivs
65+
66+
module series
67+
68+
use interfaces, only: IDeriv, IHDeriv
69+
70+
type :: Taylor
71+
class(IDeriv), allocatable :: calc
72+
contains
73+
procedure :: term1
74+
procedure :: evaluate
75+
end type Taylor
76+
77+
type :: HTaylor
78+
class(IHDeriv), allocatable :: calc
79+
contains
80+
procedure :: term1 => hterm1
81+
procedure :: term2 => hterm2
82+
procedure :: evaluate => hevaluate
83+
end type HTaylor
84+
85+
contains
86+
87+
subroutine term1(self)
88+
class(Taylor), intent(in) :: self
89+
call self%calc%deriv1()
90+
end subroutine term1
91+
92+
subroutine evaluate(self)
93+
class(Taylor), intent(in) :: self
94+
write(*,*) 'Evaluating Taylor series using'
95+
call self%term1()
96+
end subroutine evaluate
97+
98+
subroutine hterm1(self)
99+
class(HTaylor), intent(in) :: self
100+
call self%calc%deriv1()
101+
end subroutine hterm1
102+
103+
subroutine hterm2(self)
104+
class(HTaylor), intent(in) :: self
105+
call self%calc%deriv2()
106+
end subroutine hterm2
107+
108+
subroutine hevaluate(self)
109+
class(HTaylor), intent(in) :: self
110+
write(*,*) 'Evaluating Taylor series using'
111+
call self%term1()
112+
call self%term2()
113+
end subroutine hevaluate
114+
115+
end module series
116+
117+
program client
118+
119+
use derivs, only: DerivG, HDerivG
120+
use series, only: Taylor, HTaylor
121+
122+
type(Taylor), allocatable :: teval
123+
type(HTaylor), allocatable :: hteval
124+
125+
teval = Taylor( DerivG() )
126+
call teval%evaluate()
127+
128+
write(*,*)
129+
130+
hteval = HTaylor( HDerivG() )
131+
call hteval%evaluate()
132+
133+
end program client
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Java implementation of the Taylor series example program based on
2+
// subtyping given in Appendix B of the proposal "Improved run-time
3+
// polymorphism for Fortran".
4+
//
5+
6+
interface IDeriv {
7+
void deriv1();
8+
}
9+
10+
interface IHDeriv extends IDeriv {
11+
void deriv2();
12+
}
13+
14+
class DerivF implements IDeriv {
15+
public void deriv1() {
16+
System.out.println(" 1st derivative of function F!");
17+
}
18+
}
19+
20+
class HDerivF implements IHDeriv {
21+
public void deriv1() {
22+
System.out.println(" 1st derivative of function F!");
23+
}
24+
public void deriv2() {
25+
System.out.println(" 2nd derivative of function F!");
26+
}
27+
}
28+
29+
class DerivG implements IDeriv {
30+
public void deriv1() {
31+
System.out.println(" 1st derivative of function G!");
32+
}
33+
}
34+
35+
class HDerivG implements IHDeriv {
36+
public void deriv1() {
37+
System.out.println(" 1st derivative of function G!");
38+
}
39+
public void deriv2() {
40+
System.out.println(" 2nd derivative of function G!");
41+
}
42+
}
43+
44+
class Taylor {
45+
IDeriv calc;
46+
Taylor(IDeriv calculator) {
47+
calc = calculator;
48+
}
49+
public void term1() {
50+
calc.deriv1();
51+
}
52+
public void evaluate() {
53+
System.out.println("Evaluating Taylor series using");
54+
term1();
55+
}
56+
}
57+
58+
class HTaylor {
59+
IHDeriv calc;
60+
HTaylor(IHDeriv calculator) {
61+
calc = calculator;
62+
}
63+
public void term1() {
64+
calc.deriv1();
65+
}
66+
public void term2() {
67+
calc.deriv2();
68+
}
69+
public void evaluate() {
70+
System.out.println("Evaluating Taylor series using");
71+
term1();
72+
term2();
73+
}
74+
}
75+
76+
class ClientApp {
77+
78+
public static void main(String[] args) {
79+
80+
Taylor eval = new Taylor( new DerivG() );
81+
eval.evaluate();
82+
83+
System.out.println("");
84+
85+
HTaylor heval = new HTaylor( new HDerivG() );
86+
heval.evaluate();
87+
}
88+
}

0 commit comments

Comments
 (0)