[mercury-users] Mode polymorphism
Peter Hawkins
peter at hawkins.emu.id.au
Fri Jul 29 12:22:39 AEST 2005
Mark Brown wrote:
>You get this error message because Y has inst `ground' rather than
>`non_empty_list' ;-). You forgot to call map_poly instead of map.
>
>
Oops. Let's try again:
:- import_module io.
:- pred main(io.state::di, io.state::uo) is det.
:- implementation.
:- import_module list.
:- import_module int.
:- import_module exception.
% Version of map with mode polymorphism
:- func map_poly(func(T1) = T2, list(T1)) = list(T2).
:- mode map_poly((func(in(I)) = (out(O)) is det), in(list(I))) =
out(list(O))
is det.
map_poly(_Func, []) = [].
map_poly(Func, [X|Xs]) = [Y|Ys] :-
Y = apply(Func, X),
Ys = map_poly(Func, Xs).
:- func get_list = (list(list(int))::out(list(non_empty_list))) is det.
get_list = [[1],[2]].
:- pred print_non_empty_list(list(list(int))::in(list(non_empty_list)),
io.state::di, io.state::uo) is det.
print_non_empty_list([], !IO).
print_non_empty_list([X|Xs], !IO) :-
io.print(X, !IO),
io.nl(!IO),
print_non_empty_list(Xs, !IO).
:- func get_elem(list(T)::in(list(I)), int::in) = (T::out(I)) is det.
get_elem(L, X) = Out :-
(if X = 0, L = [A | _] then
Out = A
else if X > 0, L = [_ | As] then
Out = get_elem(As, X-1)
else
throw("bad")
).
main(!IO) :-
A = get_list,
B = [0,1],
Z = map_poly(get_elem(A), B),
print_non_empty_list(Z, !IO),
true.
I now get this error:
$ mmc --make -E test2
Making Mercury/cs/test2.c
test2.m:043: In clause for `main(di, uo)':
test2.m:043: mode error in conjunction. The next 4 error messages
test2.m:043: indicate possible causes of this error.
test2.m:046: In clause for `main(di, uo)':
test2.m:046: in argument 1 of call to function `test2.map_poly/2':
test2.m:046: mode error: variable `V_14' has instantiatedness `free',
test2.m:046: expected instantiatedness was `(func(((V_1 =< ground) >>
(V_1 =< ground))) = (free >> (V_2 =< ground)) is det)'.
test2.m:046: In clause for `main(di, uo)':
test2.m:046: mode error: argument 2 became too instantiated.
test2.m:046: Final instantiatedness of `V_21' was
`bound(list.'[|]'(ground, ground))',
test2.m:046: expected final instantiatedness was `(V_2 =< ground)'.
test2.m:047: In clause for `main(di, uo)':
test2.m:047: in argument 1 of call to predicate
`test2.print_non_empty_list/3':
test2.m:047: mode error: variable `Z' has instantiatedness `free',
test2.m:047: expected instantiatedness was `bound((list.[]) ;
list.'[|]'(bound(list.'[|]'(ground, ground)), ...))'.
=)
Peter
--------------------------------------------------------------------------
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