1/*  Part of Extended Libraries for SWI-Prolog
    2
    3    Author:        Edison Mera
    4    E-mail:        efmera@gmail.com
    5    WWW:           https://github.com/edisonm/xlibrary
    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(abstract_slicer,
   36          [abstract_slice/3,
   37           abstract_slice/4,
   38           apply_mode/5,
   39           slicer_abstraction/7]).   40
   41:- use_module(library(apply)).   42:- use_module(library(option)).   43:- use_module(library(pure)).   44:- use_module(library(abstract_interpreter)).   45:- use_module(library(terms_share)).   46
   47:- meta_predicate
   48    abstract_slice(0,+,?),
   49    abstract_slice(0,+,?,-),
   50    slicer_abstraction(+,+,+,0,?, ?,?).

Abstract slicer

Implements the next abstract domain: find possible matches of output arguments of the given predicate.

*/

   59abstract_slice(M:Head, Mode, OptL) :-
   60    abstract_slice(M:Head, Mode, OptL, _).
 abstract_slice(:Head, +Mode:list, +Options, +State) is multi
Returns on backtracking, the possible instances in the Head of those argument positions marked with a (-) in Mode. State is unified with the result of the predicate abstract_interpreter/4, which is called inside.

Example:

consider the next predicate:

popt('option'(A), [])    :- member(A, [1,2,3]).
popt('option 1', Answer) :- append(_,_,Answer).
popt('option 2', Answer) :- member(Answer, [1,2,3]).
popt('option 3', []) :- member(_Answer, [1,2,3]).

If we just execute the predicate with Answer uninstatiated, we will get infinite solutions, but:

?- abstract_slice(popt(A,X),[+,?],[]).
A = option(1) ;
A = option(2) ;
A = option(3) ;
A = 'option 1' ;
A = 'option 2' ;
A = 'option 3'.

Will 'abstract' the execution of popt/2 to get all the matches of A, slicing out X

   95abstract_slice(M:Head, Mode, OptL, State) :-
   96    apply_mode(Head, Mask, Mode, Spec, RevS),
   97    term_variables(RevS, VarsR),
   98    option(eval_scope(Scope), OptL, body),
   99    abstract_interpreter(M:Mask, slicer_abstraction(Spec, VarsR, Scope), OptL, State),
  100    % In Mask the output arguments are variable, so the binding is performed
  101    % after the abstract interpretation. This is a bit inefficient, but correct:
  102    Head = Mask.
  103
  104apply_mode(Call, Mask, Mode, Spec, RevS) :-
  105    functor(Call, F, A),
  106    functor(Mask, F, A),
  107    functor(Spec, F, A),
  108    functor(RevS, F, A),
  109    apply_mode_arg(1, Call, Mask, Mode, Spec, RevS).
  110
  111apply_mode_arg(N1, Call, Mask, Mode, Spec, RevS) :-
  112    arg(N1, Call, Arg), !,
  113    arg(N1, Mask, Var),
  114    arg(N1, Mode, MSp),
  115    arg(N1, Spec, ASp),
  116    arg(N1, RevS, ARs),
  117    ( MSp = -
  118    ->ASp = Var,
  119      ARs = -
  120    ; ASp = +,
  121      ARs = Arg,
  122      Arg = Var
  123    ),
  124    succ(N1, N),
  125    apply_mode_arg(N, Call, Mask, Mode, Spec, RevS).
  126apply_mode_arg(_, _, _, _, _, _).
  127
  128chain_of_dependencies(Spec, VarsR, Goal, ContL) :-
  129    \+ ground(Goal),
  130    ( terms_share(Spec, VarsR, Goal)
  131    ->true
  132    ; select(Cont, ContL, ContL2),
  133      terms_share(Cont, VarsR, Goal),
  134      chain_of_dependencies(Spec, VarsR, Cont, ContL2)
  135    ),
  136    !.
  137
  138slicer_abstraction(Spec, VarsR, Scope, MGoal, Body) -->
  139    {predicate_property(MGoal, interpreted)},
  140    !,
  141    {strip_module(MGoal, M, Goal)},
  142    get_state(state(Loc1, EvalL, OnErr, CallL, Data, Cont, Result1)),
  143    { \+ ground(Spec),
  144      chain_of_dependencies(Spec, VarsR, Goal, Cont)
  145    ->match_head_body(M:Goal, Body1, Loc),
  146      ( Scope = body
  147      ->( terms_share(Spec, VarsR, Goal)
  148        ->Body = Body1
  149        ; Body1 = CM:Body2,
  150          Body = CM:once(Body2)
  151        )
  152      ; terms_share(Spec, VarsR, Goal)
  153      ->Body = Body1
  154      ; Body = M:true
  155      )
  156    ; % check if the body trivially fails:
  157      ( Scope = body
  158      ->once(( match_head_body(M:Goal, Body1, Loc),
  159               % if no side effects, increase precision executing the body:
  160               ( is_pure_body(Body1)
  161               ->call(Body1)
  162               ; true
  163               )
  164             ))
  165      ; Loc = Loc1
  166      ),
  167      Body = M:true
  168    },
  169    { Scope = head
  170    ->Result = bottom % Kludge to avoid cut remove solutions
  171    ; Result = Result1
  172    },
  173    put_state(state(Loc, EvalL, OnErr, CallL, Data, Cont, Result)).
  174slicer_abstraction(_, _, _, MGoal, M:true) -->
  175    get_state(state(Loc, _, OnError, CallL, _, _, _)),
  176    { call(OnError, error(existence_error(evaluation_rule, MGoal), Loc)),
  177      call(OnError, call_stack(CallL)),
  178      strip_module(MGoal, M, _)
  179    },
  180    bottom.
  181
  182prolog:message(call_stack(CallL)) --> foldl(call_at, CallL).
  183
  184call_at(Call-Loc) -->
  185    ["    "], '$messages':swi_location(Loc), ["~q"-[Call], nl]