[mercury-users] Rewriting if-then-else as (CondGoal, ThenGoal; not(CondGoal), ElseGoal)

Mark Brown mark at csse.unimelb.edu.au
Mon Sep 5 13:39:42 AEST 2011


Hi Daniel,

On 04-Sep-2011, Daniel Patterson <lists.mercury at dbp.mm.st> wrote:
> Hi,
> 
> sorry if this is a really beginner question,

Beginner questions are more than welcome.  :-)

...


> What am I not understanding? I read the section on determinism and on goals
> (that talks about if-then-else having different semantics) in the reference
> manual, but I'm having a hard time figuring out how it is applying here, and
> how to fix the code! 

What Peter said is correct.  The relevant part of the language reference
is sec 6.2, "Determinism checking and inference", specifically the definition
of switch:

    A disjunction is a switch if each disjunct has near its start a
    unification that tests the same bound variable against a different
    function symbol.

Not everything that is logically a switch can be detected as such.  You can
find a related post from Zoltan here:

    <http://www.mercury.csse.unimelb.edu.au/mailing-lists/mercury-users/mercury-users.201108/0016.html>

> :- pred euler1(int::in,int::out) is det.
> 
> euler1(N,P) :- 
>   Np = N mod 3, Nq = N mod 5,
>   ( if 
>       N =< 0 
>     then 
>       P = 0 
>     else
>         (
>           if (Np = 0; Nq = 0)
>           then euler1(N-1,Ps), P = N + Ps
>           else euler1(N-1,P)
>         )
>   ).

Note that this can also be written without the brackets around the
if-then-else in the else branch.  E.g.:

euler1(N, P) :- 
    Np = N mod 3,
    Nq = N mod 5,
    ( if N =< 0 then 
        P = 0 
    else if (Np = 0; Nq = 0) then
        euler1(N - 1, Ps),
        P = N + Ps
    else
        euler1(N - 1, P)
    ).

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