[m-dev.] try syntax

Julien Fischer juliensf at csse.unimelb.edu.au
Thu Jan 22 23:51:18 AEDT 2009


On Thu, 22 Jan 2009, Peter Wang wrote:

> Catching exceptions is too cumbersome.  I think we should have built in
> syntax, e.g. (adjust accordingly if the parser is too dumb)

I agree too, a similar argument was the reason for replacing
promise_only_solution with promise_equivalent_solutions scopes.

>    try [io(!IO)] (
> 	f(X, Y, Z, !IO)

Presumably, this can be some arbitrary goal, not just a call?

>    ) then (
>        on_success(X, Y, Z)	% outputs in scope
>    ) else (
>        on_failure
>    ) catch [_ : exc1] (	% zero or more catch blocks
>        on_catch_exc1
>    ) catch [E] (
>        rethrow(exception(E))
>    ) finally (			% optional
>        cleanup
>    )
>
> io(!IO) selects try_io.
> store(!S) selects try_store.

And stm(!S) should select try_stm (at least when/if STM
in Mercury is publicly documented and if that the STM
state is still used in that version.)


> all(Solutions) selects try_all.
> Otherwise use try.

Would I be able to do the following:

 	try [io(!IO), store(!S1), store(!S2)] (
 		f(X, Y, Z, !IO, !S1, !S2)
 	)

etc ....
?

> We'd implement it by doing a tranformation to call the underlying
> predicates in the exception module.  The following assumes no `finally'
> block.
>
>    try_io(
>        (pred({X0, Y0, Z0}::out, !.IO::di, !:IO::uo) is det :-
>            f(X0, Y0, Z0, !IO)
>        ), TryResult, !IO),
>    (
>        TryResult = succeeded({X, Y, Z}),
>        on_success(X, Y, Z)
>    ;
>        TryResult = failed,
>        on_failure
>    ;
>        TryResult = exception(E0),
>        ( univ_to_type(E0, _ : exc1) ->
>            on_catch_exc1
>        ; univ_to_type(E0, E) ->
>            rethrow(exception(E))
>        )
>    )

It would be nice if we could avoid (1) having to construct and call
the closure and (2) having to tuple and untuple the outputs.  I guess
this could be done by implementing exception handlers in the code generator(s)
rather than as library predictes?  (It's would almost certainly be a lot
of work though.)

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



More information about the developers mailing list