1/*  Part of Extended Tools for SWI-Prolog
    2
    3    Author:        Edison Mera Menendez
    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(check_non_mutually_exclusive, []).   36
   37:- use_module(library(apply)).   38:- use_module(library(option)).   39:- use_module(library(lists)).   40:- use_module(library(ordsets)).   41:- use_module(library(pairs)).   42:- use_module(library(checkers/checker)).   43:- use_module(library(check), []).   44:- use_module(library(clambda)).   45:- use_module(library(normalize_head)).   46:- use_module(library(location_utils)).   47:- use_module(library(option_utils)).   48:- use_module(library(referenced_by)).   49:- use_module(library(from_utils)).   50
   51:- multifile
   52    prolog:message//1,
   53    mutually_exclusive_predicate/2,
   54    mutually_exclusive_predicate_key/3.   55
   56mutually_exclusive_predicate(MH) :-
   57    strip_module(MH, M, H),
   58    mutually_exclusive_predicate(H, M).
   59
   60mutually_exclusive_predicate(check(_, _, _), checker).
   61mutually_exclusive_predicate_key(check(K, _, _), checker, K).
   62
   63checker:check(non_mutually_exclusive, Result, Options) :-
   64    option_module_files(Options, MFileD),
   65    option(predicate(Ref), Options, Ref),
   66    findall(Pairs, check_non_mutually_exclusive(MFileD, Ref, Pairs), Result).
   67
   68check_non_mutually_exclusive(MFileD, Ref, warning-(Ref-LocIdx)) :-
   69    normalize_head(Ref, MH),
   70    mutually_exclusive_predicate(MH),
   71    collect_non_mutually_exclusive(MFileD, MH, LocIdxL),
   72    member(LocIdx, LocIdxL).
   73
   74cleanup_redundant_groups([], _, []).
   75cleanup_redundant_groups([Key-Clause-ClauseNME|ClauseKeyU], ClauseKeyI, ClauseKeyR) :-
   76    ( \+ ( member(Key2-Clause2-ClauseNME2, ClauseKeyU),
   77           subset([Key-Clause|ClauseNME], [Key2-Clause2|ClauseNME2]),
   78           \+subset([Key2-Clause2|ClauseNME2], [Key-Clause|ClauseNME])
   79         ),
   80      \+ ( member(Key2-Clause2-ClauseNME2, ClauseKeyI),
   81           subset([Key-Clause|ClauseNME], [Key2-Clause2|ClauseNME2])
   82         )
   83    ->ClauseKeyR=[Key-Clause-ClauseNME|ClauseKeyR2],
   84      ClauseKeyI2=[Key-Clause-ClauseNME|ClauseKeyI]
   85    ; ClauseKeyR=ClauseKeyR2,
   86      ClauseKeyI2=ClauseKeyI
   87    ),
   88    cleanup_redundant_groups(ClauseKeyU, ClauseKeyI2, ClauseKeyR2).
   89
   90:- meta_predicate collect_non_mutually_exclusive(+, 0, -).   91
   92collect_non_mutually_exclusive(MFileD, MH, LocPL) :-
   93    strip_module(MH, M, H),
   94    get_dict(M, MFileD, FileD),
   95    findall(I-(Key-Clause),
   96            ( clause(MH, _, Clause),
   97              nth_clause(MH, I, Clause),
   98              From = clause(Clause),
   99              from_to_file(From, File),
  100              get_dict(File, FileD, _),
  101              ( mutually_exclusive_predicate_key(H, M, Key)
  102              ->true
  103              ; Key = MH
  104              )
  105            ),
  106            ClauseKeyU),
  107    ClauseKeyU \= [],
  108    list_to_ord_set(ClauseKeyU, ClauseKeyL),
  109    findall(Key-Clause-ClauseNME,
  110            [Clause, Key, ClauseNME, ClauseKeyL] +\
  111            ( select(_-(Key-Clause), ClauseKeyL, ClauseKeyS),
  112              exclude([Key] +\ (_-(SKey-_)) ^ (SKey\=Key), ClauseKeyS, ClauseKeyNME),
  113              ClauseKeyNME \= [],
  114              pairs_values(ClauseKeyNME, ClauseNME)
  115            ),
  116            ClausePR),
  117    cleanup_redundant_groups(ClausePR, [], ClausePL),
  118    maplist(\ (Key1-Clause1-ClauseNME1)^((Loc1-Idx1/Key1)/LocL)
  119           ^( nth_clause(_, Idx1, Clause1),
  120              from_location(clause(Clause1), Loc1),
  121              maplist(\ (Key2-Clause2)^(Loc2-Idx2/Key2)
  122                     ^( nth_clause(_, Idx2, Clause2),
  123                        from_location(clause(Clause2), Loc2)
  124                      ),
  125                      ClauseNME1,
  126                      LocU),
  127              sort(LocU, LocL)
  128            ), ClausePL, LocPU),
  129    sort(LocPU, LocPL).
  130
  131prolog:message(acheck(non_mutually_exclusive)) -->
  132    ['Non Mutually Exclusive Predicates',nl,
  133     '---------------------------------',nl,
  134     'The predicates below are marked as mutually_exclusive, but they have', nl,
  135     'non mutually exclusive clauses. You can resolve the ambiguity unifying', nl,
  136     'the non mutual exclusive clauses or changing the specification of such', nl,
  137     'predicates.', nl, nl].
  138prolog:message(acheck(non_mutually_exclusive, PI-LocCIs)) -->
  139    check:predicate(PI),
  140    [' has non mutually exclusive clauses:', nl],
  141    foldl(group_non_mut_ex, LocCIs).
  142
  143locindex_index(_-I/_, I).
  144
  145locindex_loccl(Loc-I/K, Loc/[' clause ~w'-[I, K]]).
  146
  147group_non_mut_ex((Loc-Idx/Key)/LocIdxL) -->
  148    {maplist(locindex_index, LocIdxL, Idxs)},
  149    {maplist(locindex_loccl, LocIdxL, LCIs)},
  150    Loc, ['Clause ~w (key ~w) match with clauses ~w at'-[Idx, Key, Idxs], nl],
  151    referenced_by(LCIs)