[mercury-users] Problem with state variable

Julien Fischer juliensf at cs.mu.OZ.AU
Tue Jun 20 01:26:41 AEST 2006


On Mon, 19 Jun 2006 valery at freesurf.fr wrote:

> Hello,
>
> I have defined a range_foldl which is pretty much a for-loop
>
> :- func range_foldl(func(int, T) = T, int, int, T) = T.
> :- mode range_foldl(func(in, in) = out is det, in, in, in) = out is det.
> :- mode range_foldl(func(in, di) = uo is det, in, in, di) = uo is det.
> range_foldl(F, From, To, X) =
>  (if From > To then X else range_foldl(F, From + 1, To, F(From, X))).

This is just duplicating functionality that is available in the standard
library, see int.fold_up, int.fold_down etc.

> and used it to print a board line by line
>
> main(!IO) :-
>  N = 3,
>  some [!B] (
>   !:B = init(N*N, 0),
>   !:B = (!.B ^ elem(1) := N),
>   !:IO = range_foldl(func(J::in, !.IO::di) = (!:IO::uo) is det :-
>    !:IO = range_foldl(func(I::in, !.IO::di) = (!:IO::uo) is det :-
>     (io.format("%d\t", [i(!.B ^ elem(I + N * J))], !IO) % !! ERROR !!
>     ,io.format("\n", [], !IO)),
>    0, N - 1, !.IO),
>   0, N - 1, !.IO)
>  ).
>
> but the compiler complains that !.B in not visible in the first io.format.

That appears to be a compiler bug (in the code that implements state
variables).  We'll take a look at it.

> I also get a mode error there ; I hope it'll go away with the visibility
> error.

It will.

> What's wrong ?

See above.  In the meantime I suggest the following workaround:

	main(!IO) :-
	  N = 3,
	  some [!B] (
	   !:B = init(N*N, 0),
	   !:B = (!.B ^ elem(1) := N),
	    Tmp = !.B,
	    !:IO = range_foldl(func(J::in, !.IO::di) = (!:IO::uo) is det :-
	    !:IO = range_foldl(func(I::in, !.IO::di) = (!:IO::uo) is det :-
	     (io.format("%d\t", [i(Tmp ^ elem(I + N * J))], !IO) % !! ERROR !!
	     ,io.format("\n", [], !IO)),
	    0, N - 1, !.IO),
	   0, N - 1, !.IO)
	 ).


(Incidently, I think you're printing out the newline in the wrong spot.)

BTW, I think that above program would be written more idiomatically in
Mercury as:

	main(!IO) :-
	     N = 3,
	     some [!B] (
		 !:B = init(N * N, 0),
		 set(1, N, !B),
		 Tmp = !.B,
		 PrintCol = (pred(J::in, !.IO::di, !:IO::uo) is det :-
		    PrintRow = (pred(I::in, !.IO::di, !:IO::uo) is det :-
			io.format("%d\t", [i(Tmp ^ elem(I + N * J))], !IO)
		    ),
		    int.fold_up(PrintRow, 0, N - 1, !IO),
		    io.nl(!IO)
		),
		int.fold_up(PrintCol, 0, N - 1, !IO)
	    ).

Julien.
--------------------------------------------------------------------------
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