[m-rev.] diff: fix dynamic linking examples

Julien Fischer juliensf at csse.unimelb.edu.au
Mon May 7 14:17:56 AEST 2007


Estimated hours taken: 0.5
Branches: main

Fix the dynamic linking examples in extras.

extras/dynamic_linking/dl_test.m:
extras/dynamic_linking/dl_test2.m:
extras/dynamic_linking/hello.m:
 	Conform to name changes in the dl module.

 	Clean up the code so that it conforms to our current coding standard.

Julien.

Index: dl_test.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/extras/dynamic_linking/dl_test.m,v
retrieving revision 1.4
diff -u -r1.4 dl_test.m
--- dl_test.m	11 Jan 2001 05:05:29 -0000	1.4
+++ dl_test.m	7 May 2007 04:11:53 -0000
@@ -1,114 +1,133 @@
+%-----------------------------------------------------------------------------%
+% vim: ft=mercury ts=4 sw=4 et
+%-----------------------------------------------------------------------------%
+%
  % Example program using dynamic linking.
  % This module loads in the object code for the module `hello'
  % from the file `libhello.so', looks up the address of the
  % procedure hello/2 in that module, and then calls that procedure.
-
+%
  % This source file is hereby placed in the public domain.  -fjh (the author).
+%
+%----------------------------------------------------------------------------%

  :- module dl_test.
  :- interface.
  :- import_module io.

-:- pred main(state::di, state::uo) is det.
+:- pred main(io::di, io::uo) is det.

-:- implementation.
-:- import_module dl, name_mangle, string, list.
+%----------------------------------------------------------------------------%
+%----------------------------------------------------------------------------%

-main -->
-	%
-	% Load in the object code for the module `hello' from
-	% the file `libhello.so'.
-	%
-	dl__open("./libhello.so", lazy, local, MaybeHandle),
-	( 
-		{ MaybeHandle = error(Msg) },
-		print("dlopen failed: "), print(Msg), nl
-	;
-		{ MaybeHandle = ok(Handle) },
-		%
-		% Look up the address of the first mode (mode number 0)
-		% of the predicate hello/2 in the module hello.
-		%
-		{ HelloProc = mercury_proc(predicate, unqualified("hello"),
-					"hello", 2, 0) },
-		dl__mercury_sym(Handle, HelloProc, MaybeHello),
-		(
-			{ MaybeHello = error(Msg) },
-			print("dlsym failed: "), print(Msg), nl
-		;
-			{ MaybeHello = ok(HelloPred0) },
-			%
-			% Cast the higher-order term that we obtained
-			% to the correct higher-order inst.
-			%
-			{ HelloPred = inst_cast(HelloPred0) },
-			%
-			% Call the procedure whose address
-			% we just obtained.
-			%
-			HelloPred
-		),
-
-		{ Add3IntProc = mercury_proc(function, unqualified("hello"),
-					"add3int", 3, 0) },
-		dl__mercury_sym(Handle, Add3IntProc, MaybeAdd3Int),
-		(
-			{ MaybeAdd3Int = error(Msg2) },
-			print("dlsym failed: "), print(Msg2), nl
-		;
-			{ MaybeAdd3Int = ok(Add3IntFunc0) },
-			%
-			% Cast the higher-order term that we obtained
-			% to the correct higher-order inst.
-			%
-			{ wrapper(Add3IntFunc) =
-				inst_cast_add3int(wrapper(Add3IntFunc0)) },
-			%
-			% Call the procedure whose address
-			% we just obtained.
-			%
-			{ SumInt = Add3IntFunc(1, 2, 3) },
-			io__format("1 + 2 + 3 = %d\n", [i(SumInt)])
-		),
-
-		%
-		% unload the object code in the libhello.so file
-		%
-		dl__close(Handle, Result),
-		(
-			{ Result = error(CloseMsg) },
-			print("dlclose failed: "), print(CloseMsg), nl
-		;
-			{ Result = ok }
-		)
-	).
+:- implementation.

