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

Mark Brown mark at csse.unimelb.edu.au
Wed Jan 11 01:33:15 AEDT 2012


Hi,

On 09-Jan-2012, Jeff Thompson <jeff at thefirst.org> 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, short of avoiding nondet
>> code to begin with (i.e., implement your own search in det/semidet code).
>
> Thanks for the feedback.  Can you say something more about the approach to 
> implement my own search?

Standard depth-first-search: keep a stack containing, at each level, the
search state and the unexplored choices; discard the stack once you've
found the solution(s) you want.

>
> Also, solutions.do_while in the library can "stop before all solutions have 
> been found".  Does it also use try/catch internally?

Not internally.  But do_while is cc_multi, so in a situation like yours it
will be enclosed in a commit, and the commit will use try/catch.

Cheers,
Mark.

--------------------------------------------------------------------------
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