[m-users.] Confusion over try goal determinism.

Julien Fischer jfischer at opturion.com
Sun Nov 19 13:45:48 AEDT 2023


On Sat, 18 Nov 2023, Sean Charles (emacstheviking) wrote:

> In my main execution code, I have predicate that I want to loop, catch
> any exceptions and then just rinse-repeat until the user ends the
> session:
> 
>     171 :- pred merth_session(mstate::in, mstate::out, io::di, io::uo) is det.
>     172 
>     173 merth_session(!S, !IO) :-
>     174     try [ io(!IO) ]
>     175         % promise_equivalent_solutions [!S, !IO]
>     176         merth_session_(!S, !IO)
>     177     then
>     178         merth_session(!S, !IO)
>     179 
>     180     catch stack_depth(X, Y) ->
>     181         console_error("Stack depth! %i %i", [ i(X), i(Y)], !IO)
>     182 
>     183     catch stack_type_int(T) ->
>     184         console_error("Wrong type, int expected, got %s",
>     185             [ s(string(T)) ], !IO)
>     186 
>     187     catch compile_time_word(Word) ->
>     188         console_error("%s is compile time only", [ s(Word) ], !IO)
>     189 
>     190     catch interpret_time_word(Word) ->
>     191         console_error("%s is interpret time only", [ s(Word) ], !IO).
> 
> The compiler error is:
> 
> merth.m:171: In `merth_session'(in, out, di, uo):
> merth.m:171:   error: determinism declaration not satisfied.
> merth.m:171:   Declared `det', inferred `multi'.
> merth.m:171:   The reason for the difference is the following.
> merth.m:174:   Call to
> merth.m:174:   `exception.magic_exception_result'(out((exception.cannot_fail)))
> merth.m:174:   can succeed more than once.
> merth.m:174: Error: call to predicate `exception.magic_exception_result'/1 with
> merth.m:174:   determinism `cc_multi' occurs in a context which requires all
> merth.m:174:   solutions.
> 
> 
> In the user manual, seciton 14, Exception handling, at the top it says:
> 
> Goal must have one of the following determinisms: det, semidet, cc_multi, or cc_nondet.

"Goal" here means the goal _inside_ the try (i.e the one that you're
trying to catch any exceptions from).

> And then further down it says:
> 
> A try goal has determinism cc_multi.

"try goal" means the entire try ... then ... catch etc. construct.

> I am just a little confused at this point as to what I am being told
> by the compiler, I can't even see 'magic_exception' so it's inferred
> something perhaps? Do I need a catch_any, that's optional though?

What the compiler is trying to tell is that merth_session/4 should have
determinism cc_multi (because the try goal in its body has determinism
cc_multi).

The stuff about the magic_exception_result arises from the
source-to-source transformation that is used to implement try goals.

Julien.


More information about the users mailing list