[m-users.] How do you go about logging?

Left Right olegsivokon at gmail.com
Sun Apr 6 16:20:54 AEST 2014

Thanks for reply! But wouldn't you mind elaborating a bit? Here's what
I've tried, with the error message that resulted:

% e3.m:011: Error: trailed mutable in non-trailing grade.
% e3.m:018: In clause for `nonedivides(in, out)':
% e3.m:018:   in argument 2 of call to predicate `io.write_string'/3:
% e3.m:018:   unique-mode error: the called procedure would clobber
its argument,
% e3.m:018:   but variable `STATE_VARIABLE_IO_9' is still live.
% e3.m:020:   The goal could not be reordered, because it was followed by an
% e3.m:020:   impure goal.
% e3.m:016:   This is the location of the impure goal.

:- mutable(logging_level, int, 0, ground, []).

:- pred nonedivides(int::in, list(int)::out) is semidet.
nonedivides(_, []).
nonedivides(Test, [Factor | Factors]):-
    trace [io(!IO), state(logging_level, !LoggingLevel)]
    (!.LoggingLevel > 1 ->
     io.write_string("Factor = ", !IO),
     io.write(Factor, !IO),
     io.nl(!IO) ; true),
    Factor mod Test \= 0, nonedivides(Test, Factors).

Sorry, I have to admit that I am completely at a loss interpreting the
message. Apparently the example in the documentation is not enough to
produce the output. It probably means I need to add some... compiler
options? declarations of ...? To me it looks like it completely didn't
understand what the code was supposed to do because it tries to treat
!IO variable not in the way it should (?) or am I completely off the



On Sun, Apr 6, 2014 at 4:13 AM, Michael Richter <ttmrichter at gmail.com> wrote:
> On 6 April 2014 02:51, Oleg Sivokon <olegsivokon at gmail.com> wrote:
>> I'm trying to log the values of the arguments while at every iteration
>> of the predicate in order to understand what went wrong, but I can't
>> because IO arguments aren't allowed in a semi-deterministic
>> predicate... so, how do I print something from a predicate like that?
> You use trace goals[1].  One of the reasons I like Mercury is that it's pure
> ... until purity gets in the way.  Then it gives you controlled ways out of
> the purity (which you use at your own risk, of course!).
> [1]
> http://mercurylang.org/information/doc-release/mercury_ref/Trace-goals.html#Trace-goals
> --
> "Perhaps people don't believe this, but throughout all of the discussions of
> entering China our focus has really been what's best for the Chinese people.
> It's not been about our revenue or profit or whatnot."
> --Sergey Brin, demonstrating the emptiness of the "don't be evil" mantra.

More information about the users mailing list