Is this duplicated source code a bug with listing/1 when used with a list argument?

Was using listing/1 with a list as an argument. Was expecting the source code listing to be just the collection of the results of running each item in the list separately, but the result contained some duplicated source code lines, some additional source code and additional comments.

Details

This is based on Using prolog_walk_code/1 to identify meta predicate calls? when looking at the code after the meta_predicate line was added.

listing/1 done individually for three modules.
Comments added to identify unique groups of line(s), e.g. % 2

?- listing(add:Add_predicates).

:- meta_predicate add_some(-,1,-).  % 1

add_some(List, Goal, Sum) :-  % 2
    add_some_(List, Goal, Sum).

add_some_([], _, []).   % 3
add_some_([H0|T0], Goal, R) :-
    (   call(Goal, H0)
    ->  R=[H0|T]
    ;   R=T
    ),
    add_some_(T0, Goal, T).

add_all(List, Sum) :-  % 4
    sum_list(List, Sum).
true.

?- listing(filters:Predicates).

less_than_three(N) :-  % 5
    integer(N),
    N<3.
true.

?- listing(broken:Predicates).

go :-  % 6
    add_some([1, 2, 3, 4, 5], less_than_three, S),
    writeln(S).
true.

listing/1 done with a list.
Comments added to identify unique groups of line(s), e.g. % 2

?- listing([add:Add_predicates,filters:Filters_predicates,broken:Broken_predicates]).
add:add_all(List, Sum) :-  % 4
    sum_list(List, Sum).

:- meta_predicate add:add_some(-,1,-).  % 1

add:add_some(List, Goal, Sum) :-  % 2
    add_some_(List, Goal, Sum).

add:add_some_([], _, []).  % 3
add:add_some_([H0|T0], Goal, R) :-
    (   call(Goal, H0)
    ->  R=[H0|T]
    ;   R=T
    ),
    add_some_(T0, Goal, T).

lists:sum_list(Xs, Sum) :-  % missing from separate listings
    sum_list(Xs, 0, Sum).

filters:less_than_three(N) :-  % 5
    integer(N),
    N<3.

add:add_all(List, Sum) :-  % 4 duplicate
    sum_list(List, Sum).

:- meta_predicate add:add_some(-,1,-).  % 1 duplicate

add:add_some(List, Goal, Sum) :-  % 2 duplicate
    add_some_(List, Goal, Sum).

broken:go :-  % 6
    add_some([1, 2, 3, 4, 5], less_than_three, S),
    writeln(S).

filters:less_than_three(N) :-  % 5 duplicate
    integer(N),
    N<3.

%   Foreign: system:writeln/1  % missing from separate listings

true.

Notice that the following source code was duplicated.

:- meta_predicate add:add_some(-,1,-).  % 1 duplicate

add:add_some(List, Goal, Sum) :-  % 2 duplicate
    add_some_(List, Goal, Sum).

filters:less_than_three(N) :-  % 5 duplicate
    integer(N),
    N<3.

add:add_all(List, Sum) :-  % 4 duplicate
    sum_list(List, Sum).

Notice that the following are new lines when using listing/1 with an argument that is a list.

lists:sum_list(Xs, Sum) :-  % missing from separate listings
    sum_list(Xs, 0, Sum).

%   Foreign: system:writeln/1  % missing from separate listings

Obviously the purpose for using listing/1 was to see if using a meta_predicate/1 would modify the results of listing/1.

I think listing also lists imported predicate. So, if you have a module x and a module y that imports things from x you get the stuff in x twice. Not sure though. The source will tell you whether all list items are handled independently (I think they are).

1 Like