1:- module(
    2       da_clause,
    3       [
    4           da_clause_source_span/2,
    5           da_clause_source_span/3,
    6           da_clause_variable_names/2,
    7           da_clause_source_term/7
    8       ]
    9   ).   10
   11:- use_module(source).   12:- use_module(library(prolog_source)).

DAP library module for reasoning about Prolog clauses

This module contains predicates for retrieving information about Prolog clauses for debugging purposes, such as relating clauses to their source code.

*/

 da_clause_variable_names(+ClauseRef, -VarNames) is det
   23:- det(da_clause_variable_names/2).   24da_clause_variable_names(null, varnames) :- !.
   25da_clause_variable_names(ClauseRef, VarNames) :-
   26    da_clause_decompiled(ClauseRef, Module, DecompiledClause, VariablesOffset),
   27    da_clause_source_term(ClauseRef, Module, DecompiledClause, VariablesOffset, _, _, _, VarNames).
 da_clause_source_span(+ClauseRef, -SourceSpan) is det
   31:- det(da_clause_source_span/2).   32da_clause_source_span(ClauseRef, SourceSpan) :-
   33    da_clause_source_span(ClauseRef, SourceSpan, []).
   34
   35
   36:- predicate_options(da_clause_source_span/3, 3, [pc(number), pass_to(da_clause_subterm_source_span/4, 4)]).
 da_clause_source_span(+ClauseRef, -SourceSpan, +Options) is det
   40:- det(da_clause_source_span/3).   41da_clause_source_span(null, span(reference(0), 0, 0, 0, 0), _Options) :- !.
   42da_clause_source_span(ClauseRef, SourceSpan, Options) :-
   43    option(pc(PC), Options),
   44    !,
   45    '$clause_term_position'(ClauseRef, PC, SubTermLayoutPath),
   46    da_clause_subterm_source_span(ClauseRef, SubTermLayoutPath, SourceSpan, Options).
   47da_clause_source_span(ClauseRef, SourceSpan, Options) :-
   48    da_clause_subterm_source_span(ClauseRef, [], SourceSpan, Options).
   49
   50
   51:- predicate_options(da_clause_subterm_source_span/4, 4, [pass_to(da_source:da_source_subterm_span/5, 5)]).
 da_clause_subterm_source_span(+ClauseRef, +SubTermLayoutPath, -SourceSpan, +Options) is det
   55:- det(da_clause_subterm_source_span/4).   56da_clause_subterm_source_span(ClauseRef, SubTermLayoutPath, SourceSpan, Options) :-
   57    da_clause_source_layout(ClauseRef, SourceFile, SourceLayout),
   58    da_source_subterm_span(SourceFile, SourceLayout, SubTermLayoutPath, SourceSpan, Options).
 da_clause_source_layout(+ClauseRef, -SourceFile, -SourceLayout) is det
   63:- det(da_clause_source_layout/3).   64da_clause_source_layout(ClauseRef, SourceFile, SourceLayout) :-
   65    da_clause_decompiled(ClauseRef, Module, DecompiledClause, VariablesOffset),
   66    da_clause_source_term(ClauseRef, Module, DecompiledClause, VariablesOffset, _SourceClause, SourceFile, SourceLayout).
 da_clause_source_term(+ClauseRef, +Module, +DecompiledClause, +VariablesOffset, -SourceClause, -SourceFile, -SourceLayout) is det
   70:- det(da_clause_source_term/7).   71da_clause_source_term(ClauseRef, Module, DecompiledClause, VariablesOffset, SourceClause, SourceFile, SourceLayout) :-
   72    da_clause_source_term(ClauseRef, Module, DecompiledClause, VariablesOffset, SourceClause, SourceFile, SourceLayout, _VarNames).
 da_clause_source_term(+ClauseRef, +Module, +DecompiledClause, +VariablesOffset, -SourceClause, -SourceFile, -SourceLayout, VarNames) is det
   77:- det(da_clause_source_term/8).   78da_clause_source_term(ClauseRef, Module, DecompiledClause, VariablesOffset, SourceClause, path(SourceFile), SourceLayout, VarNames) :-
   79    clause_property(ClauseRef, file(SourceFile)),
   80    SourceFile \== user,
   81    !,
   82    clause_property(ClauseRef, line_count(Line)),
   83    setup_call_cleanup(prolog_clause:try_open_source(SourceFile, Stream),
   84                       read_source_term_at_location(Stream,
   85                                                    SourceClause,
   86                                                    [ line(Line),
   87                                                      module(Module),
   88                                                      subterm_positions(SourceLayout0),
   89                                                      variable_names(VarNames0)
   90                                                    ]),
   91                       close(Stream)),
   92    prolog_clause:unify_clause(SourceClause, DecompiledClause, Module, SourceLayout0, SourceLayout),
   93    prolog_clause:make_varnames(SourceClause, DecompiledClause, VariablesOffset, VarNames0, VarNames), !.
   94da_clause_source_term(ClauseRef, Module, DecompiledClause, VariablesOffset, SourceClause, reference(SourceReference), SourceLayout, VarNames) :-
   95    (   da_source_clause_cached_reference(ClauseRef, SourceReference)
   96    ->  true
   97    ;   da_source_clause_reference(ClauseRef, SourceReference)
   98    ),
   99    setup_call_cleanup(new_memory_file(MemFile),
  100                       ( setup_call_cleanup(open_memory_file(MemFile, write, MemOut),
  101                                            portray_clause(MemOut, DecompiledClause, [module(Module)]),
  102                                            close(MemOut)),
  103                         setup_call_cleanup(open_memory_file(MemFile, read, MemIn),
  104                                            read_source_term_at_location(MemIn, SourceClause,
  105                                                                         [ module(Module),
  106                                                                           subterm_positions(SourceLayout0),
  107                                                                           variable_names(VarNames0)
  108                                                                         ]),
  109                                           close(MemIn))),
  110                       free_memory_file(MemFile)),
  111    prolog_clause:unify_clause(SourceClause, DecompiledClause, Module, SourceLayout0, SourceLayout),
  112    prolog_clause:make_varnames(SourceClause, DecompiledClause, VariablesOffset, VarNames0, VarNames), !