+:- import_module dl.
+:- import_module name_mangle.
+:- import_module list.
+:- import_module string.
+
+%----------------------------------------------------------------------------%
+
+main(!IO) :-
+    %
+    % Load in the object code for the module `hello' from
+    % the file `libhello.so'.
+    %
+    dl.open("./libhello.so", lazy, local, MaybeHandle, !IO),
+    ( 
+        MaybeHandle = dl_error(OpenMsg),
+        io.format("dlopen failed: %s\n", [s(OpenMsg)], !IO)
+    ;
+        MaybeHandle = dl_ok(Handle),
+        %
+        % Look up the address of the first mode (mode number 0)
+        % of the predicate hello/2 in the module hello.
+        %
+        HelloProc = mercury_proc(predicate, unqualified("hello"), "hello", 2,
+            0),
+        dl.mercury_sym(Handle, HelloProc, MaybeHello, !IO),
+        (
+            MaybeHello = dl_error(Msg),
+            io.format("dlsym failed: %s\n", [s(Msg)], !IO)
+        ;
+            MaybeHello = dl_ok(HelloPred0),
+            %
+            % Cast the higher-order term that we obtained to the correct
+            % higher-order inst.
+            %
+            HelloPred = inst_cast(HelloPred0),
+            %
+            % Call the procedure whose address we just obtained.
+            %
+            HelloPred(!IO)
+        ),
+
+        Add3IntProc = mercury_proc(function, unqualified("hello"), "add3int",
+            3, 0),
+        dl.mercury_sym(Handle, Add3IntProc, MaybeAdd3Int, !IO),
+        (
+            MaybeAdd3Int = dl_error(Msg2),
+            io.format("dlsym failed: %s\n", [s(Msg2)], !IO)
+        ;
+            MaybeAdd3Int = dl_ok(Add3IntFunc0),
+            %
+            % Cast the higher-order term that we obtained to the correct
+            % higher-order inst.
+            %
+            wrapper(Add3IntFunc) = inst_cast_add3int(wrapper(Add3IntFunc0)),
+            %
+            % Call the procedure whose address we just obtained.
+            %
+            SumInt = Add3IntFunc(1, 2, 3),
+            io.format("1 + 2 + 3 = %d\n", [i(SumInt)], !IO)
+        ),
+        %
+        % Unload the object code in the libhello.so file.
+        %
+        dl.close(Handle, CloseResult, !IO),
+        (
+            CloseResult = dl_error(CloseMsg),
+            io.format("dlclose failed: %s\n", [s(CloseMsg)], !IO)
+        ;
+            CloseResult = dl_ok
+        )
+    ).
+
+% dl.mercury_sym returns a higher-order term with inst `ground'.
+% We need to cast it to the right higher-order inst, which for the `hello'
+% procedure is `pred(di, uo) is det', before we can actually call it.  The
+% function inst_cast/1 defined below does that.
  %
-% dl__mercury_sym returns a higher-order term with inst `ground'.
-% We need to cast it to the right higher-order inst, which for the
-% `hello' procedure is `pred(di, uo) is det', before we can actually
-% call it.  The function inst_cast/1 defined below does that.
-%
-
-:- type io_pred == pred(io__state, io__state).
+:- type io_pred == pred(io, io).
  :- inst io_pred == (pred(di, uo) is det).

