[m-users.] Adding impurity to Mercury code: Backtracking in IO states

Tomas By tomas at basun.net
Sun Jun 28 21:16:21 AEST 2015


Several problems.

1) you need the out parameter also in the call to "write_string":

    io.write_string(X, IOState_in, IOState_out)

2) "print" needs to take di/uo parameters:

    :- pred print(io::di, io::uo) is det.

3) the definition of "print" has two clauses with the same head and is
declared det. You need to either combine them, or make the heads disjoint,
or declare the predicate multi/nondet (in which case it cannot take di/uo
params).

It seems that what you intend for "print" to do is print out the list of
strings in the "test" predicate. You need "solutions" for this I believe:

  print(!IO) :-
    solutions(text,Ts),
    io.write_list(Ts,"\n",io.write_string,!IO).

/Tomas



On Sun, June 28, 2015 04:35, barthwal wrote:
> Hi,
>
> In one of the module, I am trying to add impurity to Mercury. I code looks
> like below and I get the following error:
>
> ex2.m:023: In clause for predicate `ex2.print'/2:
> ex2.m:023: warning: variable `IOState_out' occurs only once in this scope.
> ex2.m:025: In clause for `print(in, out)':
> ex2.m:025: in argument 2 of call to predicate `io.write_string'/3:
> ex2.m:025: mode error: variable `IOState_in' has instantiatedness
> `ground',
> ex2.m:025: expected instantiatedness was `unique'.
> ex2.m:034: In clause for `main(di, uo)':
> ex2.m:034: in argument 2 of call to predicate `io.write_string'/3:
> ex2.m:034: mode error: variable `IOState_temp' has instantiatedness
> `ground',
> ex2.m:034: expected instantiatedness was `unique'.
>
> How do I solve it?
>
> I don't want to use the All solutions predicate or even Foreign language
> interface!
>
>
> :- module ex2.
> :- interface.
> :- import_module io.
> :- pred main(io::di, io::uo) is det.
> :- implementation.
> :- pred text(string::out) is multi.
> :- pred print(io::in, io::out) is det.
>
> text("A").
> text("B").
> text("C").
> text("D").
> text("E").
> text("F").
>
> print(IOState_in, IOState_out) :-
> text(X),
> io.write_string(X, IOState_in, _),
> X = "D".
> print(IOState_in, IOState_out) :-
> IOState_out = IOState_in.
>
> main(IOState_in, IOState_out) :-
> print(IOState_in, IOState_temp),
> io.write_string("Hello, World!\n", IOState_temp,
> IOState_out).





More information about the users mailing list