[mercury-users] Request for code review

Fergus Henderson fjh at cs.mu.OZ.AU
Tue May 27 12:55:55 AEST 2003


On 27-May-2003, David Overton <dmo at cs.mu.OZ.AU> wrote:
> On Mon, May 26, 2003 at 11:40:28PM +0100, Keith Braithwaite wrote:
> > 2) how this code could be improved, particularly
> > 	a: is there any reflection like mechanism available to avoid having 
> > 	to give the name of the test function?

Yes, we have added some support for deconstructing higher-order terms
in Mercury version 0.11.  You can use `deconstruct.functor(Pred,
include_details_cc, Name, _)' to get the predicate name if it is available.
I have enclosed below a patch to the version of the source that David
posted which shows how to do this.

However, there are some caveats to this.  Firstly, higher-order terms that
represent the same logical relation but which happen to be constructed
using different predicate names are considered equal, and the compiler
is allowed to substitute them, so the name that you get might not be
exactly the one that you expect.  Secondly, because of this, calls which
deconstruct higher-order terms have determinism `cc_multi'; you may also
need to declare all callers as `cc_multi' too.  Finally, deconstructing
higher-order terms in this way currently only works with the original
low-level back-end of the Mercury compiler, not with the --high-level-code
back; for the --high-level-code back-end, you'll just get back the
string "<<predicate>>" rather than the predicate name.

diff -u orig/mtest.m ./mtest.m
--- orig/mtest.m	Tue May 27 12:15:57 2003
+++ ./mtest.m	Tue May 27 12:37:50 2003
@@ -22,22 +22,24 @@
 
 :- type suite == string.
 
-:- pred run_tests(suite::in, tests::in(tests), io::di, io::uo) is det.
+:- pred run_tests(suite::in, tests::in(tests), io::di, io::uo) is cc_multi.
 
 :- implementation.
 
 :- import_module list.
 :- import_module bool.
+:- import_module deconstruct.
 
 :- pred do_test(tests::in(tests), list(string)::in, list(string)::out, 
-			io::di, io::uo) is det.
+			io::di, io::uo) is cc_multi.
 
 do_test([Test | RemainingTests], !Failures, !IO) :-
 	( call(Test ^ case) ->
 		write_string(".", !IO)
 	;
 		write_string("F", !IO),
-		!:Failures = [Test ^ name | !.Failures]
+		functor(Test ^ case, include_details_cc, Name, Arity),
+		!:Failures = [Name | !.Failures]
 	), 
 	do_test(RemainingTests, !Failures, !IO).
 
diff -u orig/test.m ./test.m
--- orig/test.m	Tue May 27 12:15:57 2003
+++ ./test.m	Tue May 27 12:19:24 2003
@@ -4,7 +4,7 @@
 :- interface.
 :- import_module io.
 
-:- pred main(io::di, io::uo) is det.
+:- pred main(io::di, io::uo) is cc_multi.
 
 :- implementation.
 

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list