[mercury-users] inst of list in non-empty branch

Ralph Becket rafe at csse.unimelb.edu.au
Fri Mar 30 08:39:34 AEST 2007


doug.auclair at logicaltypes.com, Thursday, 29 March 2007:
> Dear Ralph (and all), you and I wrote:
> 
> >> print_kids(Element, Kids@[_|_], !IO) :-
> >>          format("The child element%s of ", [s(singular_plural(Kids))], !IO),
> >>          print(Element, !IO),
> >>          is_are(Kids, !IO),
> >>          print(Kids, !IO),
> >>          nl(!IO).
> 
> >The mode for format/4 is pred(in, in, di, uo), so while the compiler
> >knows that Kids has an inst matching non_empty_list before the call to
> >format, it only knows it has inst ground afterwards.
> 
> Okay.  I think, however, that the mode for Kids is being set to just 'in'
> anyway, because when I reduce the clause to:
> 
> print_kids(_, Kids@[_|_], !_IO) :-
>         TheS = singular_plural(Kids).
> 
> -- recall that singular_plural/1's mode is in(non_empty_list) -- I still
> get the same error:
> 
> test_xml_kids.m:071: In clause for `print_kids(in, in, di, uo)':
> test_xml_kids.m:071:   in argument 1 of call to function
> test_xml_kids.m:071:   `test_xml_kids.singular_plural'/1:
> test_xml_kids.m:071:   mode error: variable `Kids' has instantiatedness
> test_xml_kids.m:071:   `ground',
> test_xml_kids.m:071:   expected instantiatedness was `bound(list.'[|]'(ground,
> test_xml_kids.m:071:   ground))'.

Hmm.  I tried this:

:- module foo.
:- interface.
:- import_module io.

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

:- implementation.
:- import_module list, string.

main(!IO) :-
    print_first_or_nothing(["one", "two", "three"], !IO).


:- pred print_first_or_nothing(list(string)::in, io::di, io::uo) is det.

% This leads to a mode error:
% print_first_or_nothing([], !IO).
% 
% print_first_or_nothing(Xs @ [_ | _], !IO) :-
%     print_first(Xs, !IO).

% This leads to working code:
print_first_or_nothing(Xs, !IO) :-
    (
        Xs = []
    ;
        Xs = [_ | _],
        print_first(Xs, !IO)
    ).


:- pred print_first(list(string)::in(bound('[|]'(ground, ground))),
        io::di, io::uo) is det.

print_first([X | _], !IO) :-
    io.write_string(X, !IO),
    io.nl(!IO).


I'm not sure what's going on here, but we'll have to look into it.
Unfortunately we're rather short on time for the next month or so.

Cheers,
-- Ralph
--------------------------------------------------------------------------
mercury-users mailing list
Post messages to:       mercury-users at csse.unimelb.edu.au
Administrative Queries: owner-mercury-users at csse.unimelb.edu.au
Subscriptions:          mercury-users-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the users mailing list