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

Mark Brown mark at csse.unimelb.edu.au
Sat Jan 14 15:46:00 AEDT 2012

Hi Jeff,

On 12-Jan-2012, Jeff Thompson <jeff at thefirst.org> wrote:
> On 1/9/2012 11:15 PM, Jeff Thompson wrote:
>> On 1/8/2012 8:58 PM, Mark Brown wrote:
>>> 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.
>>>> 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,

Okay, I'm probably wrong there - I expect your solution using "yield" would
be more efficient.  (Note that the paper I referred to earlier dates from
several years before yield was added to .NET).

>>> short of avoiding 
>>> nondet
>>> code to begin with (i.e., implement your own search in det/semidet code).
> I'm still struggling with this one. I set pred test to the "fast version" 
> which tests "X > 2" which fails and does not throw an exception. A 
> benchmark with 10,000,000 calls to this takes only 0.45 seconds. (Awesome!)
> When I set the test back to "X > 0" where it throws the exception, the 
> benchmark takes 171 seconds.  That's 380 times as long.  This is what 
> "message passing with exceptions is inefficient" means.  It really hurts.  
> Is there no help to be gotten from the compiler?

Well, the first case never "succeeds on the first solution", so it doesn't
actually measure the functionality you're asking for.  How does the version
using exceptions compare to the handwritten version using yield?

Also, how do the savings, if any, compare to the amount of time required to
perform a typical search?  The reason I ask is that the project I work on
uses nondet search extensively, but the searches are millisecond-scale at
the very least.  Hence microsecond-scale costs of exceptions are not as
painful for us as the rule-of-thumb would suggest.


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