FORTRAN Interface

If the FORTRAN file f.f is compiled as shown below, then loading the Prolog file as shown will produce the indicated results.

                                        
f.f
C f1(+integer, [-integer]) integer function f1(a) integer a f1 = a + 9 return end C f2(-integer) subroutine f2(a) integer a a = 99 return end C f11(+atom, [-atom]) integer function f11(a) integer a f11 = a return end C f21(+atom, -atom) subroutine f21(a,b) integer a integer b b = a return end C f3(+float, [-float]) real function f3(a) real a f3 = a + 9.0 return end C f4(-float) subroutine f4(a) real a a = 9.9 return end C f5(+string(10), [-string(10)]) character*10 function f5(s) character*10 s f5 = s return end C f6(-string(10)) subroutine f6(s) character*10 s s = 'output' return end

At the command level:

     % f77 -c f.f
     

Produces the object file.

                                       
f.pl
foreign_file(f, [f1_, f2_, f11_, f21_, f3_, f4_, f5_, f6_]). foreign(f1_, fortran, f1(+integer, [-integer])). foreign(f2_, fortran, f2(-integer)). foreign(f11_, fortran, f11(+atom, [-atom])). foreign(f21_, fortran, f21(+atom, -atom)). foreign(f3_, fortran, f3(+float, [-float])). foreign(f4_, fortran, f4(-float)). foreign(f5_, fortran, f5(+string(10),[-string(10)])). foreign(f6_, fortran, f6(-string(10))). :- load_foreign_files([f], ['-lF77']), abolish(foreign_file,2), abolish(foreign,3).

Loading the Prolog file (see foreign/3) into Prolog and invoking the following query gives the following results:

     | ?- f1(1,X1), f2(X2), f11(foo,X11), f21(foo,X21), f3(1.5,X3), f4(X4),
          f5('parameter',X5), f6(X6).
     
     X1 = 10,
     X2 = 99,
     X11 = X21 = foo,
     X3 = 10.5,
     X4 = 9.89999 ;
     X5 = parameter ;
     X6 = output ;
     
     no
     

When you load FORTRAN code into a C program, you must ensure that any necessary FORTRAN run-time support code is loaded as well. The FORTRAN run-time library is divided into three parts in UNIX systems based on 4.2BSD:

UNIX systems based on System V have libF77.a and libI77.a but not libU77.a.

To ensure that these libraries will be loaded, use the linker options -lF77, -lI77, or -lU77 respectively.

You should check your FORTRAN documentation for advice about combining FORTRAN subroutines with a C main program.

Notes:

  1. Passing of unsized strings (for example, use of the string argument specification in a foreign/3 fact) is not supported in this interface. Instead, padded strings (the string(N) argument specification) must be used.
  2. The names of subroutines passed to the predicates foreign_file/2 and foreign/3 must end with an underscore (_) to comply with the way in which f77 generates external symbols.
  3. The FORTRAN run-time library has been seen documented as -lf77. As case is significant in loader options, be sure to load this library using -lF77.