If the Pascal file p.p is compiled as shown below, then loading the
Prolog file as shown will produce the indicated results.
p.p
type
alfa = packed array[1..10] of char;
(* p1(+integer, [-integer]) *)
function p1(a: integer32): integer32;
begin
p1 := a + 9;
end;
(* p2(-integer) *)
procedure p2(var a: integer32);
begin
a := 99;
end;
(* p11(+atom, [-atom]) *)
function p11(a: integer32) : integer32;
begin
p11 := a;
end;
(* p21(+atom, -atom) *)
procedure p21(a: integer32; var b: integer32);
begin
b := a;
end;
(* p3(+float, [-float]) *)
function p3(a: real) : real;
begin
p3 := a + 9.0;
end;
(* p4(-float) *)
procedure p4(var a: real);
begin
a := 9.9;
end;
(* p5(+string(10), -string(10)) *)
procedure p5(var s: alfa; var t: alfa);
begin
t := s;
end;
(* p6(-string(10)) *)
procedure p6(var s: alfa);
begin
s := 'output';
end;
At the command level:
% pc -c p.p
Produces the object file.
p.pl
foreign_file(p, [p1, p2, p11, p21, p3, p4, p5, p6]).
foreign(p1, pascal, p1(+integer, [-integer])).
foreign(p2, pascal, p2(-integer)).
foreign(p11, pascal, p11(+atom, [-atom])).
foreign(p21, pascal, p21(+atom, -atom)).
foreign(p3, pascal, p3(+float, [-float])).
foreign(p4, pascal, p4(-float)).
foreign(p6, pascal, p5(+string(10),-string(10))).
foreign(p5, pascal, p6(-string(10))).
:- load_foreign_files([p], ['-lpc']),
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:
| ?- p1(1,X1), p2(X2), p11(foo,X11), p21(foo,X21), p3(1.5,X3), p4(X4),
p5('parameter',X5), p6(X6).
X1 = 10,
X2 = 99,
X11 = X21 = foo,
X3 = 10.5,
X4 = 9.89999,
X5 = parameter,
X6 = output ;
no
Notes:
string argument specification
in a foreign/3 fact) is not supported in this interface since pc does
not have a convention for passing variable length arrays. Instead, padded
strings (the string(N) argument specification) must be used.
Notice that the corresponding parameter of +string(N) declaration is
actually a call by reference parameter in Pascal procedures.
-lpc must be included in the call to
load_foreign_files/2
so that the foreign code routine will have
access to the standard Pascal library.