[m-users.] Adding impurity to Mercury code: Backtracking in IO states
Left Right
olegsivokon at gmail.com
Sun Jun 28 15:34:53 AEST 2015
I'm sure others will give you more detailed answers, but from the
first reading, there are several problems with your code. One problem
is that you are trying to unify two grounded terms, knowing they are
already grounded and distinct. For instance: IOState_out = IOState_in
- I don't think this will ever work, i.e. this predicate will never
succeed (what were you trying to achieve with it?). By the way, if you
are interested, there's more idiomatic way to write it:
print(X, X).
Other warnings are related to the fact that whenever a formal argument
of a predicate is declared, it is expected that it will be used
somewhere in the body of the predicate. This warning is also known as
"singleton variable". you have IOState_out parameter in your first
print predicate, but you never use it. The next error says that you
are trying to evaluate a predicate with variables, whose instantiation
mode doesn't match the predicate's requirement. In particular,
io.write_string needs its third argument to be instantiated as
"unique" (this is a Mercury's way of saying it has to be obtained from
doing some other I/O).
Best.
Oleg
On Sun, Jun 28, 2015 at 5:35 AM, barthwal <barthwal at protonmail.com> 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).
>
>
> _______________________________________________
> users mailing list
> users at lists.mercurylang.org
> https://www.mercurylang.org/lists/listinfo/users
>
More information about the users
mailing list