| 1 | | % (c) 2009-2026 Lehrstuhl fuer Softwaretechnik und Programmiersprachen, |
| 2 | | % Heinrich Heine Universitaet Duesseldorf |
| 3 | | % This software is licenced under EPL 1.0 (http://www.eclipse.org/org/documents/epl-v10.html) |
| 4 | | |
| 5 | | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 6 | | % check an LTL formula on a concrete path |
| 7 | | |
| 8 | | :- module(ltl_verification, [evaluate_ltl_formula/6, |
| 9 | | evaluate_ltl_state_property/3, |
| 10 | | is_ltl_state_property/1, |
| 11 | | evaluate_ltl_fairness/6]). |
| 12 | | |
| 13 | | /* SICSTUS libraries */ |
| 14 | | :- use_module(library(lists)). |
| 15 | | :- use_module(library(between),[numlist/3]). |
| 16 | | :- use_module(library(ordsets),[ord_intersection/3]). |
| 17 | | |
| 18 | | /* ProB standard modules */ |
| 19 | | :- use_module(probsrc(error_manager),[add_error/3, add_warning/3]). |
| 20 | | |
| 21 | | :- use_module(probsrc(module_information),[module_info/2]). |
| 22 | | :- module_info(group,ltl). |
| 23 | | :- module_info(description,'This module provides predicates for checking an LTL formula on a concrete path.'). |
| 24 | | |
| 25 | | /* |
| 26 | | * evaluate_ltl_formula(Formula,StateTransitions,Type,CallbackAP,CallbackTP,Result): |
| 27 | | * Formula: AST of the LTL formula |
| 28 | | * StateTransitions: List of the form [strans(StateId,TransId),...] which describes the |
| 29 | | * sequence of states and transitions between them. If there is no next state in |
| 30 | | * the sequence, TransId can be 'none' |
| 31 | | * Type: form of the counter-example. If it's a lasso-like form, Type should be |
| 32 | | * loop(ReEntry) where ReEntry is the offset of the first state of the loop |
| 33 | | * in the sequence, starting with 0. |
| 34 | | * If Type is not loop(_) (e.g., no_loop), the sequence is regarded as finite |
| 35 | | * CallbackAP: Callback predicate to check an atomic proposition. Callbacks are of |
| 36 | | * the form CallbackAP(AP,StateId) where AP is the specification of an atomic |
| 37 | | * proposition ap(AP) in the AST of the formula and StateId the the ID of the |
| 38 | | * state in which it should be evaluated. |
| 39 | | * If the call succeeds, the proposition is regarded as true, if not as false. |
| 40 | | * CallbackTP: Like CallbackAP, CallbackTP is a predicate to check a proposition |
| 41 | | * TP on a transition as it appears as action(TP) in the LTL formula. |
| 42 | | * The call is of the form CallbackTP(TP,StateId,TransID). |
| 43 | | * Result: true, false or unknown |
| 44 | | * A formula can only be unknown if the sequence is finite (without a loop). |
| 45 | | */ |
| 46 | | :- meta_predicate evaluate_ltl_formula(+,+,+,2,3,-). |
| 47 | | evaluate_ltl_formula(Formula,StateTransitions,Type,CallbackAP,CallbackTP,Result) :- |
| 48 | | check_sequence(StateTransitions,Type), |
| 49 | | length(StateTransitions,N), |
| 50 | | path_type_for_formula(Type,N,Formula,PathType), |
| 51 | | Path = path(StateTransitions,N,PathType,CallbackAP,CallbackTP), |
| 52 | | eval_ltl_formula(Formula,0,Path,Result), |
| 53 | | % In case of an unknown result, check if the sequence if finite |
| 54 | | check_result(Type,Result,Formula). |
| 55 | | |
| 56 | | :- use_module(probltlsrc(ltl_translate),[pp_ltl_formula/2]). |
| 57 | | % evaluate a LTL state property in a single state |
| 58 | | % TODO: check that we really have a state property |
| 59 | | evaluate_ltl_state_property(Formula,CurID,Result) :- |
| 60 | | % set_current_state_for_external_fun ? |
| 61 | | StateTransitions = [strans(CurID,none)], |
| 62 | | (is_ltl_state_property(Formula) -> true |
| 63 | | ; pp_ltl_formula(Formula,FS), |
| 64 | | add_warning(ltl_verification, |
| 65 | | 'This LTL formula is not a state property (evaluation may lead to unknown result): ',FS)), |
| 66 | | evaluate_ltl_formula(Formula,StateTransitions,no_loop,ltl:check_ap,ltl:callback_tp,Result). |
| 67 | | |
| 68 | | is_ltl_state_property(true). |
| 69 | | is_ltl_state_property(false). |
| 70 | | is_ltl_state_property(ap(_)). |
| 71 | | is_ltl_state_property(not(X)) :- is_ltl_state_property(X). |
| 72 | | is_ltl_state_property(and(X,Y)) :- is_ltl_state_property(X),is_ltl_state_property(Y). |
| 73 | | is_ltl_state_property(or(X,Y)) :- is_ltl_state_property(X),is_ltl_state_property(Y). |
| 74 | | is_ltl_state_property(implies(X,Y)) :- is_ltl_state_property(X),is_ltl_state_property(Y). |
| 75 | | is_ltl_state_property(equivalent(X,Y)) :- is_ltl_state_property(X),is_ltl_state_property(Y). |
| 76 | | |
| 77 | | |
| 78 | | % On a lasso, formulas are evaluated at positions of the infinite unrolled path s0..s(N-1), |
| 79 | | % s(ReEntry)..s(N-1), ...; we call them virtual positions. Wrapping a position back into the |
| 80 | | % sequence would be wrong for past operators (Y, S, O, H, T), which need the real history. |
| 81 | | % The value of a formula with past-operator nesting depth D is periodic (period LoopLen = N-ReEntry) |
| 82 | | % from position Cap = ReEntry+D*LoopLen on, so positions are normalised into Cap..Cap+LoopLen-1 |
| 83 | | % and an until only needs to look at positions up to max(Index,Cap)+LoopLen-1. |
| 84 | | path_type_for_formula(loop(ReEntry),N,Formula,lasso(ReEntry,LoopLen,Cap)) :- !, |
| 85 | | LoopLen is N-ReEntry, |
| 86 | | past_depth(Formula,D), |
| 87 | | Cap is ReEntry+D*LoopLen. |
| 88 | | path_type_for_formula(Type,_,_,Type). |
| 89 | | |
| 90 | | past_depth(ap(_),D) :- !, D=0. |
| 91 | | past_depth(action(_),D) :- !, D=0. |
| 92 | | past_depth(F,D) :- |
| 93 | | F =.. [Op|Args], |
| 94 | | maplist(past_depth,Args,Ds), max_member_or_zero(Ds,MaxD), |
| 95 | | (past_operator(Op) -> D is MaxD+1 ; D=MaxD). |
| 96 | | max_member_or_zero([],0). |
| 97 | | max_member_or_zero([H|T],M) :- max_member(M,[H|T]). |
| 98 | | past_operator(yesterday). |
| 99 | | past_operator(since). |
| 100 | | past_operator(once). |
| 101 | | past_operator(historically). |
| 102 | | past_operator(trigger). |
| 103 | | |
| 104 | | % the index into the state sequence for a (virtual) position |
| 105 | | concrete_index(Index,path(_,N,lasso(ReEntry,LoopLen,_),_,_),CIndex) :- Index>=N, !, |
| 106 | | CIndex is ReEntry + (Index-ReEntry) mod LoopLen. |
| 107 | | concrete_index(Index,_,Index). |
| 108 | | |
| 109 | | % map a virtual position beyond the periodic region back into it |
| 110 | | normalise_index(Index,path(_,_,lasso(_,LoopLen,Cap),_,_),NIndex) :- Index>=Cap+LoopLen, !, |
| 111 | | NIndex is Cap + (Index-Cap) mod LoopLen. |
| 112 | | normalise_index(Index,_,Index). |
| 113 | | |
| 114 | | % unit tests on lassos: states are atoms, is(S) holds in state S, t(T) holds for transition T |
| 115 | | :- use_module(probsrc(self_check)). |
| 116 | | test_ap(is(S),S). |
| 117 | | test_tp(t(T),_,T). |
| 118 | | test_eval(F,ST,Type,R) :- evaluate_ltl_formula(F,ST,Type,ltl_verification:test_ap,ltl_verification:test_tp,R). |
| 119 | | % past operators must see the real history after the loop was closed |
| 120 | | :- assert_must_succeed(test_eval(next(yesterday(true)),[strans(s0,a)],loop(0),true)). |
| 121 | | :- assert_must_succeed(test_eval(globally(finally(yesterday(true))),[strans(s0,a)],loop(0),true)). |
| 122 | | :- assert_must_succeed(test_eval(next(next(yesterday(ap(is(s1))))),[strans(s0,a),strans(s1,b)],loop(1),true)). |
| 123 | | :- assert_must_succeed(test_eval(next(next(yesterday(ap(is(s0))))),[strans(s0,a),strans(s1,b)],loop(1),false)). |
| 124 | | :- assert_must_succeed(test_eval(globally(once(ap(is(s0)))),[strans(s0,a),strans(s1,b)],loop(1),true)). |
| 125 | | :- assert_must_succeed(test_eval(globally(implies(ap(is(s1)),yesterday(ap(is(s0))))), |
| 126 | | [strans(s0,a),strans(s1,b),strans(s2,c)],loop(1),false)). |
| 127 | | :- assert_must_succeed(test_eval(globally(implies(ap(is(s2)),yesterday(ap(is(s1))))), |
| 128 | | [strans(s0,a),strans(s1,b),strans(s2,c)],loop(1),true)). |
| 129 | | :- assert_must_succeed(test_eval(finally(globally(since(not(ap(is(s0))),ap(is(s2))))), |
| 130 | | [strans(s0,a),strans(s1,b),strans(s2,c)],loop(1),true)). |
| 131 | | :- assert_must_succeed(test_eval(globally(since(not(ap(is(s0))),ap(is(s2)))), |
| 132 | | [strans(s0,a),strans(s1,b),strans(s2,c)],loop(1),false)). |
| 133 | | % future operators on lassos and finite paths are unchanged |
| 134 | | :- assert_must_succeed(test_eval(globally(finally(ap(is(s2)))),[strans(s0,a),strans(s1,b),strans(s2,c)],loop(1),true)). |
| 135 | | :- assert_must_succeed(test_eval(finally(globally(ap(is(s1)))),[strans(s0,a),strans(s1,b),strans(s2,c)],loop(1),false)). |
| 136 | | :- assert_must_succeed(test_eval(next(next(next(ap(is(s1))))),[strans(s0,a),strans(s1,b),strans(s2,c)],loop(1),true)). |
| 137 | | :- assert_must_succeed(test_eval(globally(action(t(a))),[strans(s0,a),strans(s1,b),strans(s2,c)],loop(1),false)). |
| 138 | | :- assert_must_succeed(test_eval(next(action(t(a))),[strans(s0,a)],no_loop,unknown)). |
| 139 | | :- assert_must_succeed((past_depth(globally(since(yesterday(ap(x)),ap(y))),D), D==2)). |
| 140 | | |
| 141 | | check_sequence(StateTransitions,Type) :- |
| 142 | | ( Type=loop(_),last(StateTransitions,strans(_,none)) -> |
| 143 | | add_error(ltl_verification,'last element of lasso sequence has no transition id', |
| 144 | | StateTransitions) |
| 145 | | ; true). |
| 146 | | |
| 147 | | check_result(loop(_),unknown,Formula) :- |
| 148 | | !,add_error(ltl_verification, |
| 149 | | 'evaluate_ltl_formula computed unknown result for infinite sequence', |
| 150 | | Formula). |
| 151 | | check_result(loop(_),deadlock,Formula) :- |
| 152 | | !,add_error(ltl_verification, |
| 153 | | 'evaluate_ltl_formula computed unknown result for dead locking sequence', |
| 154 | | Formula). |
| 155 | | check_result(_Type,_Result,_Formula). |
| 156 | | |
| 157 | | safe_eval_ltl_formula(A,Index,Path,VA) :- |
| 158 | | if(eval_ltl_formula(A,Index,Path,VA),true, |
| 159 | | (format('Validation of LTL formula failed at index ~w: ~w~n Path=~w~n',[Index,A,Path]), |
| 160 | | fail)). |
| 161 | | |
| 162 | | eval_ltl_formula(false,_Index,_Path,false). |
| 163 | | eval_ltl_formula(true,_Index,_Path,true). |
| 164 | | eval_ltl_formula(ap(AP),Index,Path,Value) :- |
| 165 | | (access_index_in_path(Index,Path,State,_Trans) |
| 166 | | -> path_ap(Path,Call), |
| 167 | | (call(Call,AP,State) -> Value=true ; Value=false) |
| 168 | | ; add_warning(ltl_verification,'Cannot access index in path for ap: ',Index), |
| 169 | | Value=unknown |
| 170 | | ). |
| 171 | | eval_ltl_formula(action(TP),Index,Path,Value) :- |
| 172 | | (access_index_in_path(Index,Path,State,Trans) |
| 173 | | -> ( Trans=none -> |
| 174 | | (eval_ltl_is_deadlock(Index,Path) -> Value=false ; Value=unknown) |
| 175 | | ; |
| 176 | | path_tp(Path,Call), |
| 177 | | (call(Call,TP,State,Trans) -> Value=true ; Value=false) |
| 178 | | ) |
| 179 | | ; add_warning(ltl_verification,'Cannot access index in path for action: ',Index), |
| 180 | | Value=unknown |
| 181 | | ). |
| 182 | | eval_ltl_formula(not(A),Index,Path,Value) :- |
| 183 | | eval_ltl_formula(A,Index,Path,VA), |
| 184 | | ltl_eval_not(VA,Value),!. |
| 185 | | eval_ltl_formula(and(A,B),Index,Path,Value) :- |
| 186 | | eval_ltl_formula(A,Index,Path,VA), |
| 187 | | eval_ltl_formula(B,Index,Path,VB), |
| 188 | | ltl_eval_and(VA,VB,Value),!. |
| 189 | | eval_ltl_formula(or(A,B),Index,Path,Value) :- |
| 190 | | eval_ltl_formula(A,Index,Path,VA), |
| 191 | | eval_ltl_formula(B,Index,Path,VB), |
| 192 | | ltl_eval_or(VA,VB,Value),!. |
| 193 | | eval_ltl_formula(implies(A,B),Index,Path,Value) :- |
| 194 | | eval_ltl_formula(A,Index,Path,VA), |
| 195 | | eval_ltl_formula(B,Index,Path,VB), |
| 196 | | ltl_eval_implies(VA,VB,Value),!. |
| 197 | | eval_ltl_formula(equivalent(A,B),Index,Path,Value) :- |
| 198 | | eval_ltl_formula(A,Index,Path,VA), |
| 199 | | eval_ltl_formula(B,Index,Path,VB), |
| 200 | | ltl_eval_equivalent(VA,VB,Value),!. |
| 201 | | eval_ltl_formula(finally(A),Index,Path,Value) :- |
| 202 | | eval_ltl_formula(until(true,A),Index,Path,Value). % TODO: Do not use normalisation |
| 203 | | eval_ltl_formula(globally(A),Index,Path,Value) :- |
| 204 | | eval_ltl_formula(not(finally(not(A))),Index,Path,Value). % TODO: Do not use normalisation |
| 205 | | eval_ltl_formula(weakuntil(A,B),Index,Path,Value) :- |
| 206 | | eval_ltl_formula(or(globally(A),until(A,B)),Index,Path,Value). % TODO: Do not use normalisation |
| 207 | | eval_ltl_formula(release(A,B),Index,Path,Value) :- |
| 208 | | eval_ltl_formula(not(until(not(A),not(B))),Index,Path,Value). % TODO: Do not use normalisation |
| 209 | | eval_ltl_formula(once(A),Index,Path,Value) :- |
| 210 | | eval_ltl_formula(since(true,A),Index,Path,Value). % TODO: Do not use normalisation |
| 211 | | eval_ltl_formula(historically(A),Index,Path,Value) :- |
| 212 | | eval_ltl_formula(not(once(not(A))),Index,Path,Value). % TODO: Do not use normalisation |
| 213 | | eval_ltl_formula(trigger(A,B),Index,Path,Value) :- |
| 214 | | eval_ltl_formula(not(since(not(A),not(B))),Index,Path,Value). % TODO: Do not use normalisation |
| 215 | | eval_ltl_formula(next(A),Index,Path,Value) :- |
| 216 | | last_index_in_path(Path,Last), |
| 217 | | ( path_type(Path,lasso(_,_,_)) -> % on a lasso there is always a next |
| 218 | | Next0 is Index+1, normalise_index(Next0,Path,Next), % (virtual) position |
| 219 | | eval_ltl_formula(A,Next,Path,Value) |
| 220 | | ; Index<Last -> % we're are somewhere in the path, |
| 221 | | Next is Index+1,eval_ltl_formula(A,Next,Path,Value) % just use the next position |
| 222 | | ; eval_ltl_is_deadlock(Index,Path) -> % End of the path (no loop): In a |
| 223 | | Value=false % deadlock X(...) is always false |
| 224 | | ; % End of the path: We don't know the |
| 225 | | Value=unknown). % next state |
| 226 | | eval_ltl_formula(yesterday(A),Index,Path,Value) :- |
| 227 | | ( Index > 0 -> |
| 228 | | Y is Index-1,eval_ltl_formula(A,Y,Path,Value) |
| 229 | | ; % Y(...) is always false in the |
| 230 | | Value=false). % initial state. |
| 231 | | eval_ltl_formula(until(Inv,Goal),Index,Path,Value) :- |
| 232 | | future_indices(Index,Path,Indices), |
| 233 | | eval_until(Indices,Inv,Goal,Path,Value). |
| 234 | | eval_ltl_formula(since(Inv,Goal),Index,Path,Value) :- |
| 235 | | eval_since(Index,Inv,Goal,Path,Value). |
| 236 | | |
| 237 | | eval_ltl_is_deadlock(Index,Path) :- |
| 238 | | eval_ltl_formula(ap(deadlock),Index,Path,true). |
| 239 | | |
| 240 | | access_index_in_path(Index,Path,State,Transition) :- |
| 241 | | path_statetransitions(Path,StateTransitions), |
| 242 | | concrete_index(Index,Path,CIndex), |
| 243 | | nth0(CIndex,StateTransitions,strans(State,Transition)). |
| 244 | | last_index_in_path(Path,Last) :- |
| 245 | | path_length(Path,N), |
| 246 | | Last is N-1. % using N-1 is important for test 1221, 1759; |
| 247 | | % but also means that animate_until_ltl 'F Y [end]' for groovyTests/machines/Simple.mch will need one extra step |
| 248 | | |
| 249 | | path_statetransitions(path(StateTransitions,_N,_Type,_AP,_TP),StateTransitions). |
| 250 | | path_type(path(_ST,_N,Type,_AP,_TP),Type). |
| 251 | | path_length(path(_ST,N,_Type,_AP,_TP),N). |
| 252 | | path_ap(path(_ST,_N,_Type,AP,_TP),AP). |
| 253 | | path_tp(path(_ST,_N,_Type,_AP,TP),TP). |
| 254 | | |
| 255 | | eval_until([],_,_,Path,Result) :- |
| 256 | | last_index_in_path(Path,Last), |
| 257 | | ( path_type(Path,lasso(_,_,_)) -> Result=false |
| 258 | | ; eval_ltl_is_deadlock(Last,Path) -> Result=false |
| 259 | | ; Result=unknown). |
| 260 | | eval_until([H|T],Inv,Goal,Path,Result) :- |
| 261 | | eval_ltl_formula(Goal,H,Path,GValue), |
| 262 | | ( GValue = true -> Result=true |
| 263 | | ; |
| 264 | | eval_ltl_formula(Inv,H,Path,IValue), |
| 265 | | eval_until(T,Inv,Goal,Path,NValue), |
| 266 | | ltl_eval_until(IValue,GValue,NValue,Result),!). |
| 267 | | |
| 268 | | future_indices(Index,Path,Result) :- |
| 269 | | ( path_type(Path,lasso(_,LoopLen,Cap)) -> % virtual positions, see path_type_for_formula |
| 270 | | Upper is max(Index,Cap)+LoopLen-1, |
| 271 | | numlist(Index,Upper,Result) |
| 272 | | ; |
| 273 | | last_index_in_path(Path,Last), |
| 274 | | numlist(Index,Last,Result)). |
| 275 | | |
| 276 | | eval_since(Index,Inv,Goal,Path,Result) :- |
| 277 | | eval_ltl_formula(Goal,Index,Path,GValue), |
| 278 | | ( Index = 0 -> Result=GValue |
| 279 | | ; GValue = true -> Result=true |
| 280 | | ; |
| 281 | | eval_ltl_formula(Inv,Index,Path,IValue), |
| 282 | | ( GValue=false,IValue=false -> Result=false |
| 283 | | ; |
| 284 | | Y is Index-1, |
| 285 | | eval_since(Y,Inv,Goal,Path,NValue), |
| 286 | | ltl_eval_until(IValue,GValue,NValue,Result),! % same logic as until |
| 287 | | ) |
| 288 | | ). |
| 289 | | |
| 290 | | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 291 | | % evalute a fairness condition on a given path |
| 292 | | |
| 293 | | /* |
| 294 | | * evaluate_ltl_fairness(WeakStrong,StateTransitions,Type,FairCall,FairCallExecuted,Result): |
| 295 | | * WeakStrong: weak or strong: Which fairness type should be checked |
| 296 | | * StateTransitions: [strans(StateId,TransId)] like in evaluate_ltl_formula/6 |
| 297 | | * Type: like in evaluate_ltl_formula/6, for all types but loop(_) the is result always fair |
| 298 | | * FairCall: Callback predicate to check which transitions occur that match the Spec. |
| 299 | | * The call has the form FairCall(+StateId,TransId,FairId). |
| 300 | | * StateId is an ID of a state and TransId the ID of a transition starting from that |
| 301 | | * state. If the predicate fails, it is assumed that the transition is not subject to |
| 302 | | * fairness. It it succeeds, the predicate should return a term that is used to |
| 303 | | * destinguish between actions. |
| 304 | | * E.g. it could return 'op' for both op(1) and op(2) such that all op(...) transitions |
| 305 | | * are treated as equal. |
| 306 | | * The call can be used to enumerate all transition IDs with their fairness ID that |
| 307 | | * start in the state and match the fairness criteria. |
| 308 | | * FairCallExecuted: like FairCall, called with the TransId of the transition taken in the |
| 309 | | * loop to check whether it executes FairId (usually the same predicate as FairCall). |
| 310 | | * Result: 'fair' if the sequence is fair, or unfair(Ids) if the sequence is unfair where |
| 311 | | * Ids is the list of FairId that are enabled (permanently in the case of weak fairness) |
| 312 | | * in the loop but never executed. |
| 313 | | */ |
| 314 | | :- meta_predicate evaluate_ltl_fairness(+,+,+,3,3,-). |
| 315 | | :- meta_predicate find_enabled_fairids_for_state(3,-,-). |
| 316 | | :- meta_predicate find_enabled_fairids(-,3,-,-). |
| 317 | | :- meta_predicate eval_fairness(-,-,3,3,-). |
| 318 | | :- meta_predicate is_executed(3,-,-). |
| 319 | | :- meta_predicate is_executed2(3,-,-). |
| 320 | | |
| 321 | | evaluate_ltl_fairness(WeakStrong,StateTransitions,loop(ReEntry),FairCallEnabled,FairCallExecuted,Result) :- |
| 322 | | !,append_length(Loop,StateTransitions,ReEntry), |
| 323 | | eval_fairness(WeakStrong,Loop,FairCallEnabled,FairCallExecuted,Result). |
| 324 | | evaluate_ltl_fairness(_WeakStrong,_ST,_Type,_FairCallEn,_FairCallEx,fair). % Fairness is only relevant for loops |
| 325 | | eval_fairness(Type,Loop,FairCallEnabled,FairCallExecuted,Result) :- |
| 326 | | maplist(strans_state,Loop,States), |
| 327 | | find_enabled_fairids(Type,FairCallEnabled,States,RelevantIds), |
| 328 | | exclude(is_executed(FairCallExecuted,Loop),RelevantIds,NotExecuted), |
| 329 | | (NotExecuted = [] -> Result=fair ; Result=unfair(NotExecuted)). |
| 330 | | strans_state(strans(StateId,_TransId),StateId). |
| 331 | | |
| 332 | | find_enabled_fairids(weak,FairCall,[StateId|RLoop],Ids) :- |
| 333 | | find_enabled_fairids_for_state(FairCall,StateId,Unsorted), |
| 334 | | sort(Unsorted,InitialIds), |
| 335 | | find_enabled_fairids_weak(RLoop,FairCall,InitialIds,Ids). |
| 336 | | find_enabled_fairids(strong,FairCall,Loop,Ids) :- |
| 337 | | maplist(find_enabled_fairids_for_state(FairCall),Loop,LEnabled), |
| 338 | | append(LEnabled,Unsorted), |
| 339 | | sort(Unsorted,Ids). |
| 340 | | |
| 341 | | :- assert_must_succeed((findall(Ids,find_enabled_fairids_weak([],_,[],Ids),L), L == [[]])). |
| 342 | | |
| 343 | | find_enabled_fairids_weak([],_FairCall,Ids,Ids) :- !. % otherwise a second solution for Ids=[] |
| 344 | | find_enabled_fairids_weak(_Loop,_FairCall,[],[]) :- !. |
| 345 | | find_enabled_fairids_weak([StateId|RLoop],FairCall,InitialIds,Ids) :- |
| 346 | | find_enabled_fairids_for_state(FairCall,StateId,LocalIds), |
| 347 | | % we use intersect here, only those actions should be tested |
| 348 | | % for "executed in loop" that are enabled in each state of the loop |
| 349 | | ord_intersection(InitialIds,LocalIds,Ids1), |
| 350 | | find_enabled_fairids_weak(RLoop,FairCall,Ids1,Ids). |
| 351 | | |
| 352 | | find_enabled_fairids_for_state(FairCall,StateId,Enabled) :- |
| 353 | | findall(FairId,call(FairCall,StateId,_TransId,FairId),Unsorted), |
| 354 | | sort(Unsorted,Enabled). |
| 355 | | |
| 356 | | |
| 357 | | is_executed(FairCall,Loop,FairId) :- |
| 358 | | somechk(is_executed2(FairCall,FairId),Loop). |
| 359 | | % the transition taken in the loop must match, not just any transition of the state |
| 360 | | is_executed2(FairCall,FairId,strans(StateId,TransId)) :- |
| 361 | | call(FairCall,StateId,TransId,FairId),!. |
| 362 | | |
| 363 | | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 364 | | % determine unknown values by giving rules for known values |
| 365 | | |
| 366 | | compute_unknown(CorePred,Arity,TargetPred) :- |
| 367 | ? | enumerate_true_false_unknown(Arity,InputArgs), |
| 368 | ? | compute_unknown_values(CorePred,InputArgs,Result), |
| 369 | | ToStore =.. [TargetPred | Result], |
| 370 | | % print( assertz(ToStore) ),nl, |
| 371 | | assertz( ToStore ), |
| 372 | | fail. |
| 373 | | compute_unknown(_CorePred,_Arity,_TargetPred). |
| 374 | | |
| 375 | | enumerate_true_false_unknown(0,[]). |
| 376 | | enumerate_true_false_unknown(Arity,[Value|Rest]) :- |
| 377 | ? | Arity > 0, member(Value,[true,false,unknown]), |
| 378 | ? | A2 is Arity-1, enumerate_true_false_unknown(A2,Rest). |
| 379 | | |
| 380 | | compute_unknown_values(CorePred,InputArgs,Result) :- |
| 381 | | length(InputArgs,N), N1 is N+1, |
| 382 | | length(Args,N1), |
| 383 | | append(IArgs,[R],Args), |
| 384 | | Call =.. [CorePred | Args], |
| 385 | | findall( R, (maplist(enumerate_unknown,InputArgs,IArgs), |
| 386 | | call(Call)), |
| 387 | | Outputs), |
| 388 | | sort(Outputs,SOutputs), |
| 389 | | ( SOutputs = [O] -> true |
| 390 | | ; SOutputs = [_|_] -> O=unknown), |
| 391 | | append(InputArgs,[O],Result). |
| 392 | | |
| 393 | | enumerate_unknown(true,true). |
| 394 | | enumerate_unknown(false,false). |
| 395 | ? | enumerate_unknown(unknown,V) :- member(V,[true,false]). |
| 396 | | |
| 397 | | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 398 | | % basic rules |
| 399 | | |
| 400 | | :- public ltl_eval_not_base/2, ltl_eval_and_base/3, ltl_eval_or_base/3, ltl_eval_equivalent_base/3. |
| 401 | | :- public ltl_eval_implies_base/3, ltl_eval_until_base/4. |
| 402 | | |
| 403 | | ltl_eval_not_base(true, false). |
| 404 | | ltl_eval_not_base(false,true). |
| 405 | | |
| 406 | | ltl_eval_and_base(false,_, false). |
| 407 | | ltl_eval_and_base(true, false,false). |
| 408 | | ltl_eval_and_base(true, true, true). |
| 409 | | |
| 410 | | ltl_eval_or_base(true, _, true). |
| 411 | | ltl_eval_or_base(false,true, true). |
| 412 | | ltl_eval_or_base(false,false,false). |
| 413 | | |
| 414 | | ltl_eval_implies_base(false,_, true). |
| 415 | | ltl_eval_implies_base(true, true, true). |
| 416 | | ltl_eval_implies_base(true, false,false). |
| 417 | | |
| 418 | | ltl_eval_equivalent_base(false, false,true). |
| 419 | | ltl_eval_equivalent_base(true, true,true). |
| 420 | | ltl_eval_equivalent_base(true, false,false). |
| 421 | | ltl_eval_equivalent_base(false,true,false). |
| 422 | | |
| 423 | | ltl_eval_until_base(_, true, _,true). |
| 424 | | ltl_eval_until_base(false,false,_,false). |
| 425 | | ltl_eval_until_base(true, false,N,N). |
| 426 | | |
| 427 | | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 428 | | % lifted to unknown values |
| 429 | | |
| 430 | | :- dynamic ltl_eval_not/2, ltl_eval_and/3, ltl_eval_or/3, ltl_eval_implies/3, ltl_eval_equivalent/3. |
| 431 | | :- dynamic ltl_eval_until/4. |
| 432 | | :- compute_unknown(ltl_eval_not_base,1,ltl_eval_not). |
| 433 | | :- compute_unknown(ltl_eval_and_base,2,ltl_eval_and). |
| 434 | | :- compute_unknown(ltl_eval_or_base,2,ltl_eval_or). |
| 435 | | :- compute_unknown(ltl_eval_implies_base,2,ltl_eval_implies). |
| 436 | | :- compute_unknown(ltl_eval_equivalent_base,2,ltl_eval_equivalent). |
| 437 | | :- compute_unknown(ltl_eval_until_base,3,ltl_eval_until). |