1/*  Part of Extended Tools for SWI-Prolog
    2
    3    Author:        Edison Mera
    4    E-mail:        efmera@gmail.com
    5    WWW:           https://github.com/edisonm/xtools
    6    Copyright (C): 2015, Process Design Center, Breda, The Netherlands.
    7    All rights reserved.
    8
    9    Redistribution and use in source and binary forms, with or without
   10    modification, are permitted provided that the following conditions
   11    are met:
   12
   13    1. Redistributions of source code must retain the above copyright
   14       notice, this list of conditions and the following disclaimer.
   15
   16    2. Redistributions in binary form must reproduce the above copyright
   17       notice, this list of conditions and the following disclaimer in
   18       the documentation and/or other materials provided with the
   19       distribution.
   20
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32    POSSIBILITY OF SUCH DAMAGE.
   33*/
   34
   35:- module(mark_preds,
   36          [ cleanup_marked/0,
   37            gen_lit_marks/2,
   38            marked/1,
   39            match_head_clause/2,
   40            mark_compile_time_called/0,
   41            put_mark/1
   42          ]).   43
   44:- use_module(library(qualify_meta_goal)).   45:- use_module(library(calls_to)).   46:- use_module(library(compilation_module)).   47:- use_module(library(solution_sequences)).   48:- init_expansors.   49
   50:- dynamic
   51    marked_assertion/2,
   52    marked_predid/2,
   53    marked_clause/1,
   54    marked_initialization/0,
   55    marked_declaration/0,
   56    marked_exported/2.   57
   58cleanup_marked :-
   59    retractall(marked_clause(_)),
   60    retractall(marked_assertion(_, _)),
   61    retractall(marked_predid(_, _)),
   62    retractall(marked_initialization),
   63    retractall(marked_declaration),
   64    retractall(marked_exported(_, _)).
   65
   66marked('<assertion>'(M:H)) :- marked_assertion(H, M).
   67marked('<public>'(M:H))    :- marked_predid(H, M).
   68marked(M:H)                :- marked_predid(H, M).
   69marked(clause(Ref))        :- marked_clause(Ref).
   70marked('<initialization>') :- marked_initialization.
   71marked('<declaration>')    :- marked_declaration.
   72marked('<exported>'(M:H))  :- marked_exported(H, M).
   73
   74record_marked('<assertion>'(M:H)) :- assertz(marked_assertion(H, M)).
   75record_marked('<public>'(M:H))    :- assertz(marked_predid(H, M)).
   76record_marked(M:H)                :- assertz(marked_predid(H, M)).
   77record_marked(clause(Ref))        :- assertz(marked_clause(Ref)).
   78record_marked('<initialization>') :- assertz(marked_initialization).
   79record_marked('<declaration>'   ) :- assertz(marked_declaration).
   80record_marked('<exported>'(M:H) ) :- assertz(marked_exported(H, M)).
   81
   82resolve_meta_goal(H, M, G) :-
   83    ( ( predicate_property(M:H, meta_predicate(Meta))
   84                                % don`t use inferred_meta_predicate(M:H, Meta)
   85                                % since actually it is not being used by the
   86                                % compiler and would lead to incorrect results
   87      )
   88    ->qualify_meta_goal(M:H, Meta, G)
   89    ; G = H
   90    ).
   91
   92is_marked(CRef) :-
   93    copy_term(CRef, Term),
   94    marked(Term),
   95    subsumes_term(Term, CRef).
   96
   97check_and_mark(CRef) :-
   98    \+ is_marked(CRef),
   99    record_marked(CRef).
  100
  101put_mark(CRef) :-
  102    ( with_mutex(check_and_mark, check_and_mark(CRef))
  103    ->forall(( calls_to(CRef, w, CM, Callee),
  104               strip_module(CM:Callee, M, H),
  105               predicate_property(M:H, implementation_module(IM))
  106             ),
  107             mark_rec(H, IM))
  108    ; true
  109    ).
  110
  111mark_rec(H, M) :-
  112    resolve_meta_goal(H, M, G),
  113    forall(gen_lit_marks(M:G, CRef), % Widening
  114           put_mark(CRef)).
  115
  116:- meta_predicate gen_lit_marks(0, ?).
 gen_lit_marks(:Goal, Ref)
Generalization step, we lose precision but avoid loops --EMM

The order of this clauses matters, because we record as marked from the most specific to the most generic one !!!

The logic is: a call to a predicate will potentially use:

(1) all the assertions (2) the clauses that match, and (3) the dynamic calls that match

  131gen_lit_marks(M:G, '<assertion>'(M:P)) :-
  132    functor(G, F, A),
  133    functor(P, F, A).          % Build a fresh head without undesirable bindings
  134gen_lit_marks(MG, clause(Clause)) :-
  135    match_head_clause(MG, Clause),
  136    clause_property(Clause, file(_)).    % Static clauses only
  137gen_lit_marks(G, P) :- copy_term(G, P). % copy term to avoid undesirable bindings
  138
  139:- meta_predicate match_head_clause(0, -).  140match_head_clause(MH, Clause) :-
  141    catch(clause(MH, _, Clause), _, fail).
  142
  143mark_compile_time_called :-
  144    forall(distinct(M:H,
  145                    ( compile_time_called(H, M, C),
  146                      M \= C
  147                    )),
  148           put_mark('<exported>'(M:H)))