[mercury-users] Determinism of if-then-elses

Mark Brown mark at cs.mu.OZ.AU
Wed Apr 12 12:37:49 AEST 2006


On 12-Apr-2006, Nicholas Nethercote <njn at csse.unimelb.edu.au> wrote:
> Hi,
> 
> I have some code like this:
> 
>   :- pred get_int(int::in) : parser(literal) `with_inst` parser.
> 
>   get_int(Int, Lit, S) -->
>       ( if      char('.', S)                    then get_int_dot(Lit, S)
>         else if ( char('E', S) ; char('e', S) ) then get_exponent(Lit, S)
>         else                                    { Lit = int(Int) }
> 
> I get a determinism error:
> 
>  zinc.m:1966: In `get_int(in, out, in, in, out)':
>  zinc.m:1966:   error: determinism declaration not satisfied.
>  zinc.m:1966:   Declared `semidet', inferred `nondet'.
>  zinc.m:1991:   Disjunction has multiple clauses with solutions.
> 
> Fair enough -- the ref manual says an if-then-else:
> 
>   can succeed more than once if any one of the condition, the "then" part
>   and the "else" part can succeed more than once.

Yep, that's right.

> 
> So why does the following code from library/lexer.m compile?  The line 
> marked XXX seems to be doing exactly what causes problems in my code.

That goal has no output variables.  In such a case all "solutions" must
be equivalent, so the compiler (safely) prunes away any solutions after
the first.  This changes the nondet goal into a semidet one.

In your case, I guess either S or one of the DCG arguments is an output,
so the pruning cannot happen.

Cheers,
Mark.

>                 Result = ok(Char),
>                 ( char__is_whitespace(Char) ->
>                         lexer__get_token_2(Token, Context, !IO)
>                 ; ( char__is_upper(Char) ; Char = '_' ) ->        XXX
--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list