1 % (c) 2014-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 :- module(ltl_fairness,[initialise_fairness_assumptions/4,
6 fairness_check_on/2,
7 ev_fairness_id/4,
8 get_fairness_specification/3,
9 check_scc_ce/2]).
10
11 :- use_module(probsrc(module_information)).
12 :- module_info(group,ltl).
13 :- module_info(description,'This module provides LTL fairness checking on models.').
14
15 % SICSTUS libraries
16 :- use_module(library(lists)).
17 :- use_module(library(ordsets)).
18 :- use_module(library(codesio)). % for read_from_codes/2
19
20 % ProB libraries
21 :- use_module(probsrc(specfile),[animation_mode/1]).
22 :- use_module(probsrc(self_check)).
23 :- use_module(probsrc(error_manager)).
24 :- use_module(probsrc(debug)).
25 :- use_module(probsrc(state_space),[transition/4]).
26
27 :- use_module(probltlsrc(ltl_tools), [preprocess_tp/2]).
28 :- use_module(probltlsrc(ltl_propositions), [check_transition_pred/5]).
29
30 % import the low-level C-library wrapper
31 :- use_module(extension('ltlc/ltlc')).
32
33 % We allow that more than one action can be given in a single LTL formula
34 % for checking the found SCC component for fairness in regard to those actions.
35 % There are two types of fairness which we want to support for the LTL Model Checker: strong and weak fairness.
36
37 % We use the operators SF(-) and WF(-) for picking actions for the fairness assumption. If we want to check
38 % an LTL formula "f" by setting strong fairness constraints for the action a, we write then "SF(a) => f".
39 % It is also possible to express fairness constraints for more than one actions by using the boolean operators
40 % & and or.
41
42 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43
44
45 %%%%%%%%%%%%%%%%%%%% Initialising the fairness assumptions (begin) %%%%%%%%%%%%%%%%%%%%%
46 % temporary implemented prediates for testing the fairness check implementation.
47
48 % if you want to check if a trace is strongly fair w.r.t. multiple actions (e.g. Req2 and Req1)
49 % then conjugate these with the & operator and place every action between two braces
50
51 % in case fairness should be ignored, just give an empty atom ''.
52
53 fairness_check_on(all,_StrongFairnessAssumption).
54 fairness_check_on([_|_],_StrongFairnessAssumption).
55 fairness_check_on(_WeakFairnessAssumption,all).
56 fairness_check_on(_WeakFairnessAssumption,[_|_]).
57
58 initialise_fairness_assumptions(Ltl,WeakFairnessArgs,StrongFairnessArgs,LtlFormula) :-
59 (Ltl = fairnessimplication(FairnessFormula,LtlFormula) ->
60 get_strong_weak_fairness_arguments(FairnessFormula,WeakFairnessArgs,StrongFairnessArgs),
61 debug_println(9,get_strong_weak_fairness_arguments(FairnessFormula,WeakFairnessArgs,StrongFairnessArgs))
62 ;
63 WeakFairnessArgs = [], StrongFairnessArgs = [], LtlFormula = Ltl).
64
65 get_strong_weak_fairness_arguments(FairnessFormula,WeakFairnessArgs,StrongFairnessArgs) :-
66 (FairnessFormula = and(strongassumptions(SAssumptions),weakassumptions(WAssumptions))
67 -> get_fairness_assumption_actions(SAssumptions,StrongFairnessArgs),
68 get_fairness_assumption_actions(WAssumptions,WeakFairnessArgs)
69 ; FairnessFormula = and(weakassumptions(WAssumptions),strongassumptions(SAssumptions))
70 -> get_fairness_assumption_actions(SAssumptions,StrongFairnessArgs),
71 get_fairness_assumption_actions(WAssumptions,WeakFairnessArgs)
72 ; FairnessFormula = weakassumptions(F)
73 -> StrongFairnessArgs =[], get_fairness_assumption_actions(F,WeakFairnessArgs)
74 ; FairnessFormula = strongassumptions(F)
75 -> WeakFairnessArgs = [], get_fairness_assumption_actions(F,StrongFairnessArgs)
76 ;
77 add_internal_error('Unrecognised fairness assumption: ', get_strong_weak_fairness_arguments(FairnessFormula,WeakFairnessArgs,StrongFairnessArgs))
78 ).
79
80 get_fairness_assumption_actions(F,Res) :-
81 (F=all ->
82 Res = all
83 ;
84 normalise_to_dnf(F,DNF),
85 (debug:debug_mode(on) ->
86 ltl_translate: pp_ltl_formula(DNF,Text),
87 print('Disjunctive Normal Form for Fairness Forlmula: '),
88 print(Text),nl
89 ; true),
90 get_or_args(DNF,R),
91 list_to_ord_set(R,Res)
92 ).
93
94 :- assert_must_succeed((normalise_to_dnf(and(or(ap(a),ap(b)),ap(c)),R), R == or(and(ap(a),ap(c)),and(ap(b),ap(c))))).
95 :- assert_must_succeed((normalise_to_dnf(and(ap(a),ap(b)),R), R == and(ap(a),ap(b)))).
96 :- assert_must_succeed((normalise_to_dnf(or(ap(a),ap(b)),R), R == or(ap(a),ap(b)))).
97 :- assert_must_succeed((normalise_to_dnf(ap(a),R), R == ap(a))).
98 :- assert_must_succeed((normalise_to_dnf(and(or(ap(a),ap(b)),or(ap(c),ap(d))),R),
99 R == or(or(and(ap(a),ap(c)),and(ap(a),ap(d))),or(and(ap(b),ap(c)),and(ap(b),ap(d)))) )).
100
101 normalise_to_dnf(F,DNF) :-
102 ex2disj(F,DNF).
103
104 ex2disj(or(A,B),or(AR,BR)) :- !,
105 ex2disj(A,AR),
106 ex2disj(B,BR).
107 ex2disj(and(A,B),R) :- !,
108 ex2disj(A,AR),
109 ex2disj(B,BR),
110 join(AR,BR,R).
111 ex2disj(ap(X),ap(X)) :- !.
112
113 join(or(A,B),C,or(AC,BC)) :- !,
114 join(A,C,AC),
115 join(B,C,BC).
116 join(C,or(A,B),or(AC,BC)) :-
117 C\=or(_,_),!,
118 join(C,A,AC),
119 join(C,B,BC).
120 join(A,B,and(A,B)) :-
121 A\=or(_,_),B\=or(_,_),!.
122
123 % left argument of get_and_args/2 is a boolean formula in DNF without any not(-) operators
124 get_or_args(ap(strong_fair(A)),[[R]]) :- !,
125 preprocess_tp(A,R).
126 get_or_args(ap(weak_fair(A)),[[R]]) :- !,
127 preprocess_tp(A,R).
128 %get_or_args(ap(A),[[A]]) :- atom(A),!.
129 get_or_args(and(A,B),Args) :- !,
130 get_and_args(and(A,B),Res),
131 list_to_ord_set(Res,Res1),
132 Args = [Res1].
133 get_or_args(or(A,B),Res) :- !,
134 get_or_args(A,R1),
135 get_or_args(B,R2),
136 append(R1,R2,Res).
137
138 get_and_args(ap(strong_fair(A)),[R]) :- !, preprocess_tp(A,R).
139 get_and_args(ap(weak_fair(A)),[R]) :- !, preprocess_tp(A,R).
140 %get_and_args(ap(A),[A]) :- atom(A),!.
141 get_and_args(and(A,B),Res) :- !,
142 get_and_args(A,R1),
143 get_and_args(B,R2),
144 append(R1,R2,Res).
145 %%%%%%%%%%%%%%%%%%%% Initialising the fairness assumptions (end) %%%%%%%%%%%%%%%%%%%%%
146
147
148 %%%%%%%%%%%%%%%%%%%% Callback predicates (begin) %%%%%%%%%%%%%%%%%%%%%
149 % Callback from LTLc (fairness):
150
151 % check_fairness(FairnessAssumption,[anode(AtomId,Nodeid,[trans(NextAtomId,NextNodeId,[TransitionId])])])
152 % FairnessAssumtion is the Fairness Condition of the LTL formula.
153 % For instance, if we have the LTL formula "SF ([a] & [b]) => Phi" then FairnessAssumption consists of the actions [a,b]
154
155 % Tested only for B and CSP models
156 %% :- use_module(library(timeout)).
157
158 %get_transition_ids(Arg,StateIds,LTransIds) :-
159 % time_out(get_transition_ids_1(Arg,StateIds,LTransIds1),5000,R),
160 % (R = time_out -> LTransIds = []; LTransIds = LTransIds1).
161
162 :- public cltl_get_transition_ids_callback/3.
163 %%% infolog-analysis: call-back predicate from C
164 cltl_get_transition_ids_callback(Arg,StateIds,LTransIds) :-
165 (var(Arg) ->
166 add_internal_error('Action argument is a variable!',cltl_get_transition_ids_callback(Arg,StateIds,LTransIds))
167 ; var(StateIds) ->
168 % if StateIds is a variable it can cause endless loop (memory use increases rapidly)
169 add_internal_error('Unexpected argument for state ids. Expected a list of state ids, but got a variable!',cltl_get_transition_ids_callback(Arg,StateIds,LTransIds))
170 ;
171 get_transition_ids(Arg,StateIds,LTransIds),
172 length(StateIds,NStates),length(LTransIds,NTransitions),
173 debug_println(9,get_transition_ids(fainress_argument(Arg),state_ids(NStates,StateIds),transition_ids(NTransitions,LTransIds)))
174 ).
175
176 get_transition_ids(Arg,StateIds,LTransIds) :-
177 findall(TransId,(member(NodeId,StateIds),match_transition(NodeId,Arg,TransId)),LTransIds).
178
179 match_transition(NodeId,Arg,TransId) :-
180 %% print(match_transition(NodeId,Arg,TransId)),nl,
181 ? transition(NodeId,OpName,TransId,Dst),
182 match_transition_with_argument(Arg,OpName,NodeId,TransId,Dst).
183 %% check_transition(Arg,OpName,NodeId,_Dst).
184
185 % A bop spec comes in two forms:
186 % - from a fairness formula like WF(inc(x+1)): ArgPatterns/ResPatterns are lists of Index/Expr,
187 % and Expr is evaluated in the source state NodeId of the transition;
188 % - from get_tp_specification_for_b (fairness for all actions): they are the concrete argument and
189 % result values of a transition, which must be equal.
190 match_transition_with_argument(bop(Name,NumberArgs,NumberResults,ArgPatterns,ResPatterns),Transition,NodeId,_,_Dst) :- !,
191 functor(Op,Name,NumberArgs),
192 ( NumberResults == 0 ->
193 Transition = Op
194 ;
195 Transition = '-->'(Op,ReturnValues),
196 length(ReturnValues,NumberResults),
197 (is_index_pattern_list(ResPatterns)
198 -> ltl_propositions: check_bop_res_patterns(ResPatterns,NodeId,State,ReturnValues)
199 ; ReturnValues = ResPatterns)
200 ),
201 (is_index_pattern_list(ArgPatterns)
202 -> ltl_propositions: check_bop_arg_patterns(ArgPatterns,NodeId,State,Op)
203 ; Op =.. [_Name|ArgPatterns]).
204 match_transition_with_argument(Op,Transition,NodeId,TransId,DstId) :-
205 check_transition_pred(Op,Transition,NodeId,TransId,DstId).
206
207 % patterns Index/Expr (B values never have the functor //2); the empty list imposes no constraint
208 is_index_pattern_list(List) :- maplist(is_index_pattern,List).
209 is_index_pattern(Index/_) :- integer(Index).
210
211 :- assert_must_succeed(is_index_pattern_list([])).
212 :- assert_must_succeed(is_index_pattern_list([1/b(identifier(x),integer,[])])).
213 :- assert_must_fail(is_index_pattern_list([int(1)])).
214
215 :- public cltl_get_enabled_actions_callback/2.
216 % In case we have general fairness checking for all actions of the model
217 %%% infolog-analysis: call-back predicate from C
218 cltl_get_enabled_actions_callback(StateIds,ActionsSet) :-
219 % is it possible to get an empty list?
220 findall(Action,(member(StateId,StateIds),get_enabled_action(StateId,Action)),Actions),
221 list_to_ord_set(Actions,ActionsSet),
222 debug_println(9,enabled_actions(StateIds,ActionsSet)).
223
224 get_enabled_action(StateId,Action):-
225 transition(StateId,A,_AId,_DstId),
226 create_action_argument(A,Action).
227
228 create_action_argument(A,Action) :-
229 animation_mode(Mode),
230 (Mode == cspm ->
231 get_tp_specification_for_csp(A,Action)
232 ; Mode == b ->
233 get_tp_specification_for_b(A,Action)
234 ; Mode == csp_and_b ->
235 get_tp_specification_for_csp_and_b(A,Action)
236 ; Mode == xtl -> %TO DO: review wether ok
237 create_xtl_action_argument(A,Action)
238 ; % TODO: implement general fairness check support also for the other formalisms
239 animation_mode(Mode),
240 add_error(ltl_wrap_action,'Fairness not supported for ',action(A,Mode))
241 ).
242
243 create_xtl_action_argument(Op,xtl(OpName,Arity,Args)) :-
244 functor(Op,OpName,Arity), Op =.. [_|Args].
245
246 get_tp_specification_for_csp_and_b(A,Action) :-
247 % plain CSP event
248 functor(A,F,_),
249 member(F,[tau,io]),!,
250 get_tp_specification_for_csp(A,Action).
251 get_tp_specification_for_csp_and_b(A,Action) :-
252 get_tp_specification_for_b(A,Action).
253
254 :- public get_transition_id/4. % used by C Callback ??
255 get_transition_id(Arg,NodeId,TransId,DstId) :-
256 transition(NodeId,Op,TransId,DstId),
257 match_transition_with_argument(Arg,Op,NodeId,TransId,DstId).
258 % %% check_transition(Arg,Op,NodeId,DstId).
259
260 %%%%%%%%%%%%%%%%%%%% Callback predicates (end) %%%%%%%%%%%%%%%%%%%%%
261
262 % unit tests on a small hand-made state space: states ltlf_s0 (x=0) and ltlf_s1 (x=1),
263 % each with transitions inc(int(1)), inc(int(2)) and get --> [x]
264 :- use_module(probsrc(state_space),[state_space_add/2, retract_visited_expression/2]).
265 :- use_module(probsrc(bsyntaxtree),[create_texpr/4]).
266 with_fairness_test_states(Goal) :-
267 call_cleanup((add_fairness_test_state(ltlf_s0,0), add_fairness_test_state(ltlf_s1,1), call(Goal)),
268 (retract_visited_expression(ltlf_s0,_), retract_visited_expression(ltlf_s1,_),
269 retractall(state_space:transition(ltlf_s0,_,_,_)),
270 retractall(state_space:transition(ltlf_s1,_,_,_)))).
271 add_fairness_test_state(Id,X) :-
272 state_space_add(Id,[bind(x,int(X))]),
273 atom_concat(Id,'_inc1',T1), atom_concat(Id,'_inc2',T2), atom_concat(Id,'_get',T3),
274 assertz(state_space:transition(Id,inc(int(1)),T1,Id)),
275 assertz(state_space:transition(Id,inc(int(2)),T2,Id)),
276 assertz(state_space:transition(Id,'-->'(get,[int(X)]),T3,Id)).
277 % spec for WF(inc(x+1)): the argument must be x+1, evaluated in the source state
278 x_plus_one_spec(bop(inc,1,0,[1/XPlusOne],[])) :-
279 create_texpr(identifier(x),integer,[],X), create_texpr(integer(1),integer,[],One),
280 create_texpr(add(X,One),integer,[],XPlusOne).
281 :- assert_must_succeed(with_fairness_test_states((x_plus_one_spec(S),
282 get_transition_ids(S,[ltlf_s0],L0), L0 == [ltlf_s0_inc1],
283 get_transition_ids(S,[ltlf_s1],L1), L1 == [ltlf_s1_inc2]))).
284 :- assert_must_succeed(with_fairness_test_states((x_plus_one_spec(S),
285 findall(T,ev_fairness_id(S,ltlf_s1,T,_),L), L == [ltlf_s1_inc2]))).
286 % concrete specs as generated for fairness on all actions must match their own transitions
287 :- assert_must_succeed(with_fairness_test_states((
288 findall(T,ev_fairness_id(bop(inc,1,0,[int(2)],[]),ltlf_s0,T,_),L1), L1 == [ltlf_s0_inc2],
289 findall(T,ev_fairness_id(bop(get,0,1,[],[int(1)]),ltlf_s1,T,_),L2), L2 == [ltlf_s1_get],
290 findall(T,ev_fairness_id(bop(get,0,1,[],[int(1)]),ltlf_s0,T,_),L3), L3 == []))).
291 % no duplicate transition ids for specs without patterns
292 :- assert_must_succeed(with_fairness_test_states((
293 get_transition_ids(bop(inc,1,0,[],[]),[ltlf_s0],L), L == [ltlf_s0_inc1,ltlf_s0_inc2]))).
294
295 %%%%%%%%%%%%%%%%% Predicates for checking whether the given SCC is a possible counter example for a checked Ltl formula %%%%%%%%%%%%%%%%
296
297 :- use_module(probltlsrc(ltl_tools),[temporal_parser/3]).
298 :- use_module(probltlsrc(ltl),[ltl_model_check_ast/5]).
299
300 check_scc_ce(LtlFormula,SCC) :-
301 Max = 10000, Mode = init,
302 (temporal_parser(LtlFormula,ltl,Ltl) -> true ; add_error_fail(ltl,'LTL Parser failed: ',LtlFormula)),
303 ltl_model_check_ast(Ltl,Max,Mode,_,Status), % TO DO: why do we call this internal predicate ??
304 clean_scc(Status,Result),
305 atom_codes(SCC,Codes),
306 read_from_codes(Codes,SCCTerm),
307 (match_ce_result(SCCTerm,Result) ->
308 true
309 ;
310 add_error(ltl_scc_check,'No one of the SCCs could be reproduced as counter example: ',SCC/Result)
311 ).
312
313 % predicates for removing all unnecessary information like atomids and ap/tp evaluations
314 clean_scc(model(Trace,Entry),Result) :-
315 maplist(get_nodeid,Trace,CleanedTrace),
316 Result = model(CleanedTrace,Entry).
317
318 get_nodeid(atom(StateId,_Evaluation,_AtomId),StateId).
319 get_nodeid(El,StateId) :- add_internal_error('Unrecognised trace element: ', get_nodeid(El,StateId)).
320
321 match_ce_result([],_) :- !,fail.
322 match_ce_result([H|T],Result) :-
323 (H = Result -> true; match_ce_result(T,Result)).
324
325 %%%%%%%%%%%%%%% To get fairness specifications for the particular loop %%%%%%%%%%%%%%%
326 get_fairness_specification(loop(ReEntry),StateTransitions,Assumption) :- !,
327 append_length(Loop,StateTransitions,ReEntry),
328 maplist(get_specifications,Loop,ListsofSpecs),
329 append(ListsofSpecs,Specs),
330 list_to_ord_set(Specs,OrdSet),
331 Assumption = [OrdSet].
332 get_fairness_specification(_,_,[[]]).
333
334 get_specifications(strans(StateId,_),Specs) :-
335 findall(Spec,
336 (transition(StateId,Transition,_TransId,_DstId),get_tp_specification(Transition,Spec)),
337 Specs).
338
339 get_tp_specification(Transition,Spec) :- animation_mode(Mode),
340 get_tp_specification(Mode,Transition,Spec).
341
342 get_tp_specification(b,Transition,Spec) :- !,
343 get_tp_specification_for_b(Transition,Spec).
344 get_tp_specification(csp_and_b,Transition,Spec) :- !,
345 get_tp_specification_for_csp_and_b(Transition,Spec).
346 get_tp_specification(cspm,Transition,Spec) :- !,
347 get_tp_specification_for_csp(Transition,Spec).
348 get_tp_specification(xtl, Transition,Spec) :- !, % TO DO: review wether ok
349 create_xtl_action_argument(Transition,Spec).
350 get_tp_specification(Mode,Transition,_Spec) :-
351 add_error_fail(ltl_verification, 'Fairness not implemented for formalism yet ',Mode:Transition).
352
353 /* B transition specification */
354 get_tp_specification_for_b('-->'(Op,ReturnValues),Spec) :- !,
355 Op =.. [OpName|Args],
356 length(Args,NumberArgs),
357 length(ReturnValues,NumberResults),
358 Spec=bop(OpName,NumberArgs,NumberResults,Args,ReturnValues).
359 get_tp_specification_for_b(Transition,Spec) :-
360 Transition =.. [OpName|Args],
361 length(Args,NumberArgs),
362 Spec=bop(OpName,NumberArgs,0,Args,[]).
363
364 /* CSP transition specification */
365 get_tp_specification_for_csp(tick(_),Res) :- !, Res = csp(tick,0,[]).
366 get_tp_specification_for_csp(tau(_),Res) :- !, Res=csp(tau,0,[]).
367 get_tp_specification_for_csp(io(Vals,Channel,_Span),Res) :- !,
368 length(Vals,N),
369 Res = csp(Channel,N,Vals).
370 get_tp_specification_for_csp(A,Action) :-
371 add_internal_error('Unknown CSP event: ',get_tp_specification_for_csp(A,Action)),
372 Action=csp(unknown(A),0,[]).
373
374 % the fairness id is Spec itself: as in the C model checker, all transitions matching Spec
375 % (e.g. a(1) and a(2) for Spec a) count as the same action
376 % we use the same matching as the C callbacks, which also handles the concrete specs for all actions
377 ev_fairness_id(Spec,StateId,TransId,Spec) :-
378 ? transition(StateId,Op,TransId,DstId),
379 match_transition_with_argument(Spec,Op,StateId,TransId,DstId).