[mercury-users] Possible state var / uniqueness bug Re: Request for comments on this code

Peter Moulder Peter.Moulder at infotech.monash.edu.au
Mon Jul 10 15:49:37 AEST 2006


On Sun, Jul 09, 2006 at 07:13:36PM +0200, Nicolas Pelletier wrote:

> >   ( clock(R0, !IO) ->
> >   	( solutions(queens(N), Q) ->
> > 		clock(R1, !IO),
> > 		...
> > 	 ; false
> > 	)
> >    ; false
> >   )

> As this code is in the det mode predicate main, I cannot afford to
> call false and used error instead.

Actually, the compiler knows that the clock and solutions calls always
succeed, so it's fine to use false: the compiler still knows that the
if-then-else as a whole always succeeds, because its `if' and `then'
goals always succeed.

> However, the code is rejected (see below): the (clock(R0, ! IO) ->
> part is taken to mean that the call to clock may fail, and thus !.IO
> on that line does not get instantiated with the right mode.

This seems to be an issue with state variables: it works fine if I
change `!IO' to `S0, S1' and so on.

Here's a simplified version of the problem:

  :- module tst2.
  :- interface.
  :- import_module io.
  :- pred main(io::di, io::uo) is det.
  :- implementation.
  main(!IO):-
        ( if io.write_string("hello, world\n", !IO) then
                true
         else
                false
        ).

  $ mmc -d all -E --make tst2
  Making Mercury/cs/tst2.c
  tst2.m:007: In clause for `main(di, uo)':
  tst2.m:007:   in argument 2 of call to predicate `io.write_string/3':
  tst2.m:007:   mode error: variable `STATE_VARIABLE_IO_0' has instantiatedness `mostly_unique',
  tst2.m:007:   expected instantiatedness was `unique'.
  ** Error making `Mercury/cs/tst2.c'.

(I tried doing ‘, !.IO, Tmp) then !:IO = Tmp’ and some variations on
that, but to no avail.)

Interestingly, if I copy tst2.hlds_dump.099-front_end to tst3.m, and put back
the `:-' lines and s/tst2/tst3/g, then it works fine:

  :- module tst3.
  :- interface.
  :- import_module io.
  :- pred main(io::di, io::uo) is det.
  :- implementation.
  tst3.main(STATE_VARIABLE_IO_0, STATE_VARIABLE_IO) :-
        (if
                V_7 = "hello, world\n",
                io.write_string(V_7, STATE_VARIABLE_IO_0, STATE_VARIABLE_IO_1)
        then
                STATE_VARIABLE_IO_3 = STATE_VARIABLE_IO_1
        else
                builtin.false
        ),
        STATE_VARIABLE_IO = STATE_VARIABLE_IO_3.

  $ mmc --make tst3
  Making Mercury/cs/tst3.c
  tst3.m:013: Warning: the condition of this if-then-else cannot fail.
  Making Mercury/os/tst3.o
  Making tst3
  $ ./tst3
  hello, world

Is this a compiler bug?  Or how should tst2.m be written such that it
uses state-variable syntax for its io vars, and has an I/O call as the
condition of an if-then-else?

pjrm.
--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at csse.unimelb.edu.au
administrative address: owner-mercury-users at csse.unimelb.edu.au
unsubscribe: Address: mercury-users-request at csse.unimelb.edu.au Message: unsubscribe
subscribe:   Address: mercury-users-request at csse.unimelb.edu.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list