[m-rev.] for review: try_func

Zoltan Somogyi zs at cs.mu.OZ.AU
Tue Jun 26 14:20:35 AEST 2001


For review by anyone.

library/exception.m:
	Add a predicate, try_func, which is like try but takes a function, not
	a predicate, as its argument.

Zoltan.

cvs diff: Diffing .
Index: exception.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/exception.m,v
retrieving revision 1.48
diff -u -b -r1.48 exception.m
--- exception.m	2001/05/31 06:00:04	1.48
+++ exception.m	2001/06/26 04:19:05
@@ -66,6 +66,21 @@
 :- mode try(pred(out) is cc_nondet, out)              is cc_multi.
 
 %
+% try_func(Func, Result):
+%    Operational semantics:
+%	Call R = Func.
+%	If Goal succeeds, succeed with Result = succeeded(R).
+%	If Goal throws an exception E, succeed with Result = exception(E).
+%    Declarative semantics:
+%       try_func(Func, Result) <=>
+%               ( R = Func, Result = succeeded(R)
+%               ; Result = exception(_)
+%		).
+%
+:- pred try_func((func) = T,	    exception_result(T)).
+:- mode try_func((func) = out is det, out(cannot_fail)) is cc_multi.
+
+%
 % try_io(Goal, Result, IO_0, IO):
 %    Operational semantics:
 %	Call Goal(R, IO_0, IO_1).
@@ -408,6 +423,10 @@
 :- mode wrap_success(pred(out) is cc_nondet, out) is cc_nondet.
 wrap_success(Goal, succeeded(R)) :- Goal(R).
 
+:- pred wrap_success_func((func) = T, exception_result(T)) is det.
+:- mode wrap_success_func((func) = out is det, out) is det.
+wrap_success_func(Func, succeeded(R)) :- R = apply(Func).
+
 :- pred wrap_success_or_failure(pred(T), exception_result(T)) is det.
 :- mode wrap_success_or_failure(pred(out) is det, out) is det.
 :- mode wrap_success_or_failure(pred(out) is semidet, out) is det.
@@ -444,7 +463,6 @@
 		wrap_exception, Result0),
 	cc_multi_equal(Result0, Result).
 try(cc_multi, Goal, Result) :-
-
 	builtin_catch(
 		(pred(R::out) is cc_multi :-
 				wrap_success_or_failure(Goal, R)
@@ -455,6 +473,12 @@
 				wrap_success_or_failure(Goal, R)),
 		wrap_exception, Result).
 
+
+try_func(Func, Result) :-
+	builtin_catch((pred(R::out) is det :-
+				wrap_success_func(Func, R)),
+		wrap_exception, Result0),
+	cc_multi_equal(Result0, Result).
 
 /**********
 % This doesn't work, due to 
--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list