[mercury-users] if-then-else transformation

Peter Schachte schachte at unimelb.edu.au
Wed Oct 12 15:36:21 AEDT 2011

On 10/12/2011 01:44 PM, Chris King wrote:
> Mercury's if/then/else is currently limited in that all variables in
> the condition *must* be bound somewhere in the condition or outside
> the if/then/else, even if said variables are bound within the then and
> else.  This precludes "reversal" of certain functions, viz.:

This is a really good question.  I very recently gave up on trying to
make some Mercury code work in multiple modes for exactly this reason.
I can't see why code like this couldn't be made reversible.

> However a solution exists.  We can use an if/then/else to decide
> odd(X) and bind a boolean variable indicating success.  A disjunct can
> then switch on this variable:
> make_even(X, Y) :-
>     (if odd(X) then Odd = yes else Odd = no),
>     (Odd = yes, Y = X + 1; Odd = no, Y = X).

Nice.  I hadn't thought of trying something like that.  But I would
think the compiler could achieve the same effect more simply by leaving
the code as originally written in modes that can schedule the condition
before the then and else parts, and transforming it into your second
version (the disjunctive one) for other modes.  Anyway, it would be a
lot easier to write multi-modal predicates if Mercury could handle
conditionals in modes that don't allow scheduling the condition first.

Peter Schachte              What we are looking at is good and evil,
University of Melbourne     right and wrong. A new world order!
schachte at unimelb.edu.au         -- George Bush
Phone: +61 3 8344 1338
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