Skip to content

Commit

Permalink
Style and formatting fixes for Java interface samples.
Browse files Browse the repository at this point in the history
samples/java_interface/java_calls_mercury/mercury_main.m:
samples/java_interface/mercury_calls_java/java_main_int.m:
samples/java_interface/standalone_java/JavaMain.java:
samples/java_interface/standalone_java/Makefile:
samples/java_interface/standalone_java/mercury_lib.m:
    As above.
  • Loading branch information
juliensf committed Jan 15, 2022
1 parent 2434915 commit 1702444
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 38 deletions.
8 changes: 4 additions & 4 deletions samples/java_interface/java_calls_mercury/mercury_main.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
:- implementation.

% Nothing from mercury_lib is used in mercury_main.
% The import is needed to make sure mmake includes
% mercury_lib in the executable.
% The import is needed to make sure mmake includes mercury_lib in the
% executable.
:- import_module mercury_lib.

% import the module which defines the Mercury interface to the
% Import the module which defines the Mercury interface to the
% Java method java_main().
:- import_module java_main_int.

% main just invokes java_main
% main/2 just invokes java_main/2.
main(!IO) :-
io.write_string("In Mercury main, about to call java_main...\n", !IO),
java_main(!IO),
Expand Down
23 changes: 11 additions & 12 deletions samples/java_interface/mercury_calls_java/java_main_int.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
% This module java_main_int defines a Mercury predicate java_main which acts as an
% interface to the Java method java_main(), which is defined in JavaMain.java.
% This module defines a Mercury predicate java_main/2 that acts as an interface
% to the Java method java_main(), which is defined in the file JavaMain.java.

% This source file is hereby placed in the public domain.

Expand All @@ -8,24 +8,23 @@
:- import_module io.

% Since the java_main() function has side effects, we declare the corresponding
% Mercury predicate as one that takes an io__state pair. If we didn't do
% Mercury predicate as one that takes an I/O satate pair. If we did not do
% this, the Mercury compiler might optimize away calls to it!

:- pred java_main(io::di, io::uo) is det.

:- implementation.

% Import the Java class containing the method java_main.
% As usual for Java, this is not necessary; you may also
% fully qualify the method at the call site.
% Import the Java class containing the method java_main.
% As usual for Java, this is not necessary; you may also
% fully qualify the method at the call site.
:- pragma foreign_decl("Java", "import my_package.JavaMain;").

% Define the Mercury predicate java_main to call the Java function
% java_main.
% Define the Mercury predicate java_main/2 to call the Java method
% java_main().
:- pragma foreign_proc("Java",
java_main(IO0::di, IO::uo),
[promise_pure, will_not_call_mercury],
java_main(_IO0::di, _IO::uo),
[promise_pure, will_not_call_mercury],
"
JavaMain.java_main();
IO = IO0;
JavaMain.java_main();
").
6 changes: 3 additions & 3 deletions samples/java_interface/standalone_java/JavaMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static void main(String[] args)
runProgram(args);
} finally {
// When we have finished calling Mercury procedures then we need
// to tell the Mercury Runtime that we've finished using it.
// to tell the Mercury Runtime that we have finished using it.
// The static method finalise() in the MercuryRuntime class does
// this. This call is (currently) mandatory otherwise the JVM
// may not exit cleanly, therefore it should be called in a
Expand All @@ -63,8 +63,8 @@ public static void main(String[] args)
System.exit(MercuryRuntime.getExitStatus());
}

public static void runProgram(String[] args) {

public static void runProgram(String[] args)
{
// This is a call to an exported Mercury procedure that does some I/O.
// The mercury_lib class contains a static method for each procedure
// that is foreign exported to Java.
Expand Down
3 changes: 2 additions & 1 deletion samples/java_interface/standalone_java/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ MMC = mmc
JAVAC = javac
JAVA = java

# We need to tell javac about the Mercury libraries.
# We need to tell javac and java where to find the Mercury runtime and
# standard libraries.
GRADE = java
MER_LIB_DIR = $(dir $(shell which mmc))../lib/mercury/lib/$(GRADE)
MER_JARS = $(MER_LIB_DIR)/mer_std.jar:$(MER_LIB_DIR)/mer_rt.jar
Expand Down
36 changes: 18 additions & 18 deletions samples/java_interface/standalone_java/mercury_lib.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,47 +35,47 @@

%-----------------------------------------------------------------------------%

:- pragma foreign_export("Java", write_hello(di, uo),
"writeHello").
:- pragma foreign_export("Java", write_hello(di, uo), "writeHello").

write_hello(!IO) :-
io.print_line("Hello World", !IO).

%-----------------------------------------------------------------------------%

:- pragma foreign_export("Java", cube(in) = out,
"cube").
:- pragma foreign_export("Java", cube(in) = out, "cube").

cube(X) = X * X * X.

%-----------------------------------------------------------------------------%

%
% Trivial concurrency test. No one would normally write fibs this way.
% Trivial concurrency test.
%
:- pragma foreign_export("Java", fibs(in) = out,
"fibs").

% No one would normally write fibs this way.

:- pragma foreign_export("Java", fibs(in) = out, "fibs").

fibs(N) = fibs_par(N).

:- func fibs_par(int) = int.

fibs_par(N) = F :-
( N < 2 ->
( if N < 2 then
F = 1
; N > fibs_thresh ->
F2 = future((func) = fibs_par(N-2)),
F1 = fibs_par(N-1),
else if N > fibs_thresh then
F2 = future((func) = fibs_par(N - 2)),
F1 = fibs_par(N - 1),
F = F1 + wait(F2)
;
F = fibs_seq(N-1) + fibs_seq(N-2)
else
F = fibs_seq(N - 1) + fibs_seq(N - 2)
).

:- func fibs_seq(int) = int.

fibs_seq(N) =
( N < 2 ->
( if N < 2 then
1
;
else
fibs_seq(N-2) + fibs_seq(N-1)
).

Expand All @@ -85,7 +85,7 @@

%-----------------------------------------------------------------------------%
%
% Initialiser for this library
% Initialiser for this library.
%

:- initialise initialiser/2.
Expand All @@ -97,7 +97,7 @@

%-----------------------------------------------------------------------------%
%
% Finaliser for this library
% Finaliser for this library.
%

:- finalise finaliser/2.
Expand Down

0 comments on commit 1702444

Please sign in to comment.