Library(prolog_coverage) - would like to see into call/1 when used, e.g. catch/3

$ swipl --version
SWI-Prolog version 10.0.0 for x64-win64

Currently using library(prolog_coverage).

For predicates like catch/3, which reference call/1 in their documentation (with the reality not being so apparent), the coverage report doesn’t capture the details of how call/1 is used. I realize this may be difficult—or even impossible—to implement, so I’m wondering if there might be a way to simulate such behavior to test both success and failure cases during testing.

Actual report

  341 ++15     catch(
  342              (   find_prolog_files('fixtures', Files),
  343                  is_list(Files)
  344              ),
  345              _,
  346              true
  347          ).

Something like the following is desired.

  341 ++15     catch(
  342 ++15         (   find_prolog_files('fixtures', Files),
  343 ++12             is_list(Files)
  344              ),
  345              _,
  346 ++3          true
  347          ).

The success of the catch/3 basically confirms the success of what it calls :slight_smile: But no, at the moment there is no proper position support for meta-calling. Would be nice to have as it would also improve the debugger. Not easy to implement though :frowning:

For good debugging support I advice to avoid complex meta-calls, i.e., use a helper predicate. This is also faster. The system can be asked to do this transformation automatically, but this too looses source location information. Maybe there is a solution there by faking the source location information of the generated clause to reflect the positions from the meta-call …

1 Like

This is a step in the right direction, at least for my need.

  123                    %% persist_assert_pred_def(+Name, +Arity, +Module, +File, +Line)
  124                    %  Safely persists pred_def with duplicate detection and error handling
  125                    % persist_assert_pred_def(Name, Arity, Module, File, Line) :-
  126                    %     catch(
  127                    %         (   % Check if already exists (duplicate detection)
  128                    %             (   pred_def(Name, Arity, Module, File, Line)
  129                    %             ->  % Already exists - log warning and succeed
  130                    %                 log_warning(debug_kb_consistency,
  131                    %                     duplicate_found(pred_def(Name, Arity, Module, File, Line)))
  132                    %             ;   % Not a duplicate - proceed with assertion
  133                    %                 % Log the intent to assert
  134                    %                 log_debug(debug_kb_assert,
  135                    %                     pred_def(Name, Arity, Module, File, Line)),
  136                    %                 % Use auto-generated persist predicate
  137                    %                 assert_pred_def(Name, Arity, Module, File, Line)
  138                    %             )
  139                    %         ),
  140                    %         Error,
  141                    %         % Log failure and re-throw
  142                    %         (   log_error(debug_kb_assert,
  143                    %                 operation_failed(pred_def(Name, Arity, Module, File, Line), Error)),
  144                    %             throw(Error)
  145                    %         )
  146                    %     ).
  147                    
  148 ++55,870           persist_assert_pred_def(Name, Arity, Module, File, Line) :-
  149 ++55,870               catch(
  150                            goal_a_success(Name, Arity, Module, File, Line),
  151                            Error,        
  152                            goal_a_failure(Name, Arity, Module, File, Line, Error)
  153                        ).
  154                    
  155 ++55,870           goal_a_success(Name, Arity, Module, File, Line) :-
  156                        (   % Check if already exists (duplicate detection)
  157 +20-55,850                     (   pred_def(Name, Arity, Module, File, Line)
  158                                ->  % Already exists - log warning and succeed
  159 ++20                               log_warning(debug_kb_consistency,
  160                                        duplicate_found(pred_def(Name, Arity, Module, File, Line)))
  161                                ;   % Not a duplicate - proceed with assertion
  162                                    % Log the intent to assert
  163 ++55,850                           log_debug(debug_kb_assert,
  164                                        pred_def(Name, Arity, Module, File, Line)),
  165                                    % Use auto-generated persist predicate
  166 ++55,850                           assert_pred_def(Name, Arity, Module, File, Line)
  167                                )
  168                            ).
  169                    
  170 ###                goal_a_failure(Name, Arity, Module, File, Line, Error) :-
  171                        % Log failure and re-throw
  172                        (   log_error(debug_kb_assert,
  173                                operation_failed(pred_def(Name, Arity, Module, File, Line), Error)),
  174                            throw(Error)
  175                        ).