[mercury-users] First solution in csharp without try/catch

Mark Brown mark at csse.unimelb.edu.au
Mon Jan 9 15:58:25 AEDT 2012

Hi Jeff,

On 06-Jan-2012, Jeff Thompson <jeff at thefirst.org> wrote:
> Hello again.  I'm using the csharp grade to compile the following.
> :- pred multiPred(int::out) is multi.
> multiPred(1).
> multiPred(2).
> :- pred test is semidet.
> test :- if multiPred(X), X > 0 then true else fail.
> In pred test, in the if statement, when "multiPred(X), X > 0" first 
> succeeds, the predicate succeeds, else fails.  The compiled C# code works 
> correctly.  But the implementation throws a new runtime.Commit() exception 
> when it first succeeds, and catches this in an outer try/catch block (see 
> below).  I can understand why the authors would have implemented it this 
> way, but using exceptions for message passing in normal operation is very 
> inefficient.

In general you need a way to wind back the stack by an arbitrary number
of frames for a commit, and in C# throwing an exception may well be the
quickest way to achieve that.  In principle the compilation model could
return a flag for each nondet call, but that would need to be tested after
every call even if not actually required.  That would slow things down in
many (probably most) cases.

There is some discussion of this issue in section 3.5 of this paper:


> Is there another way (where the implementation doesn't use try/catch) to 
> make a semidet predicate which succeeds on the first solution of some code 
> block?

I don't think there is a more efficient solution, short of avoiding nondet
code to begin with (i.e., implement your own search in det/semidet code).

> This is similar to Prolog once/1?

Yes, except that Mercury has enough determinism information to infer the
commit rather than it needing to be explicit.


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

More information about the users mailing list