% mmc -s hlc.gc --optimize-constructor-last-call --no-inlining bag_to_list

:- module bag_to_list.
:- interface.
:- import_module io.

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

:- implementation.

:- import_module assoc_list, bag, int, list, pair.

main(!IO) :-
    Bag = bag.from_list([1,1,1,2,3,3,4]),
    bag.to_assoc_list(Bag, AL),
    to_list_2(AL, L),
    io.write(L, !IO),
    io.nl(!IO).

:- pred to_list_2(assoc_list(T, int)::in, list(T)::out) is det.

to_list_2([], []).
to_list_2([X - Int | Xs ], Out) :-
    ( Int =< 0 ->
        to_list_2(Xs, Out)
    ;
        NewInt = Int - 1,
        to_list_2([X - NewInt | Xs], Out0),
        Out = [X | Out0]
    ).
