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 :- module(ltl_translate,[pp_ltl_formula/2, pp_formulas/2, pp_ap/2, pp_tp/2, get_ltl_sub_formulas/3]).
6
7 /* SICSTUS Libraries */
8 :- use_module(library(lists)).
9 :- use_module(library(between),[numlist/3]).
10 %% :- use_module(library(codesio)).
11 %% :- use_module(library(terms)).
12
13 /* ProB Libraries */
14 %% :- use_module(probsrc(error_manager)).
15 %% :- use_module(probsrc(debug)).
16 :- use_module(probsrc(self_check)).
17 :- use_module(probsrc(specfile),[csp_with_bz_mode/0]).
18 :- use_module(probsrc(tools),[ajoin/2]).
19 :- use_module(probsrc(translate), [translate_bexpression/2,translate_event/2]).
20 :- use_module(probsrc(state_space),[transition/4]).
21 :- use_module(library(codesio),[write_to_codes/2]).
22
23 :- use_module(probsrc(module_information),[module_info/2]).
24 :- module_info(group,ltl).
25 :- module_info(description,'This module provides utility predicates for pretty-printing LTL and CTL formulae.').
26
27 pp_ltl_formula(Formula,Text) :-
28 pp_ltl(Formula,Atoms,[]),
29 ajoin(Atoms,Text).
30
31 /* Pretty printer predicates */
32 :- public pp_op/2.
33 pp_op(Id,PP) :-
34 transition(_Src,Action,Id,_Dst),!,
35 translate_event(Action,PP).
36
37 pp_formulas([],[]).
38 pp_formulas([ApTp|Rest],[PP|PPrest]) :-
39 (pp_formula(ApTp,PP) -> true ; PP = '???'),
40 pp_formulas(Rest,PPrest).
41 pp_formula(ap(AP),ap(PP)) :-
42 pp_ap(AP,PP).
43 pp_formula(action(AP),tp(PP)) :-
44 pp_tp(AP,PP).
45
46 % pretty-print Atomic Properties (ap)
47 pp_ap(bpred(B),PP) :- !,
48 translate_bexpression(B,PP1),
49 ajoin(['{',PP1,'}'],PP).
50 pp_ap(xtl_predicate_check(P),PP) :- atom(P),!,
51 ajoin(['{',P,'}'],PP).
52 pp_ap(enabled(TP),PP) :- !,
53 pp_tp2(TP,TPP),
54 ajoin(['e(',TPP,')'],PP).
55 pp_ap(available(TP),PP) :- !, % TO DO: check_ap currently has no rule for Av(.) / available Operator !!
56 % Was introduced for David Williams.
57 % Only relevant for CSP and rewritten to [tau] U (e(tau) & not [tau]) or (e(a) & not e(tau))).
58 % This is done in the Prolog module with the C interface for LTL.
59 pp_tp2(TP,TPP),
60 ajoin(['Av(',TPP,')'],PP).
61 pp_ap(weak_fair(TP),PP) :- !,
62 pp_tp2(TP,TPP),
63 ajoin(['WF(',TPP,')'],PP).
64 pp_ap(strong_fair(TP),PP) :- !,
65 pp_tp2(TP,TPP),
66 ajoin(['SF(',TPP,')'],PP).
67 pp_ap(dlk(L),PP) :- !,
68 maplist(pp_tp2,L,S),
69 tools: ajoin_with_sep(S,',',SText),
70 ajoin(['deadlock(',SText,')'],PP).
71 pp_ap(ctrl(L),PP) :- !,
72 maplist(pp_tp2,L,S),
73 tools: ajoin_with_sep(S,',',SText),
74 ajoin(['controller(',SText,')'],PP).
75 pp_ap(det(L),PP) :- !,
76 maplist(pp_tp2,L,S),
77 tools: ajoin_with_sep(S,',',SText),
78 ajoin(['deterministic(',SText,')'],PP).
79 pp_ap(state_error,PP) :- !, PP=error. % the LTL syntax is error
80 pp_ap(Expr,PP) :-
81 write_to_codes(Expr,Codes),
82 atom_codes(PP,Codes).
83
84 % pretty-print Transition Properties (tp)
85 pp_tp(change_expr(Comparator,TE),PP) :-
86 pp_comp(Comparator,PPC), !,
87 translate_bexpression(TE,TPP),
88 ajoin([PPC,'({',TPP,'})'],PP).
89 pp_tp(before_after(TE),PP) :-
90 !,
91 translate_bexpression(TE,TPP),
92 ajoin(['BA({',TPP,'})'],PP).
93 pp_tp(operation_call(Action),PP) :- !, % written calls(Op) without square brackets
94 pp_tp2(operation_call(Action),PP).
95 pp_tp(T,PP) :-
96 pp_tp2(T,PP1),
97 ajoin(['[',PP1,']'],PP).
98 pp_tp2(operation_call(Action),OpCall) :-
99 translate_action(Action,PP),!, ajoin(['calls(',PP,')'],OpCall).
100 pp_tp2(btrans(event(Name)),Name) :- !.
101 pp_tp2(btrans(event(Name,Predicate,_)),PP) :- !,
102 translate_bexpression(Predicate,PP1),
103 ajoin([Name,' | ',PP1],PP).
104 pp_tp2(csp(tau,_Arity,[]),PP) :- !, PP = tau.
105 pp_tp2(csp(tick,_Arity,[]),PP) :- !, PP = tick.
106 pp_tp2(xtl(Op,0,[]),PP) :- !, PP = Op.
107 pp_tp2(xtl(Op,_Arity,PatternArgs),PP) :- !, Term =.. [Op|PatternArgs], pp_tp2(Term,PP).
108 pp_tp2(Action,PP) :-
109 translate_action(Action,PP),!.
110 pp_tp2(F,AF) :-
111 (translate: translate_event(F,AF) -> true; AF='???').
112
113 pp_comp(eq,unchanged).
114 pp_comp(neq,changed).
115 pp_comp(lt,increasing).
116 pp_comp(gt,decreasing).
117
118 translate_action(bop(Name,NumberArgs,_NumberResults,ArgPatterns,_ResPatterns),Transition) :-
119 ArgPatterns = [_/_|_], integer(NumberArgs), !,
120 % patterns Index/Expr only for the given arguments: print _ for the others (e.g. set(_,2))
121 numlist(1,NumberArgs,Indexes),
122 maplist(pp_bop_arg(ArgPatterns),Indexes,PPArgs),
123 Transition =.. [Name|PPArgs].
124 translate_action(bop(Name,_NumberArgs,_NumberResults,ArgPatterns,_ResPatterns),Transition) :- !,
125 maplist(get_arg,ArgPatterns,ArgPatterns1),
126 maplist(translate_bexpression,ArgPatterns1,PPArgPatterns),
127 Op =.. [Name|PPArgPatterns],
128 Transition = Op.
129 /* ( NumberResults == 0 ->
130 Transition = Op
131 ;
132 maplist(translate:translate_bexpression,ResPatterns,PPResPatterns),
133 Transition = '-->'(Op,PPResPatterns)
134 ).
135 */
136 translate_action(csp(Channel,_Arity,Args),PP) :-
137 (csp_with_bz_mode ->
138 % in this mode we need to remove 'CSP:'
139 translate: translate_event2(io(Args,Channel,_SPan),[_|S],[])
140 ;
141 translate: translate_event2(io(Args,Channel,_SPan),S,[])
142 ), ajoin(S,PP).
143
144 pp_bop_arg(ArgPatterns,Index,PP) :-
145 (member(I/Expr,ArgPatterns), I==Index -> translate_bexpression(Expr,PP) ; PP='_').
146
147 get_arg(N/Arg,Arg) :- nonvar(N),!.
148 get_arg(Arg,Arg).
149
150 /* Tests for pp_ltl_formula/2 */
151
152 :- assert_must_succeed((pp_ltl_formula(implies(globally(finally(until(true,false))),true),Text),
153 Text = '(G (F ((true U false))) => true)')).
154
155 :- assert_must_succeed((pp_ltl_formula(trigger(ap(goal),once(sink)),Text), Text == '(goal T O (sink))')).
156 % printed formulas must be parseable again with the same meaning
157 :- assert_must_succeed((pp_ltl_formula(globally(action(bop(set,2,0,[2/b(integer(2),integer,[])],[]))),Text),
158 Text == 'G ([set(_,2)])')).
159 :- assert_must_succeed((pp_ltl_formula(globally(ap(enabled(bop(set,2,0,[1/b(integer(1),integer,[])],[])))),Text),
160 Text == 'G (e(set(1,_)))')).
161 :- assert_must_succeed((pp_ltl_formula(globally(ap(state_error)),Text), Text == 'G (error)')).
162 :- assert_must_succeed((pp_ltl_formula(globally(action(operation_call(bop(inc,0,0,[],[])))),Text), Text == 'G (calls(inc))')).
163 :- assert_must_succeed((pp_ltl_formula(historically(not(release(yesterday(sink),next(deadlock)))),Text),
164 Text = 'H (not((Y (sink) R X (deadlock))))')).
165
166 :- assert_must_succeed((
167 pp_ltl_formula(implies(
168 ap(enabled(btrans(event('A')))),
169 or(weakuntil(
170 ap(bpred(equal(identifier(op('x')),integer(1)))),
171 ap(bpred(equal(identifier(op('x')),integer(2)))) ),
172 finally(action(btrans(event('A')))))),Text),
173 Text = '(e(A) => (({x = 1} W {x = 2}) or F ([A])))')).
174
175 :- assert_must_succeed((
176 pp_ltl_formula(implies(
177 current,
178 and(once(ap(bpred(equal(identifier(op('x')),integer(1))))),
179 action(btrans(event('A',equal(identifier(op('x')),integer(2)),_))))),Text),
180 Text = '(current => (O ({x = 1}) & [A | x = 2]))')).
181
182 :- assert_must_succeed((
183 pp_ltl_formula(fairnessimplication(
184 and(strongassumptions(and(ap(strong_fair(btrans(event('A')))),
185 ap(strong_fair(btrans(event('B')))))),
186 weakassumptions(or(ap(weak_fair(btrans(event('A')))),
187 ap(weak_fair(btrans(event('B'))))))),
188 true),Text),
189 Text = '(((SF(A) & SF(B))) & (WF(A) or WF(B)) => true)')).
190
191 :- assert_must_succeed((
192 pp_ltl_formula(fairnessimplication(
193 and(weakassumptions(and(ap(weak_fair(btrans(event('A')))),
194 ap(weak_fair(btrans(event('B')))))),
195 strongassumptions(or(ap(strong_fair(btrans(event('A')))),
196 ap(strong_fair(btrans(event('B'))))))),
197 true),Text),
198 Text = '(((WF(A) & WF(B))) & (SF(A) or SF(B)) => true)')).
199
200 :- assert_must_succeed((
201 pp_ltl_formula(globally(finally(ap(available(btrans(event('A')))))),Text),
202 Text = 'G (F (Av(A)))')).
203
204 /* Simple Pretty Printer for LTL/CTL Formulas */
205
206 pp_ltl(true,['true'|S],S).
207 pp_ltl(false,['false'|S],S).
208 pp_ltl(deadlock,['deadlock'|S],S).
209 pp_ltl(sink,['sink'|S],S).
210 pp_ltl(goal,['goal'|S],S).
211 pp_ltl(state_error,['error'|S],S).
212 pp_ltl(det_output,['det_output'|S],S).
213 pp_ltl(non_det,['non_det'|S],S).
214 pp_ltl(current,['current'|S],S).
215 pp_ltl(globally(F),['G ('| S],T) :- !, pp_ltl(F,S,[')'|T]).
216 pp_ltl(finally(F),['F ('| S],T) :- !, pp_ltl(F,S,[')'|T]).
217 pp_ltl(next(F),['X ('|S],T) :- !, pp_ltl(F,S,[')'|T]).
218 pp_ltl(yesterday(F),['Y ('|S],T) :- !, pp_ltl(F,S,[')'|T]).
219 pp_ltl(since(F,G),['('|S],T) :- !,
220 pp_ltl(F,S,[' S '| S1]),
221 pp_ltl(G,S1,[')'|T]).
222 pp_ltl(once(F),['O ('|S],T) :- !, pp_ltl(F,S,[')'|T]).
223 pp_ltl(historically(F),['H ('|S],T) :- !, pp_ltl(F,S,[')'|T]).
224 pp_ltl(until(F,G),['('|S],T) :- !,
225 pp_ltl(F,S,[' U '| S1]),
226 pp_ltl(G,S1,[')'|T]).
227 pp_ltl(weakuntil(F,G),['('|S],T) :- !,
228 pp_ltl(F,S,[' W '| S1]),
229 pp_ltl(G,S1,[')'|T]).
230 pp_ltl(release(F,G),['('|S],T) :- !,
231 pp_ltl(F,S,[' R '| S1]),
232 pp_ltl(G,S1,[')'|T]).
233 pp_ltl(trigger(F,G),['('|S],T) :- !,
234 pp_ltl(F,S,[' T '| S1]),
235 pp_ltl(G,S1,[')'|T]).
236 pp_ltl(implies(F,G),['('|S],T) :- !,
237 pp_ltl(F,S,[' => '| S1]),
238 pp_ltl(G,S1,[')'|T]).
239 pp_ltl(equivalent(F,G),['('|S],T) :- !,
240 pp_ltl(F,S,[' <=> '| S1]),
241 pp_ltl(G,S1,[')'|T]).
242 pp_ltl(fairnessimplication(FairAssump,Formula),['('|S],T) :- !,
243 pp_ltl_fairness(FairAssump,S,[' => '| S1]),
244 pp_ltl(Formula,S1,[')'|T]).
245 pp_ltl(or(F,G),['('|S],T) :- !,
246 pp_ltl(F,S,[' or '| S1]),
247 pp_ltl(G,S1,[')'|T]).
248 pp_ltl(and(F,G),['('|S],T) :- !,
249 pp_ltl(F,S,[' & '| S1]),
250 pp_ltl(G,S1,[')'|T]).
251 pp_ltl(not(F),['not('| S],T) :- !, pp_ltl(F,S,[')'|T]).
252 pp_ltl(ap(AP),[Pred|S],S) :- !, pp_ap(AP,Pred).
253 pp_ltl(action(A),[Action|S],S) :- !, pp_tp(A,Action).
254
255 /* Pretty Print CTL Part */
256 pp_ltl(ena(F),['EX ('|S],T) :- !, pp_ltl(F,S,[')'|T]).
257 pp_ltl(en(F), ['EX ('|S],T) :- !, pp_ltl(F,S,[')'|T]).
258 pp_ltl(an(F), ['AX ('|S],T) :- !, pp_ltl(F,S,[')'|T]).
259 pp_ltl(eg(F), ['EG ('|S],T) :- !, pp_ltl(F,S,[')'|T]).
260 pp_ltl(ef(F), ['EF ('|S],T) :- !, pp_ltl(F,S,[')'|T]).
261 pp_ltl(ag(F), ['AG ('|S],T) :- !, pp_ltl(F,S,[')'|T]).
262 pp_ltl(af(F), ['AF ('|S],T) :- !, pp_ltl(F,S,[')'|T]).
263 pp_ltl(eu(F,G), ['( E '|S],T) :- !,
264 pp_ltl(F,S,[' U '|S1]),
265 pp_ltl(G,S1,[')'|T]).
266
267 /* Pretty Print Fairness Assumptions */
268 pp_ltl_fairness(strongassumptions(F),S1,T) :- !,
269 (F=all ->
270 S1 = ['SEF'|S], T = S
271 ;
272 S1 = ['('|S], pp_ltl_fairness1(F,S,[')'|T])
273 ).
274 pp_ltl_fairness(weakassumptions(F),S1,T) :- !,
275 (F=all ->
276 S1 = ['WEF'|S], T = S
277 ;
278 S1 = ['('|S], pp_ltl_fairness1(F,S,[')'|T])
279 ).
280 pp_ltl_fairness(and(F,G),S,T) :- !,
281 pp_ltl_fairness(F,S,[' & '|S1]),
282 pp_ltl_fairness(G,S1,T).
283
284 pp_ltl_fairness1(and(F,G),['('|S],T) :- !,
285 pp_ltl_fairness1(F,S,[' & '|S1]),
286 pp_ltl_fairness1(G,S1,[')'|T]).
287 pp_ltl_fairness1(or(F,G),S,T) :- !,
288 pp_ltl_fairness1(F,S,[' or '|S1]),
289 pp_ltl_fairness1(G,S1,T).
290 pp_ltl_fairness1(ap(AP),[Pred|S],S) :- !,
291 pp_ap(AP,Pred).
292
293
294
295
296 % TODO: extend and possibly use in pp_ltl above
297 get_ltl_sub_formulas(and(A,B),'&',[A,B]).
298 get_ltl_sub_formulas(or(A,B),'or',[A,B]).
299 get_ltl_sub_formulas(implies(A,B),'=>',[A,B]).
300 get_ltl_sub_formulas(equivalent(A,B),'<=>',[A,B]).
301 get_ltl_sub_formulas(until(A,B),'U',[A,B]).
302 get_ltl_sub_formulas(weakuntil(A,B),'W',[A,B]).
303 get_ltl_sub_formulas(release(A,B),'R',[A,B]).
304 get_ltl_sub_formulas(since(A,B),'S',[A,B]).
305 get_ltl_sub_formulas(trigger(A,B),'T',[A,B]).
306 get_ltl_sub_formulas(globally(A),'G',[A]).
307 get_ltl_sub_formulas(finally(A),'F',[A]).
308 get_ltl_sub_formulas(not(A),'not',[A]).
309 get_ltl_sub_formulas(next(A),'X',[A]).
310 get_ltl_sub_formulas(yesterday(A),'Y',[A]).
311 get_ltl_sub_formulas(historically(A),'H',[A]).
312 get_ltl_sub_formulas(once(A),'O',[A]).