-:- func inst_cast(io_pred) = io_pred.
-:- mode inst_cast(in) = out(io_pred) is det.
-:- pragma c_code(inst_cast(X::in) = (Y::out(io_pred)),
-	[will_not_call_mercury, thread_safe], "Y = X").
+:- func inst_cast(io_pred::in) = (io_pred::out(io_pred)) is det.
+:- pragma foreign_proc("C",
+    inst_cast(X::in) = (Y::out(io_pred)),
+    [promise_pure, will_not_call_mercury, thread_safe],
+"
+    Y = X;
+").

  % Likewise for `add3int'.
-% Note that for arguments of function type, the function type
-% normally gets automatically propagated into the inst.
-% We use a wrapper type to avoid that.
-
+% Note that for arguments of function type, the function type normally gets
+% automatically propagated into the inst.  We use a wrapper type to avoid that.
+%
  :- type add3int == (func(int, int, int) = int).
  :- type add3int_wrapper ---> wrapper(add3int).
  :- inst add3int_wrapper ---> wrapper(func(in, in, in) = out is det).

-:- func inst_cast_add3int(add3int_wrapper) = add3int_wrapper.
-:- mode inst_cast_add3int(in) = out(add3int_wrapper) is det.
-:- pragma c_code(inst_cast_add3int(X::in) = (Y::out(add3int_wrapper)),
-	[will_not_call_mercury, thread_safe], "Y = X").
+:- func inst_cast_add3int(add3int_wrapper::in)
+    = (add3int_wrapper::out(add3int_wrapper)) is det.

+:- pragma foreign_proc("C",
+    inst_cast_add3int(X::in) = (Y::out(add3int_wrapper)),
+    [promise_pure, will_not_call_mercury, thread_safe],
+"
+    Y = X;
+").
+
+%-----------------------------------------------------------------------------%
+:- end_module dl_test.
+%-----------------------------------------------------------------------------%
Index: dl_test2.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/extras/dynamic_linking/dl_test2.m,v
retrieving revision 1.1
diff -u -r1.1 dl_test2.m
--- dl_test2.m	11 Jan 2001 05:05:29 -0000	1.1
+++ dl_test2.m	7 May 2007 04:11:04 -0000
@@ -1,70 +1,78 @@
+%-----------------------------------------------------------------------------%
+% vim: ft=mercury ts=4 sw=4 et
+%-----------------------------------------------------------------------------%
+%
  % Example program using dynamic linking.
  % This example tests calling functions with floating point arguments.
-
+%
  % This module loads in the object code for the module `hello'
  % from the file `libhello.so', looks up the address of the
  % function add3/3 in that module, and then calls that procedure.
-
+%
  % This source file is hereby placed in the public domain.  -fjh (the author).
+%
+%----------------------------------------------------------------------------%

  :- module dl_test2.
  :- interface.
  :- import_module io.

-:- pred main(state::di, state::uo) is det.
+:- pred main(io::di, io::uo) is det.
+
+%----------------------------------------------------------------------------%
+%----------------------------------------------------------------------------%

  :- implementation.
-:- import_module dl, name_mangle, string, list.

-main -->
-	%
-	% Load in the object code for the module `hello' from
-	% the file `libhello.so'.
-	%
-	dl__open("./libhello.so", lazy, local, MaybeHandle),
-	( 
-		{ MaybeHandle = error(Msg) },
-		print("dlopen failed: "), print(Msg), nl
-	;
-		{ MaybeHandle = ok(Handle) },
-
-		{ Add3Proc = mercury_proc(function, unqualified("hello"),
-					"add3", 3, 0) },
-		dl__mercury_sym(Handle, Add3Proc, MaybeAdd3),
-		(
-			{ MaybeAdd3 = error(Msg3) },
-			print("dlsym failed: "), print(Msg3), nl
-		;
-			{ MaybeAdd3 = ok(Add3Func0) },
-			%
-			% Cast the higher-order term that we obtained
-			% to the correct higher-order inst.
-			%
-			{ wrapper(Add3Func) =
-				inst_cast_add3(wrapper(Add3Func0)) },
-			%
-			% Call the procedure whose address
-			% we just obtained.
-			%
-			{ Sum = Add3Func(1.0, 2.0, 3.0) },
-			io__format("1.0 + 2.0 + 3.0 = %f\n", [f(Sum)])
-		),
-
-
-		%
-		% unload the object code in the libhello.so file
-		%
-		dl__close(Handle, Result),
-		(
-			{ Result = error(CloseMsg) },
-			print("dlclose failed: "), print(CloseMsg), nl
-		;
-			{ Result = ok }
-		)
-	).
+:- import_module dl.
+:- import_module list.
+:- import_module name_mangle.
+:- import_module string.
+
+%----------------------------------------------------------------------------%
+
+main(!IO) :-
+    %
+    % Load in the object code for the module `hello' from
+    % the file `libhello.so'.
+    %
+    dl.open("./libhello.so", lazy, local, MaybeHandle, !IO),
+    ( 
+        MaybeHandle = dl_error(OpenMsg),
+        io.format("dlopen failed: %s\n", [s(OpenMsg)], !IO)
+    ;
+        MaybeHandle = dl_ok(Handle),
+        Add3Proc = mercury_proc(function, unqualified("hello"), "add3", 3, 0),
+        dl.mercury_sym(Handle, Add3Proc, MaybeAdd3, !IO),
+        (
+            MaybeAdd3 = dl_error(Msg3),
+            io.format("dlsym failed: %s\n", [s(Msg3)], !IO)
+        ;
+            MaybeAdd3 = dl_ok(Add3Func0),
+            %
+            % Cast the higher-order term that we obtained to the correct
+            % higher-order inst.
+            %
+            wrapper(Add3Func) = inst_cast_add3(wrapper(Add3Func0)),
+            %
+            % Call the procedure whose address we just obtained.
+            %
+            Sum = Add3Func(1.0, 2.0, 3.0),
+            io.format("1.0 + 2.0 + 3.0 = %f\n", [f(Sum)], !IO)
+        ),
+        %
+        % Unload the object code in the libhello.so file.
+        %
+        dl.close(Handle, CloseResult, !IO),
+        (
+            CloseResult = dl_error(CloseMsg),
+            io.format("dlclose failed: %s\n", [s(CloseMsg)], !IO)
+        ;
+            CloseResult = dl_ok
+        )
+    ).

-%
-% dl__mercury_sym returns a higher-order term with inst `ground'.
+% dl.mercury_sym returns a higher-order term with inst `ground'.
  % We need to cast it to the right higher-order inst, which for the
  % `add3' function is `func(in, in, in) = out is det', before we can actually
  % call it.  The function inst_cast_add3/1 defined below does that.
@@ -77,8 +85,15 @@
  :- type add3_wrapper ---> wrapper(add3).
  :- inst add3_wrapper ---> wrapper(func(in, in, in) = out is det).

-:- func inst_cast_add3(add3_wrapper) = add3_wrapper.
-:- mode inst_cast_add3(in) = out(add3_wrapper) is det.
-:- pragma c_code(inst_cast_add3(X::in) = (Y::out(add3_wrapper)),
-	[will_not_call_mercury, thread_safe], "Y = X").
-
+:- func inst_cast_add3(add3_wrapper::in) = (add3_wrapper::out(add3_wrapper))
+    is det.
+:- pragma foreign_proc("C",
+    inst_cast_add3(X::in) = (Y::out(add3_wrapper)),
+    [promise_pure, will_not_call_mercury, thread_safe],
+"
+    Y = X;
+").
+
+%----------------------------------------------------------------------------%
+:- end_module dl_test2.
+%----------------------------------------------------------------------------%
Index: hello.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/extras/dynamic_linking/hello.m,v
retrieving revision 1.3
diff -u -r1.3 hello.m
--- hello.m	12 Jan 2001 01:31:52 -0000	1.3
+++ hello.m	7 May 2007 04:14:01 -0000
@@ -1,27 +1,49 @@
+%-----------------------------------------------------------------------------%
+% vim: ft=mercury ts=4 sw=4 et
+%-----------------------------------------------------------------------------%
+%
  % Example module for use with dynamic linking.
  % The driver program dl_test.m dynamically loads the object code
  % for this module and then calls the procedures defined here,
  % e.g. hello/2.
-
+%
  % This source file is hereby placed in the public domain.  -fjh (the author).
+%
+%-----------------------------------------------------------------------------%

  :- module hello.
  :- interface.
+
+:- import_module float.
+:- import_module int.
  :- import_module io.
-:- import_module float, int.

-% a very basic test: print "Hello world"
-:- pred hello(state::di, state::uo) is det.
+%-----------------------------------------------------------------------------%
+
+    % A very basic test: print "Hello world".
+    %
+:- pred hello(io::di, io::uo) is det.

-% test passing floating point arguments
+    % Test passing floating point arguments.
+    %
  :- func add3(float, float, float) = float.

-% test passing integer arguments
+    % Test passing integer arguments.
+    %
  :- func add3int(int, int, int) = int.

+%-----------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
+
  :- implementation.

-hello --> print("Hello, world\n").
+hello(!IO) :-
+    io.print("Hello, world\n", !IO).

  add3(X, Y, Z) = X + Y + Z.
+
  add3int(X, Y, Z) = X + Y + Z.
+
+%-----------------------------------------------------------------------------%
+:- end_module hello.
+%-----------------------------------------------------------------------------%

--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list