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