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(assrt_meta, []).   36
   37:- use_module(library(assertions)).   38:- use_module(library(assertions_op)).   39:- use_module(library(location_utils)).   40:- use_module(library(predicate_from)).   41:- use_module(library(globprops)).   42:- init_expansors.   43
   44:- create_prolog_flag(assrt_meta_pred, none, [type(atom)]).   45
   46:- meta_predicate
   47    with_amp(0, +, +).   48
   49:- table
   50    am_head_prop_idx/5.   51
   52meta_has_mode_info(Meta) :-
   53    arg(_, Meta, Spec),
   54    memberchk(Spec, [+,-]),
   55    !.
   56
   57with_amp(Goal, OldFlag, NewFlag) :-
   58    setup_call_cleanup(
   59        set_prolog_flag(assrt_meta_pred, NewFlag),
   60        Goal,
   61        set_prolog_flag(assrt_meta_pred, OldFlag)).
   62
   63am_head_prop_idx(Head, M, Meta, From) :-
   64    current_prolog_flag(assrt_meta_pred, Flag),
   65    Flag \= none,
   66    copy_term_nat(Head, Term),
   67    with_amp(am_head_prop_idx(Flag, Term, M, Meta, From), Flag, none),
   68    Head = Term.
   69
   70black_list_module(rtchecks_rt).
   71black_list_module(ctrtchecks).
   72black_list_module(qualify_meta_goal).
   73black_list_module(assertions).
   74
   75am_head_prop_idx(Flag, Head, M, Meta, From) :-
   76    var(Meta),
   77    !,
   78    Pred = M:Head,
   79    ( var(Head)
   80    ->module_property(M, class(user)),
   81      current_predicate(M:F/A),
   82      functor(Head, F, A)
   83    ; functor(Head, F, A),
   84      current_predicate(M:F/A), % Narrow answer set for M
   85      module_property(M, class(user))
   86    ),
   87    \+ black_list_module(M),
   88    predicate_property(Pred, implementation_module(M)),
   89    % if something can not be debugged, can not be rtchecked either:
   90    \+ predicate_property(Pred, nodebug),
   91    '$predicate_property'(meta_predicate(Meta), Pred),
   92    % predicate_property(Pred, meta_predicate(Meta)),
   93    meta_has_mode_info(Meta),
   94    ( Flag = all
   95    ->
   96      \+ ( prop_asr(Head, M, check, _, _, _, Asr),
   97           prop_asr(glob, no_meta_modes(_), _, Asr)
   98         )
   99    ; Flag = specific
  100    ->once(( prop_asr(Head, M, check, _, _, _, Asr),
  101             prop_asr(glob, meta_modes(_), _, Asr)
  102           ))
  103    ),
  104    findall(From1,
  105            once(( property_from(M:Pred, meta_predicate, From1)
  106                 ; predicate_from(Pred, From1)
  107                 )), [From]).
  108am_head_prop_idx(_, _, _, _, _).
  109
  110assertions:asr_head_prop(am_asr(M, H, S, F), M, H, check, (comp), [], M, F) :-
  111    am_head_prop_idx(H, M, S, F).
  112assertions:asr_glob(am_asr(M, H, S, F), assrt_meta,
  113                    check_call(rt, [am_asr2(M, H, S, F)], _), F) :-
  114    am_head_prop_idx(H, M, S, F).
  115
  116assertions:asr_aprop(am_asr2(M, H, _, From), head,   M:H, From).
  117assertions:asr_aprop(am_asr2(_, _, _, From), stat, check, From).
  118assertions:asr_aprop(am_asr2(_, _, _, From), type,  pred, From).
  119assertions:asr_aprop(am_asr2(M, H, Meta, From), Type,  Prop, From) :-
  120    (nonvar(Type) -> memberchk(Type, [call, succ]) ; true),
  121    assertions:current_decomposed_assertion(pred