[mercury-users] Mode polymorphism
Mark Brown
mark at cs.mu.OZ.AU
Fri Jul 29 11:46:17 AEST 2005
On 29-Jul-2005, Peter Hawkins <peter at hawkins.emu.id.au> wrote:
> Hi...
>
> Why doesn't this work?
>
> :- module test2.
> :- interface.
> :- import_module io.
>
> :- pred main(io.state::di, io.state::uo) is det.
>
> :- implementation.
>
> :- import_module list.
>
> % 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]].
>
> :- func do_something(list(int)::in(non_empty_list)) =
> (list(int)::out(non_empty_list)) is det.
> do_something(X) = X.
>
> :- 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).
>
> main(!IO) :-
> X = get_list,
> Y = map(do_something, X),
> print_non_empty_list(Y, !IO),
> true.
>
> I get this error message:
> $ mmc --make test2
> Making Mercury/int3s/test2.int3
> Making Mercury/cs/test2.c
> test2.m:038: In clause for `main(di, uo)':
> test2.m:038: in argument 1 of call to predicate
> `test2.print_non_empty_list/3':
> test2.m:038: mode error: variable `Y' has instantiatedness `ground',
> test2.m:038: expected instantiatedness was `bound((list.[]) ;
> list.'[|]'(bound(list.'[|]'(ground, ground)), ...))'.
You get this error message because Y has inst `ground' rather than
`non_empty_list' ;-). You forgot to call map_poly instead of map.
Cheers,
Mark.
--------------------------------------------------------------------------
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