[m-users.] Might this be optimized away?

Volker Wysk post at volker-wysk.de
Sat Dec 16 18:13:02 AEDT 2023


Hi!

I have this type:

:- type odbc.result
    ---> odbc.ok(odbc.warnings)
    ;    odbc.error(odbc.messages).

I want to check for errors and throw an exception, if there are any. I'm NOT
doing it like this:

:- pred throw_db_error(odbc.result::in) is det.

throw_db_error(Res) :-
    (
        Res1 = odbc.ok(_)
    ;
        Res1 = odbc.error(Messages),
        throw(db_error(errors(Messages)))
    ).

The reason is, that this might be optimized away in a semantics other than
the default strict commutative semantics, because the predicate has no
output argument.

Instead, I want to do it like this:

:- pred throw_db_error(odbc.result::in, odbc.result::out) is det.

throw_db_error(Res1, Res2) :-
    (
        Res1 = odbc.ok(_),
        Res2 = Res1
    ;
        Res1 = odbc.error(Messages),
        throw(db_error(errors(Messages)))
    ).

Now, my question is, what happens when Res2 isn't used after the call to
throw_db_error? It could be like this:

  odbc.execute("insert into ...", Res, !DB),
  throw_db_error(Res, _)

Do we have the same situation as above, when throw_db_error had only one
argument? Meaning, might it be optimized away?

Cheers,
Volker


More information about the users mailing list