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(translate,
6 [print_bexpr_or_subst/1, l_print_bexpr_or_subst/1,
7 print_bexpr/1, debug_print_bexpr/1,
8 nested_print_bexpr/1, nested_print_bexpr_up_to/2,
9 nested_print_bexpr_as_classicalb/1,
10 print_bexpr_stream/2,
11 print_components/1,
12 print_bexpr_with_limit/2, print_bexpr_with_limit_and_typing/3,
13 print_unwrapped_bexpr_with_limit/1,print_bvalue/1, l_print_bvalue/1, print_bvalue_stream/2,
14 translate_params_for_dot/2, translate_params_for_dot_nl/2,
15 print_machine/1,
16 translate_machine/3,
17 set_unicode_mode/0, unset_unicode_mode/0, unicode_mode/0,
18 unicode_translation/2, % unicode translation of a symbol/keyword
19 set_latex_mode/0, unset_latex_mode/0, latex_mode/0,
20 set_atelierb_mode/1, unset_atelierb_mode/0,
21 set_force_eventb_mode/0, unset_force_eventb_mode/0,
22 get_translation_mode/1, set_translation_mode/1, unset_translation_mode/1,
23 with_translation_mode/2,
24 get_language_mode/1, set_language_mode/1, with_language_mode/2,
25 translate_bexpression_to_unicode/2,
26 translate_bexpression/2, translate_subst_or_bexpr_in_mode/3,
27 translate_bexpression_with_limit/3, translate_bexpression_with_limit/2,
28 translate_bexpression_to_codes/2,
29 translate_bexpr_to_parseable/2,
30 translate_predicate_into_machine/3, nested_print_sequent_as_classicalb/6,
31 get_bexpression_column_template/4,
32 translate_subst_or_bexpr/2, translate_subst_or_bexpr_with_limit/3,
33 translate_substitution/2, print_subst/1,
34 convert_and_ajoin_ids/2,
35 translate_bvalue/2, translate_bvalue_to_codes/2, translate_bvalue_to_codes_with_limit/3,
36 translate_bvalue_to_parseable_classicalb/2,
37 translate_bvalue_for_dot/2,
38 translate_bvalue_with_limit/3,
39 translate_bvalue_with_type/3, translate_bvalue_with_type_and_limit/4,
40 translate_bvalue_for_expression/3, translate_bvalue_for_expression_with_limit/4,
41 translate_bvalue_with_tlatype/3,
42 translate_bvalue_kind/2,
43 print_state/1,
44 translate_bstate/2, translate_bstate_limited/2, translate_bstate_limited/3,
45 print_bstate/1, print_bstate_limited/3,
46 translate_b_state_to_comma_list/3,
47 translate_context/2, print_context/1,
48 translate_any_state/2,
49 print_value_variable/1,
50 print_cspm_state/1, translate_cspm_state/2,
51 print_csp_value/1, translate_csp_value/2,
52 translate_cspm_expression/2,
53 translate_properties_with_limit/2,
54 translate_event/2,translate_events/2,
55 translate_event_with_target_id/4,
56 translate_event_with_src_and_target_id/4, translate_event_with_src_and_target_id/5,
57 get_non_det_modified_vars_in_target_id/3,
58 translate_event_with_limit/3,
59 translate_state_errors/2,translate_state_error/2,
60 translate_event_error/2,
61 translate_call_stack/2, render_call_short/2,
62 translate_prolog_constructor/2, translate_prolog_constructor_in_mode/2,
63 get_texpr_top_level_symbol/4,
64 pretty_type/2, % pretty-prints a type (pp_type, translate_type)
65 explain_state_error/3, get_state_error_span/2,
66 explain_event_trace/3,
67 explain_transition_info/2,
68 generate_typing_predicates/2, % keeps sequence typing info
69
70 print_raw_machine_terms/1,
71 print_raw_bexpr/1, l_print_raw_bexpr/1,
72 translate_raw_bexpr/2, translate_raw_bexpr_with_limit/3,
73 transform_raw/2,
74
75 print_span/1, print_span_nl/1, translate_span/2,
76 translate_span_with_filename/2,
77 get_definition_context_from_span/2,
78
79 %set_type_to_maximal_texpr/2, type_set/2, % now in typing_tools as create_type_set
80
81 translate_error_term/2, translate_error_term/3,
82 translate_prolog_exception/2,
83 portray_open_streams/0, print_open_stream_stats/0,
84
85 set_translation_constants/1, set_translation_context/1,
86 clear_translation_constants/0,
87
88 set_print_type_infos/1,
89 set_print_type_infos/2, reset_print_type_infos/1,
90 suppress_rodin_positions/1, reset_suppress_rodin_positions/1,
91 add_normal_typing_predicates/3,
92
93 install_b_portray_hook/0,remove_b_portray_hook/0,
94
95 translate_eventb_to_classicalb/3,
96 translate_eventb_direct_definition_header/3, translate_eventb_direct_definition_body/2,
97 return_csp_closure_value/2,
98 latex_to_unicode/2, get_latex_keywords/1, get_latex_keywords_with_backslash/1,
99 ascii_to_unicode/2,
100
101 translate_xtl_value/2
102
103 ]).
104
105 :- meta_predicate call_pp_with_no_limit_and_parseable(0).
106 :- meta_predicate with_translation_mode(+, 0).
107 :- meta_predicate with_language_mode(+, 0).
108
109 :- use_module(tools).
110 :- use_module(tools_lists,[is_list_simple/1]).
111 :- use_module(extrasrc(json_parser), [json_write_stream/1]).
112
113 :- use_module(module_information).
114 :- module_info(group,tools).
115 :- module_info(description,'This module is responsible for pretty-printing B and CSP, source spans, ...').
116
117 :- use_module(library(lists)).
118 :- use_module(library(codesio)).
119 :- use_module(library(terms)).
120 :- use_module(library(avl)).
121
122 :- use_module(debug).
123 :- use_module(error_manager).
124 :- use_module(self_check).
125 :- use_module(b_global_sets).
126 :- use_module(specfile,[csp_with_bz_mode/0,process_algebra_mode/0,
127 animation_minor_mode/1,set_animation_minor_mode/1,
128 remove_animation_minor_mode/0,
129 animation_mode/1,set_animation_mode/1, csp_mode/0,
130 translate_operation_name/2]).
131 :- use_module(bsyntaxtree).
132 %:- use_module('smv/smv_trans',[smv_print_initialisation/2]).
133 :- use_module(preferences,[get_preference/2, set_preference/2, eclipse_preference/2]).
134 :- use_module(bmachine_structure).
135 :- use_module(avl_tools,[check_is_non_empty_avl/1]).
136
137 :- set_prolog_flag(double_quotes, codes).
138
139 % print a list of expressions or substitutions
140 l_print_bexpr_or_subst([]).
141 l_print_bexpr_or_subst([H|T]) :-
142 print_bexpr_or_subst(H),
143 (T=[] -> true
144 ; (get_texpr_type(H,Type),is_subst_type(Type) -> write('; ') ; write(', ')),
145 l_print_bexpr_or_subst(T)
146 ).
147
148 is_subst_type(T) :- var(T),!,fail.
149 is_subst_type(subst).
150 is_subst_type(op(_,_)).
151
152 print_bexpr_or_subst(E) :- get_texpr_type(E,T),is_subst_type(T),!, print_subst(E).
153 print_bexpr_or_subst(precondition(A,B)) :- !, print_subst(precondition(A,B)).
154 print_bexpr_or_subst(any(A,B,C)) :- !, print_subst(any(A,B,C)).
155 print_bexpr_or_subst(select(A)) :- !, print_subst(select(A)). % TO DO: add more cases ?
156 print_bexpr_or_subst(E) :- print_bexpr(E).
157
158 print_unwrapped_bexpr_with_limit(Expr) :- print_unwrapped_bexpr_with_limit(Expr,200).
159 print_unwrapped_bexpr_with_limit(Expr,Limit) :-
160 translate:print_bexpr_with_limit(b(Expr,pred,[]),Limit),nl.
161 debug_print_bexpr(E) :- debug_mode(off) -> true ; print_bexpr(E).
162 print_bexpr(Expr) :- translate_bexpression(Expr,R), write(R).
163 print_bexpr_with_limit(Expr,Limit) :- translate_bexpression_with_limit(Expr,Limit,R), write(R).
164 print_bvalue(Val) :- translate_bvalue(Val,TV), write(TV).
165 print_bexpr_stream(S,Expr) :- translate_bexpression(Expr,R), write(S,R).
166 print_bvalue_stream(S,Val) :- translate_bvalue(Val,R), write(S,R).
167
168 print_bexpr_with_limit_and_typing(Expr,Limit,TypeInfos) :-
169 set_print_type_infos(TypeInfos,CHNG),
170 (get_texpr_type(Expr,pred)
171 -> find_typed_identifier_uses(Expr, TUsedIds),
172 add_typing_predicates(TUsedIds,Expr,Expr2)
173 ; Expr2=Expr),
174 call_cleanup(print_bexpr_with_limit(Expr2,Limit),
175 reset_print_type_infos(CHNG)).
176
177 print_components(C) :- print_components(C,0).
178 print_components([],Nr) :- write('Nr of components: '),write(Nr),nl.
179 print_components([component(Pred,Vars)|T],Nr) :- N1 is Nr+1,
180 write('Component: '), write(N1), write(' over '), write(Vars),nl,
181 print_bexpr(Pred),nl,
182 print_components(T,N1).
183
184 l_print_bvalue([]).
185 l_print_bvalue([H|T]) :- print_bvalue(H), write(' : '),l_print_bvalue(T).
186
187 nested_print_bexpr_as_classicalb(E) :- nested_print_bexpr_as_classicalb2(E,0).
188
189 nested_print_bexpr_as_classicalb2(E,InitialPeanoIndent) :-
190 (animation_minor_mode(X)
191 -> remove_animation_minor_mode,
192 call_cleanup(nested_print_bexpr2(E,InitialPeanoIndent), set_animation_minor_mode(X))
193 ; nested_print_bexpr2(E,InitialPeanoIndent)).
194
195
196 nested_print_bexpr_up_to(State,ExpandAvlUpTo) :-
197 temporary_set_preference(expand_avl_upto,ExpandAvlUpTo,CHNG),
198 call_cleanup(nested_print_bexpr(State),
199 reset_temporary_preference(expand_avl_upto,CHNG)).
200
201 % can also print lists of predicates and lists of lists, ...
202 nested_print_bexpr(Expr) :- nested_print_bexpr2(Expr,0).
203
204 % a version where one can specify the initial indent in peano numbering
205 nested_print_bexpr2([],_) :- !.
206 nested_print_bexpr2([H],InitialIndent) :- !,nested_print_bexpr2(H,InitialIndent).
207 nested_print_bexpr2([H|T],II) :- !,
208 nested_print_bexpr2(H,II),
209 print_indent(II), write('&'),nl,
210 nested_print_bexpr2(T,II).
211 nested_print_bexpr2(Expr,II) :- nbp(Expr,conjunct,II).
212
213 nbp(b(E,_,Info),Type,Indent) :- !,nbp2(E,Type,Info,Indent).
214 nbp(E,Type,Indent) :- format(user_error,'Missing b/3 wrapper!~n',[]),
215 nbp2(E,Type,[],Indent).
216 nbp2(E,Type,_Info,Indent) :- get_binary_connective(E,NewType,Ascii,LHS,RHS),!,
217 inc_indent(NewType,Type,Indent,NIndent),
218 print_bracket(Indent,NIndent,'('),
219 nbp(LHS,NewType,NIndent),
220 print_indent(NIndent),
221 translate_in_mode(NewType,Ascii,Symbol), write(Symbol),nl,
222 (is_associative(NewType) -> NewTypeR=NewType % no need for parentheses if same operator on right
223 ; NewTypeR=right(NewType)),
224 nbp(RHS,NewTypeR,NIndent),
225 print_bracket(Indent,NIndent,')').
226 nbp2(lazy_let_pred(TID,LHS,RHS),Type,_Info,Indent) :-
227 def_get_texpr_id(TID,ID),!,
228 NewType=lazy_let_pred(TID),
229 inc_indent(NewType,Type,Indent,NIndent),
230 print_indent(Indent), format('LET ~w = (~n',[ID]),
231 nbp(LHS,NewType,NIndent),
232 print_indent(NIndent), write(') IN ('),nl,
233 nbp(RHS,NewType,NIndent),
234 print_indent(NIndent),write(')'),nl.
235 nbp2(negation(LHS),_Type,_Info,Indent) :- !,
236 inc_indent(negation,false,Indent,NIndent),
237 print_indent(Indent),
238 translate_in_mode(negation,'not',Symbol), format('~s(~n',[Symbol]),
239 nbp(LHS,negation,NIndent),
240 print_indent(Indent), write(')'),nl.
241 nbp2(let_predicate(Ids,Exprs,Pred),_Type,_Info,Indent) :- !,
242 inc_indent(let_predicate,false,Indent,NIndent),
243 pp_expr_ids_in_mode(Ids,_LR,Codes,[]),
244 print_indent(Indent),format('#~s.( /* LET */~n',[Codes]),
245 pp_expr_let_pred_exprs(Ids,Exprs,_LimitReached,Codes2,[]),
246 print_indent(Indent), format('~s~n',[Codes2]),
247 print_indent(NIndent), write('&'),nl,
248 nbp(Pred,let_predicate,NIndent),
249 print_indent(Indent), write(')'),nl.
250 nbp2(let_expression(Ids,Exprs,Pred),_Type,_Info,Indent) :- !,
251 inc_indent(let_expression,false,Indent,NIndent),
252 pp_expr_ids_in_mode(Ids,_LR,Codes,[]),
253 print_indent(Indent),format('LET ~s BE~n',[Codes]),
254 pp_expr_let_pred_exprs(Ids,Exprs,_LimitReached,Codes2,[]),
255 print_indent(Indent), format('~s~n',[Codes2]),
256 print_indent(NIndent), write('IN'),nl,
257 nbp(Pred,let_expression,NIndent),
258 print_indent(Indent), write('END'),nl.
259 nbp2(exists(Ids,Pred),_Type,_Infos,Indent) :- !,
260 inc_indent(exists,false,Indent,NIndent),
261 pp_expr_ids_in_mode(Ids,_LR,Codes,[]),
262 print_indent(Indent),
263 %(member(allow_to_lift_exists,_Infos) -> write('/* LIFT */ ') ; true),
264 exists_symbol(ExistsSymbol,[]), format('~s~s.(~n',[ExistsSymbol,Codes]),
265 nbp(Pred,exists,NIndent),
266 print_indent(Indent), write(')'),nl.
267 nbp2(forall(Ids,LHS,RHS),_Type,_Info,Indent) :- !,
268 inc_indent(forall,false,Indent,NIndent),
269 pp_expr_ids_in_mode(Ids,_LR,Codes,[]),
270 print_indent(Indent),
271 forall_symbol(ForallSymbol,[]), format('~s~s.(~n',[ForallSymbol,Codes]),
272 nbp(LHS,forall,NIndent),
273 print_indent(NIndent),
274 translate_in_mode(implication,'=>',Symbol),write(Symbol),nl,
275 nbp(RHS,forall,NIndent),
276 print_indent(Indent), write(')'),nl.
277 nbp2(if_then_else(Test,LHS,RHS),_Type,_Info,Indent) :- !,
278 inc_indent(if_then_else,false,Indent,NIndent),
279 print_indent(Indent), write('IF'),nl,
280 nbp(Test,if_then_else,NIndent),
281 print_indent(Indent), write('THEN'),nl,
282 nbp(LHS,if_then_else,NIndent),
283 print_indent(Indent), write('ELSE'),nl,
284 nbp(RHS,if_then_else,NIndent),
285 print_indent(Indent), write('END'),nl.
286 nbp2(BOP,_Type,_Info,Indent) :-
287 indent_binary_pred(BOP,LHS,RHS,NewType,Ascii),
288 get_texpr_id(LHS,_Id),
289 \+ simple_expr(RHS),
290 !,
291 print_indent(Indent),print_bexpr(LHS),write(' '),
292 translate_in_mode(NewType,Ascii,Symbol), write(Symbol),nl,
293 inc_indent(NewType,false,Indent,NIndent),
294 nbp(RHS,equal,NIndent). % do we need to put parentheses around this ?
295 nbp2(value(V),_,Info,Indent) :- !,
296 print_indent(Indent), print_bexpr(b(value(V),any,Info)),nl.
297 nbp2(E,_,Info,Indent) :-
298 print_indent(Indent), print_bexpr(b(E,pred,Info)),nl.
299
300 indent_binary_pred(equal(LHS,RHS),LHS,RHS,equal,'=').
301 indent_binary_pred(member(LHS,RHS),LHS,RHS,member,':').
302 %indent_binary_pred(couple(LHS,RHS),LHS,RHS,couple,'|->'). % TODO: process parentheses above
303 %indent_binary_pred(union(LHS,RHS),LHS,RHS,union,'\\/').
304 %indent_binary_pred(concat(LHS,RHS),LHS,RHS,concat,'^').
305 % ...
306
307 % simple texpr without subarguments
308 simple_expr(BExpr) :-
309 syntaxtraversion(BExpr,Expr,_Type,_Infos,Subs,_Names),
310 Subs=[],
311 (Expr=value(V),nonvar(V), V=closure(_,_,_) -> fail % closure value probably not simple; TODO: check for interval
312 ; true).
313
314 % all left-associative
315 get_binary_connective(conjunct(LHS,RHS),conjunct,'&',LHS,RHS).
316 get_binary_connective(disjunct(LHS,RHS),disjunct,'or',LHS,RHS).
317 get_binary_connective(implication(LHS,RHS),implication,'=>',LHS,RHS).
318 get_binary_connective(equivalence(LHS,RHS),equivalence,'<=>',LHS,RHS).
319
320 inc_indent(Type,CurType,I,NewI) :- (Type=CurType -> NewI=I ; NewI=s(I)).
321 print_bracket(I,I,_) :- !.
322 print_bracket(I,_NewI,Bracket) :-
323 print_indent(I), write(Bracket),nl.
324
325 print_indent(s(X)):- !,
326 write(' '),
327 print_indent(X).
328 print_indent(_).
329
330
331 /* =============================================== */
332 /* Translating expressions and values into strings */
333 /* =============================================== */
334
335 translate_params_for_dot(List,TransList) :-
336 translate_params_for_dot(List,TransList,3,-3).
337 translate_params_for_dot_nl(List,TransList) :- % newline after every entry
338 translate_params_for_dot(List,TransList,1,-1).
339
340 translate_params_for_dot([],'',_,_).
341 translate_params_for_dot([H|T],Res,Lim,Nr) :-
342 translate_property_with_limit(H,100,TH),
343 (Nr>=Lim -> N1=1 % Limit reached, add newline
344 ; N1 is Nr+1),
345 translate_params_for_dot(T,TT,Lim,N1),
346 string_concatenate(TH,TT,Res1),
347 (N1=1
348 -> string_concatenate(',\n',Res1,Res)
349 ; (Nr>(-Lim) -> string_concatenate(',',Res1,Res)
350 ; Res=Res1)).
351
352
353 translate_channel_values(X,['_'|T],T) :- var(X),!.
354 translate_channel_values([],S,S) :- !.
355 translate_channel_values([tuple([])|T],S,R) :- !,
356 translate_channel_values(T,S,R).
357 translate_channel_values([in(tuple([]))|T],S,R) :- !,
358 translate_channel_values(T,S,R).
359 translate_channel_values([H|T],['.'|S],R) :- !,
360 ((nonvar(H),H=in(X))
361 -> Y=X
362 ; Y=H
363 ),
364 pp_csp_value(Y,S,S2),
365 translate_channel_values(T,S2,R).
366 translate_channel_values(tail_in(X),S,T) :-
367 (X=[] ; X=[_|_]), !, translate_channel_values(X,S,T).
368 translate_channel_values(_X,['??'|S],S).
369
370
371
372 pp_single_csp_value(V,'_') :- var(V),!.
373 pp_single_csp_value(X,'_cyclic_') :- cyclic_term(X),!.
374 pp_single_csp_value(int(X),A) :- atomic(X),!,number_chars(X,Chars),atom_chars(A,Chars).
375
376 :- assert_must_succeed((translate_cspm_expression(listExp(rangeOpen(2)),R), R == '<2..>')).
377 :- assert_must_succeed((translate_cspm_expression(listFrom(2),R), R == '<2..>')).
378 :- assert_must_succeed((translate_cspm_expression(listFromTo(2,6),R), R == '<2..6>')).
379 :- assert_must_succeed((translate_cspm_expression(setFromTo(2,6),R), R == '{2..6}')).
380 :- assert_must_succeed((translate_cspm_expression('#'(listFromTo(2,6)),R), R == '#<2..6>')).
381 :- assert_must_succeed((translate_cspm_expression(inGuard(x,setFromTo(1,5)),R), R == '?x:{1..5}')).
382 :- assert_must_succeed((translate_cspm_expression(builtin_call(int(3)),R), R == '3')).
383 :- assert_must_succeed((translate_cspm_expression(set_to_seq(setValue([int(1),int(2)])),R), R == 'seq({1,2})')).
384 :- assert_must_succeed((translate_cspm_expression(diff(setValue([int(1)]),setValue([])),R), R == 'diff({1},{})')).
385 :- assert_must_succeed((translate_cspm_expression(inter(setValue([int(1)]),setValue([])),R), R == 'inter({1},{})')).
386 :- assert_must_succeed((translate_cspm_expression(lambda([x,y],'*'(x,y)),R), R == '\\ x,y @ (x*y)')).
387 :- assert_must_succeed((translate_cspm_expression(lambda([x,y],'/'(x,y)),R), R == '\\ x,y @ (x/y)')).
388 :- assert_must_succeed((translate_cspm_expression(lambda([x,y],'%'(x,y)),R), R == '\\ x,y @ (x%y)')).
389 :- assert_must_succeed((translate_cspm_expression(rename(x,y),R), R == 'x <- y')).
390 :- assert_must_succeed((translate_cspm_expression(link(x,y),R), R == 'x <-> y')).
391 :- assert_must_succeed((translate_cspm_expression(agent_call_curry(f,[[a,b],[c]]),R), R == 'f(a,b)(c)')).
392
393 translate_cspm_expression(Expr, Text) :-
394 (pp_csp_value(Expr,Atoms,[]) -> ajoin(Atoms,Text)
395 ; write('Pretty printing expression failed: '),print(Expr),nl).
396
397 pp_csp_value(X,[A|S],S) :- pp_single_csp_value(X,A),!.
398 pp_csp_value(setValue(L),['{'|S],T) :- !,pp_csp_value_l(L,',',S,['}'|T],inf).
399 pp_csp_value(setExp(rangeEnum(L)),['{'|S],T) :- !,pp_csp_value_l(L,',',S,['}'|T],inf).
400 pp_csp_value(setExp(rangeEnum(L),Gen),['{'|S],T) :- !,
401 copy_term((L,Gen),(L2,Gen2)), numbervars((L2,Gen2),1,_),
402 pp_csp_value_l(L2,',',S,['|'|S2],inf),
403 pp_csp_value_l(Gen2,',',S2,['}'|T],inf).
404 pp_csp_value(avl_set(A),['{'|S],T) :- !, check_is_non_empty_avl(A),
405 avl_domain(A,L),pp_csp_value_l(L,',',S,['}'|T],inf).
406 pp_csp_value(setExp(rangeClosed(L,U)),['{'|S],T) :- !, pp_csp_value(L,S,['..'|S2]),pp_csp_value(U,S2,['}'|T]).
407 pp_csp_value(setExp(rangeOpen(L)),['{'|S],T) :- !, pp_csp_value(L,S,['..}'|T]).
408 % TO DO: pretty print comprehensionGuard; see prints in coz-example.csp ; test 1846
409 pp_csp_value(comprehensionGenerator(Var,Body),S,T) :- !, pp_csp_value(Var,S,['<-'|S1]),
410 pp_csp_value(Body,S1,T).
411 pp_csp_value(listExp(rangeEnum(L)),['<'|S],T) :- !,pp_csp_value_l(L,',',S,['>'|T],inf).
412 pp_csp_value(listExp(rangeClosed(L,U)),['<'|S],T) :- !, pp_csp_value(L,S,['..'|S2]),pp_csp_value(U,S2,['>'|T]).
413 pp_csp_value(listExp(rangeOpen(L)),['<'|S],T) :- !, pp_csp_value(L,S,['..>'|T]).
414 pp_csp_value(setFromTo(L,U),['{'|S],T) :- !,
415 pp_csp_value(L,S,['..'|S2]),pp_csp_value(U,S2,['}'|T]).
416 pp_csp_value(setFrom(L),['{'|S],T) :- !,
417 pp_csp_value(L,S,['..}'|T]).
418 pp_csp_value(closure(L), ['{|'|S],T) :- !,pp_csp_value_l(L,',',S,['|}'|T],inf).
419 pp_csp_value(list(L),['<'|S],T) :- !,pp_csp_value_l(L,',',S,['>'|T],inf).
420 pp_csp_value(listFromTo(L,U),['<'|S],T) :- !,
421 pp_csp_value(L,S,['..'|S2]),pp_csp_value(U,S2,['>'|T]).
422 pp_csp_value(listFrom(L),['<'|S],T) :- !,
423 pp_csp_value(L,S,['..>'|T]).
424 pp_csp_value('#'(L),['#'|S],T) :- !,pp_csp_value(L,S,T).
425 pp_csp_value('^'(X,Y),S,T) :- !,pp_csp_value(X,S,['^'|S1]), pp_csp_value(Y,S1,T).
426 pp_csp_value(linkList(L),S,T) :- !,pp_csp_value_l(L,',',S,T,inf).
427 pp_csp_value(in(X),['?'|S],T) :- !,pp_csp_value(X,S,T).
428 pp_csp_value(inGuard(X,Set),['?'|S],T) :- !,pp_csp_value(X,S,[':'|S1]),
429 pp_csp_value(Set,S1,T).
430 pp_csp_value(out(X),['!'|S],T) :- !,pp_csp_value(X,S,T).
431 pp_csp_value(alsoPat(X,_Y),S,T) :- !,pp_csp_value(X,S,T).
432 pp_csp_value(appendPat(X,_Fun),S,T) :- !,pp_csp_value(X,S,T).
433 pp_csp_value(tuple(vclosure),S,T) :- !, S=T.
434 pp_csp_value(tuple([X]),S,T) :- !,pp_csp_value_in(X,S,T).
435 pp_csp_value(tuple([X|vclosure]),S,T) :- !,pp_csp_value_in(X,S,T).
436 pp_csp_value(tuple([H|TT]),S,T) :- !,pp_csp_value_in(H,S,['.'|S1]),pp_csp_value(tuple(TT),S1,T).
437 pp_csp_value(dotTuple([]),['unit_channel'|S],S) :- ! .
438 pp_csp_value(dotTuple([H]),S,T) :- !, pp_csp_value_in(H,S,T).
439 pp_csp_value(dotTuple([H|TT]),S,T) :- !, pp_csp_value_in(H,S,['.'|S1]),
440 pp_csp_value(dotTuple(TT),S1,T).
441 pp_csp_value(tupleExp(Args),S,T) :- !,pp_csp_args(Args,S,T,'(',')').
442 pp_csp_value(na_tuple(Args),S,T) :- !,pp_csp_args(Args,S,T,'(',')').
443 pp_csp_value(record(Name,Args),['('|S],T) :- !,pp_csp_value(tuple([Name|Args]),S,[')'|T]).
444 pp_csp_value(val_of(Name,_Span),S,T) :- !, pp_csp_value(Name,S,T).
445 pp_csp_value(builtin_call(X),S,T) :- !,pp_csp_value(X,S,T).
446 pp_csp_value(seq_to_set(X),['set('|S],T) :- !,pp_csp_value(X,S,[')'|T]).
447 pp_csp_value(set_to_seq(X),['seq('|S],T) :- !,pp_csp_value(X,S,[')'|T]).
448 %pp_csp_value('\\'(B,C,S),S1,T) :- !, pp_csp_process(ehide(B,C,S),S1,T).
449 pp_csp_value(agent_call(_Span,Agent,Parameters),['('|S],T) :- !,
450 pp_csp_value(Agent,S,S1),
451 pp_csp_args(Parameters,S1,[')'|T],'(',')').
452 pp_csp_value(agent_call_curry(Agent,Parameters),S,T) :- !,
453 pp_csp_value(Agent,S,S1),
454 pp_csp_curry_args(Parameters,S1,T).
455 pp_csp_value(lambda(Parameters,Body),['\\ '|S],T) :- !,
456 pp_csp_args(Parameters,S,[' @ '|S1],'',''),
457 pp_csp_value(Body,S1,T).
458 pp_csp_value(rename(X,Y),S,T) :- !,pp_csp_value(X,S,[' <- '|S1]),
459 pp_csp_value(Y,S1,T).
460 pp_csp_value(link(X,Y),S,T) :- !,pp_csp_value(X,S,[' <-> '|S1]),
461 pp_csp_value(Y,S1,T).
462 % binary operators:
463 pp_csp_value(Expr,['('|S],T) :- bynary_numeric_operation(Expr,E1,E2,OP),!,
464 pp_csp_value(E1,S,[OP|S2]),
465 pp_csp_value(E2,S2,[')'|T]).
466 % built-in functions for sets
467 pp_csp_value(empty(A),[empty,'('|S],T) :- !, pp_csp_value(A,S,[')'|T]).
468 pp_csp_value(card(A),[card,'('|S],T) :- !, pp_csp_value(A,S,[')'|T]).
469 pp_csp_value('Set'(A),['Set','('|S],T) :- !, pp_csp_value(A,S,[')'|T]).
470 pp_csp_value('Inter'(A1),['Inter','('|S],T) :- !,pp_csp_value(A1,S,[')'|T]).
471 pp_csp_value('Union'(A1),['Union','('|S],T) :- !,pp_csp_value(A1,S,[')'|T]).
472 pp_csp_value(diff(A1,A2),[diff,'('|S],T) :- !,pp_csp_args([A1,A2],S,[')'|T],'','').
473 pp_csp_value(inter(A1,A2),[inter,'('|S],T) :- !,pp_csp_args([A1,A2],S,[')'|T],'','').
474 pp_csp_value(union(A1,A2),[union,'('|S],T) :- !,pp_csp_args([A1,A2],S,[')'|T],'','').
475 pp_csp_value(member(A1,A2),[member,'('|S],T) :- !,pp_csp_args([A1,A2],S,[')'|T],'','').
476 % built-in functions for sequences
477 pp_csp_value(null(A),[null,'('|S],T) :- !, pp_csp_value(A,S,[')'|T]).
478 pp_csp_value(length(A),[length,'('|S],T) :- !, pp_csp_value(A,S,[')'|T]).
479 pp_csp_value(head(A),[head,'('|S],T) :- !, pp_csp_value(A,S,[')'|T]).
480 pp_csp_value(tail(A),[tail,'('|S],T) :- !, pp_csp_value(A,S,[')'|T]).
481 pp_csp_value(elem(A1,A2),[elem,'('|S],T) :- !,pp_csp_args([A1,A2],S,[')'|T],'','').
482 pp_csp_value(concat(A1,A2),[concat,'('|S],T) :- !,pp_csp_args([A1,A2],S,[')'|T],'','').
483 pp_csp_value('Seq'(A),['Seq','('|S],T) :- !, pp_csp_value(A,S,[')'|T]).
484 % vclosure
485 pp_csp_value(Expr,S,T) :- is_list(Expr),!,pp_csp_value(closure(Expr),S,T).
486 % Type expressions
487 pp_csp_value(dotTupleType([H]),S,T) :- !, pp_csp_value_in(H,S,T).
488 pp_csp_value(dotTupleType([H|TT]),S,T) :- !,pp_csp_value_in(H,S,['.'|S1]), pp_csp_value(dotTupleType(TT),S1,T).
489 pp_csp_value(typeTuple(Args),S,T) :- !, pp_csp_args(Args,S,T,'(',')').
490 pp_csp_value(dataType(T),[T|S],S) :- ! .
491 pp_csp_value(boolType,['Bool'|S],S) :- ! .
492 pp_csp_value(intType,['Int'|S],S) :- ! .
493 pp_csp_value(dataTypeDef([H]),S,T) :- !, pp_csp_value(H,S,T).
494 pp_csp_value(dataTypeDef([H|TT]),S,T) :- !, pp_csp_value(H,S,['|'|S1]),
495 pp_csp_value(dataTypeDef(TT),S1,T).
496 pp_csp_value(constructor(Name),[Name|S],S) :- ! .
497 pp_csp_value(constructorC(C,Type),[C,'('|S],T) :- !, pp_csp_value(Type,S,[')'|T]).
498 % Argument of function can be process
499
500 pp_csp_value(Expr,S,T) :- pp_csp_process(Expr,S,T),!. % pp_csp_process has a catch-all !!! TO DO: look at this
501 pp_csp_value(Expr,S,T) :- csp_with_bz_mode,!,pp_value(Expr,S,T).
502 pp_csp_value(X, [A|S], S) :- % ['<< ',A,' >>'|S],S) :- % the << >> pose problems when checking against FDR
503 write_to_codes(X,Codes),atom_codes_with_limit(A,Codes).
504
505 pp_csp_value_in(H,S,T) :- nonvar(H),H=in(X),!, pp_csp_value(X,S,T).
506 pp_csp_value_in(H,S,T) :- pp_csp_value(H,S,T).
507
508 print_csp_value(Val) :- pp_csp_value(Val,Atoms,[]), ajoin(Atoms,Text),
509 write(Text).
510
511 translate_csp_value(Val,Text) :- pp_csp_value(Val,Atoms,[]), ajoin(Atoms,Text).
512
513 return_csp_closure_value(closure(S),List) :- pp_csp_value_l1(S,List).
514 return_csp_closure_value(setValue(S),List) :- pp_csp_value_l1(S,List).
515
516 pp_csp_value_l1([Expr|Rest],List) :-
517 ( nonvar(Rest),Rest=[] ->
518 pp_csp_value(Expr,T,[]),ajoin(T,Value),List=[Value]
519 ; pp_csp_value_l1(Rest,R),pp_csp_value(Expr,T,[]),ajoin(T,Value),List=[Value|R]
520 ).
521
522 pp_csp_args([],T,T,_LPar,_RPar).
523 pp_csp_args([H|TT],[LPar|S],T,LPar,RPar) :- pp_csp_value(H,S,S1), pp_csp_args2(TT,S1,T,RPar).
524 pp_csp_args2([],[RPar|T],T,RPar).
525 pp_csp_args2([H|TT],[','|S],T,RPar) :- pp_csp_value(H,S,S1), pp_csp_args2(TT,S1,T,RPar).
526
527 pp_csp_curry_args([],T,T).
528 pp_csp_curry_args([H|TT],S,T) :- is_list(H), pp_csp_args(H,S,S1,'(',')'), pp_csp_curry_args(TT,S1,T).
529
530 pp_csp_value_l(V,_Sep,['...'|S],S,N) :- (var(V) ; (N \= inf -> N<1;fail)), !.
531 pp_csp_value_l([],_Sep,S,S,_).
532 pp_csp_value_l([Expr|Rest],Sep,S,T,Nr) :-
533 ( nonvar(Rest),Rest=[] ->
534 pp_csp_value(Expr,S,T)
535 ;
536 (Nr=inf -> N1 = Nr ; N1 is Nr-1),
537 pp_csp_value(Expr,S,[Sep|S1]),pp_csp_value_l(Rest,Sep,S1,T,N1)).
538
539 :- assert_must_succeed((translate:convert_set_into_sequence([(int(1),int(5))],Seq),
540 check_eqeq(Seq,[int(5)]))).
541 :- assert_must_succeed((translate:convert_set_into_sequence([(int(2),X),(int(1),int(5))],Seq),
542 check_eq(Seq,[int(5),X]))).
543
544 convert_set_into_sequence(Set,Seq) :-
545 nonvar(Set), \+ eventb_translation_mode,
546 convert_set_into_sequence1(Set,Seq).
547 convert_set_into_sequence1(avl_set(A),Seq) :- !, check_is_non_empty_avl(A),
548 avl_size(A,Sz),size_is_in_set_limit(Sz),convert_avlset_into_sequence(A,Seq).
549 convert_set_into_sequence1([],Seq) :- !, Seq=[].
550 convert_set_into_sequence1(Set,Seq) :-
551 convert_set_into_sequence2(Set,0,_,SetElems,Seq),ground(SetElems).
552 convert_set_into_sequence2([],_Max,([],[]),_,_Seq).
553 convert_set_into_sequence2([Pair|T],Max,Last,SetElems,Seq) :-
554 nonvar(Pair),nonvar(T),Pair=(Index,H),ground(Index),
555 Index=int(Nr),
556 insert_el_into_seq(Nr,H,Seq,SetElems,L),
557 (Nr>Max -> NMax=Nr,NLast=L ; NMax=Max,NLast=Last),
558 convert_set_into_sequence2(T,NMax,NLast,SetElems,Seq).
559 insert_el_into_seq(1,H,[H|L],[set|L2],(L,L2)) :- !.
560 insert_el_into_seq(N,H,[_|T],[_|T2],Last) :- N>1, N1 is N-1, insert_el_into_seq(N1,H,T,T2,Last).
561
562 convert_avlset_into_sequence(Avl,Sequence) :-
563 \+ eventb_translation_mode,
564 convert_avlset_into_sequence2(Avl,1,Sequence).
565 convert_avlset_into_sequence2(Avl,_Index,[]) :-
566 empty_avl(Avl),!.
567 convert_avlset_into_sequence2(Avl,Index,[Head|Tail]) :-
568 avl_del_min(Avl, Min, _ ,NewAvl),
569 nonvar(Min), Min=(L,Head),
570 ground(L), L=int(Index),
571 Index2 is Index + 1,
572 convert_avlset_into_sequence2(NewAvl,Index2,Tail).
573
574 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
575 % translate new syntax tree -- work in progress
576 :- assert_must_succeed((translate_cspm_state(lambda([x,y],'|~|'(prefix(_,[],x,skip(_),_),prefix(_,[],y,skip(_),_),_)),R), R == 'CSP: \\ x,y @ (x->SKIP) |~| (y->SKIP)')).
577 :- assert_must_succeed((translate_cspm_state(agent_call_curry('F',[[a,b],[c]]),R), R == 'CSP: F(a,b)(c)')).
578 :- assert_must_succeed((translate_cspm_state(ifte(bool_not('<'(x,3)),';'(esharing([a],'/\\'('P1','P2',span),procRenaming([rename(r,s)],'Q',span),span),lParallel([link(b,c)],'R','S',span),span),'[>'(elinkParallel([link(h1,h2)],'G1','G2',span),exception([a],'H1','H2',span),span),span1,span2,span3),R), R == 'CSP: if not((x<3)) then ((P1) /\\ (P2) [|{|a|}|] Q[[r <- s]]) ; (R [{|b <-> c|}] S) else (G1 [{|h1 <-> h2|}] G2) [> (H1 [|{|a|}|> H2)')).
579 :- assert_must_succeed((translate_cspm_state(aParallel([a,b],'P',[b,c],'Q',span),R), R == 'CSP: P [{|a,b|} || {|b,c|}] Q')).
580 :- assert_must_succeed((translate_cspm_state(eaParallel([a,b],'P',[b,c],'Q',span),R), R == 'CSP: P [{|a,b|} || {|b,c|}] Q')).
581 :- assert_must_succeed((translate_cspm_state(eexception([a,b],'P','Q',span),R), R == 'CSP: P [|{|a,b|}|> Q')).
582
583 print_cspm_state(State) :- translate_cspm_state(State,T), write(T).
584
585 translate_cspm_state(State,Text) :-
586 ( pp_csp_process(State,Atoms,[]) -> true
587 ; print(pp_csp_process_failed(State)),nl,Atoms=State),
588 ajoin(['CSP: '|Atoms],Text).
589
590 pp_csp_process(skip(_Span),S,T) :- !, S=['SKIP'|T].
591 pp_csp_process(stop(_Span),S,T) :- !, S=['STOP'|T].
592 pp_csp_process('CHAOS'(_Span,Set),['CHAOS('|S],T) :- !,
593 pp_csp_value(Set,S,[')'|T]).
594 pp_csp_process(val_of(Agent,_Span),S,T) :- !,
595 pp_csp_value(Agent,S,T).
596 pp_csp_process(builtin_call(X),S,T) :- !,pp_csp_process(X,S,T).
597 pp_csp_process(agent(F,Body,_Span),S,T) :- !,
598 F =.. [Agent|Parameters],
599 pp_csp_value(Agent,S,S1),
600 pp_csp_args(Parameters,S1,[' = '|S2],'(',')'),
601 pp_csp_value(Body,S2,T).
602 pp_csp_process(agent_call(_Span,Agent,Parameters),S,T) :- !,
603 pp_csp_value(Agent,S,S1),
604 pp_csp_args(Parameters,S1,T,'(',')').
605 pp_csp_process(agent_call_curry(Agent,Parameters),S,T) :- !,
606 pp_csp_value(Agent,S,S1),
607 pp_csp_curry_args(Parameters,S1,T).
608 pp_csp_process(lambda(Parameters,Body),['\\ '|S],T) :- !,
609 pp_csp_args(Parameters,S,[' @ '|S1],'',''),
610 pp_csp_value(Body,S1,T).
611 pp_csp_process('\\'(B,C,S),S1,T) :- !, pp_csp_process(ehide(B,C,S),S1,T).
612 pp_csp_process(ehide(Body,ChList,_Span),['('|S],T) :- !,
613 pp_csp_process(Body,S,[')\\('|S1]),
614 pp_csp_value(ChList,S1,[')'|T]).
615 pp_csp_process(let(Decls,P),['let '| S],T) :- !,
616 maplist(translate_cspm_state,Decls,Texts),
617 ajoin_with_sep(Texts,' ',Text),
618 S=[Text,' within '|S1],
619 pp_csp_process(P,S1,T).
620 pp_csp_process(Expr,['('|S],T) :- binary_csp_op(Expr,X,Y,Op),!,
621 pp_csp_process(X,S,[') ',Op,' ('|S1]),
622 pp_csp_process(Y,S1,[')'|T]).
623 pp_csp_process(Expr,S,T) :- sharing_csp_op(Expr,X,Middle,Y,Op1,Op2),!,
624 pp_csp_process(X,S,[Op1|S1]),
625 pp_csp_value(Middle,S1,[Op2|S2]),
626 pp_csp_process(Y,S2,T).
627 pp_csp_process(Expr,S,T) :- asharing_csp_op(Expr,X,MiddleX,MiddleY,Y,Op1,MOp,Op2),!,
628 pp_csp_process(X,S,[Op1|S1]),
629 pp_csp_value(MiddleX,S1,[MOp|S2]),
630 pp_csp_value(MiddleY,S2,[Op2|S3]),
631 pp_csp_process(Y,S3,T).
632 pp_csp_process(Expr,S,T) :- renaming_csp_op(Expr,X,RList,Op1,Op2),!,
633 pp_csp_process(X,S,[Op1|S1]),
634 pp_csp_value_l(RList,',',S1,[Op2|T],10).
635 pp_csp_process(prefix(_SPAN1,Values,ChannelExpr,CSP,_SPAN2),S,T) :- !,
636 pp_csp_value_l([ChannelExpr|Values],'',S,['->'|S2],20),
637 pp_csp_process(CSP,S2,T).
638 pp_csp_process('&'(Test,Then),S,T) :- !,
639 pp_csp_bool_expr(Test,S,['&'|S2]),
640 pp_csp_process(Then,S2,T).
641 pp_csp_process(ifte(Test,Then,Else,_SPAN1,_SPAN2,_SPAN3),[' if '|S],T) :- !,
642 pp_csp_bool_expr(Test,S,[' then '|S2]),
643 pp_csp_process(Then,S2,[' else '|S3]),
644 pp_csp_process(Else,S3,T).
645 pp_csp_process(head(A),[head,'('|S],T) :- !, pp_csp_process(A,S,[')'|T]).
646 pp_csp_process(X,[X|T],T).
647
648 pp_csp_bool_expr(bool_not(BE),['not('|S],T) :- !, pp_csp_bool_expr(BE,S,[')'|T]).
649 pp_csp_bool_expr(BE,['('|S],T) :- binary_bool_op(BE,BE1,BE2,OP), !,
650 pp_csp_bool_expr(BE1,S,[OP|S2]),
651 pp_csp_bool_expr(BE2,S2,[')'|T]).
652 pp_csp_bool_expr(BE,[OP,'('|S],T) :- binary_pred(BE,BE1,BE2,OP), !,
653 pp_csp_value(BE1,S,[','|S2]),
654 pp_csp_value(BE2,S2,[')'|T]).
655 pp_csp_bool_expr(BE,S,T) :- pp_csp_value(BE,S,T).
656
657 bynary_numeric_operation('+'(X,Y),X,Y,'+').
658 bynary_numeric_operation('-'(X,Y),X,Y,'-').
659 bynary_numeric_operation('*'(X,Y),X,Y,'*').
660 bynary_numeric_operation('/'(X,Y),X,Y,'/').
661 bynary_numeric_operation('%'(X,Y),X,Y,'%').
662
663 binary_pred('member'(X,Y),X,Y,member).
664 binary_pred('<'(X,Y),X,Y,'<').
665 binary_pred('>'(X,Y),X,Y,'>').
666 binary_pred('>='(X,Y),X,Y,'>=').
667 binary_pred('<='(X,Y),X,Y,'=<').
668 binary_pred('elem'(X,Y),X,Y,is_elem_list).
669 binary_pred('=='(X,Y),X,Y,equal_element).
670 binary_pred('!='(X,Y),X,Y,not_equal_element).
671
672
673 binary_bool_op('<'(X,Y),X,Y,'<').
674 binary_bool_op('>'(X,Y),X,Y,'>').
675 binary_bool_op('>='(X,Y),X,Y,'>=').
676 binary_bool_op('<='(X,Y),X,Y,'=<').
677 binary_bool_op('=='(X,Y),X,Y,'==').
678 binary_bool_op('!='(X,Y),X,Y,'!=').
679 binary_bool_op(bool_and(X,Y),X,Y,'&&').
680 binary_bool_op(bool_or(X,Y),X,Y,'||').
681
682 binary_csp_op('|||'(X,Y,_Span),X,Y,'|||').
683 binary_csp_op('[]'(X,Y,_Span),X,Y,'[]').
684 binary_csp_op('|~|'(X,Y,_Span),X,Y,'|~|').
685 binary_csp_op(';'(X,Y,_Span),X,Y,';').
686 binary_csp_op('[>'(P,Q,_SrcSpan),P,Q,'[>').
687 binary_csp_op('/\\'(P,Q,_SrcSpan),P,Q,'/\\').
688
689 sharing_csp_op(esharing(CList,X,Y,_SrcSpan),X,CList,Y,' [|','|] ').
690 sharing_csp_op(sharing(CList,X,Y,_SrcSpan),X,CList,Y,' [|','|] ').
691 sharing_csp_op(lParallel(LinkList,X,Y,_Span),X,LinkList,Y,' [','] ').
692 sharing_csp_op(elinkParallel(LinkList,X,Y,_Span),X,LinkList,Y,' [','] ').
693 sharing_csp_op(exception(CList,X,Y,_SrcSpan),X,CList,Y,' [|','|> ').
694 sharing_csp_op(eexception(CList,X,Y,_SrcSpan),X,CList,Y,' [|','|> ').
695
696 asharing_csp_op(aParallel(CListX,X,CListY,Y,_SrcSpan),X,CListX,CListY,Y,' [',' || ','] ').
697 asharing_csp_op(eaParallel(CListX,X,CListY,Y,_SrcSpan),X,CListX,CListY,Y,' [',' || ','] ').
698
699 renaming_csp_op(procRenaming(RenameList,X,_SrcSpan),X,RenameList,'[[',']]').
700 renaming_csp_op(eprocRenaming(RenameList,X,_SrcSpan),X,RenameList,'[[',']]').
701
702 :- use_module(bmachine,[b_get_machine_operation_parameter_types/2, b_is_operation_name/1]).
703
704 translate_events([],[]).
705 translate_events([E|Erest],[Out|Orest]) :-
706 translate_event(E,Out),
707 translate_events(Erest,Orest).
708
709
710 % a version of translate_event which has access to the target state id:
711 % this allows to translate setup_constants, intialise by inserting target constants or values
712
713 translate_event_with_target_id(Term,Dst,Limit,Str) :-
714 translate_event_with_src_and_target_id(Term,unknown,Dst,Limit,Str).
715 translate_event_with_src_and_target_id(Term,Src,Dst,Str) :-
716 translate_event_with_src_and_target_id(Term,Src,Dst,5000,Str).
717
718 translate_event_with_src_and_target_id(Term,Src,Dst,Limit,Str) :-
719 get_preference(expand_avl_upto,CurLim),
720 SetLim is Limit//2,% at least two symbols per element
721 (CurLim<0 ; SetLim < CurLim),!,
722 temporary_set_preference(expand_avl_upto,SetLim,CHNG),
723 call_cleanup(translate_event_with_target_id2(Term,Src,Dst,Limit,Str),
724 reset_temporary_preference(expand_avl_upto,CHNG)).
725 translate_event_with_src_and_target_id(Term,Src,Dst,Limit,Str) :-
726 translate_event_with_target_id2(Term,Src,Dst,Limit,Str).
727
728 setup_cst_functor('$setup_constants',"SETUP_CONSTANTS").
729 setup_cst_functor('$partial_setup_constants',"PARTIAL_SETUP_CONSTANTS").
730
731 translate_event_with_target_id2(Term,_,Dst,Limit,Str) :-
732 functor(Term,Functor,_),
733 setup_cst_functor(Functor,UI_Name),
734 get_preference(show_initialisation_arguments,true),
735 state_space:visited_expression(Dst,concrete_constants(State)),
736 get_non_det_constant(State,NonDetState),
737 !,
738 translate_b_state_to_comma_list_codes(UI_Name,NonDetState,Limit,Codes),
739 atom_codes_with_limit(Str,Limit,Codes).
740 translate_event_with_target_id2(Term,_,Dst,Limit,Str) :-
741 functor(Term,'$initialise_machine',_),
742 get_preference(show_initialisation_arguments,true),
743 bmachine:b_get_operation_non_det_modifies('$initialise_machine',NDModVars),
744 state_space:visited_expression(Dst,State), get_variables(State,VarsState),
745 (NDModVars \= []
746 ->
747 include(non_det_modified_var(NDModVars),VarsState,ModVarsState) % first show non-det variables
748 % we could add a preference for whether to show the deterministicly assigned variables at all
749 %exclude(non_det_modified_var(NDModVars),VarsState,ModVarsState2),
750 %append(ModVarsState1,ModVarsState2,ModVarsState)
751 ; ModVarsState = VarsState),
752 !,
753 translate_b_state_to_comma_list_codes("INITIALISATION",ModVarsState,Limit,Codes),
754 atom_codes_with_limit(Str,Limit,Codes).
755 translate_event_with_target_id2(Term,Src,Dst,Limit,Str) :-
756 atomic(Term), % only applied to operations without parameters
757 specfile:b_mode,
758 get_non_det_modified_vars_in_target_id(Term,Dst,ModVarsState0), % only show non-det variables
759 (Src \= unknown,
760 state_space:visited_expression(Src,SrcState), get_variables(SrcState,PriorVarsState)
761 -> exclude(var_not_really_modified(PriorVarsState),ModVarsState0,ModVarsState)
762 % we could also optionally filter out vars which have the same value for all outgoing transitions of Src
763 ; ModVarsState = ModVarsState0
764 ),
765 !,
766 atom_codes(Term,TermCodes),
767 translate_b_state_to_comma_list_codes(TermCodes,ModVarsState,Limit,Codes),
768 atom_codes_with_limit(Str,Limit,Codes).
769 translate_event_with_target_id2(Term,_,_,Limit,Str) :- translate_event_with_limit(Term,Limit,Str).
770
771
772 get_non_det_modified_vars_in_target_id(OpName,DstId,ModVarsState0) :-
773 bmachine:b_get_operation_non_det_modifies(OpName,NDModVars),
774 NDModVars \= [], % a variable is non-deterministically written
775 state_space:visited_expression(DstId,State), % TO DO: unpack only NModVars
776 get_variables(State,VarsState),
777 include(non_det_modified_var(NDModVars),VarsState,ModVarsState0).
778
779 :- use_module(library(ordsets)).
780 non_det_modified_var(NDModVars,bind(Var,_)) :- ord_member(Var,NDModVars).
781
782 var_not_really_modified(PriorState,bind(Var,Val)) :-
783 (member(bind(Var,PVal),PriorState) -> PVal=Val).
784
785 get_variables(const_and_vars(_,VarsState),S) :- !, S=VarsState.
786 get_variables(S,S).
787
788 :- dynamic non_det_constants/2.
789
790 % compute which constants are non-deterministically assigned and which ones not
791 % TODO: maybe move to state space and invalidate info in case execute operation by predicate used
792 get_non_det_constant(Template,Result) :- non_det_constants(A,B),!, (A,B)=(Template,Result).
793 get_non_det_constant(Template,Result) :-
794 state_space:transition(root,_,DstID),
795 state_space:visited_expression(DstID,concrete_constants(State)), %write(get_non_det_constant(DstID)),nl,
796 !,
797 findall(D,(state_space:transition(root,_,D),D \= DstID),OtherDst),
798 compute_non_det_constants2(OtherDst,State),
799 non_det_constants(Template,Result).
800 get_non_det_constant(A,A).
801
802 compute_non_det_constants2([],State) :- adapt_state(State,Template,Result),
803 (Result = [] -> assertz(non_det_constants(A,A)) % in case all variables are deterministic: just show them
804 ; assertz(non_det_constants(Template,Result))).
805 compute_non_det_constants2([Dst|T],State) :-
806 state_space:visited_expression(Dst,concrete_constants(State2)),
807 lub_state(State,State2,NewState),
808 compute_non_det_constants2(T,NewState).
809
810 lub_state([],[],[]).
811 lub_state([bind(V,H1)|T1],[bind(V,H2)|T2],[bind(V,H3)|T3]) :-
812 (H1==H2 -> H3=H1 ; H3='$NONDET'), lub_state(T1,T2,T3).
813
814 adapt_state([],[],[]).
815 adapt_state([bind(ID,Val)|T],[bind(ID,X)|TX],[bind(ID,X)|TY]) :- Val='$NONDET',!,
816 adapt_state(T,TX,TY).
817 adapt_state([bind(ID,_)|T],[bind(ID,_)|TX],TY) :- % Value is deterministic: do not copy
818 adapt_state(T,TX,TY).
819
820
821
822 % ------------------------------------
823
824 translate_event_with_limit(Event,Limit,Out) :-
825 translate_event2(Event,Atoms,[]),!,
826 ajoin_with_limit(Atoms,Limit,Out).
827 %,write(done),debug:print_debug_stats,nl.% , write(Out),nl.
828 translate_event_with_limit(Event,_,Out) :- add_error(translate_event_with_limit,'Could not translate event: ', Event),
829 Out = '???'.
830
831 translate_event(Event,Out) :- %write(translate),print_debug_stats,nl,
832 translate_event2(Event,Atoms,[]),!,
833 ajoin(Atoms,Out).
834 %,write(done),debug:print_debug_stats,nl.% , write(Out),nl.
835 translate_event(Event,Out) :-
836 add_error(translate_event,'Could not translate event: ', Event),
837 Out = '???'.
838 /* BEGIN CSP */
839 translate_event2(start_cspm(Process),['start_cspm('|S],T) :- process_algebra_mode,!,pp_csp_value(Process,S,[')'|T]).
840 %% translate_event2(i(_Span),['i'|T],T) :- process_algebra_mode,!. /* CSP */ %% deprecated
841 translate_event2(tick(_Span),['tick'|T],T) :- process_algebra_mode,!. /* CSP */
842 translate_event2(tau(hide(Action)),['tau(hide('|S],T) :- process_algebra_mode,nonvar(Action), !,
843 translate_event2(Action,S,['))'|T]). /* CSP */
844 translate_event2(tau(link(Action1,Action2)),['tau(link('|S],T) :- /* CSP */
845 nonvar(Action1), nonvar(Action2), process_algebra_mode, !,
846 translate_event2(Action1,S,['<->'|S1]),
847 translate_event2(Action2,S1,['))'|T]).
848 translate_event2(tau(Info),['tau(',Fun,')'|T],T) :-
849 nonvar(Info), process_algebra_mode,!, /* CSP */
850 functor(Info,Fun,_). %(translate_event(Info,Fun) -> true ; functor(Info,Fun,_)),
851 translate_event2(io(V,Ch,_Span),S,T) :- process_algebra_mode,!, /* CSP */
852 (specfile:csp_with_bz_mode ->
853 S=['CSP:'|S1],
854 translate_event2(io(V,Ch),S1,T)
855 ;
856 translate_event2(io(V,Ch),S,T)
857 ).
858 translate_event2(io(X,Channel),S,T) :- process_algebra_mode,!, /* CSP */
859 (X=[] -> translate_event2(Channel,S,T)
860 ; (translate_event2(Channel,S,S1),
861 translate_channel_values(X,S1,T))
862 ).
863 /* END CSP */
864 translate_event2(Op,[A|T],T) :-
865 % this clause must be after the CSP code, test 756 sets process_algebra_mode via prob_pragma_string
866 % this allows xtl interpreters to use tau,tick,io events
867 animation_mode(xtl),
868 !,
869 translate_xtl_value(Op,A). /* XTL transitions can be arbitrary terms */
870 translate_event2('$JUMP'(Name),[A|T],T) :- write_to_codes(Name,Codes),
871 atom_codes_with_limit(A,Codes).
872 translate_event2('-->'(Operation,ResultValues),S,T) :- nonvar(ResultValues),
873 ResultValues=[_|_],!,
874 translate_event2(Operation,S,['-->',ValuesStr|T]),
875 translate_bvalues(ResultValues,ValuesStr).
876 translate_event2(Op,S,T) :-
877 nonvar(Op), Op =.. [OpName|Args],
878 translate_b_operation_call(OpName,Args,S,T),!.
879 translate_event2(Op,[A|T],T) :-
880 %['<< ',A,' >>'|T],T) :- % the << >> pose problems when checking against FDR
881 write_to_codes(Op,Codes),
882 atom_codes_with_limit(A,Codes).
883
884
885
886
887 % translate a B operation call to list of atoms in dcg style
888 translate_b_operation_call(OpName,Args,[TOpName|S],T) :-
889 translate_operation_name(OpName,TOpName),
890 ( Args=[] -> S=T
891 ;
892 S=['(',ValuesStr,')'|T],
893 ( get_preference(show_eventb_any_arguments,false), % otherwise we have additional ANY parameters !
894 \+ is_init(OpName), % order of variables does not always correspond to Variable order used by b_get_machine_operation_parameter_types ! TO DO - Fix this in b_initialise_machine2 (see Interlocking.mch CSP||B model)
895 specfile:b_mode,
896 b_is_operation_name(OpName),
897 b_get_machine_operation_parameter_types(OpName,ParTypes),
898 ParTypes \= []
899 -> translate_bvalues_with_types(Args,ParTypes,ValuesStr)
900 %; is_init(OpName) -> b_get_machine_operation_typed_parameters(OpName,TypedParas),
901 ; translate_bvalues(Args,ValuesStr))
902 ).
903
904 % -----------------
905
906 % translate call stacks as stored in wait flag info fields
907 % (managed by push_wait_flag_call_stack_info)
908
909 translate_call_stack(Stack,Msg) :-
910 Opts = [detailed],
911 split_calls(Stack,DStack),
912 get_cs_avl_limit(ALimit),
913 temporary_set_preference(expand_avl_upto,ALimit,CHNG),
914 set_unicode_mode,
915 call_cleanup(render_call_stack(DStack,1,Opts,A,[]),
916 (unset_unicode_mode,
917 reset_temporary_preference(expand_avl_upto,CHNG))),
918 ajoin(['call stack: '|A],Msg).
919 render_call_stack([],_,_) --> [].
920 render_call_stack([H],Nr,Opts) --> !,
921 render_nr(Nr,H,_,Opts), render_call(H,Opts).
922 render_call_stack([H|T],Nr,Opts) -->
923 render_nr(Nr,H,Nr1,Opts),
924 render_call(H,Opts),
925 render_seperator(Opts),
926 render_call_stack(T,Nr1,Opts).
927
928 % render nr of call in call stack
929 render_nr(Pos,H,Pos1,Opts) --> {member(detailed,Opts)},!, ['\n '], render_pos_nr(Pos,H,Pos1).
930 render_nr(Pos,_,Pos,_) --> [].
931
932 render_pos_nr(Pos,definition_call(_,_),Pos) --> !,
933 [' ']. % definition calls are virtual and can appear multiple times for different entries in the call stack
934 % see e.g., public_examples/B/FeatureChecks/DEFINITIONS/DefCallStackDisplay2.mch
935 render_pos_nr(Pos,_,Pos1) --> [Pos] , {Pos1 is Pos+1}, [': '].
936
937 render_seperator(Opts) --> {member(detailed,Opts)},!. % we put newlines in render_nr
938 render_seperator(_Opts) -->
939 {call_stack_arrow_atom_symbol(Symbol)}, [Symbol].
940
941 render_call(definition_call(Name,Pos),Opts) --> !,
942 ['within DEFINITION call '],[Name],
943 render_span(Pos,Opts).
944 render_call(operation_call(Op,Paras,Pos),Opts) --> !,
945 translate_b_operation_call(Op,Paras), % TODO: limit size?
946 render_span(Pos,Opts).
947 render_call(using_state(Name,State),_Opts) --> !,
948 [Name], [' with state: '],
949 {get_cs_limit(Limit),translate_bstate_limited(State,Limit,Str)},
950 [Str].
951 render_call(after_event(OpTerm),_Opts) --> !,
952 ['after event: '],
953 {get_cs_limit(Limit),translate_event_with_limit(OpTerm,Limit,Str)},
954 [Str].
955 render_call(function_call(Fun,Paras,Pos),Opts) --> !,
956 render_function_call(Fun,Paras),
957 render_span(Pos,Opts).
958 render_call(b_operator_call(OP,Paras,Pos),Opts) --> !,
959 render_operator_arg(b_operator(OP,Paras)),
960 render_span(Pos,Opts).
961 render_call(id_equality_evaluation(ID,Kind,Pos),Opts) --> !,
962 ['equality for '],[Kind],[' '],[ID],
963 render_span(Pos,Opts).
964 render_call(b_operator_arg_evaluation(OP,PosNr,Args,Pos),Opts) --> !,
965 ['arg '],[PosNr],[' of '],
966 render_operator_arg(b_operator(OP,Args)),
967 render_span(Pos,Opts).
968 render_call(external_call(Name,Paras,Pos),Opts) --> !,
969 ['external call '], [Name],['('],
970 {get_cs_limit(Limit),translate_bvalues_with_limit(Paras,Limit,PS)},[PS], [')'],
971 render_span(Pos,Opts).
972 render_call(prob_command_context(Name,Pos),Opts) --> !,
973 ['checking '], render_prob_command(Name),
974 render_span(Pos,Opts).
975 render_call(quantifier_call(comprehension_set,ParaNames,ParaValues,Pos),Opts) --> % special case for lambda
976 {nth1(LPos,ParaNames,LambdaRes,RestParaNames),
977 is_lambda_result_name(LambdaRes,_),
978 nth1(LPos,ParaValues,LambdaVal,RestParaValues)},!, % we have found '_lambda_res_' amongst paras
979 render_quantifier(lambda), ['('],
980 render_paras(RestParaNames,RestParaValues),
981 ['|'], render_para_val(LambdaVal),
982 [')'],
983 render_span(Pos,Opts).
984 render_call(quantifier_call(Kind,ParaNames,ParaValues,Pos),Opts) --> !,
985 render_quantifier(Kind), ['('],
986 render_paras(ParaNames,ParaValues), [')'],
987 render_span(Pos,Opts).
988 render_call(top_level_call(SpanPred),Opts) -->
989 render_call(SpanPred,Opts).
990 render_call(b_expr_call(Context,Expr),Opts) --> !,
991 [Context],[': '],
992 render_b_expr(Expr),
993 render_span(Expr,Opts).
994 render_call(b_subst_call(Context,Subst),Opts) --> !,
995 [Context],[': '],
996 render_b_subst(Subst),
997 render_span(Subst,Opts).
998 render_call(span_predicate(Pred,LS,S),Opts) --> % Pred can also be an expression like function/2
999 % infos could contain was(extended_expr(Op)); special case for: assertion_expression
1000 {Pred=b(_,_,Pos),
1001 b_compiler:b_compile(Pred,[],LS,S,CPred,no_wf_available) % inline actual parameters
1002 },
1003 !,
1004 render_b_expr(CPred),
1005 render_function_name(Pred), % try show function name from uncompiled Expr
1006 render_span(Pos,Opts).
1007 render_call(Other,_) --> [Other].
1008
1009 % get a brief description of call in call_stack
1010 render_call_short(after_event(OpTerm,_),R) :- !,R=OpTerm.
1011 render_call_short(using_state(Name,_),R) :- !,R=Name.
1012 render_call_short(definition_call(Name,_,_),R) :- !,R=Name.
1013 render_call_short(operation_call(Name,_,_),R) :- !,R=Name.
1014 render_call_short(function_call(Name,_,_),R) :- !,R=Name.
1015 render_call_short(b_operator_call(Name,_,_),R) :- !,R=Name.
1016 render_call_short(b_operator_arg_evaluation(Name,_,_,_),R) :- !,R=Name.
1017 render_call_short(external_call(Name,_,_),R) :- !,R=Name.
1018 render_call_short(prob_command_context(Name,_),R) :- !,R=Name.
1019 render_call_short(quantifier_call(Kind,_,_,_),R) :- !, R=Kind.
1020 render_call_short(top_level_call(_),R) :- !, R=top_level.
1021 render_call_short(E,F) :- functor(E,F,_).
1022
1023
1024 %render_operator(OP) -->
1025 % {(unicode_translation(OP,Unicode) -> FOP=Unicode ; FOP=OP)}, [FOP].
1026
1027 % render b operator arguments/calls:
1028 render_operator_arg(Var) --> {var(Var)},!,['_VARIABLE_']. % should not happen
1029 render_operator_arg(b_operator(OP,[Arg1,Arg2])) -->
1030 {binary_infix_in_mode(OP,Symbol,_,_)},!,
1031 {(unicode_translation(OP,Unicode) -> FOP=Unicode ; FOP=Symbol)}, %TODO: add parentheses if necessary
1032 render_operator_arg(Arg1),
1033 [' '],[FOP], [' '],
1034 render_operator_arg(Arg2).
1035 render_operator_arg(b_operator(OP,Args)) --> !,
1036 {(unicode_translation(OP,Unicode) -> FOP=Unicode ; function_like(OP,FOP) -> true ; FOP=OP)},
1037 [FOP], ['('],
1038 render_operator_args(Args),
1039 [')'].
1040 render_operator_arg(bind(Name,Value)) --> !,
1041 [Name], ['='],
1042 render_operator_arg(Value).
1043 render_operator_arg(identifier(ID)) --> !, [ID].
1044 render_operator_arg(Val) --> render_para_val(Val).
1045
1046 render_operator_args([]) --> !, [].
1047 render_operator_args([H]) --> !, render_operator_arg(H).
1048 render_operator_args([H|T]) --> !, render_operator_arg(H), [','], render_operator_args(T).
1049 render_operator_args(A) --> {add_internal_error('Not a list: ',A)}, ['???'].
1050
1051 render_prob_command(check_pred_command(PredKind,Arg)) --> !, ['predicate '], render_pred_nr(Arg), ['of '],[PredKind].
1052 render_prob_command(eval_expr_command(Kind,Arg)) --> !, ['expression '], render_pred_nr(Arg), ['of '],[Kind].
1053 render_prob_command(trace_replay(OpName,FromId)) --> !, ['Trace replay predicate for '],[OpName], [' from '],[FromId].
1054 render_prob_command(Cmd) --> [Cmd].
1055
1056 render_pred_nr(0) --> !. % 0 is special value to indicate we have no number/id within outer kind
1057 render_pred_nr(Nr) --> {number(Nr)},!,['# '],[Nr],[' '].
1058 render_pred_nr('') --> !.
1059 render_pred_nr(AtomId) --> ['for '], [AtomId],[' '].
1060
1061 render_function_name(b(function(Fun,_),_,_)) --> {try_get_identifier(Fun,FID)},!,
1062 % TODO: other means of extracting name; maybe we should render anything that is not a value?
1063 ['\n (Function applied: '], [FID], [')'].
1064 render_function_name(b(_,_,Infos)) --> {member(was(extended_expr(OpID)),Infos)},!,
1065 ['\n (Theory operator applied: '], [OpID], [')'].
1066 render_function_name(_) --> [].
1067
1068 try_get_identifier(Expr,Id) :- (get_texpr_id(Expr,Id) -> true ; get_was_identifier(Expr,Id)).
1069
1070 render_span(Span,Opts) --> {member(detailed,Opts),translate_span(Span,Atom), Atom \= ''},!,
1071 ['\n '], [Atom],
1072 ({member(additional_descr,Opts),translate_additional_description(Span,Descr)}
1073 -> [' within ',Descr]
1074 ; []).
1075 render_span(_,_) --> [].
1076
1077 render_function_call(Fun,Paras) -->
1078 {(atomic(Fun) -> FS=Fun ; translate_bexpr_for_call_stack(Fun,FS))}, % memoization will only register atomic name
1079 [FS],['('], render_para_val(Paras), [')'].
1080
1081 render_b_expr(b(function(Fun,Paras),_,_)) --> !, % ensure we print both function and paras at least partially
1082 {translate_bexpr_for_call_stack(Fun,FS)}, [FS],['('],
1083 {translate_bexpr_for_call_stack(Paras,PS)},[PS], [')'].
1084 render_b_expr(b(assertion_expression(Pred,Msg,b(value(_),string,_)),_,_)) --> !,
1085 % Body is not source of error; probably better to use special call stack entry for assertion_expression
1086 ['ASSERT '],[Msg],['\n '],
1087 {translate_bexpr_for_call_stack(Pred,PS)}, [PS].
1088 render_b_expr(CPred) --> {translate_bexpr_for_call_stack(CPred,PS)}, [PS].
1089
1090 translate_bexpr_for_call_stack(Expr,TS) :-
1091 get_cs_limit(Limit),
1092 translate_bexpr_with_limit_tl(Expr,Limit,TS).
1093
1094 render_b_subst(CPred) --> % TODO: try and fit this on a single line ?
1095 {get_cs_limit(Limit),translate_subst_or_bexpr_with_limit(CPred,Limit,PS)}, [PS].
1096
1097 % a variation to ensure that top-level operator is guaranteed to be shown
1098 % does not yet guarantee propert parentheses around arguments !
1099 % useful for showing call stack so that we at least see the operator and part of both args
1100 translate_bexpr_with_limit_tl(b(Special,pred,_),Limit,TS) :-
1101 special_binary_op(Special,LHS,RHS,Op),
1102 binary_infix_in_mode(Op,Trans,_Prio,_Assoc),
1103 !, Lim2 is (Limit+1)//2,
1104 translate_bexpression_with_limit(LHS,Lim2,TS1),
1105 translate_bexpression_with_limit(RHS,Lim2,TS2),
1106 ajoin([TS1,' ',Trans,' ',TS2],TS).
1107 translate_bexpr_with_limit_tl(Expr,Limit,TS) :-
1108 translate_bexpression_with_limit(Expr,Limit,TS).
1109
1110 special_binary_op(member(LHS,RHS),LHS,RHS,member).
1111 special_binary_op(not_member(LHS,RHS),LHS,RHS,not_member).
1112 special_binary_op(equal(LHS,RHS),LHS,RHS,equal).
1113 special_binary_op(not_equal(LHS,RHS),LHS,RHS,not_equal).
1114 special_binary_op(subset(LHS,RHS),LHS,RHS,subset).
1115 special_binary_op(subset_strict(LHS,RHS),LHS,RHS,subset_strict).
1116
1117 get_cs_limit(2000) :- !.
1118 get_cs_limit(Limit) :- debug_mode(on),!, debug_level(Level), % 19 regular, 5 very verbose
1119 Limit is 1000 - Level*20.
1120 get_cs_limit(200) :- get_preference(provide_trace_information,true),!.
1121 get_cs_limit(100).
1122
1123 get_cs_avl_limit(40) :- debug_mode(on),!.
1124 get_cs_avl_limit(6) :- get_preference(provide_trace_information,true),!.
1125 get_cs_avl_limit(4).
1126
1127 get_call_stack_span(operation_call(_,_,Pos),Pos).
1128 %get_call_stack_span(after_event(_),unknown).
1129 get_call_stack_span(function_call(_,_,Pos),Pos).
1130 get_call_stack_span(id_equality_evaluation(_ID,_Kind,Pos),Pos).
1131 get_call_stack_span(quantifier_call(_,_,_,Pos),Pos).
1132 get_call_stack_span(definition_call(_,Pos),Pos).
1133 get_call_stack_span(external_call(_,_,Pos),Pos).
1134 get_call_stack_span(prob_command_context(_,Pos),Pos).
1135 get_call_stack_span(top_level_call(Pos),Pos).
1136 get_call_stack_span(b_operator_call(_,_,Pos),Pos).
1137 get_call_stack_span(b_operator_arg_evaluation(_,_,_,Pos),Pos).
1138 get_call_stack_span(b_expr_call(_,Expr),Expr).
1139 get_call_stack_span(b_subst_call(_,Expr),Expr).
1140 get_call_stack_span(span_predicate(A,B,C),span_predicate(A,B,C)).
1141
1142 nop_call(top_level_call(X)) :- \+ is_top_level_function_call(X).
1143 % just there to insert virtual DEFINITION calls at top-level of call-stack
1144 is_top_level_function_call(span_predicate(b(Expr,_,_),_,_)) :-
1145 Expr = function(_,_),
1146 get_preference(provide_trace_information,false).
1147 % otherwise we push function_calls onto the stack; see opt_push_wait_flag_call_stack_info
1148
1149 % expand the call stack by creating entries for the definition calls
1150 split_calls([],[]).
1151 split_calls([Call|T],NewCalls) :- nop_call(Call),!, %write(nop(Call)),nl,
1152 split_calls(T,NewCalls).
1153 split_calls([Call|T],NewCalls) :-
1154 get_call_stack_span(Call,Span),!,
1155 NewCalls = [Call|New2],
1156 extract_def_calls(Span,New2,ST),
1157 split_calls(T,ST).
1158 split_calls([Call|T],[Call|ST]) :-
1159 split_calls(T,ST).
1160
1161 extract_def_calls(Span) -->
1162 {extract_pos_context(Span,MainPos,Context,CtxtPos)},
1163 {Context = definition_call(Name)},
1164 !,
1165 extract_def_calls(MainPos),
1166 [definition_call(Name,CtxtPos)],
1167 extract_def_calls(CtxtPos). % do we need this??
1168 extract_def_calls(_) --> [].
1169
1170 % a shorter version of extract_additional_description only accepting definition_calls
1171 translate_additional_description(Span,Desc) :-
1172 extract_pos_context(Span,MainPos,Context,CtxtPos),
1173 translate_span(CtxtPos,CtxtAtom),
1174 extract_def_context_msg(Context,OuterCMsg),
1175 (translate_additional_description(MainPos,InnerCMsg)
1176 -> ajoin([InnerCMsg,' within ',OuterCMsg, ' ', CtxtAtom],Desc)
1177 ; ajoin([OuterCMsg, ' ', CtxtAtom],Desc)
1178 ).
1179
1180 % try and get an immediate definition call context for a position
1181 get_definition_context_from_span(Span,DefCtxtMsg) :-
1182 extract_pos_context(Span,_MainPos,Context,_CtxtPos),
1183 extract_def_context_msg(Context,DefCtxtMsg).
1184
1185 extract_def_context_msg(definition_call(Name),Msg) :- !, % static Definition macro expansion call stack
1186 ajoin(['DEFINITION call of ',Name],Msg).
1187
1188 render_paras([],[]) --> !, [].
1189 render_paras([],_Vals) --> ['...?...']. % should not happen
1190 render_paras([Name],[Val]) --> !, render_para_name(Name), ['='], render_para_val(Val).
1191 render_paras([Name|TN],[Val|TV]) --> !,
1192 render_para_name(Name), ['='], render_para_val(Val), [','],
1193 render_paras(TN,TV).
1194 render_paras([N|Names],[]) --> !, render_para_name(N), render_paras(Names,[]). % value list can be empty
1195
1196 render_para_val(Val) --> {get_cs_limit(Limit),translate_bvalue_with_limit(Val,Limit,VS)}, [VS].
1197
1198 % accept typed and atomic ids
1199 render_para_name(b(identifier(ID),_,_)) --> !, {translated_identifier(ID,TID)},[TID].
1200 render_para_name(ID) --> {translated_identifier(ID,TID)},[TID].
1201
1202 render_quantifier(lambda) --> !, {unicode_translation(lambda,Symbol)},[Symbol]. % ['{|}'].
1203 render_quantifier(comprehension_set) --> !, ['{|}'].
1204 render_quantifier(comprehension_set(NegationContext)) --> !,
1205 render_negation_context(NegationContext), [' {|}'].
1206 render_quantifier(exists) --> !, {unicode_translation(exists,Symbol)},[Symbol].
1207 render_quantifier(let_quantifier) --> !, ['LET'].
1208 render_quantifier(optimize) --> !, ['#optimize'].
1209 render_quantifier(forall) --> !, {unicode_translation(forall,Symbol)},[Symbol].
1210 render_quantifier(not(Q)) --> !, {unicode_translation(negation,Symbol)}, % not(exists)
1211 [Symbol, '('], render_quantifier(Q), [')'].
1212 render_quantifier(Q) --> !, [Q].
1213
1214 render_negation_context(positive) --> !, ['one solution'].
1215 render_negation_context(negative) --> !, ['no solution'].
1216 render_negation_context(all_solutions) --> !,['all solutions'].
1217 render_negation_context(C) --> [C].
1218
1219 call_stack_arrow_atom_symbol(' \x2192\ '). % see total function
1220 %call_stack_arrow_atom_symbol('\x27FF\ '). % long rightwards squiggle arrow
1221
1222 % -----------------
1223
1224
1225 is_init('$initialise_machine').
1226 is_init('$setup_constants').
1227
1228 translate_bvalues_with_types(Values,Types,Output) :-
1229 %set_up_limit_reached(Codes,1000,LimitReached),
1230 pp_value_l_with_types(Values,',',Types,_LimitReached,Codes,[]),!,
1231 atom_codes_with_limit(Output,Codes).
1232 translate_bvalues_with_types(Values,T,Output) :-
1233 add_internal_error('Call failed: ',translate_bvalues_with_types(Values,T,Output)),
1234 translate_bvalues(Values,Output).
1235
1236 pp_value_l_with_types([],_Sep,[],_) --> !.
1237 pp_value_l_with_types([Expr|Rest],Sep,[TE|TT],LimitReached) -->
1238 ( {nonvar(Rest),Rest=[]} ->
1239 pp_value_with_type(Expr,TE,LimitReached)
1240 ;
1241 pp_value_with_type(Expr,TE,LimitReached),ppatom(Sep),
1242 pp_value_l_with_types(Rest,Sep,TT,LimitReached)).
1243
1244
1245 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1246
1247 % pretty-print properties
1248 translate_properties_with_limit([],[]).
1249 translate_properties_with_limit([P|Prest],[Out|Orest]) :-
1250 translate_property_with_limit(P,320,Out), % reduced limit as we now have evaluation view + possibility to inspect all of value
1251 translate_properties_with_limit(Prest,Orest).
1252
1253 translate_property_with_limit(Prop,Limit,Output) :-
1254 (pp_property(Prop,Limit,Output) -> true ; (add_error(translate_property,'Could not translate property: ',Prop),Output='???')).
1255 pp_property(Prop,Limit,Output) :-
1256 pp_property_without_plugin(Prop,Limit,Output).
1257 pp_property_without_plugin(=(Key,Value),_,A) :-
1258 !,ajoin([Key,' = ',Value],A).
1259 pp_property_without_plugin(':'(Key,Value),_,A) :-
1260 !,ajoin([Key,' : ',Value],A).
1261 pp_property_without_plugin(info(I),_,I) :- !.
1262 pp_property_without_plugin(Prop,Limit,A) :-
1263 write_to_codes(Prop,Codes),
1264 atom_codes_with_limit(A,Limit,Codes).
1265
1266
1267 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1268 :- use_module(tools_meta,[translate_term_into_atom_with_max_depth/3]).
1269
1270 % pretty-print errors belonging to a certain state
1271 translate_error_term(Term,S) :- translate_error_term(Term,unknown,S).
1272 translate_error_term(Var,_,S) :- var(Var),!,
1273 translate_term_into_atom_with_max_depth(Var,5,S).
1274 translate_error_term('@fun'(X,F),Span,S) :-
1275 translate_bvalue(X,TX),
1276 (get_function_from_span(Span,Fun,LocState,State),
1277 translate_bexpression_with_limit(Fun,200,TSF)
1278 -> % we managed to extract the function from the span_predicate
1279 (is_compiled_value(Fun)
1280 -> (get_was_identifier(Fun,WasFunId) -> Rest = [', function: ',WasFunId | Rest1]
1281 ; Rest = Rest1
1282 ),
1283 TVal=TSF % use as value
1284 ; Rest = [', function: ',TSF | Rest1],
1285 % try and extract value from span_predicate (often F=[] after traversing avl)
1286 (get_texpr_id(Fun,FID),
1287 (member(bind(FID,FVal),LocState) ; member(bind(FID,FVal),State))
1288 -> translate_bvalue(FVal,TVal)
1289 ; translate_bvalue(F,TVal)
1290 )
1291 )
1292 ; Rest=[], translate_bvalue(F,TVal)
1293 ),!,
1294 % translate_term_into_atom_with_max_depth('@fun'(TX,TVal),5,S).
1295 (get_error_span_for_value(F,NewSpanTxt) % triggers in test 953
1296 -> Rest1 = [' defined at ',NewSpanTxt]
1297 ; Rest1 = []
1298 ),
1299 ajoin(['Function argument: ',TX, ', function value: ',TVal | Rest],S).
1300 translate_error_term('@rel'(Arg,Res1,Res2),_,S) :-
1301 translate_bvalue(Arg,TA), translate_bvalue(Res1,R1),
1302 translate_bvalue(Res2,R2),!,
1303 ajoin(['Function argument: ',TA, ', two possible values: ',R1,', ',R2],S).
1304 translate_error_term([Op|T],_,S) :- T==[], nonvar(Op), Op=operation(Name,Env),
1305 translate_any_state(Env,TEnv), !,
1306 translate_term_into_atom_with_max_depth(operation(Name,TEnv),10,S).
1307 translate_error_term(error(E1,E2),_,S) :- !, translate_prolog_error(E1,E2,S).
1308 translate_error_term(b(B,T,I),_,S) :-
1309 translate_subst_or_bexpr_with_limit(b(B,T,I),1000,do_not_report_errors,S),!. % do not report errors, otherwise we end in an infinite loop of adding errors while adding errors
1310 translate_error_term([H|T],_,S) :- nonvar(H), H=b(_,_,_), % typically a list of typed ids
1311 E=b(sequence_extension([H|T]),any,[]),
1312 translate_subst_or_bexpr_with_limit(E,1000,do_not_report_errors,S),!.
1313 translate_error_term([H|T],_,S) :- nonvar(H), H=bind(_,_), % a store
1314 translate_bstate_limited([H|T],1000,S),!.
1315 translate_error_term(Term,_,S) :-
1316 is_bvalue(Term),
1317 translate_bvalue_with_limit(Term,1000,S),!.
1318 translate_error_term(T,_,S) :-
1319 (debug_mode(on) -> Depth = 20 ; Depth = 5),
1320 translate_term_into_atom_with_max_depth(T,Depth,S).
1321
1322 get_function_from_span(Var,Fun,_,_) :- var(Var), !,
1323 add_internal_error('Variable span:',get_function_from_span(Var,Fun)),fail.
1324 get_function_from_span(pos_context(Span,_,_),Fun,LS,S) :- get_function_from_span(Span,Fun,LS,S).
1325 get_function_from_span(span_predicate(b(function(Function,_Arg),_T,_I),LocalState,State),Function,LocalState,State).
1326
1327 is_compiled_value(b(value(_),_,_)).
1328
1329 get_was_identifier(b(_,_,Info),Id) :- member(was_identifier(Id),Info). % added e.g. by b_compiler
1330
1331 % TODO: complete this
1332 % for recognising B values as error terms and automatically translating them
1333 is_bvalue(V) :- var(V),!,fail.
1334 is_bvalue([]).
1335 is_bvalue(closure(_,_,_)).
1336 is_bvalue(fd(_,_)).
1337 is_bvalue(freetype(_)).
1338 is_bvalue(freeval(_,_,_)).
1339 is_bvalue(avl_set(_)).
1340 is_bvalue(int(_)).
1341 is_bvalue(global_set(_)).
1342 is_bvalue(pred_true).
1343 is_bvalue(pred_false).
1344 is_bvalue(string(_)).
1345 is_bvalue(term(_)). % typically term(floating(_))
1346 is_bvalue(rec(Fields)) :- nonvar(Fields), Fields=[F1|_], nonvar(F1),
1347 F1=field(_,V1), is_bvalue(V1).
1348 is_bvalue((A,B)) :-
1349 (nonvar(A) -> is_bvalue(A) ; true),
1350 (nonvar(B) -> is_bvalue(B) ; true).
1351
1352 % try and get error location for span:
1353 get_error_span_for_value(Var,_) :- var(Var),!,fail.
1354 get_error_span_for_value(closure(_,_,Body),Span) :- translate_span_with_filename(Body,Span), Span \= ''.
1355
1356
1357 % translate something that was caught with catch/3
1358 translate_prolog_exception(user_interrupt_signal,R) :- !, R='User-Interrupt (CTRL-C)'.
1359 translate_prolog_exception(enumeration_warning(_,_,_,_,_),R) :- !, R='Enumeration Warning'.
1360 translate_prolog_exception(error(E1,E2),S) :- !, translate_prolog_error(E1,E2,S).
1361 translate_prolog_exception(E1,S) :- translate_term_into_atom_with_max_depth(E1,8,S).
1362
1363 % translate a Prolog error(E1,E2) exception
1364 translate_prolog_error(existence_error(procedure,Pred),_,S) :- !,
1365 translate_term_into_atom_with_max_depth('Unknown Prolog predicate:'(Pred),8,S).
1366 translate_prolog_error(existence_error(source_sink,File),_,S) :- !,
1367 translate_term_into_atom_with_max_depth('File does not exist:'(File),8,S).
1368 translate_prolog_error(permission_error(Action,source_sink,File),_,S) :- !, % Action = open, ...
1369 ajoin(['Permission denied to ',Action,' the file: ',File],S).
1370 translate_prolog_error(permission_error(Action,past_end_of_stream,File),_,S) :- !, % Action = open, ...
1371 ajoin(['Permission denied to ',Action,' past end of file: ',File],S).
1372 translate_prolog_error(resource_error(memory),_,S) :- !,
1373 S = 'Resource error: Out of memory'. % GLOBALSTKSIZE=500M probcli ... could help ???
1374 translate_prolog_error(resource_error(file_handle),_,S) :- !,
1375 (debug_mode(on) -> print_open_stream_stats ; true),
1376 S = 'Resource error: Too many open files'.
1377 translate_prolog_error(system_error,system_error('SPIO_E_NET_CONNRESET'),S) :- !,
1378 S = 'System error: connection to process lost (SPIO_E_NET_CONNRESET)'.
1379 translate_prolog_error(system_error,system_error('SPIO_E_ENCODING_UNMAPPABLE'),S) :- !,
1380 S = 'System error: illegal character or encoding encountered (SPIO_E_ENCODING_UNMAPPABLE)'.
1381 translate_prolog_error(system_error,system_error('SPIO_E_NET_HOST_NOT_FOUND'),S) :- !,
1382 S = 'System error: could not find host (SPIO_E_NET_HOST_NOT_FOUND)'.
1383 translate_prolog_error(system_error,system_error('SPIO_E_CHARSET_NOT_FOUND'),S) :- !,
1384 S = 'System error: could not find character set encoding'.
1385 translate_prolog_error(system_error,system_error('SPIO_E_OS_ERROR'),S) :- !,
1386 S = 'System error due to some OS/system call (SPIO_E_OS_ERROR)'.
1387 translate_prolog_error(system_error,system_error('SPIO_E_END_OF_FILE'),S) :- !,
1388 S = 'System error: end of file (SPIO_E_END_OF_FILE)'.
1389 translate_prolog_error(system_error,system_error('SPIO_E_TOO_MANY_OPEN_FILES'),S) :- !,
1390 S = 'System error: too many open files (SPIO_E_TOO_MANY_OPEN_FILES)'.
1391 translate_prolog_error(system_error,system_error(dlopen(Msg)),S) :- !,
1392 translate_term_into_atom_with_max_depth(Msg,4,MS),
1393 ajoin(['System error: could not load dynamic library (you may have to right-click on the library and open it in the macOS Finder): ', MS],S).
1394 translate_prolog_error(system_error,system_error(Err),S) :- !,
1395 % or Err is an atom dlopen( mach-o file, but is an incompatible architecture ...)
1396 % E.g., SPIO_E_NOT_SUPPORTED when doing open('/usr',r,S)
1397 translate_term_into_atom_with_max_depth('System error:'(Err),8,S).
1398 translate_prolog_error(existence_error(procedure,Module:Pred/Arity),_,S) :- !,
1399 ajoin(['Prolog predicate does not exist: ',Module,':', Pred, '/',Arity],S).
1400 translate_prolog_error(instantiation_error,instantiation_error(Call,_ArgNo),S) :- !,
1401 translate_term_into_atom_with_max_depth('Prolog instantiation error:'(Call),8,S).
1402 translate_prolog_error(uninstantiation_error(_),uninstantiation_error(Call,_ArgNo,_Culprit),S) :- !,
1403 translate_term_into_atom_with_max_depth('Prolog uninstantiation error:'(Call),8,S).
1404 translate_prolog_error(evaluation_error(zero_divisor),evaluation_error(Call,_,_,_),S) :- !,
1405 translate_term_into_atom_with_max_depth('Division by zero error:'(Call),8,S).
1406 translate_prolog_error(evaluation_error(float_overflow),evaluation_error(Call,_,_,_),S) :- !,
1407 translate_term_into_atom_with_max_depth('Float overflow:'(Call),8,S).
1408 translate_prolog_error(representation_error(Err),representation_error(Call,_,_),S) :-
1409 memberchk(Err, ['CLPFD integer overflow','max_clpfd_integer','min_clpfd_integer']),!,
1410 translate_term_into_atom_with_max_depth('Prolog CLP(FD) overflow:'(Call),8,S).
1411 % TODO: domain_error(list_to_fdset(FDLIST,_989819),_,_,_L)
1412 translate_prolog_error(syntax_error(Err),_,S) :- !,
1413 translate_term_into_atom_with_max_depth('Prolog syntax error:'(Err),8,S).
1414 translate_prolog_error(_,resource_error(open(File,Mode,_),Kind),S) :- !, % Kind e.g. file_handle
1415 ajoin(['Resource error (',Kind,
1416 '), could not open file ',File,' in mode ',Mode],S).
1417 :- if(predicate_property(message_to_string(_, _), _)).
1418 translate_prolog_error(E1,E2,S) :-
1419 % SWI-Prolog way to translate an arbitrary message term (such as an exception) to a string,
1420 % the same way that the built-in message handling system would print it.
1421 message_to_string(error(E1,E2), String),
1422 !,
1423 atom_string(S, String).
1424 :- endif.
1425 translate_prolog_error(E1,_,S) :- translate_term_into_atom_with_max_depth(E1,8,S).
1426 % we also have permission_error, context_error, domain_error
1427
1428 portray_open_streams :- current_stream(File,Mode,Stream),
1429 format('~w file: ~w, stream: ~w~n',[Mode,File,Stream]),
1430 fail.
1431 portray_open_streams :- print_open_stream_stats.
1432
1433 :- use_module(tools_lists,[count_occurences/2]).
1434 print_open_stream_stats :- findall(Mode,current_stream(_,Mode,_),L),
1435 count_occurences(L,Occ),
1436 length(L,Nr), format('Open streams: ~w ~w~n',[Nr,Occ]).
1437
1438
1439 translate_state_errors([],[]).
1440 translate_state_errors([E|ERest],[Out|ORest]) :-
1441 ( E = eventerror(Event,EError,_) ->
1442 translate_event_error(EError,Msg),
1443 ajoin([Event,': ',Msg],Out)
1444 ; translate_state_error(E,Out) -> true
1445 ; functor(E,Out,_) ),
1446 translate_state_errors(ERest,ORest).
1447
1448 translate_error_context(E,TE) :- translate_error_context2(E,Codes,[]),
1449 atom_codes_with_limit(TE,Codes).
1450 translate_error_context2(span_context(Span,Context)) --> !,
1451 translate_error_context2(Context),
1452 translate_span(Span,only_subsidiary).
1453 translate_error_context2([H]) --> !,translate_error_context2(H).
1454 translate_error_context2(checking_invariant) --> !,
1455 {get_specification_description_codes(invariant,A)}, A. %"INVARIANT".
1456 translate_error_context2(checking_assertions) --> !,
1457 {get_specification_description_codes(assertions,A)}, A. %"ASSERTIONS".
1458 translate_error_context2(checking_negation_of_invariant(_State)) --> !,
1459 "not(INVARIANT)".
1460 translate_error_context2(operation(OpName,_State)) --> !,
1461 {translate_operation_name(OpName,TOp)},
1462 ppterm(TOp).
1463 translate_error_context2(checking_context(Check,Name)) --> !,
1464 ppterm(Check),ppterm(Name).
1465 translate_error_context2(loading_context(_Name)) --> !.
1466 translate_error_context2(visb_error_context(Class,SvgId,OpNameOrAttr,Span)) --> !,
1467 "VisB ", ppterm(Class), " for SVG ID ",
1468 ppterm(SvgId), " and attribute/event ",
1469 {translate_operation_name(OpNameOrAttr,TOp)},
1470 ppterm(TOp),
1471 translate_span(Span,only_subsidiary).
1472 translate_error_context2(X) --> "???:", ppterm(X).
1473
1474 print_span(Span) :- translate_span(Span,Atom), !, write(Atom).
1475 print_span(S) :- print(span(S)).
1476
1477 print_span_nl(Span) :- translate_span(Span,Atom), !,(Atom='' -> true ; write(Atom)),nl.
1478 print_span_nl(S) :- print(span(S)),nl.
1479
1480
1481 translate_span(Span,Atom) :- translate_span(Span,only_subsidiary,Codes,[]),
1482 atom_codes_with_limit(Atom,Codes).
1483 translate_span_with_filename(Span,Atom) :-
1484 translate_span(Span,always_print_filename,Codes,[]),
1485 atom_codes_with_limit(Atom,Codes).
1486
1487 translate_span(Span,_) --> {var(Span)},!, {add_internal_error('Variable span:',translate_span(Span,_))}, "_".
1488 translate_span(Span,PrintFileNames) --> {extract_line_col(Span,Srow,Scol,_Erow,_Ecol)},!,
1489 "(Line:",ppterm(Srow)," Col:",ppterm(Scol),
1490 %"-",ppterm(Erow),":",ppterm(Ecol),
1491 translate_span_file_opt(Span,PrintFileNames),
1492 % TO DO: print short version of extract_additional_description ?
1493 ")".
1494 translate_span(Span,_PrintFileNames) --> {extract_symbolic_label(Span,Label)},!, "(label @",ppterm(Label),")".
1495 translate_span(Span,PrintFileNames) -->
1496 % for Event-B, e.g., line-col fails but we can get a section/file name
1497 "(File:",translate_span_file(Span,PrintFileNames),!,")".
1498 translate_span(_,_PrintFileNames) --> "".
1499
1500 translate_span_file(Span,always_print_filename) -->
1501 {extract_tail_file_name(Span,Filename)},!,
1502 %{bmachine:b_get_main_filenumber(MainFN), Nr \= MainFN},!,
1503 " File:", ppterm(Filename).
1504 translate_span_file(Span,_) -->
1505 {extract_subsidiary_tail_file_name(Span,Filename)},
1506 %{bmachine:b_get_main_filenumber(MainFN), Nr \= MainFN},!,
1507 !,
1508 " File:", ppterm(Filename).
1509 translate_span_file_opt(Span,Print) --> translate_span_file(Span,Print),!.
1510 translate_span_file_opt(_,_) --> "".
1511
1512
1513 explain_span_file(Span) -->
1514 {extract_subsidiary_tail_file_name(Span,Filename)},
1515 %{bmachine:b_get_main_filenumber(MainFN), Nr \= MainFN},!,
1516 "\n### File: ", ppterm(Filename).
1517 explain_span_file(_) --> "".
1518
1519 explain_span(V) --> {var(V)},!, "Internal error: Illegal variable span".
1520 explain_span(span_predicate(Pred,LS,S)) --> !, explain_span2(span_predicate(Pred,LS,S)),
1521 explain_local_state(LS). %, explain_global_state(S).
1522 explain_span(Span) --> explain_span2(Span).
1523 explain_span2(Span) --> {extract_line_col(Span,Srow,Scol,Erow,Ecol)},!,
1524 "\n### Line: ", ppterm(Srow), ", Column: ", ppterm(Scol),
1525 " until Line: ", ppterm(Erow), ", Column: ", ppterm(Ecol),
1526 explain_span_file(Span),
1527 explain_span_context(Span).
1528 explain_span2(Span) --> {extract_symbolic_label_pos(Span,Msg)},!,
1529 "\n @label: ", ppterm(Msg),
1530 explain_span_context(Span).
1531 explain_span2(Span) --> explain_span_context(Span).
1532
1533 explain_span_context(Span) --> {extract_additional_description(Span,Msg),!},
1534 "\n### within ", ppterm(Msg). % context of span, such as definition call hierarchy
1535 explain_span_context(_) --> "".
1536
1537 explain_local_state([]) --> !, "".
1538 explain_local_state(LS) --> "\n Local State: ", pp_b_state(LS,1000).
1539 %explain_global_state([]) --> !, "".
1540 %explain_global_state(LS) --> "\n Global State: ", pp_b_state(LS).
1541
1542 translate_event_error(Error,Out) :-
1543 ( translate_event_error2(Error,Out) -> true
1544 ;
1545 functor(Error,F,_),
1546 ajoin(['** Unable to translate event error: ',F,' **'],Out)).
1547 translate_event_error2(no_witness_found(Type,Var,_Predicate),Out) :-
1548 def_get_texpr_id(Var,Id),
1549 ajoin(['no witness was found for ',Type,' ',Id],Out).
1550 translate_event_error2(simulation_error(_Events),Out) :-
1551 Out = 'no matching abstract event was found'.
1552 translate_event_error2(action_not_executable(_Action,WDErr),Out) :-
1553 (WDErr=wd_error_possible -> Out = 'action was not executable (maybe with WD error)'
1554 ; Out = 'action was not executable').
1555 translate_event_error2(invalid_modification(Var,_Pre,_Post),Out) :-
1556 def_get_texpr_id(Var,Id),
1557 ajoin(['modification of variable ', Id, ' not allowed'],Out).
1558 translate_event_error2(variant_negative(_CType,_Variant,_Value),Out) :-
1559 Out = 'enabled for negative variant'.
1560 translate_event_error2(invalid_variant(anticipated,_Expr,_Pre,_Post),Out) :-
1561 Out = 'variant increased'.
1562 translate_event_error2(invalid_variant(convergent,_Expr,_Pre,_Post),Out) :-
1563 Out = 'variant not decreased'.
1564 translate_event_error2(invalid_theorem_in_guard(_Theorem),Out) :-
1565 Out = 'theorem in guard evaluates to false'.
1566 translate_event_error2(event_wd_error(_TExpr,Source),Out) :-
1567 ajoin(['WD error for ',Source],Out).
1568 translate_event_error2(event_other_error(Msg),Out) :- Out=Msg.
1569
1570 translate_state_error(abort_error(_TYPE,Msg,ErrTerm,ErrorContext),Out) :- !,
1571 translate_error_term(ErrTerm,ES),
1572 translate_error_context(ErrorContext,EC),
1573 ajoin([EC,': ',Msg,': ',ES],Out).
1574 translate_state_error(clpfd_overflow_error(Context),Out) :- !, % 'CLPFD_integer_overflow'
1575 ajoin(['CLPFD integer overflow while ', Context],Out).
1576 translate_state_error(max_state_errors_reached(Nr),Out) :- !,
1577 ajoin(['Max. number of state errors reached: ', Nr],Out).
1578 translate_state_error(Unknown,Out) :-
1579 add_error(translate_state_error,'Unknown state error: ',Unknown),
1580 Out = '*** Unknown State Error ***'.
1581
1582
1583 get_span_from_context([H],Span) :- !, get_span_from_context(H,Span).
1584 get_span_from_context(span_context(Span,_),Res) :- !, Res=Span.
1585 get_span_from_context(_,unknown).
1586
1587 explain_error_context1([H]) --> !,explain_error_context1(H).
1588 explain_error_context1(span_context(Span,Context)) --> !,
1589 explain_span(Span),"\n",
1590 explain_error_context2(Context).
1591 explain_error_context1(Ctxt) --> explain_error_context2(Ctxt).
1592
1593 explain_error_context2([H]) --> !,explain_error_context2(H).
1594 explain_error_context2(span_context(Span,Context)) --> !,
1595 explain_span(Span),"\n",
1596 explain_error_context2(Context).
1597 explain_error_context2(checking_invariant) --> !,
1598 {get_specification_description_codes(invariant,I)}, I, ":\n ", %"INVARIANT:\n ",
1599 pp_current_state. % assumes explain is called in the right state ! ; otherwise we need to store the state id
1600 explain_error_context2(checking_assertions) --> !,
1601 {get_specification_description_codes(assertions,A)}, A, ":\n ", %"ASSERTIONS:\n ",
1602 pp_current_state. % assumes explain is called in the right state ! ; otherwise we need to store the state id
1603 explain_error_context2(checking_negation_of_invariant(State)) --> !,
1604 "not(INVARIANT):\n State: ",
1605 pp_b_state(State,1000).
1606 explain_error_context2(operation('$setup_constants',StateID)) --> !,
1607 {get_specification_description_codes(properties,P)}, P, ":\n State: ",
1608 pp_context_state(StateID).
1609 explain_error_context2(operation(OpName,StateID)) --> !,
1610 {get_specification_description_codes(operation,OP)}, OP, ": ", %"EVENT/OPERATION: ",
1611 {translate_operation_name(OpName,TOp)},
1612 ppterm(TOp), "\n ",
1613 pp_context_state(StateID).
1614 explain_error_context2(checking_context(Check,Name)) --> !,
1615 ppterm(Check),ppterm(Name), "\n ".
1616 explain_error_context2(loading_context(Name)) --> !,
1617 "Loading: ",ppterm(Name), "\n ".
1618 explain_error_context2(visb_error_context(Class,SvgId,OpNameOrAttr,Span)) --> !,
1619 translate_error_context2(visb_error_context(Class,SvgId,OpNameOrAttr,Span)).
1620 explain_error_context2(X) --> "UNKNOWN ERROR CONTEXT:\n ", ppterm(X).
1621
1622 :- use_module(specfile,[get_specification_description/2]).
1623 get_specification_description_codes(Tag,Codes) :- get_specification_description(Tag,Atom), atom_codes(Atom,Codes).
1624
1625 explain_state_error(Error,Span,Out) :-
1626 explain_state_error2(Error,Span,Out,[]),!.
1627 explain_state_error(_Error,unknown,"Sorry, the detailed output failed.\n").
1628
1629 explain_abort_error_type(well_definedness_error) --> !, "An expression was not well-defined.\n".
1630 explain_abort_error_type(card_overflow_error) --> !, "The cardinality of a finite set was too large to be represented.\n".
1631 explain_abort_error_type(while_variant_error) --> !, "A while-loop VARIANT error occurred.\n".
1632 explain_abort_error_type(while_invariant_violation) --> !, "A while-loop INVARIANT error occurred.\n".
1633 explain_abort_error_type(precondition_error) --> !, "A precondition (PRE) error occurred.\n".
1634 explain_abort_error_type(feasibility_error) --> !, "A feasibility error occurred.\n".
1635 explain_abort_error_type(assert_error) --> !, "An ASSERT error occurred.\n".
1636 explain_abort_error_type(Type) --> "Error occurred: ", ppterm(Type), "\n".
1637
1638 explain_state_error2(abort_error(TYPE,Msg,ErrTerm,ErrContext),Span) -->
1639 explain_abort_error_type(TYPE),
1640 "Reason: ", ppterm(Msg), "\n",
1641 {get_span_from_context(ErrContext,Span)},
1642 ({ErrTerm=''} -> ""
1643 ; "Details: ", {translate_error_term(ErrTerm,Span,ErrS)},ppterm(ErrS), "\n"
1644 ),
1645 "Context: ", explain_error_context1(ErrContext).
1646 explain_state_error2(max_state_errors_reached(Nr),unknown) -->
1647 "Too many error occurred for this state.\n",
1648 "Not all errors are shown.\n",
1649 "Number of errors is at least: ", ppterm(Nr).
1650 explain_state_error2(eventerror(_Event,Error,Trace),Span) --> % TO DO: also extract loc info ?
1651 {translate_event_error(Error,Msg)},
1652 ppatom(Msg),
1653 "\nA detailed trace containing the error:\n",
1654 "--------------------------------------\n",
1655 explain_event_trace(Trace,Span).
1656 explain_state_error2(clpfd_overflow_error(Context),unknown) --> % CLPFD_integer_overflow
1657 "An overflow occurred inside the CLP(FD) library.\n",
1658 "Context: ", ppterm(Context), "\n",
1659 "You may try and set the CLPFD preference to FALSE.\n".
1660
1661 % try and get span from state error:
1662 get_state_error_span(abort_error(_,_,_,Context),Span) :- get_span_context_span(Context,Span).
1663
1664 get_span_context_span(span_context(Span,_),Span).
1665 get_span_context_span([H],Span) :- get_span_context_span(H,Span).
1666
1667
1668
1669 show_parameter_values([],[]) --> !.
1670 show_parameter_values([P|Prest],[V|Vrest]) -->
1671 show_parameter_value(P,V),
1672 show_parameter_values(Prest,Vrest).
1673 show_parameter_value(P,V) -->
1674 " ",pp_expr(P,_,_LR)," = ",pp_value(V),"\n".
1675
1676 % translate an Event-B error trace (error occurred during multi-level animation)
1677 % into a textual description (Codes) and a span_predicate term which can be visualised
1678 explain_event_trace(Trace,Codes,Span) :-
1679 explain_event_trace(Trace,Span,Codes,[]).
1680
1681 explain_event_trace(Trace,span_predicate(SpanPred,[],[])) -->
1682 % evaluating the span predicate will require access to current state, which needs to be added later
1683 explain_event_trace4(Trace,'?','?',SpanPred).
1684
1685 explain_event_trace4([],_,_,b(truth,pred,[])) --> !.
1686 explain_event_trace4([event(Name,Section)|Trest],_,_,SpanPred) --> !,
1687 "\n",
1688 "Event ",ppterm(Name)," in model ",ppterm(Section),
1689 ":\n",
1690 % pass new current event name and section for processing tail:
1691 explain_event_trace4(Trest,Name,Section,SpanPred).
1692 explain_event_trace4([Step|Trest],Name,Section,SpanPred) -->
1693 "\n",
1694 ( explain_event_step4(Step,StepPred) -> ""
1695 ; {functor(Step,F,_)} ->
1696 " (no rule to explain event step ",ppatom(F),")\n"),
1697 explain_event_trace4(Trest,Name,Section,RestSpanPred),
1698 {combine_span_pred(StepPred,RestSpanPred,Name,Section,SpanPred)}.
1699
1700 % create a span predicate from the event error trace to display relevant values and predicates
1701 combine_span_pred(unknown,S,_,_,Res) :- !, Res=S.
1702 combine_span_pred(new_scope(Kind,Paras,Vals,P1),P2,Name,Section,Res) :- !,
1703 maplist(create_tvalue,Paras,Vals,TVals),
1704 add_span_label(Kind,Name,Section,P1,P1L),
1705 conjunct_predicates([P1L,P2],Body),
1706 (Paras=[] -> Res=Body ; Res = b(let_predicate(Paras,TVals,Body),pred,[])). % translate:print_bexpr(Res),nl.
1707 % we could also do: add_texpr_description
1708 combine_span_pred(P1,P2,_,_,Res) :-
1709 conjunct_predicates([P1,P2],Res).
1710
1711 add_span_label(Kind,Name,Section,Pred,NewPred) :-
1712 (Kind=[Label] -> true % already has position info
1713 ; create_label(Kind,Name,Section,Label)),
1714 add_labels_to_texpr(Pred,[Label],NewPred).
1715 create_label(Kind,Name,Section,Label) :- ajoin([Kind,' in ',Section,':',Name],Label).
1716
1717 create_tvalue(b(_,Type,_),Value,b(value(Value),Type,[])).
1718
1719 explain_event_step4(true_guard(Parameters,Values,Guard),new_scope('guard true',Parameters,Values,Guard)) --> !,
1720 ( {Parameters==[]} -> ""
1721 ; " for the parameters:\n",
1722 show_parameter_values(Parameters,Values)),
1723 " the guard is true:",
1724 explain_predicate(Guard,4),"\n".
1725 explain_event_step4(eval_witness(Type,Id,Value,Predicate),new_scope('witness',[Id],[Value],Predicate)) -->
1726 witness_intro(Id,Predicate,Type),
1727 " found witness:\n",
1728 " ", pp_expr(Id,_,_LR), " = ", pp_value(Value), "\n".
1729 explain_event_step4(simulation_error(Errors),SpanPred) -->
1730 " no guard of a refined event was satisfiable:\n",
1731 explain_simulation_errors(Errors,Guards),
1732 {disjunct_predicates(Guards,SpanPred)}.
1733 explain_event_step4(invalid_theorem_in_guard(Theorem),new_scope('false theorem',[],[],Theorem)) -->
1734 " the following theorem evaluates to false:",
1735 explain_predicate(Theorem,4),"\n".
1736 explain_event_step4(invalid_modification(Var,Pre,Post),
1737 new_scope('invalid modification',[Var],[Post],b(falsity,pred,[]))) -->
1738 " the variable ", pp_expr(Var,_,_LR), " has been modified.\n",
1739 " The event is not allowed to modify the variable because its abstract event does not modify it.\n",
1740 " Old value: ", pp_value(Pre), "\n",
1741 " New value: ", pp_value(Post), "\n".
1742 explain_event_step4(action_not_executable(TAction,WDErr),new_scope('action not executable',[],[],Equalities)) -->
1743 {exctract_span_pred_from_subst(TAction,Equalities)},
1744 explain_action_not_executable(TAction,WDErr).
1745 explain_event_step4(Step,unknown) -->
1746 explain_event_step(Step).
1747 % TODO: add span predicates for the errors below:
1748
1749 extract_equality(Infos,TID,NewExpr,b(equal(TID,NewExpr),pred,Infos)). % TODO: introduce TID' primed?
1750 exctract_span_pred_from_subst(b(assign(TIDs,Exprs),subst,Infos),SpanPred) :-
1751 maplist(extract_equality(Infos),TIDs,Exprs,List),
1752 conjunct_predicates(List,SpanPred).
1753 % todo: becomes_such, ...
1754
1755 explain_event_step(variant_checked_pre(CType,Variant,Value)) -->
1756 " ",ppatom(CType)," event: checking if the variant is non-negative:\n",
1757 " variant: ",pp_expr(Variant,_,_LR),"\n",
1758 " its value: ",pp_value(Value),"\n".
1759 explain_event_step(variant_negative(CType,Variant,Value)) -->
1760 explain_event_step(variant_checked_pre(CType,Variant,Value)),
1761 " ERROR: variant is negative\n".
1762 explain_event_step(variant_checked_post(CType,Variant,EntryValue,ExitValue)) -->
1763 " ",ppatom(CType)," event: checking if the variant is ",
1764 ( {CType==convergent} -> "decreased:\n" ; "not increased:\n"),
1765 " variant: ", pp_expr(Variant,_,_LR), "\n",
1766 " its value before: ", pp_value(EntryValue),"\n",
1767 " its value after: ", pp_value(ExitValue),"\n".
1768 explain_event_step(invalid_variant(CType,Variant,EntryValue,ExitValue)) -->
1769 explain_event_step(variant_checked_post(CType,Variant,EntryValue,ExitValue)),
1770 " ERROR: variant has ",
1771 ({CType==convergent} -> "not been decreased\n"; "has been increased\n").
1772 explain_event_step(no_witness_found(Type,Id,Predicate)) -->
1773 witness_intro(Id,Predicate,Type),
1774 " ERROR: no solution for witness predicate found!\n".
1775 explain_event_step(action(Lhs,_Rhs,Values)) -->
1776 " executing an action:\n",
1777 show_assignments(Lhs,Values).
1778 explain_event_step(action_set(Lhs,_Rhs,ValueSet,Values)) -->
1779 " executing an action:\n ",
1780 pp_expr_l(Lhs,_LR)," :: ",pp_value(ValueSet),"\n choosing\n",
1781 show_assignments(Lhs,Values).
1782 explain_event_step(action_pred(Ids,Pred,Values)) -->
1783 " executing an action:\n ",
1784 pp_expr_l(Ids,_LR1)," :| ",pp_expr(Pred,_,_LR2),"\n choosing\n",
1785 show_assignments(Ids,Values).
1786 explain_event_step(error(Error,_Id)) -->
1787 % the error marker serves to link to a stored state-error by its ID
1788 explain_event_step(Error).
1789 explain_event_step(event_wd_error(TExpr,Source)) -->
1790 " Well-Definedness ERROR for ", ppatom(Source), "\n",
1791 " ", pp_expr(TExpr,_,_LR), "\n".
1792 explain_event_step(event_other_error(Msg)) --> ppatom(Msg).
1793
1794 explain_action_not_executable(TAction,no_wd_error) --> {is_assignment_to(TAction,IDs)},!,
1795 " ERROR: the following assignment to ", ppatoms(IDs),"was not executable\n",
1796 " (probably in conflict with another assignment, check SIM or EQL PO):", % or WD error
1797 translate_subst_with_indention_and_label(TAction,4).
1798 explain_action_not_executable(TAction,wd_error_possible) --> !,
1799 " ERROR: the following action was not executable\n",
1800 " (possibly due to a WD error):",
1801 translate_subst_with_indention_and_label(TAction,4).
1802 explain_action_not_executable(TAction,_WDErr) -->
1803 " ERROR: the following action was not executable:",
1804 translate_subst_with_indention_and_label(TAction,4).
1805
1806 is_assignment_to(b(assign(LHS,_),_,_),IDs) :- get_texpr_ids(LHS,IDs).
1807 is_assignment_to(b(assign_single_id(LHS,_),_,_),IDs) :- get_texpr_ids([LHS],IDs).
1808
1809
1810 witness_intro(Id,Predicate,Type) -->
1811 " evaluating witness for abstract ", ppatom(Type), " ", pp_expr(Id,_,_LR1), "\n",
1812 " witness predicate: ", pp_expr(Predicate,_,_LR2), "\n".
1813
1814 show_assignments([],[]) --> !.
1815 show_assignments([Lhs|Lrest],[Val|Vrest]) -->
1816 " ",pp_expr(Lhs,_,_LimitReached), " := ", pp_value(Val), "\n",
1817 show_assignments(Lrest,Vrest).
1818
1819 /* unused at the moment:
1820 explain_state([]) --> !.
1821 explain_state([bind(Varname,Value)|Rest]) --> !,
1822 " ",ppterm(Varname)," = ",pp_value(Value),"\n",
1823 explain_state(Rest).
1824 explain_guards([]) --> "".
1825 explain_guards([Event|Rest]) -->
1826 {get_texpr_expr(Event,rlevent(Name,_Section,_Status,_Params,Guard,_Theorems,_Act,_VWit,_PWit,_Unmod,_Evt))},
1827 "\n",ppatom(Name),":",
1828 explain_predicate(Guard),
1829 explain_guards(Rest).
1830 explain_predicate(Guard,I,O) :-
1831 explain_predicate(Guard,2,I,O).
1832 */
1833 explain_predicate(Guard,Indention,I,O) :-
1834 pred_over_lines(0,'@grd',Guard,(Indention,I),(_,O)).
1835
1836 explain_simulation_errors([],[]) --> !.
1837 explain_simulation_errors([Error|Rest],[Grd|Gs]) -->
1838 explain_simulation_error(Error,Grd),
1839 explain_simulation_errors(Rest,Gs).
1840 explain_simulation_error(event(Name,Section,Guard),SpanPred) -->
1841 {add_span_label('guard false',Name,Section,Guard,SpanPred)},
1842 " guard for event ", ppatom(Name),
1843 " in ", ppatom(Section), ":",
1844 explain_predicate(Guard,6),"\n".
1845
1846
1847 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1848
1849 % explain a b_interpreter Classical B Path transition info
1850
1851 explain_transition_info(eventtrace(Trace),Codes) :- explain_event_trace(Trace,Codes,_Span).
1852 explain_transition_info(path(Trace),Codes) :- explain_classicb_path(Trace,0,Codes,[]).
1853
1854 explain_classicb_path(skip,I) --> indent_ws(I), "skip".
1855 explain_classicb_path(parallel(L),I) --> indent_ws(I), "BEGIN\n", {I1 is I+1}, explain_parallel(L,I1), " END".
1856 explain_classicb_path(sequence(A,B),I) --> explain_classicb_path(A,I), " ;\n", explain_classicb_path(B,I).
1857 explain_classicb_path(if_skip,I) --> indent_ws(I), "IF skipped (no branch applicable)".
1858 explain_classicb_path(if(CaseNr,Path),I) --> indent_ws(I), "IF branch ", ppnumber(CaseNr),"\n",
1859 {I1 is I+1}, explain_classicb_path(Path,I1).
1860 explain_classicb_path(pre(Cond,Path),I) --> indent_ws(I), "PRE ",
1861 {translate_bvalue_with_limit(Cond,50,CS),I1 is I+1}, ppatom(CS), " THEN\n", explain_classicb_path(Path,I1).
1862 explain_classicb_path(let(Path),I) --> indent_ws(I), "LET\n", {I1 is I+1}, explain_classicb_path(Path,I1).
1863 explain_classicb_path(assertion_violated,I) --> indent_ws(I), "ASSERT FALSE".
1864 explain_classicb_path(assertion(Path),I) --> indent_ws(I), "ASSERT TRUE THEN\n",
1865 {I1 is I+1}, explain_classicb_path(Path,I1).
1866 explain_classicb_path(witness(Path),I) --> indent_ws(I), "WITNESS TRUE THEN\n",
1867 {I1 is I+1}, explain_classicb_path(Path,I1).
1868 explain_classicb_path(any(_,Path),I) --> indent_ws(I), "ANY\n", {I1 is I+1}, explain_classicb_path(Path,I1).
1869 explain_classicb_path(var(Names,Path),I) --> indent_ws(I), "VAR ",
1870 {convert_and_ajoin_ids(Names,NS),I1 is I+1}, ppatom(NS), " IN\n", explain_classicb_path(Path,I1).
1871 explain_classicb_path(select(Nr,Path),I) --> indent_ws(I), "SELECT branch ", ppnumber(Nr), "\n",
1872 {I1 is I+1}, explain_classicb_path(Path,I1).
1873 explain_classicb_path(choice(Nr,Path),I) --> indent_ws(I), "CHOICE branch ", ppnumber(Nr), "\n",
1874 {I1 is I+1}, explain_classicb_path(Path,I1).
1875 explain_classicb_path(while(Variant,while_bpath(LoopCount,LastIterPath)),I) --> indent_ws(I), {translate_bvalue_with_limit(Variant,400,VS)},
1876 "WHILE (VARIANT = ", ppatom(VS), ", iterations=", ppnumber(LoopCount), ")",
1877 ({LastIterPath=none} -> ""
1878 ; " DO (last iteration)\n", {I1 is I+1}, explain_classicb_path(LastIterPath,I1)).
1879 explain_classicb_path(assign_single_id(ID,Value),I) --> {translate_bvalue_with_limit(Value,400,VS)},
1880 indent_ws(I), ppatom(ID), " := ", ppatom(VS).
1881 explain_classicb_path(assign(LHS,Vals),I) -->
1882 {translate_bexpression_with_limit(LHS,LS),translate_bvalues_with_limit(Vals,400,VS)},
1883 indent_ws(I), ppatom(LS), " := ", ppatom(VS).
1884 explain_classicb_path(becomes_element_of(LHS,Value),I) -->
1885 {translate_bexpression_with_limit(LHS,LS),translate_bvalue_with_limit(Value,400,VS)},
1886 indent_ws(I), ppatom(LS), " :: {", ppatom(VS), "}".
1887 explain_classicb_path(becomes_such(Names,Values),I) --> indent_ws(I),
1888 {convert_and_ajoin_ids(Names,NS),translate_bvalues_with_limit(Values,400,VS)},
1889 ppatom(NS), " : ( ", ppatom(VS)," )".
1890 explain_classicb_path(operation_call(Name,ResultNames,Paras,Results, IPath),I) --> indent_ws(I),
1891 {translate_bvalues_with_limit(Paras,400,PS)},
1892 ({Results=[_|_],translate_bvalues_with_limit(Results,400,RS),
1893 translate_bexpression_with_limit(ResultNames,RNS)}
1894 -> ppatom(RNS), " := ", ppatom(RS)," <-- " ; ""),
1895 ppatom(Name), "(", ppatom(PS), ") == BEGIN\n", explain_classicb_path(IPath,I), "\n",
1896 indent_ws(I), "END".
1897 explain_classicb_path(external_subst(Name),I) --> indent_ws(I), ppatom(Name).
1898 explain_classicb_path([H|T],I) -->
1899 {member(path(Path),[H|T]),I1 is I+1},!,explain_classicb_path(Path,I1). % inner path of operation_call
1900 explain_classicb_path(P,_I) --> {write(unknown_path(P)),nl}, "??".
1901
1902 explain_parallel([],_I) --> "".
1903 explain_parallel([H],I) --> !, explain_classicb_path(H,I).
1904 explain_parallel([H|T],I) --> explain_classicb_path(H,I), " ||\n", explain_parallel(T,I).
1905
1906 indent_ws(N) --> {N<1},!,"".
1907 indent_ws(L) --> " ", {L1 is L-1}, indent_ws(L1).
1908
1909
1910 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1911 % pretty-print a state
1912
1913
1914 print_state(State) :- b_state(State), !,print_bstate(State).
1915 print_state(csp_and_b_root) :- csp_with_bz_mode, !,
1916 write('(MAIN || B)').
1917 print_state(csp_and_b(CSPState,BState)) :- csp_with_bz_mode, !,
1918 print_bstate(BState), translate_cspm_state(CSPState,Text), write(Text).
1919 print_state(CSPState) :- csp_mode,!,translate_cspm_state(CSPState,Text), write(Text).
1920 print_state(State) :- animation_mode(xtl),!,translate_xtl_value(State,Text), write(Text).
1921 print_state(State) :- write('*** Unknown state: '),print(State).
1922
1923 b_state(root).
1924 b_state(concrete_constants(_)).
1925 b_state(const_and_vars(_,_)).
1926 b_state(expanded_const_and_vars(_,_,_,_)).
1927 b_state(expanded_vars(_,_)).
1928 b_state([bind(_,_)|_]).
1929 b_state([]).
1930
1931 print_bstate(State) :- print_bstate_limited(State,1000,-1).
1932 print_bstate_limited(State,VarLimit,OverallLimit) :-
1933 translate_bstate_limited(State,VarLimit,OverallLimit,Output),
1934 write(' '),write(Output).
1935
1936 translate_any_state(State,Output) :-
1937 get_pp_state_limit(Limit),
1938 pp_any_state(State,Limit,Codes,[]),
1939 atom_codes_with_limit(Output,Codes).
1940 translate_bstate(State,Output) :-
1941 get_pp_state_limit(Limit),
1942 pp_b_state(State,Limit,Codes,[]),
1943 atom_codes_with_limit(Output,Codes).
1944
1945 get_pp_state_limit(Limit) :-
1946 (get_preference(expand_avl_upto,-1) -> Limit = -1 ; Limit = 1000).
1947
1948 % a version which tries to generate smaller strings
1949 translate_bstate_limited(State,Output) :-
1950 temporary_set_preference(expand_avl_upto,2,CHNG),
1951 call_cleanup(translate_bstate_limited(State,200,Output),
1952 reset_temporary_preference(expand_avl_upto,CHNG)).
1953
1954 translate_bstate_limited(State,Limit,Output) :-
1955 translate_bstate_limited(State,Limit,Limit,Output).
1956 translate_bstate_limited(State,VarLimit,Limit,Output) :-
1957 pp_b_state(State,VarLimit,Codes,[]), % this limit VarLimit applies to every variable
1958 atom_codes_with_limit(Output,Limit,Codes). % Limit applies to the full translation
1959
1960 pp_b_state(X,Limit) --> try_pp_b_state(X,Limit),!.
1961 pp_b_state(X,_Limit) --> {add_error(pp_b_state,'Could not translate state: ',X)}.
1962
1963 % Limit is pretty-print limit for every value printed
1964 try_pp_b_state(VAR,_) --> {var(VAR)},!, "_?VAR?_", {add_error(pp_b_state,'Variable state: ',VAR)}.
1965 try_pp_b_state(root,_) --> !, "root".
1966 try_pp_b_state(concrete_constants(Constants),Limit) --> !,"Constants: ",
1967 pp_b_state(Constants,Limit).
1968 try_pp_b_state(const_and_vars(ID,Vars),Limit) --> !,
1969 "Constants:",ppterm(ID),", Vars:",
1970 {set_translation_constants(ID)}, /* extract constants which stand for deferred set elements */
1971 pp_b_state(Vars,Limit),
1972 {clear_translation_constants}.
1973 try_pp_b_state(expanded_const_and_vars(ID,Vars,_,_Infos),Limit) --> !, "EXPANDED ",
1974 try_pp_b_state(const_and_vars(ID,Vars),Limit).
1975 try_pp_b_state(expanded_vars(Vars,_Infos),Limit) --> !, "EXPANDED ",
1976 try_pp_b_state(Vars,Limit).
1977 try_pp_b_state([],_) --> !, "/* empty state */".
1978 try_pp_b_state([bind(Varname,Value)|Rest],Limit) --> !,
1979 "( ",ppterm(Varname),"=",
1980 dcg_set_up_limit_reached(Limit,LimitReached),
1981 pp_value(Value,LimitReached),
1982 ({Rest = []} -> []; " ",and_symbol,"\n "),
1983 pp_b_state_list(Rest,Limit).
1984
1985
1986 pp_b_state_list([],_) --> !, " )".
1987 pp_b_state_list([bind(Varname,Value)|Rest],Limit) --> !,
1988 ppterm(Varname),"=",
1989 dcg_set_up_limit_reached(Limit,LimitReached),
1990 pp_value(Value,LimitReached),
1991 ({Rest = []} -> [] ; " ",and_symbol,"\n "),
1992 pp_b_state_list(Rest,Limit).
1993 pp_b_state_list(X,_) --> {add_error(pp_b_state_list,'Could not translate: ',X)}.
1994
1995 % a version of pp which generates no newline; can be used for printing SETUP_CONSTANTS, INITIALISATION
1996 pp_b_state_comma_list([],_,_) --> !, ")".
1997 pp_b_state_comma_list(_,Cur,Limit) --> {Cur >= Limit}, !, "...".
1998 pp_b_state_comma_list([bind(Varname,Value)|Rest],Cur,Limit) --> !,
1999 %{write(c(Varname,Cur,Limit)),nl},
2000 start_size(Ref),
2001 ppterm(Varname),"=",
2002 pp_value(Value),
2003 ({Rest = []}
2004 -> ")"
2005 ; ",",
2006 end_size(Ref,Size), % compute size increase wrt Ref point
2007 {Cur1 is Cur+Size},
2008 pp_b_state_comma_list(Rest,Cur1,Limit)
2009 ).
2010 pp_b_state_comma_list(X,_,_) --> {add_error(pp_b_state_comma_list,'Could not translate: ',X)}.
2011
2012 start_size(X,X,X).
2013 end_size(RefVar,Len,X,X) :- % compute how many chars the dcg has added wrt start_size
2014 len(RefVar,X,Len).
2015 len(Var,X,Len) :- (var(Var) ; Var==X),!, Len=0.
2016 len([],_,0).
2017 len([_|T],X,Len) :- len(T,X,L1), Len is L1+1.
2018
2019 % can be used e.g. for setup_constants, initialise
2020 translate_b_state_to_comma_list_codes(FUNCTORCODES,State,Limit,ResCodes) :-
2021 pp_b_state_comma_list(State,0,Limit,Codes,[]),
2022 append("(",Codes,C0),
2023 append(FUNCTORCODES,C0,ResCodes).
2024
2025 % translate to a single line without newlines
2026 translate_b_state_to_comma_list(State,Limit,ResAtom) :-
2027 pp_b_state_comma_list(State,0,Limit,Codes,[]),
2028 append("(",Codes,C0),
2029 atom_codes(ResAtom,C0).
2030
2031 % ----------------
2032
2033 % printing and translating error contexts
2034 print_context(State) :- translate_context(State,Output), write(Output).
2035
2036 translate_context(Context,Output) :-
2037 pp_b_context(Context,Codes,[]),
2038 atom_codes_with_limit(Output,250,Codes).
2039
2040 pp_b_context([]) --> !.
2041 pp_b_context([C|Rest]) --> !,
2042 pp_b_context(C),
2043 pp_b_context(Rest).
2044 pp_b_context(translate_context) --> !, " ERROR CONTEXT: translate_context". % error occurred within translate_context
2045 pp_b_context(span_context(Span,Context)) --> !,
2046 pp_b_context(Context), " ", translate_span(Span,only_subsidiary).
2047 pp_b_context(operation(Name,StateID)) --> !,
2048 " ERROR CONTEXT: ",
2049 {get_specification_description_codes(operation,OP)}, OP, ":", % "OPERATION:"
2050 ({var(Name)} -> ppterm('ALL') ; {translate_operation_name(Name,TName)},ppterm(TName)),
2051 ",",pp_context_state(StateID).
2052 pp_b_context(checking_invariant) --> !,
2053 " ERROR CONTEXT: INVARIANT CHECKING,", pp_cur_context_state.
2054 pp_b_context(checking_negation_of_invariant(State)) --> !,
2055 " ERROR CONTEXT: NEGATION_OF_INVARIANT CHECKING, State:", pp_b_state(State,1000).
2056 pp_b_context(checking_assertions) --> !,
2057 " ERROR CONTEXT: ASSERTION CHECKING,", pp_cur_context_state.
2058 pp_b_context(checking_context(Check,Name)) --> !,
2059 " ERROR CONTEXT: ", ppterm(Check),ppterm(Name).
2060 pp_b_context(loading_context(_FName)) --> !.
2061 pp_b_context(unit_test_context(Module,TotNr,Line,Call)) --> !,
2062 " ERROR CONTEXT: Unit Test ", ppterm(TotNr), " in module ", ppterm(Module),
2063 " at line ", ppterm(Line), " calling ", pp_functor(Call).
2064 pp_b_context(visb_error_context(Class,ID,OpNameOrAttr,Span)) --> !,
2065 " ERROR CONTEXT: VisB ", ppterm(Class), " with ID ", ppterm(ID),
2066 ({OpNameOrAttr='all_attributes'} -> ""
2067 ; " and attribute/event ", ppterm(OpNameOrAttr)
2068 ),
2069 " ", translate_span(Span,only_subsidiary).
2070 pp_b_context(C) --> ppterm(C),pp_cur_context_state.
2071
2072 pp_functor(V) --> {var(V)},!, ppterm(V).
2073 pp_functor(T) --> {functor(T,F,N)}, ppterm(F),"/",ppterm(N).
2074
2075 pp_cur_context_state --> {state_space:get_current_context_state(ID)}, !,pp_context_state(ID).
2076 pp_cur_context_state --> ", unknown context state.".
2077
2078 % assumes we are in the right state:
2079 pp_current_state --> {state_space:current_expression(ID,_)}, !,pp_context_state(ID).
2080 pp_current_state --> ", unknown current context state.".
2081
2082 % TO DO: limit length/size of generated error description
2083 pp_context_state(ID) --> {state_space:visited_expression(ID,State)},!, % we have a state ID
2084 " State ID:", ppterm(ID),
2085 pp_context_state2(State).
2086 pp_context_state(State) --> pp_context_state3(State).
2087
2088 pp_context_state2(_) --> {debug_mode(off)},!.
2089 pp_context_state2(State) --> ",", pp_context_state3(State).
2090
2091 pp_context_state3(State) --> " State: ",pp_any_state_with_limit(State,10).
2092
2093 pp_any_state_with_limit(State,Limit) -->
2094 { get_preference(expand_avl_upto,CurLim),
2095 (CurLim<0 ; Limit < CurLim),
2096 !,
2097 temporary_set_preference(expand_avl_upto,Limit,CHNG),
2098 VarLimit is Limit*10
2099 },
2100 pp_any_state(State,VarLimit),
2101 {reset_temporary_preference(expand_avl_upto,CHNG)}.
2102 pp_any_state_with_limit(State,_Limit) -->
2103 {get_preference(expand_avl_upto,CurLim), VarLimit is CurLim*10},
2104 pp_any_state(State,VarLimit).
2105
2106 pp_any_state(X,VarLimit) --> try_pp_b_state(X,VarLimit),!.
2107 pp_any_state(csp_and_b(P,B),VarLimit) --> "CSP: ",{pp_csp_process(P,Atoms,[])},!,atoms_to_codelist(Atoms),
2108 " || B: ", try_pp_b_state(B,VarLimit).
2109 pp_any_state(X,_) --> {animation_mode(xtl)}, !, "XTL: ",pp_xtl_value(X). % XTL/CSP state
2110 pp_any_state(P,_) --> "CSP: ",{pp_csp_process(P,Atoms,[])},!,atoms_to_codelist(Atoms).
2111 pp_any_state(X,_) --> "Other formalism: ",ppterm(X). % CSP state
2112
2113 atoms_to_codelist([]) --> [].
2114 atoms_to_codelist([Atom|T]) --> ppterm(Atom), atoms_to_codelist(T).
2115
2116 % ----------------
2117
2118 :- dynamic deferred_set_constant/3.
2119
2120 set_translation_context(const_and_vars(ConstID,_)) :- !,
2121 %% print_message(setting_translation_constants(ConstID)),
2122 set_translation_constants(ConstID).
2123 set_translation_context(expanded_const_and_vars(ConstID,_,_,_)) :- !,
2124 set_translation_constants(ConstID).
2125 set_translation_context(_).
2126
2127 set_translation_constants(_) :- clear_translation_constants,
2128 get_preference(dot_print_use_constants,false),!.
2129 set_translation_constants(ConstID) :- var(ConstID),!,
2130 add_error(set_translation_constants,'Variable used as ConstID: ',ConstID).
2131 set_translation_constants(ConstID) :-
2132 state_space:visited_expression(ConstID,concrete_constants(ConstantsStore)),!,
2133 %% print_message(setting_constants(ConstID)),%%
2134 (treat_constants(ConstantsStore) -> true ; print_message(fail)).
2135 set_translation_constants(ConstID) :-
2136 add_error(set_translation_constants,'Unknown ConstID: ',ConstID).
2137
2138 clear_translation_constants :- %print_message(clearing),%%
2139 retractall(deferred_set_constant(_,_,_)).
2140
2141 treat_constants([]).
2142 treat_constants([bind(CstName,Val)|T]) :-
2143 ((Val=fd(X,GSet),b_global_deferred_set(GSet))
2144 -> (deferred_set_constant(GSet,X,_)
2145 -> true /* duplicate def of value */
2146 ; assertz(deferred_set_constant(GSet,X,CstName))
2147 )
2148 ; true
2149 ),
2150 treat_constants(T).
2151
2152
2153
2154 translate_bvalue_with_tlatype(Value,Type,Output) :-
2155 ( pp_tla_value(Type,Value,Codes,[]) ->
2156 atom_codes_with_limit(Output,Codes)
2157 ; add_error(translate_bvalue,'Could not translate TLA value: ',Value),
2158 Output='???').
2159
2160 pp_tla_value(function(_Type1,_Type2),[]) --> !,
2161 ppcodes("<<>>").
2162 pp_tla_value(function(integer,T2),avl_set(Set)) -->
2163 {convert_avlset_into_sequence(Set,Seq)}, !,
2164 pp_tla_with_sep("<< "," >>",",",T2,Seq).
2165 pp_tla_value(function(T1,T2),Set) -->
2166 {is_printable_set(Set,Values)},!,
2167 pp_tla_with_sep("(",")"," @@ ",function_value(T1,T2),Values).
2168 pp_tla_value(function_value(T1,T2),(L,R)) -->
2169 !,pp_tla_value(T1,L),":>",pp_tla_value(T2,R).
2170 pp_tla_value(set(Type),Set) -->
2171 {is_printable_set(Set,Values)},!,
2172 pp_tla_with_sep("{","}",",",Type,Values).
2173 pp_tla_value(tuple(Types),Value) -->
2174 {pairs_to_list(Types,Value,Values,[]),!},
2175 pp_tla_with_sep("<< "," >>",",",Types,Values).
2176 pp_tla_value(record(Fields),rec(FieldValues)) -->
2177 % TODO: Check if we can safely assume that Fields and FieldValues have the
2178 % same order
2179 !, {sort_tla_fields(Fields,FieldValues,RFields,RFieldValues)},
2180 pp_tla_with_sep("[","]",", ",RFields,RFieldValues).
2181 pp_tla_value(field(Name,Type),field(_,Value)) -->
2182 !, ppatom_opt_scramble(Name)," |-> ",pp_tla_value(Type,Value).
2183 pp_tla_value(_Type,Value) -->
2184 % fallback: use B's pretty printer
2185 pp_value(Value).
2186
2187 is_printable_set(avl_set(A),List) :- avl_domain(A,List).
2188 is_printable_set([],[]).
2189 is_printable_set([H|T],[H|T]).
2190
2191 pairs_to_list([_],Value) --> !,[Value].
2192 pairs_to_list([_|Rest],(L,R)) -->
2193 pairs_to_list(Rest,L),[R].
2194
2195
2196 sort_tla_fields([],_,[],[]).
2197 sort_tla_fields([Field|RFields],ValueFields,RFieldTypes,ResultValueFields) :-
2198 ( Field=field(Name,Type) -> true
2199 ; Field= opt(Name,Type) -> true),
2200 ( selectchk(field(Name,Value),ValueFields,RestValueFields),
2201 field_value_present(Field,Value,Result) ->
2202 % Found the field in the record value
2203 RFieldTypes = [field(Name,Type) |RestFields],
2204 ResultValueFields = [field(Name,Result)|RestValues],
2205 sort_tla_fields(RFields,RestValueFields,RestFields,RestValues)
2206 ;
2207 % didn't found the field in the value -> igore
2208 sort_tla_fields(RFields,ValueFields,RFieldTypes,ResultValueFields)
2209 ).
2210 field_value_present(field(_,_),RecValue,RecValue). % Obligatory fields are always present
2211 field_value_present(opt(_,_),OptValue,Value) :-
2212 % Optional fields are present if the field is of the form TRUE |-> Value.
2213 ( is_printable_set(OptValue,Values) -> Values=[(_TRUE,Value)]
2214 ;
2215 add_error(translate,'exptected set for TLA optional record field'),
2216 fail
2217 ).
2218
2219 pp_tla_with_sep(Start,End,Sep,Type,Values) -->
2220 ppcodes(Start),pp_tla_with_sep_aux(Values,End,Sep,Type).
2221 pp_tla_with_sep_aux([],End,_Sep,_Type) -->
2222 ppcodes(End).
2223 pp_tla_with_sep_aux([Value|Rest],End,Sep,Type) -->
2224 % If a single type is given, we interpret it as the type
2225 % for each element of the list, if it is a list, we interpret
2226 % it one different type for every value in the list.
2227 { (Type=[CurrentType|RestTypes] -> true ; CurrentType = Type, RestTypes=Type) },
2228 pp_tla_value(CurrentType,Value),
2229 ( {Rest=[_|_]} -> ppcodes(Sep); {true} ),
2230 pp_tla_with_sep_aux(Rest,End,Sep,RestTypes).
2231
2232
2233 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2234 % pretty-print a value
2235
2236 translate_bvalue_for_dot(string(S),Translation) :- !,
2237 % normal quotes confuse dot
2238 %ajoin(['''''',S,''''''],Translation).
2239 string_escape(S,ES),
2240 ajoin(['\\"',ES,'\\"'],Translation).
2241 translate_bvalue_for_dot(Val,ETranslation) :-
2242 translate_bvalue(Val,Translation),
2243 string_escape(Translation,ETranslation).
2244
2245 translate_bvalue_to_codes(V,Output) :-
2246 ( pp_value(V,_LimitReached,Codes,[]) ->
2247 Output=Codes
2248 ; add_error(translate_bvalue_to_codes,'Could not translate bvalue: ',V),
2249 Output="???").
2250 translate_bvalue_to_codes_with_limit(V,Limit,Output) :-
2251 set_up_limit_reached(Codes,Limit,LimitReached), % TODO: also limit expand_avl_upto
2252 ( pp_value(V,LimitReached,Codes,[]) ->
2253 Output=Codes
2254 ; add_error(translate_bvalue_to_codes,'Could not translate bvalue: ',V),
2255 Output="???").
2256
2257 translate_bvalue(V,Output) :-
2258 %set_up_limit_reached(Codes,1000000,LimitReached), % we could set a very high-limit, like max_atom_length
2259 ( pp_value(V,_LimitReached,Codes,[]) ->
2260 atom_codes_with_limit(Output,Codes) % just catches representation error
2261 ; add_error(translate_bvalue,'Could not translate bvalue: ',V),
2262 Output='???').
2263 :- use_module(preferences).
2264 translate_bvalue_with_limit(V,Limit,Output) :-
2265 get_preference(expand_avl_upto,Max),
2266 ((Max > Limit % no sense in printing larger AVL trees
2267 ; (Max < 0, Limit >= 0)) % or setting limit to -1 for full value
2268 -> temporary_set_preference(expand_avl_upto,Limit,CHNG)
2269 ; CHNG=false),
2270 call_cleanup(translate_bvalue_with_limit_aux(V,Limit,Output),
2271 reset_temporary_preference(expand_avl_upto,CHNG)).
2272 translate_bvalue_with_limit_aux(V,Limit,OutputAtom) :-
2273 set_up_limit_reached(Codes,Limit,LimitReached),
2274 ( pp_value(V,LimitReached,Codes,[]) ->
2275 atom_codes_with_limit(OutputAtom,Limit,Codes)
2276 % ,length(Codes,Len), (Len>Limit -> format('pp(~w) codes:~w, limit:~w, String=~s~n~n',[LimitReached,Len,Limit,Codes]) ; true)
2277 ; add_error(translate_bvalue_with_limit,'Could not translate bvalue: ',V),
2278 OutputAtom='???').
2279
2280 translate_bvalues(Values,Output) :-
2281 translate_bvalues_with_limit(Values,no_limit,Output). % we could set a very high-limit, like max_atom_length
2282
2283 translate_bvalues_with_limit(Values,Limit,Output) :-
2284 (Limit==no_limit -> true %
2285 ; set_up_limit_reached(Codes,Limit,LimitReached)
2286 ),
2287 pp_value_l(Values,',',LimitReached,Codes,[]),!,
2288 atom_codes_with_limit(Output,Codes).
2289 translate_bvalues_with_limit(Values,Limit,O) :-
2290 add_internal_error('Call failed: ',translate_bvalues(Values,Limit,O)), O='??'.
2291
2292 translate_bvalue_for_expression(Value,TExpr,Output) :-
2293 animation_minor_mode(tla),
2294 expression_has_tla_type(TExpr,TlaType),!,
2295 translate_bvalue_with_tlatype(Value,TlaType,Output).
2296 translate_bvalue_for_expression(Value,TExpr,Output) :-
2297 get_texpr_type(TExpr,Type),
2298 translate_bvalue_with_type(Value,Type,Output).
2299
2300 translate_bvalue_for_expression_with_limit(Value,TExpr,_Limit,Output) :-
2301 animation_minor_mode(tla),
2302 expression_has_tla_type(TExpr,TlaType),!,
2303 translate_bvalue_with_tlatype(Value,TlaType,Output). % TO DO: treat Limit
2304 translate_bvalue_for_expression_with_limit(Value,TExpr,Limit,Output) :-
2305 get_texpr_type(TExpr,Type),
2306 translate_bvalue_with_type_and_limit(Value,Type,Limit,Output).
2307
2308 expression_has_tla_type(TExpr,Type) :-
2309 get_texpr_info(TExpr,Infos),
2310 memberchk(tla_type(Type),Infos).
2311
2312
2313 translate_bvalue_to_parseable_classicalb(Val,Str) :-
2314 % corresponds to set_print_type_infos(needed)
2315 temporary_set_preference(translate_force_all_typing_infos,false,CHNG),
2316 temporary_set_preference(translate_print_typing_infos,true,CHNG2),
2317 (animation_minor_mode(X)
2318 -> remove_animation_minor_mode,
2319 call_cleanup(translate_bvalue_to_parseable_aux(Val,Str),
2320 (reset_temporary_preference(translate_force_all_typing_infos,CHNG),
2321 reset_temporary_preference(translate_print_typing_infos,CHNG2),
2322 set_animation_minor_mode(X)))
2323 ; call_cleanup(translate_bvalue_to_parseable_aux(Val,Str),
2324 (reset_temporary_preference(translate_force_all_typing_infos,CHNG),
2325 reset_temporary_preference(translate_print_typing_infos,CHNG2)))
2326 ).
2327 translate_bvalue_to_parseable_aux(Val,Str) :-
2328 call_pp_with_no_limit_and_parseable(translate_bvalue(Val,Str)).
2329
2330
2331 translate_bexpr_to_parseable(Expr,Str) :-
2332 call_pp_with_no_limit_and_parseable(translate_bexpression(Expr,Str)).
2333
2334 % a more refined pretty printing: takes Type information into account; useful for detecting sequences
2335 translate_bvalue_with_type(Value,_,Output) :- var(Value),!,
2336 translate_bvalue(Value,Output).
2337 translate_bvalue_with_type(Value,Type,Output) :-
2338 adapt_value_according_to_type(Type,Value,NewValue),
2339 translate_bvalue(NewValue,Output).
2340
2341 translate_bvalue_with_type_and_limit(Value,Type,Limit,Output) :-
2342 (Limit < 0 -> SetLim = -1 ; SetLim is Limit//2), % at least two symbols per element
2343 get_preference(expand_avl_upto,CurLim),
2344 ((CurLim < 0, SetLim >= 0) ; SetLim < CurLim),!,
2345 temporary_set_preference(expand_avl_upto,SetLim,CHNG),
2346 translate_bvalue_with_type_and_limit2(Value,Type,Limit,Output),
2347 reset_temporary_preference(expand_avl_upto,CHNG).
2348 translate_bvalue_with_type_and_limit(Value,Type,Limit,Output) :-
2349 translate_bvalue_with_type_and_limit2(Value,Type,Limit,Output).
2350 translate_bvalue_with_type_and_limit2(Value,_,Limit,Output) :- var(Value),!,
2351 translate_bvalue_with_limit(Value,Limit,Output).
2352 translate_bvalue_with_type_and_limit2(Value,Type,Limit,Output) :-
2353 adapt_value_according_to_type(Type,Value,NewValue),
2354 translate:translate_bvalue_with_limit(NewValue,Limit,Output).
2355 %debug:watch(translate:translate_bvalue_with_limit(NewValue,Limit,Output)).
2356
2357 :- use_module(avl_tools,[quick_avl_approximate_size/2]).
2358 adapt_value_according_to_type(_,Var,R) :- var(Var),!,R=Var.
2359 adapt_value_according_to_type(T,V,R) :- var(T),!,
2360 add_internal_error('Variable type: ',adapt_value_according_to_type(T,V,R)),
2361 R=V.
2362 adapt_value_according_to_type(integer,V,R) :- !,R=V.
2363 adapt_value_according_to_type(string,V,R) :- !,R=V.
2364 adapt_value_according_to_type(boolean,V,R) :- !,R=V.
2365 adapt_value_according_to_type(global(_),V,R) :- !,R=V.
2366 adapt_value_according_to_type(couple(TA,TB),(VA,VB),R) :- !, R=(RA,RB),
2367 adapt_value_according_to_type(TA,VA,RA),
2368 adapt_value_according_to_type(TB,VB,RB).
2369 adapt_value_according_to_type(set(Type),avl_set(A),Res) :- check_is_non_empty_avl(A),
2370 quick_avl_approximate_size(A,S),S<20,
2371 custom_explicit_sets:expand_custom_set_to_list(avl_set(A),List),!,
2372 maplist(adapt_value_according_to_type(Type),List,Res).
2373 adapt_value_according_to_type(set(_Type),V,R) :- !,R=V.
2374 adapt_value_according_to_type(seq(Type),V,R) :- !, % the type tells us it is a sequence
2375 (convert_set_into_sequence(V,VS)
2376 -> l_adapt_value_according_to_type(VS,Type,AVS),
2377 R=sequence(AVS)
2378 ; R=V).
2379 adapt_value_according_to_type(record(Fields),rec(Values),R) :- !,
2380 R=rec(AdaptedValues),
2381 % fields and values should be in the same (alphabetical) order
2382 maplist(adapt_record_field_according_to_type,Fields,Values,AdaptedValues).
2383 adapt_value_according_to_type(freetype(_),Value,R) :-
2384 Value = freeval(ID,_,Term),
2385 nonvar(Term), Term=term(ID), % not a constructor, just a value
2386 !,
2387 R = Value.
2388 adapt_value_according_to_type(freetype(_),freeval(ID,Case,SubValue),R) :- nonvar(Case),
2389 !,
2390 R = freeval(ID,Case,AdaptedSubValue),
2391 (kernel_freetypes:get_freeval_type(ID,Case,SubType)
2392 -> adapt_value_according_to_type(SubType,SubValue,AdaptedSubValue)
2393 ; write(could_not_get_freeval_type(ID,Case)),nl,
2394 AdaptedSubValue = SubValue
2395 ).
2396 adapt_value_according_to_type(freetype(_),Value,R) :- !, R=Value.
2397 adapt_value_according_to_type(any,Value,R) :- !, R=Value.
2398 adapt_value_according_to_type(pred,Value,R) :- !, R=Value.
2399 adapt_value_according_to_type(_,term(V),R) :- !, R=term(V). % appears for unknown values (no_value_for) when ALLOW_INCOMPLETE_SETUP_CONSTANTS is true
2400 adapt_value_according_to_type(Type,Value,R) :- write(adapt_value_according_to_type_unknown(Type,Value)),nl,
2401 R=Value.
2402
2403 l_adapt_value_according_to_type([],_Type,R) :- !,R=[].
2404 l_adapt_value_according_to_type([H|T],Type,[AH|AT]) :-
2405 adapt_value_according_to_type(Type,H,AH),
2406 l_adapt_value_according_to_type(T,Type,AT).
2407
2408 adapt_record_field_according_to_type(field(Name,HTy),field(Name,H),field(Name,R)) :-
2409 adapt_value_according_to_type(HTy,H,R).
2410
2411
2412 pp_value_with_type(E,T,LimitReached) --> {adapt_value_according_to_type(T,E,AdaptedE)},
2413 pp_value(AdaptedE,LimitReached).
2414
2415 pp_value(V,In,Out) :-
2416 set_up_limit_reached(In,1000,LimitReached),
2417 pp_value(V,LimitReached,In,Out).
2418
2419 % LimitReached is a flag: when it is grounded to limit_reached this instructs pp_value to stop generating output
2420 pp_value(_,LimitReached) --> {LimitReached==limit_reached},!, "...".
2421 pp_value(V,_) --> {var(V)},!, pp_variable(V).
2422 pp_value('$VAR'(N),_) --> !,pp_numberedvar(N).
2423 pp_value(fd(X,GSet),_) --> {var(X)},!,
2424 ppatom(GSet),":", ppnumber(X). %":??".
2425 pp_value(fd(X,GSet),_) -->
2426 {b_global_sets:is_b_global_constant_hash(GSet,X,Res)},!,
2427 pp_identifier(Res).
2428 pp_value(fd(X,GSet),_) --> {deferred_set_constant(GSet,X,Cst)},!,
2429 pp_identifier(Cst).
2430 pp_value(fd(X,M),_) --> !,ppatom_opt_scramble(M),ppnumber(X).
2431 pp_value(int(X),_) --> !,ppnumber(X).
2432 pp_value(term(floating(X)),_) --> !,ppnumber(X).
2433 pp_value(string(X),_) --> !,string_start_symbol,ppstring_opt_scramble(X),string_end_symbol.
2434 pp_value(global_set(X),_) --> {atomic(X),integer_set_mapping(X,Kind,Y)},!,
2435 ({Kind=integer_set} -> ppatom(Y) ; ppatom_opt_scramble(X)).
2436 pp_value(term(X),_) --> {var(X)},!,"term(",pp_variable(X),")".
2437 pp_value(freetype(X),_) --> {pretty_freetype(X,P)},!,ppatom_opt_scramble(P).
2438 pp_value(pred_true /* bool_true */,_) --> %!,"TRUE". % TO DO: in latex_mode: surround by mathit
2439 {constants_in_mode(pred_true,Symbol)},!,ppatom(Symbol).
2440 pp_value(pred_false /* bool_false */,_) --> %!,"FALSE".
2441 {constants_in_mode(pred_false,Symbol)},!,ppatom(Symbol).
2442 %pp_value(bool_true) --> !,"TRUE". % old version; still in some test traces which are printed
2443 %pp_value(bool_false) --> !,"FALSE".
2444 pp_value([],_) --> !,empty_set_symbol.
2445 pp_value(closure(Variables,Types,Predicate),LimitReached) --> !,
2446 pp_closure_value(Variables,Types,Predicate,LimitReached).
2447 pp_value(avl_set(A),LimitReached) --> !,
2448 {check_is_non_empty_avl(A),
2449 avl_size(A,Sz) % we could use quick_avl_approximate_size for large sets
2450 },
2451 {set_brackets(LBrace,RBrace)},
2452 ( {size_is_in_set_limit(Sz),
2453 %(Sz>2 ; get_preference(translate_print_all_sequences,true)),
2454 get_preference(translate_print_all_sequences,true), % no longer try and convert any sequence longer than 2 to sequence notation
2455 avl_max(A,(int(Sz),_)), % a sequence has minimum int(1) and maximum int(Sz)
2456 convert_avlset_into_sequence(A,Seq)} ->
2457 pp_sequence(Seq,LimitReached)
2458 ;
2459 ( {Sz=0} -> left_set_bracket," /* empty avl_set */ ",right_set_bracket
2460 ; {(size_is_in_set_limit(Sz) ; Sz < 3)} -> % if Sz 3 we will print at least two elements anyway
2461 {avl_domain(A,List)},
2462 ppatom(LBrace),pp_value_l(List,',',LimitReached),ppatom(RBrace)
2463 ; {(Sz<5 ; \+ size_is_in_set_limit(4))} ->
2464 {avl_min(A,Min),avl_max(A,Max)},
2465 hash_card_symbol, % "#"
2466 ppnumber(Sz),":", left_set_bracket,
2467 pp_value(Min,LimitReached),",",ldots,",",pp_value(Max,LimitReached),right_set_bracket
2468 ;
2469 {avl_min(A,Min),avl_next(Min,A,Nxt),avl_max(A,Max),avl_prev(Max,A,Prev)},
2470 hash_card_symbol, % "#",
2471 ppnumber(Sz),":", left_set_bracket,
2472 pp_value(Min,LimitReached),",",pp_value(Nxt,LimitReached),",",ldots,",",
2473 pp_value(Prev,LimitReached),",",pp_value(Max,LimitReached),right_set_bracket )).
2474 pp_value( (A,B) ,LimitReached) --> !,
2475 "(",pp_inner_value(A,LimitReached),
2476 maplet_symbol,
2477 pp_value(B,LimitReached),")".
2478 pp_value(field(Name,Value),LimitReached) --> !,
2479 pp_identifier(Name),":",pp_value(Value,LimitReached). % : for fields has priority 120 in French manual
2480 pp_value(rec(Rec),LimitReached) --> !,
2481 {function_like_in_mode(rec,Symbol)},
2482 ppatom(Symbol), "(",pp_value_l(Rec,',',LimitReached),")".
2483 pp_value(struct(Rec),LimitReached) --> !,
2484 {function_like_in_mode(struct,Symbol)},
2485 ppatom(Symbol), "(", pp_value_l(Rec,',',LimitReached),")".
2486 % check for cyclic after avl_set / closure case: AVL sets can be huge !
2487 pp_value(X,_) --> {cyclic_term(X),functor(X,F,_N)},!,
2488 underscore_symbol,"cyclic",underscore_symbol,
2489 ppatom(F),underscore_symbol.
2490 pp_value(sequence(List),LimitReached) --> !,
2491 ({List=[]} -> pp_empty_sequence ; pp_sequence_with_limit(List,LimitReached)).
2492 pp_value([Head|Tail],LimitReached) --> {get_preference(translate_print_all_sequences,true),
2493 convert_set_into_sequence([Head|Tail],Elements)},
2494 !,
2495 pp_sequence(Elements,LimitReached).
2496 pp_value([Head|Tail],LimitReached) --> !, {set_brackets(L,R)},
2497 ppatom(L),
2498 pp_value_l_with_limit([Head|Tail],',',LimitReached),
2499 ppatom(R).
2500 %pp_value([Head|Tail]) --> !,
2501 % {( convert_set_into_sequence([Head|Tail],Elements) ->
2502 % (Start,End) = ('[',']')
2503 % ;
2504 % Elements = [Head|Tail],
2505 % (Start,End) = ('{','}'))},
2506 % ppatom(Start),pp_value_l(Elements,','),ppatom(End).
2507 pp_value(term(no_value_for(Id)),_) --> !,
2508 "undefined ",ppatom(Id).
2509 pp_value(freeval(Freetype,Case,Value),LimitReached) --> !,
2510 ({ground(Case),ground(Value),Value=term(Case)} -> ppatom_opt_scramble(Case)
2511 ; {ground(Case)} -> ppatom_opt_scramble(Case),"(",pp_value(Value,LimitReached),")"
2512 ; {pretty_freetype(Freetype,P)},
2513 "FREEVALUE[",ppatom_opt_scramble(P),
2514 ",",write_to_codes(Case),
2515 "](",pp_value(Value,LimitReached),")"
2516 ).
2517 pp_value(X,_) --> {animation_mode(xtl)},!,
2518 write_to_codes(X).
2519 pp_value(X,_) --> % the << >> pose problems when checking against FDR
2520 "<< ",write_to_codes(X)," >>".
2521
2522 pp_variable(V) --> write_to_codes(V). %underscore_symbol.
2523
2524 :- use_module(closures,[is_recursive_closure/3]).
2525
2526 pp_closure_value(Ids,Type,B,_LimitReached) -->
2527 {var(Ids) ; var(Type) ; var(B)},!,
2528 add_internal_error('Illegal value: ',pp_value_illegal_closure(Ids,Type,B)),
2529 "<< ILLEGAL ",write_to_codes(closure(Ids,Type,B))," >>".
2530 pp_closure_value(Variables,Types,Predicate,LimitReached) --> {\+ size_is_in_set_limit(1)},
2531 !, % do not print body; just print hash value
2532 {make_closure_ids(Variables,Types,Ids), term_hash(Predicate,PH)},
2533 left_set_bracket, % { Ids | #PREDICATE#(HASH) }
2534 pp_expr_l_pair_in_mode(Ids,LimitReached),
2535 pp_such_that_bar,
2536 " ",hash_card_symbol,"PREDICATE",hash_card_symbol,"(",ppnumber(PH),") ", right_set_bracket.
2537 pp_closure_value(Variables,Types,Predicate,LimitReached) -->
2538 {get_preference(translate_ids_to_parseable_format,true),
2539 is_recursive_closure(Variables,Types,Predicate),
2540 get_texpr_info(Predicate,Infos),
2541 member(prob_annotation(recursive(TID)),Infos),
2542 def_get_texpr_id(TID,ID)}, !,
2543 % write recursive let for f as : CHOOSE(.) or MU({f|f= SET /*@desc letrec */ })
2544 % an alternate syntax could be RECLET f BE f = SET IN f END
2545 "MU({", pp_identifier(ID), "|",
2546 pp_identifier(ID)," = ",
2547 pp_closure_value2(Variables,Types,Predicate,LimitReached),
2548 "/*@desc letrec */ }) ".
2549 pp_closure_value([Id],[Type],Membership,LimitReached) -->
2550 { get_texpr_expr(Membership,member(Elem,Set)),
2551 get_texpr_id(Elem,Id),
2552 \+ occurs_in_expr(Id,Set), % detect things like {s|s : 1 .. card(s) --> T} (test 1030)
2553 get_texpr_type(Elem,Type),
2554 !},
2555 pp_expr_m(Set,299,LimitReached).
2556 pp_closure_value(Variables,Types,Predicate,LimitReached) --> pp_closure_value2(Variables,Types,Predicate,LimitReached).
2557
2558 pp_closure_value2(Variables,Types,Predicate,LimitReached) --> !,
2559 {make_closure_ids(Variables,Types,Ids)},
2560 pp_comprehension_set(Ids,Predicate,[],LimitReached). % TODO: propagate LimitReached
2561
2562 % avoid printing parentheses:
2563 % (x,y,z) = ((x,y),z)
2564 pp_inner_value( AB , LimitReached) --> {nonvar(AB),AB=(A,B)}, !, % do not print parentheses in this context
2565 pp_inner_value(A,LimitReached),maplet_symbol,
2566 pp_value(B,LimitReached).
2567 pp_inner_value( Value , LimitReached) --> pp_value( Value , LimitReached).
2568
2569 size_is_in_set_limit(Size) :- get_preference(expand_avl_upto,Max),
2570 (Max<0 -> true /* no limit */
2571 ; Size =< Max).
2572
2573 dcg_set_up_limit_reached(Limit,LimitReached,InList,InList) :- set_up_limit_reached(InList,Limit,LimitReached).
2574
2575 % instantiate LimitReached argument as soon as a list exceeds a certain limit
2576 set_up_limit_reached(_,Neg,_) :- Neg<0,!. % negative number means unlimited
2577 set_up_limit_reached(_,0,LimitReached) :- !, LimitReached = limit_reached.
2578 set_up_limit_reached(List,Limit,LimitReached) :-
2579 block_set_up_limit_reached(List,Limit,LimitReached).
2580 :- block block_set_up_limit_reached(-,?,?).
2581 block_set_up_limit_reached([],_,_).
2582 block_set_up_limit_reached([_|T],Limit,LimitReached) :-
2583 (Limit<1 -> LimitReached=limit_reached
2584 ; L1 is Limit-1, block_set_up_limit_reached(T,L1,LimitReached)).
2585
2586 % pretty print LimitReached, requires %:- block block_set_up_limit_reached(-,?,-).
2587 /*
2588 pp_lr(LR) --> {LR==limit_reached},!, " *LR* ".
2589 pp_lr(LR) --> {frozen(LR,translate:block_set_up_limit_reached(_,Lim,_))},!, " ok(", ppnumber(Lim),") ".
2590 pp_lr(LR) --> {frozen(LR,G)},!, " ok(", ppterm(G),") ".
2591 pp_lr(_) --> " ok ".
2592 */
2593
2594
2595 pp_value_l_with_limit(V,Sep,LimitReached) --> {get_preference(expand_avl_upto,Max)},
2596 pp_value_l(V,Sep,Max,LimitReached).
2597 pp_value_l(V,Sep,LimitReached) --> pp_value_l(V,Sep,-1,LimitReached).
2598
2599 pp_value_l(V,_Sep,_,_) --> {var(V)},!,"...".
2600 pp_value_l(_,_,_,LimitReached) --> {LimitReached==limit_reached},!,"...".
2601 pp_value_l('$VAR'(N),_Sep,_,_) --> !,"}\\/{",pp_numberedvar(N),"}".
2602 pp_value_l([],_Sep,_,_) --> !.
2603 pp_value_l([Expr|Rest],Sep,Limit,LimitReached) -->
2604 ( {nonvar(Rest),Rest=[]} ->
2605 pp_value(Expr,LimitReached)
2606 ; {Limit=0} -> "..."
2607 ;
2608 pp_value(Expr,LimitReached),
2609 % no separator for closure special case
2610 ({nonvar(Rest) , Rest = closure(_,_,_)} -> {true} ; ppatom(Sep)) ,
2611 {L1 is Limit-1} ,
2612 % convert avl_set(_) in a list's tail to a Prolog list
2613 {nonvar(Rest) , Rest = avl_set(_) -> custom_explicit_sets:expand_custom_set_to_list(Rest,LRest) ; LRest = Rest} ,
2614 pp_value_l(LRest,Sep,L1,LimitReached)).
2615 pp_value_l(avl_set(A),_Sep,_,LimitReached) --> pp_value(avl_set(A),LimitReached).
2616 pp_value_l(closure(A,B,C),_Sep,_,LimitReached) --> "}\\/", pp_value(closure(A,B,C),LimitReached).
2617
2618 make_closure_ids([],[],[]).
2619 make_closure_ids([V|Vrest],[T|Trest],[TExpr|TErest]) :-
2620 (var(V) -> V2='_', format('Illegal variable identifier in make_closure_ids: ~w~n',[V])
2621 ; V2=V),
2622 create_texpr(identifier(V2),T,[],TExpr),
2623 make_closure_ids(Vrest,Trest,TErest).
2624
2625 % symbol for starting and ending a sequence:
2626 pp_begin_sequence --> {animation_minor_mode(tla)},!,"<<".
2627 pp_begin_sequence --> {get_preference(translate_print_cs_style_sequences,true)},!,"".
2628 pp_begin_sequence --> "[".
2629 pp_end_sequence --> {animation_minor_mode(tla)},!,">>".
2630 pp_end_sequence --> {get_preference(translate_print_cs_style_sequences,true)},!,"".
2631 pp_end_sequence --> "]".
2632
2633 pp_separator_sequence('') :- get_preference(translate_print_cs_style_sequences,true),!.
2634 pp_separator_sequence(',').
2635
2636 % string for empty sequence
2637 pp_empty_sequence --> {animation_minor_mode(tla)},!, "<< >>".
2638 pp_empty_sequence --> {get_preference(translate_print_cs_style_sequences,true)},!,
2639 ( {latex_mode} -> "\\lambda" ; [955]). % 955 is lambda symbol in Unicode
2640 pp_empty_sequence --> {atelierb_mode(prover(_))},!, "{}".
2641 pp_empty_sequence --> "[]".
2642
2643 % symbols for function application:
2644 pp_function_left_bracket --> {animation_minor_mode(tla)},!, "[".
2645 pp_function_left_bracket --> "(".
2646
2647 pp_function_right_bracket --> {animation_minor_mode(tla)},!, "]".
2648 pp_function_right_bracket --> ")".
2649
2650 pp_sequence(Elements,LimitReached) --> {pp_separator_sequence(Sep)},
2651 pp_begin_sequence,
2652 pp_value_l(Elements,Sep,LimitReached),
2653 pp_end_sequence.
2654 pp_sequence_with_limit(Elements,LimitReached) --> {pp_separator_sequence(Sep)},
2655 pp_begin_sequence,
2656 pp_value_l_with_limit(Elements,Sep,LimitReached),
2657 pp_end_sequence.
2658
2659 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2660 % machines
2661
2662 :- use_module(eventhandling,[register_event_listener/3]).
2663 :- register_event_listener(clear_specification,reset_translate,
2664 'Reset Translation Caches.').
2665 reset_translate :- retractall(bugly_scramble_id_cache(_,_)), retractall(non_det_constants(_,_)).
2666 %reset_translate :- set_print_type_infos(none),
2667 % set_preference(translate_suppress_rodin_positions_flag,false).
2668
2669 suppress_rodin_positions(CHNG) :- set_suppress_rodin_positions(true,CHNG).
2670 set_suppress_rodin_positions(Value,CHNG) :-
2671 temporary_set_preference(translate_suppress_rodin_positions_flag,Value,CHNG).
2672 reset_suppress_rodin_positions(CHNG) :-
2673 reset_temporary_preference(translate_suppress_rodin_positions_flag,CHNG).
2674
2675 set_print_type_infos(none) :- !,
2676 set_preference(translate_force_all_typing_infos,false),
2677 set_preference(translate_print_typing_infos,false).
2678 set_print_type_infos(needed) :- !,
2679 set_preference(translate_force_all_typing_infos,false),
2680 set_preference(translate_print_typing_infos,true).
2681 set_print_type_infos(all) :- !,
2682 set_preference(translate_force_all_typing_infos,true),
2683 set_preference(translate_print_typing_infos,true).
2684 set_print_type_infos(Err) :-
2685 add_internal_error('Illegal typing setting: ',set_print_type_infos(Err)).
2686
2687 type_info_setting(none,false,false).
2688 type_info_setting(needed,false,true).
2689 type_info_setting(all,true,true).
2690
2691 set_print_type_infos(Setting,[CHNG1,CHNG2]) :-
2692 type_info_setting(Setting,Value1,Value2),!,
2693 temporary_set_preference(translate_force_all_typing_infos,Value1,CHNG1),
2694 temporary_set_preference(translate_print_typing_infos,Value2,CHNG2).
2695 set_print_type_infos(Err,_) :-
2696 add_internal_error('Illegal typing setting: ',set_print_type_infos(Err,_)),fail.
2697 reset_print_type_infos([CHNG1,CHNG2]) :-
2698 reset_temporary_preference(translate_force_all_typing_infos,CHNG1),
2699 reset_temporary_preference(translate_print_typing_infos,CHNG2).
2700
2701 :- use_module(tools_files,[put_codes/2]).
2702 print_machine(M) :-
2703 nl, translate_machine(M,Msg,true), put_codes(Msg,user_output), nl,
2704 flush_output(user_output),!.
2705 print_machine(M) :- add_internal_error('Printing failed: ',print_machine(M)).
2706
2707 %
2708 translate_machine(M,Codes,AdditionalInfo) :-
2709 retractall(print_additional_machine_info),
2710 (AdditionalInfo=true -> assertz(print_additional_machine_info) ; true),
2711 call_pp_with_no_limit_and_parseable(translate_machine1(M,(0,Codes),(_,[]))).
2712
2713 % perform a call by forcing parseable output and removing limit to set
2714 call_pp_with_no_limit_and_parseable(PP_Call) :-
2715 temporary_set_preference(translate_ids_to_parseable_format,true,CHNG),
2716 temporary_set_preference(expand_avl_upto,-1,CHNG2),
2717 call_cleanup(call(PP_Call),
2718 (reset_temporary_preference(translate_ids_to_parseable_format,CHNG),
2719 reset_temporary_preference(expand_avl_upto,CHNG2))).
2720
2721
2722 % useful if we wish to translate just a selection of sections without MACHINE/END
2723 translate_section_list(SL,Codes) :- init_machine_translation,
2724 translate_machine2(SL,SL,no_end,(0,Codes),(_,[])).
2725
2726 translate_machine1(machine(Name,Sections)) -->
2727 indent('MACHINE '), {adapt_machine_name(Name,AName), pp_identifier(AName,CName,[])}, insertcodes(CName),
2728 {init_machine_translation},
2729 translate_machine2(Sections,Sections,end).
2730 translate_machine2([],_,end) --> !, insertstr('\nEND\n').
2731 translate_machine2([],_,_) --> !, insertstr('\n').
2732 translate_machine2([P|Rest],All,End) -->
2733 translate_mpart(P,All),
2734 translate_machine2(Rest,All,End).
2735
2736 adapt_machine_name('dummy(uses)',R) :- !,R='MAIN'.
2737 adapt_machine_name(X,X).
2738
2739 :- dynamic section_header_generated/1.
2740 :- dynamic print_additional_machine_info/0.
2741 print_additional_machine_info.
2742
2743 init_machine_translation :- retractall(section_header_generated(_)).
2744
2745 % start a part of a section
2746 mpstart(Title,I) -->
2747 insertstr('\n'),insertstr(Title),
2748 indention_level(I,I2), {I2 is I+2}.
2749 % end a part of a section
2750 mpend(I) -->
2751 indention_level(_,I).
2752
2753 mpstart_section(Section,Title,AltTitle,I,In,Out) :-
2754 (\+ section_header_generated(Section)
2755 -> mpstart(Title,I,In,Out), assertz(section_header_generated(Section))
2756 ; mpstart(AltTitle,I,In,Out) /* use alternative title; section header already generated */
2757 ).
2758
2759 translate_mpart(Section/I,All) --> %{write(Section),nl},
2760 ( {I=[]} -> {true}
2761 ; translate_mpart2(Section,I,All) -> {true}
2762 ;
2763 insertstr('\nSection '),insertstr(Section),insertstr(': '),
2764 insertstr('<< pretty-print failed >>')
2765 ).
2766 translate_mpart2(deferred_sets,I,_) -->
2767 mpstart_section(sets,'SETS /* deferred */',' ; /* deferred */',P),
2768 indent_expr_l_sep(I,';'),mpend(P).
2769 translate_mpart2(enumerated_sets,_I,_) --> []. % these are now pretty printed below
2770 %mpstart('ENUMERATED SETS',P),indent_expr_l_sep(I,';'),mpend(P).
2771 translate_mpart2(enumerated_elements,I,_) --> %{write(enum_els(I)),nl},
2772 {translate_enums(I,[],Res)},
2773 mpstart_section(sets,'SETS /* enumerated */',' ; /* enumerated */',P),
2774 indent_expr_l_sep(Res,';'),mpend(P).
2775 translate_mpart2(parameters,I,_) --> mpstart('PARAMETERS',P),indent_expr_l_sep(I,','),mpend(P).
2776 translate_mpart2(internal_parameters,I,_) --> {print_additional_machine_info},!,
2777 mpstart('/* INTERNAL_PARAMETERS',P),indent_expr_l_sep(I,','),insertstr(' */'),mpend(P).
2778 translate_mpart2(internal_parameters,_I,_) --> [].
2779 translate_mpart2(abstract_variables,I,_) --> mpstart('ABSTRACT_VARIABLES',P),indent_exprs(I),mpend(P).
2780 translate_mpart2(concrete_variables,I,_) --> mpstart('CONCRETE_VARIABLES',P),indent_exprs(I),mpend(P).
2781 translate_mpart2(abstract_constants,I,_) --> mpstart('ABSTRACT_CONSTANTS',P),indent_exprs(I),mpend(P).
2782 translate_mpart2(concrete_constants,I,_) --> mpstart('CONCRETE_CONSTANTS',P),indent_exprs(I),mpend(P).
2783 translate_mpart2(promoted,I,_) --> {print_additional_machine_info},!,
2784 mpstart('/* PROMOTED OPERATIONS',P),indent_expr_l_sep(I,','),insertstr(' */'),mpend(P).
2785 translate_mpart2(promoted,_I,_) --> [].
2786 translate_mpart2(unpromoted,I,_) --> {print_additional_machine_info},!,
2787 mpstart('/* NOT PROMOTED OPERATIONS',P),indent_expr_l_sep(I,','),insertstr(' */'),mpend(P).
2788 translate_mpart2(unpromoted,_I,_) --> [].
2789 translate_mpart2(constraints,I,All) --> mpart_typing(constraints,[parameters],All,I).
2790 translate_mpart2(invariant,I,All) --> mpart_typing(invariant, [abstract_variables,concrete_variables],All,I).
2791 translate_mpart2(linking_invariant,_I,_) --> [].
2792 translate_mpart2(properties,I,All) --> mpart_typing(properties,[abstract_constants,concrete_constants],All,I).
2793 translate_mpart2(assertions,I,_) -->
2794 mpstart_spec_desc(assertions,P),
2795 %indent_expr_l_sep(I,';'),
2796 preds_over_lines(1,'@thm','; ',I),
2797 mpend(P). % TO DO:
2798 translate_mpart2(initialisation,S,_) --> mpstart_spec_desc(initialisation,P),translate_inits(S),mpend(P).
2799 translate_mpart2(definitions,Defs,_) --> {(standard_library_required(Defs,_) ; set_pref_used(Defs))},!,
2800 mpstart('DEFINITIONS',P),
2801 insertstr('\n'),
2802 {findall(Lib,standard_library_required(Defs,Lib),Libs)},
2803 insert_library_usages(Libs),
2804 translate_set_pref_defs(Defs),
2805 mpend(P),
2806 translate_other_defpart(Defs).
2807 translate_mpart2(definitions,Defs,_) --> !, translate_other_defpart(Defs).
2808 translate_mpart2(operation_bodies,Ops,_) --> mpstart_spec_desc(operations,P),translate_ops(Ops),mpend(P).
2809 translate_mpart2(used,Used,_) --> {print_additional_machine_info},!,
2810 mpstart('/* USED',P),translate_used(Used),insertstr(' */'),mpend(P).
2811 translate_mpart2(used,_Used,_) --> [].
2812 translate_mpart2(freetypes,Freetypes,_) -->
2813 mpstart('FREETYPES',P),translate_freetypes(Freetypes),mpend(P).
2814 translate_mpart2(meta,_Infos,_) --> [].
2815 translate_mpart2(operators,Operators,_) -->
2816 insertstr('\n/* Event-B operators:'), % */
2817 indention_level(I,I2), {I2 is I+2},
2818 translate_eventb_operators(Operators),
2819 indention_level(I2,I),
2820 insertstr('\n*/').
2821 translate_mpart2(values,Values,_) -->
2822 mpstart('VALUES',P),indent_expr_l_sep(Values,';'),mpend(P).
2823
2824 indent_exprs(I) --> {force_eventb_rodin_mode},!, indent_expr_l_sep(I,' '). % Event-B Camille style
2825 indent_exprs(I) --> indent_expr_l_sep(I,',').
2826
2827
2828 % Add typing predicates to a predicate
2829 mpart_typing(Title,Section,Sections,PredI) -->
2830 {mpart_typing2(Section,Sections,PredI,PredO)},
2831 ( {is_truth(PredO)} -> [] % TO DO: in animation_minor_mode(z) for INVARIANT: force adding typing predicates (translate_print_typing_infos)
2832 ;
2833 mpstart_spec_desc(Title,P),
2834 section_pred_over_lines(0,Title,PredO),
2835 mpend(P)).
2836
2837 mpstart_spec_desc(Title,P) --> {get_specification_description(Title,Atom)},!, mpstart(Atom,P).
2838 mpstart_spec_desc(Title,P) --> mpstart(Title,P).
2839
2840 mpart_typing2(Sections,AllSections,PredI,PredO) :-
2841 get_preference(translate_print_typing_infos,true),!,
2842 get_all_ids(Sections,AllSections,Ids),
2843 add_typing_predicates(Ids,PredI,PredO).
2844 mpart_typing2(_Section,_Sections,Pred,Pred).
2845
2846 get_all_ids([],_Sections,[]).
2847 get_all_ids([Section|Srest],Sections,Ids) :-
2848 memberchk(Section/Ids1,Sections),
2849 append(Ids1,Ids2,Ids),
2850 get_all_ids(Srest,Sections,Ids2).
2851
2852 add_optional_typing_predicates(Ids,In,Out) :-
2853 ( get_preference(translate_print_typing_infos,true) -> add_typing_predicates(Ids,In,Out)
2854 ; is_truth(In) -> add_typing_predicates(Ids,In,Out)
2855 ; In=Out).
2856
2857 add_normal_typing_predicates(Ids,In,Out) :- % used to call add_typing_predicates directly
2858 (add_optional_typing_predicates(Ids,In,Out) -> true
2859 ; add_internal_error('Failed: ',add_normal_typing_predicates(Ids)), In=Out).
2860
2861 add_typing_predicates([],P,P) :- !.
2862 add_typing_predicates(Ids,Pin,Pout) :-
2863 remove_already_typed_ids(Pin,Ids,UntypedIds),
2864 KeepSeq=false,
2865 generate_typing_predicates(UntypedIds,KeepSeq,Typing),
2866 conjunction_to_list(Pin,Pins),
2867 remove_duplicate_predicates(Typing,Pins,Typing2),
2868 append(Typing2,[Pin],Preds),
2869 conjunct_predicates(Preds,Pout).
2870
2871 remove_already_typed_ids(_TExpr,Ids,Ids) :-
2872 get_preference(translate_force_all_typing_infos,true),!.
2873 remove_already_typed_ids(TExpr,Ids,UntypedIds) :-
2874 get_texpr_expr(TExpr,Expr),!,
2875 remove_already_typed_ids2(Expr,Ids,UntypedIds).
2876 remove_already_typed_ids(TExpr,Ids,Res) :-
2877 add_internal_error('Not a typed expression: ',remove_already_typed_ids(TExpr,Ids,_)),
2878 Res=Ids.
2879 remove_already_typed_ids2(conjunct(A,B),Ids,UntypedIds) :- !,
2880 remove_already_typed_ids(A,Ids,I1),
2881 remove_already_typed_ids(B,I1,UntypedIds).
2882 remove_already_typed_ids2(lazy_let_pred(_,_,A),Ids,UntypedIds) :- !,
2883 remove_already_typed_ids(A,Ids,UntypedIds). % TO DO: check for variable clases with lazy_let ids ???
2884 remove_already_typed_ids2(Expr,Ids,UntypedIds) :-
2885 is_typing_predicate(Expr,Id),
2886 create_texpr(identifier(Id),_,_,TId),
2887 select(TId,Ids,UntypedIds),!.
2888 remove_already_typed_ids2(_,Ids,Ids).
2889 is_typing_predicate(member(A,_),Id) :- get_texpr_id(A,Id).
2890 is_typing_predicate(subset(A,_),Id) :- get_texpr_id(A,Id).
2891 is_typing_predicate(subset_strict(A,_),Id) :- get_texpr_id(A,Id).
2892
2893 remove_duplicate_predicates([],_Old,[]).
2894 remove_duplicate_predicates([Pred|Prest],Old,Result) :-
2895 (is_duplicate_predicate(Pred,Old) -> Result = Rest ; Result = [Pred|Rest]),
2896 remove_duplicate_predicates(Prest,Old,Rest).
2897 is_duplicate_predicate(Pred,List) :-
2898 remove_all_infos(Pred,Pattern),
2899 memberchk(Pattern,List).
2900
2901 :- use_module(typing_tools,[create_type_set/3]).
2902 generate_typing_predicates(TIds,Preds) :-
2903 generate_typing_predicates(TIds,true,Preds).
2904 generate_typing_predicates(TIds,KeepSeq,Preds) :-
2905 maplist(generate_typing_predicate(KeepSeq), TIds, Preds).
2906 generate_typing_predicate(KeepSeq,TId,Pred) :-
2907 get_texpr_type(TId,Type),
2908 remove_all_infos_and_ground(TId,TId2), % clear all infos
2909 (create_type_set(Type,KeepSeq,TSet) -> create_texpr(member(TId2,TSet),pred,[],Pred)
2910 ; TId = b(_,any,[raw]) -> is_truth(Pred) % this comes from transform_raw
2911 ; add_error(generate_typing_predicate,'Illegal type in identifier: ',Type,TId),
2912 is_truth(Pred)
2913 ).
2914
2915
2916
2917
2918 % translate enumerated constant list into enumerate set definition
2919 translate_enums([],Acc,Acc).
2920 translate_enums([EnumCst|T],Acc,Res) :- %get_texpr_id(EnumCst,Id),
2921 get_texpr_type(EnumCst,global(GlobalSet)),
2922 insert_enum_cst(Acc,EnumCst,GlobalSet,Acc2),
2923 translate_enums(T,Acc2,Res).
2924
2925 insert_enum_cst([],ID,Type,[enumerated_set_def(Type,[ID])]).
2926 insert_enum_cst([enumerated_set_def(Type,Lst)|T],ID,Type2,[enumerated_set_def(Type,Lst2)|TT]) :-
2927 (Type=Type2
2928 -> Lst2 = [ID|Lst], TT=T
2929 ; Lst2 = Lst, insert_enum_cst(T,ID,Type2,TT)
2930 ).
2931
2932 % pretty-print the initialisation section of a machine
2933 translate_inits(Inits) -->
2934 ( {is_list_simple(Inits)} ->
2935 translate_inits2(Inits)
2936 ;
2937 indention_level(I,I2),{I2 is I+2},
2938 translate_subst_begin_end(Inits),
2939 indention_level(_,I)).
2940 translate_inits2([]) --> !.
2941 translate_inits2([init(Name,Subst)|Rest]) -->
2942 indent('/* '),insertstr(Name),insertstr(': */ '),
2943 translate_subst_begin_end(Subst),
2944 translate_inits2(Rest).
2945
2946 translate_other_defpart(Defs) --> {print_additional_machine_info},!,
2947 mpstart('/* DEFINITIONS',P),translate_defs(Defs),insertstr(' */'),mpend(P).
2948 translate_other_defpart(_) --> [].
2949
2950 % pretty-print the definitions of a machine
2951 translate_defs([]) --> !.
2952 translate_defs([Def|Rest]) --> translate_def(Def),translate_defs(Rest).
2953 translate_def(definition_decl(Name,_DefType,_Pos,_Args,Expr,_Deps)) -->
2954 {dummy_def_body(Name,Expr)},!.
2955 % this is a DEFINITION from a standard library; do not show it
2956 translate_def(definition_decl(Name,DefType,_Pos,Args,Expr,_Deps)) -->
2957 {def_description(DefType,Desc)}, indent(Desc),insertstr(Name),
2958 {transform_raw_list(Args,TArgs)},
2959 translate_op_params(TArgs),
2960 ( {show_def_body(Expr)}
2961 -> insertstr(' '),{translate_in_mode(eqeq,'==',EqEqStr)}, insertstr(EqEqStr), insertstr(' '),
2962 {transform_raw(Expr,TExpr)},
2963 (translate_def_body(DefType,TExpr) -> [] ; insertstr('CANNOT PRETTY PRINT'))
2964 ; {true}
2965 ),
2966 insertstr(';').
2967 def_description(substitution,'SUBSTITUTION ').
2968 def_description(expression,'EXPRESSION ').
2969 def_description(predicate,'PREDICATE ').
2970 translate_def_body(substitution,B) --> translate_subst_begin_end(B).
2971 translate_def_body(expression,B) --> indent_expr(B).
2972 translate_def_body(predicate,B) --> indent_expr(B).
2973
2974 show_def_body(integer(_,_)).
2975 show_def_body(boolean_true(_)).
2976 show_def_body(boolean_false(_)).
2977 % show_def_body(_) % comment in to pretty print all defs
2978
2979 % check if we have a dummy definition body from a ProB Library file for external functions:
2980 dummy_def_body(Name,Expr) :-
2981 functor(Expr,F,_), (F=external_function_call ; F=external_pred_call),
2982 arg(2,Expr,Name).
2983 %external_function_declarations:external_function_library(Name,NrArgs,DefType,_),length(Args,NrArgs)
2984
2985 % utility to print definitions in JSON format for use in a VisB file:
2986 % useful when converting a B DEFINITIONS file for use in Event-B, TLA+,...
2987 :- public print_defs_as_json/0.
2988 print_defs_as_json :-
2989 findall(json([name=string(Name), value=string(BS)]), (
2990 bmachine:b_get_definition_with_pos(Name,expression,_DefPos,_Args,RawExpr,_Deps),
2991 \+ dummy_def_body(Name,RawExpr),
2992 transform_raw(RawExpr,Body),
2993 translate_subst_or_bexpr(Body,BS)
2994 ), Defs),
2995 Json = json([svg=string(''), definitions=array(Defs)]),
2996 json_write_stream(Json).
2997
2998 set_pref_used(Defs) :- member(definition_decl(Name,_,_,[],_,_),Defs),
2999 (is_set_pref_def_name(Name,_,_) -> true).
3000
3001 is_set_pref_def_name(Name,Pref,CurValAtom) :-
3002 atom_codes(Name,Codes),append("SET_PREF_",RestCodes,Codes),
3003 atom_codes(Pref,RestCodes),
3004 (eclipse_preference(Pref,P) -> get_preference(P,CurVal), translate_pref_val(CurVal,CurValAtom)
3005 ; deprecated_eclipse_preference(Pref,_,NewP,Mapping) -> get_preference(NewP,V), member(CurVal/V,Mapping)
3006 ; get_preference(Pref,CurVal), translate_pref_val(CurVal,CurValAtom)),
3007 translate_pref_val(CurVal,CurValAtom).
3008 translate_pref_val(true,'TRUE').
3009 translate_pref_val(false,'FALSE').
3010 translate_pref_val(Nr,NrAtom) :- number(Nr),!, number_codes(Nr,C), atom_codes(NrAtom,C).
3011 translate_pref_val(Atom,Atom) :- atom(Atom).
3012
3013 is_set_pref(definition_decl(Name,_,_Pos,[],_Expr,_Deps)) :-
3014 is_set_pref_def_name(Name,_,_).
3015 translate_set_pref_defs(Defs) -->
3016 {include(is_set_pref,Defs,SPDefs),
3017 sort(SPDefs,SortedDefs)},
3018 translate_set_pref_defs1(SortedDefs).
3019 translate_set_pref_defs1([]) --> !.
3020 translate_set_pref_defs1([Def|Rest]) -->
3021 translate_set_pref_def(Def),translate_set_pref_defs1(Rest).
3022 translate_set_pref_def(definition_decl(Name,_,_Pos,[],_Expr,_Deps)) -->
3023 {is_set_pref_def_name(Name,_Pref,CurValAtom)},!,
3024 insertstr(' '),insertstr(Name),
3025 insertstr(' '),
3026 {translate_in_mode(eqeq,'==',EqEqStr)}, insertstr(EqEqStr), insertstr(' '),
3027 insertstr(CurValAtom), % pretty print current value; Expr could be a more complicated non-atomic expression
3028 insertstr(';\n').
3029 translate_set_pref_def(_) --> [].
3030
3031 standard_library_required(Defs,Library) :-
3032 member(Decl,Defs),
3033 definition_decl_from_library(Decl,Library).
3034
3035 % TODO: we could also look in the list of loaded files and search for standard libraries
3036 definition_decl_from_library(definition_decl(printf,predicate,_,[_,_],_,_Deps),'LibraryIO.def').
3037 definition_decl_from_library(definition_decl('STRING_IS_DECIMAL',predicate,_,[_],_,_Deps),'LibraryStrings.def').
3038 definition_decl_from_library(definition_decl('SHA_HASH',expression,_,[_],_,_Deps),'LibraryHash.def').
3039 definition_decl_from_library(definition_decl('CHOOSE',expression,_,[_],_,_Deps),'CHOOSE.def').
3040 definition_decl_from_library(definition_decl('SCCS',expression,_,[_],_,_Deps),'SCCS.def').
3041 definition_decl_from_library(definition_decl('SORT',expression,_,[_],_,_Deps),'SORT.def').
3042 definition_decl_from_library(definition_decl('random_element',expression,_,[_],_,_Deps),'LibraryRandom.def').
3043 definition_decl_from_library(definition_decl('SIN',expression,_,[_],_,_Deps),'LibraryMath.def').
3044 definition_decl_from_library(definition_decl('RMUL',expression,_,[_,_],_,_Deps),'LibraryReals.def').
3045 definition_decl_from_library(definition_decl('REGEX_MATCH',predicate,_,[_,_],_,_Deps),'LibraryRegex.def').
3046 definition_decl_from_library(definition_decl('ASSERT_EXPR',expression,_,[_,_,_],_,_Deps),'LibraryProB.def').
3047 definition_decl_from_library(definition_decl('svg_points',expression,_,[_],_,_Deps),'LibrarySVG.def').
3048 definition_decl_from_library(definition_decl('FULL_FILES',expression,_,[_],_,_Deps),'LibraryFiles.def').
3049 definition_decl_from_library(definition_decl('READ_XML_FROM_STRING',expression,_,[_],_,_Deps),'LibraryXML.def').
3050 definition_decl_from_library(definition_decl('READ_CSV',expression,_,[_],_,_Deps),'LibraryCSV.def').
3051
3052 insert_library_usages([]) --> [].
3053 insert_library_usages([Library|T]) -->
3054 insertstr(' "'),insertstr(Library),insertstr('";\n'), % insert inclusion of ProB standard library
3055 insert_library_usages(T).
3056
3057 % ------------- RAW EXPRESSIONS
3058
3059 % try and print raw machine term or parts thereof (e.g. sections)
3060 print_raw_machine_terms(Var) :- var(Var), !,write('VAR !!'),nl.
3061 print_raw_machine_terms([]) :- !.
3062 print_raw_machine_terms([H|T]) :- !,
3063 print_raw_machine_terms(H), write(' '),
3064 print_raw_machine_terms(T).
3065 print_raw_machine_terms(Term) :- raw_machine_term(Term,String,Sub),!,
3066 format('~n~w ',[String]),
3067 print_raw_machine_terms(Sub),nl.
3068 print_raw_machine_terms(expression_definition(A,B,C,D)) :- !,
3069 print_raw_machine_terms(predicate_definition(A,B,C,D)).
3070 print_raw_machine_terms(substitution_definition(A,B,C,D)) :- !,
3071 print_raw_machine_terms(predicate_definition(A,B,C,D)).
3072 print_raw_machine_terms(expression(A,B,C,D)) :- !,
3073 print_raw_machine_terms(predicate_definition(A,B,C,D)).
3074 print_raw_machine_terms(predicate(A,B,C,D)) :- !,
3075 print_raw_machine_terms(predicate_definition(A,B,C,D)).
3076 print_raw_machine_terms(substitution(A,B,C,D)) :- !,
3077 print_raw_machine_terms(predicate_definition(A,B,C,D)).
3078 print_raw_machine_terms(predicate_definition(_,Name,Paras,RHS)) :-
3079 Paras==[],!,
3080 format('~n ~w == ',[Name]),
3081 print_raw_machine_terms(RHS),nl.
3082 print_raw_machine_terms(predicate_definition(_,Name,Paras,RHS)) :- !,
3083 format('~n ~w(',[Name]),
3084 print_raw_machine_terms_sep(Paras,','),
3085 format(') == ',[]),
3086 print_raw_machine_terms(RHS),nl.
3087 print_raw_machine_terms(operation(_,Name,Return,Paras,RHS)) :- !,
3088 format('~n ',[]),
3089 (Return=[] -> true
3090 ; print_raw_machine_terms_sep(Return,','),
3091 format(' <-- ',[])
3092 ),
3093 print_raw_machine_terms(Name),
3094 (Paras=[] -> true
3095 ; format(' (',[]),
3096 print_raw_machine_terms_sep(Paras,','),
3097 format(')',[])
3098 ),
3099 format(' = ',[]),
3100 print_raw_machine_terms(RHS),nl.
3101 print_raw_machine_terms(Term) :- print_raw_bexpr(Term).
3102
3103
3104 print_raw_machine_terms_sep([],_) :- !.
3105 print_raw_machine_terms_sep([H],_) :- !,
3106 print_raw_machine_terms(H).
3107 print_raw_machine_terms_sep([H|T],Sep) :- !,
3108 print_raw_machine_terms(H),write(Sep),print_raw_machine_terms_sep(T,Sep).
3109
3110 raw_machine_term(machine(M),'',M).
3111 raw_machine_term(generated(_,M),'',M).
3112 raw_machine_term(machine_header(_,Name,_Params),Name,[]). % TO DO: treat Params
3113 raw_machine_term(abstract_machine(_,_,Header,M),'MACHINE',[Header,M]).
3114 raw_machine_term(properties(_,P),'PROPERTIES',P).
3115 raw_machine_term(operations(_,P),'OPERATIONS',P).
3116 raw_machine_term(definitions(_,P),'DEFINITIONS',P).
3117 raw_machine_term(constants(_,P),'CONSTANTS',P).
3118 raw_machine_term(variables(_,P),'VARIABLES',P).
3119 raw_machine_term(invariant(_,P),'INVARIANT',P).
3120 raw_machine_term(assertions(_,P),'ASSERTIONS',P).
3121 raw_machine_term(constraints(_,P),'CONSTRAINTS',P).
3122 raw_machine_term(sets(_,P),'SETS',P).
3123 raw_machine_term(deferred_set(_,P),P,[]). % TO DO: enumerated_set ...
3124 %raw_machine_term(identifier(_,P),P,[]).
3125
3126 l_print_raw_bexpr([]).
3127 l_print_raw_bexpr([Raw|T]) :- write(' '),
3128 print_raw_bexpr(Raw),nl, l_print_raw_bexpr(T).
3129
3130 print_raw_bexpr(Raw) :- % a tool (not perfect) to print raw ASTs
3131 transform_raw(Raw,TExpr),!,
3132 print_bexpr_or_subst(TExpr).
3133
3134 translate_raw_bexpr(Raw,TS) :- transform_raw(Raw,TExpr), translate_subst_or_bexpr(TExpr,TS).
3135 translate_raw_bexpr_with_limit(Raw,Limit,TS) :- transform_raw(Raw,TExpr),
3136 translate_subst_or_bexpr_with_limit(TExpr,Limit,TS).
3137
3138 transform_raw_list(Var,Res) :- var(Var),!,
3139 add_internal_error('Var raw expression list:',transform_raw_list(Var,Res)),
3140 Res= [b(identifier('$$VARIABLE_LIST$$'),any,[raw])].
3141 transform_raw_list(Args,TArgs) :- l_transform_raw(Args,TArgs).
3142
3143 :- use_module(input_syntax_tree,[raw_symbolic_annotation/2]).
3144
3145 transform_raw(Var,Res) :- %write(raw(Var)),nl,
3146 var(Var), !, add_internal_error('Var raw expression:',transform_raw(Var,Res)),
3147 Res= b(identifier('$$VARIABLE$$'),any,[raw]).
3148 transform_raw(precondition(_,Pre,Body),Res) :- !, Res= b(precondition(TP,TB),subst,[raw]),
3149 transform_raw(Pre,TP),
3150 transform_raw(Body,TB).
3151 transform_raw(typeof(_,E,_Type),Res) :- !, transform_raw(E,Res). % remove typeof operator; TODO: transform
3152 transform_raw(identifier(_,M),Res) :- !, Res= b(identifier(M),any,[raw]).
3153 transform_raw(integer(_,M),Res) :- !, Res= b(integer(M),integer,[raw]).
3154 % rules from btype_rewrite2:
3155 transform_raw(integer_set(_),Res) :- !, generate_typed_int_set('INTEGER',Res).
3156 transform_raw(natural_set(_),Res) :- !, generate_typed_int_set('NATURAL',Res).
3157 transform_raw(natural1_set(_),Res) :- !, generate_typed_int_set('NATURAL1',Res).
3158 transform_raw(nat_set(_),Res) :- !, generate_typed_int_set('NAT',Res).
3159 transform_raw(nat1_set(_),Res) :- !, generate_typed_int_set('NAT1',Res).
3160 transform_raw(int_set(_),Res) :- !, generate_typed_int_set('INT',Res).
3161 transform_raw(let_expression(_,_Ids,Eq,Body),Res) :- !,
3162 transform_raw(conjunct(_,Eq,Body),Res). % TO DO: fix and generate let_expression(Ids,ListofExprs,Body)
3163 transform_raw(let_predicate(_,_Ids,Eq,Body),Res) :- !,
3164 transform_raw(conjunct(_,Eq,Body),Res). % ditto
3165 transform_raw(forall(_,Ids,Body),Res) :- !,
3166 (Body=implication(_,LHS,RHS) -> true ; LHS=truth,RHS=Body),
3167 transform_raw(forall(_,Ids,LHS,RHS),Res).
3168 transform_raw(record_field(_,Rec,identifier(_,Field)),Res) :- !, Res = b(record_field(TRec,Field),any,[]),
3169 transform_raw(Rec,TRec).
3170 transform_raw(rec_entry(_,identifier(_,Field),Rec),Res) :- !, Res = field(Field,TRec),
3171 transform_raw(Rec,TRec).
3172 transform_raw(conjunct(_,List),Res) :- !,
3173 ? transform_raw_list_to_conjunct(List,Res). % sometimes conjunct/1 with list is used (e.g., .eventb files)
3174 transform_raw(couple(_,L),Res) :- !, transform_raw_list_to_couple(L,Res). % couples are represented by lists
3175 transform_raw(extended_expr(Pos,Op,L,_TypeParas),Res) :- !,
3176 (L=[] -> transform_raw(identifier(none,Op),Res) % no arguments
3177 ; transform_raw(function(Pos,identifier(none,Op),L),Res)).
3178 transform_raw(extended_pred(Pos,Op,L,_TypeParas),Res) :- !,
3179 transform_raw(function(Pos,identifier(none,Op),L),Res). % not of correct type pred, but seems to work
3180 transform_raw(external_function_call_auto(Pos,Name,Para),Res) :- !,
3181 transform_raw(external_function_call(Pos,Name,Para),Res). % we assume expr rather than pred and hope for the best
3182 transform_raw(function(_,F,L),Res) :- !, transform_raw(F,TF),
3183 Res = b(function(TF,Args),any,[]),
3184 transform_raw_list_to_couple(L,Args). % args are represented by lists
3185 transform_raw(Atom,Res) :- atomic(Atom),!,Res=Atom.
3186 transform_raw([H|T],Res) :- !, l_transform_raw([H|T],Res).
3187 transform_raw(Symbolic,Res) :- raw_symbolic_annotation(Symbolic,Body),!,
3188 transform_raw(Body,Res).
3189 transform_raw(OtherOp,b(Res,Type,[])) :- OtherOp =..[F,_Pos|Rest],
3190 l_transform_raw(Rest,TRest),
3191 (get_type(F,FT) -> Type=FT ; Type=any),
3192 Res =.. [F|TRest].
3193 transform_raw_list_to_couple([R],Res) :- !, transform_raw(R,Res).
3194 transform_raw_list_to_couple([R1|T],Res) :- !, Res=b(couple(TR1,TT),any,[]),
3195 transform_raw(R1,TR1),transform_raw_list_to_couple(T,TT).
3196 transform_raw_list_to_conjunct([R],Res) :- !, transform_raw(R,Res).
3197 transform_raw_list_to_conjunct([R1|T],Res) :-
3198 transform_raw(R1,TR1),
3199 ? transform_raw_list_to_conjunct2(TR1,T,Res).
3200 transform_raw_list_to_conjunct2(Res,[],Res).
3201 transform_raw_list_to_conjunct2(Conj,[R1|T],Res) :-
3202 transform_raw(R1,TR1),
3203 Conj1 = b(conjunct(Conj,TR1),pred,[]), % conjunct from left to right, e.g. [A,B,C] -> conjunct(conjunct(A,B),C)
3204 ? transform_raw_list_to_conjunct2(Conj1,T,Res).
3205
3206 l_transform_raw([],[]).
3207 l_transform_raw([H|T],[RH|RT]) :- transform_raw(H,RH), l_transform_raw(T,RT).
3208
3209 generate_typed_int_set(Name,b(integer_set(Name),set(integer),[])).
3210 get_type(truth,pred).
3211 get_type(falsity,pred).
3212 get_type(conjunct,pred).
3213 get_type(disjunct,pred).
3214 get_type(forall,pred).
3215 get_type(equivalence,pred).
3216 get_type(exists,pred).
3217 get_type(implication,pred).
3218 get_type(equal,pred).
3219 get_type(not_equal,pred).
3220 get_type(member,pred).
3221 get_type(negation,pred).
3222 get_type(not_member,pred).
3223 get_type(subset,pred).
3224 get_type(subset_strict,pred).
3225 get_type(not_subset,pred).
3226 get_type(not_subset_strict,pred).
3227 get_type(less_equal,pred).
3228 get_type(less,pred).
3229 get_type(less_equal_real,pred).
3230 get_type(less_real,pred).
3231 get_type(greater_equal,pred).
3232 get_type(greater,pred).
3233 get_type(finite,pred).
3234 get_type(card,integer).
3235 get_type(size,integer).
3236 get_type(convert_int_floor,integer).
3237 get_type(convert_int_ceiling,integer).
3238 get_type(predecessor,integer).
3239 get_type(successor,integer).
3240 get_type(boolean_false,boolean).
3241 get_type(boolean_true,boolean).
3242 get_type(convert_bool,boolean).
3243 get_type(add_real,real).
3244 get_type(convert_real,real).
3245 get_type(div_real,real).
3246 get_type(max_real,real).
3247 get_type(min_real,real).
3248 get_type(minus_real,real).
3249 get_type(multiplication_real,real).
3250 get_type(power_of_real,real).
3251 get_type(real,real). % real literal
3252 get_type(string,string). % string literal
3253 get_type(bool_set,set(boolean)).
3254 get_type(float_set,set(real)).
3255 get_type(real_set,set(real)).
3256 get_type(string_set,set(real)).
3257
3258 get_type(any,subst).
3259 get_type(assertion,subst).
3260 get_type(assign,subst).
3261 get_type(becomes_element_of,subst).
3262 get_type(becomes_such,subst).
3263 get_type(case,subst).
3264 get_type(choice,subst).
3265 get_type(external_subst_call,subst).
3266 get_type(if,subst).
3267 get_type(let,subst).
3268 get_type(operation_call,subst).
3269 get_type(parallel,subst).
3270 get_type(precondition,subst).
3271 get_type(select,subst).
3272 get_type(sequence,subst).
3273 get_type(skip,subst).
3274 get_type(var,subst).
3275 get_type(while,subst).
3276 get_type(while1,subst).
3277 get_type(witness_then,subst).
3278
3279 :- assert_must_succeed((transform_raw(conjunct(none,[member(none,identifier(none,x),integer_set(none)),
3280 less_equal(none,identifier(none,y),integer(none,5)),
3281 equal(none,integer(none,1),integer(none,1))]),R),
3282 % check that list of conjunctions is transformed left-associatively
3283 R == b(conjunct(b(conjunct(b(member(b(identifier(x),any,[raw]),b(integer_set('INTEGER'),set(integer),[])),pred,[]),
3284 b(less_equal(b(identifier(y),any,[raw]),b(integer(5),integer,[raw])),pred,[])),pred,[]),
3285 b(equal(b(integer(1),integer,[raw]),b(integer(1),integer,[raw])),pred,[])),pred,[]))).
3286
3287 % -------------
3288
3289
3290 % pretty-print the operations of a machine
3291 translate_ops([]) --> !.
3292 translate_ops([Op|Rest]) -->
3293 translate_op(Op),
3294 ({Rest=[]} -> {true}; insertstr(';'),indent),
3295 translate_ops(Rest).
3296 translate_op(Op) -->
3297 { get_texpr_expr(Op,operation(Id,Res,Params,Body)) },
3298 translate_operation(Id,Res,Params,Body).
3299 translate_operation(Id,Res,Params,Body) -->
3300 indent,translate_op_results(Res),
3301 pp_expr_indent(Id),
3302 translate_op_params(Params),
3303 insertstr(' = '),
3304 indention_level(I1,I2),{I2 is I1+2,type_infos_in_subst(Params,Body,Body2)},
3305 translate_subst_begin_end(Body2),
3306 pp_description_pragma_of(Body2),
3307 indention_level(_,I1).
3308 translate_op_results([]) --> !.
3309 translate_op_results(Ids) --> pp_expr_indent_l(Ids), insertstr(' <-- ').
3310 translate_op_params([]) --> !.
3311 translate_op_params(Ids) --> insertstr('('),pp_expr_indent_l(Ids), insertstr(')').
3312
3313 translate_subst_begin_end(TSubst) -->
3314 {get_texpr_expr(TSubst,Subst),subst_needs_begin_end(Subst),
3315 create_texpr(block(TSubst),subst,[],Block)},!,
3316 translate_subst(Block).
3317 translate_subst_begin_end(Subst) -->
3318 translate_subst(Subst).
3319
3320 subst_needs_begin_end(assign(_,_)).
3321 subst_needs_begin_end(assign_single_id(_,_)).
3322 subst_needs_begin_end(parallel(_)).
3323 subst_needs_begin_end(sequence(_)).
3324 subst_needs_begin_end(operation_call(_,_,_)).
3325
3326 type_infos_in_subst([],Subst,Subst) :- !.
3327 type_infos_in_subst(Ids,SubstIn,SubstOut) :-
3328 get_preference(translate_print_typing_infos,true),!,
3329 type_infos_in_subst2(Ids,SubstIn,SubstOut).
3330 type_infos_in_subst(_Ids,Subst,Subst).
3331 type_infos_in_subst2(Ids,SubstIn,SubstOut) :-
3332 get_texpr_expr(SubstIn,precondition(P1,S)),!,
3333 get_texpr_info(SubstIn,Info),
3334 create_texpr(precondition(P2,S),pred,Info,SubstOut),
3335 add_typing_predicates(Ids,P1,P2).
3336 type_infos_in_subst2(Ids,SubstIn,SubstOut) :-
3337 create_texpr(precondition(P,SubstIn),pred,[],SubstOut),
3338 generate_typing_predicates(Ids,Typing),
3339 conjunct_predicates(Typing,P).
3340
3341
3342
3343
3344
3345 % pretty-print the internal section about included and used machines
3346 translate_used([]) --> !.
3347 translate_used([Used|Rest]) -->
3348 translate_used2(Used),
3349 translate_used(Rest).
3350 translate_used2(includeduse(Name,Id,NewTExpr)) -->
3351 indent,pp_expr_indent(NewTExpr),
3352 insertstr(' --> '), insertstr(Name), insertstr(':'), insertstr(Id).
3353
3354 % pretty-print the internal information about freetypes
3355 translate_freetypes([]) --> !.
3356 translate_freetypes([Freetype|Frest]) -->
3357 translate_freetype(Freetype),
3358 translate_freetypes(Frest).
3359 translate_freetype(freetype(Name,Cases)) -->
3360 {pretty_freetype(Name,PName)},
3361 indent(PName),insertstr('= '),
3362 indention_level(I1,I2),{I2 is I1+2},
3363 translate_freetype_cases(Cases),
3364 indention_level(_,I1).
3365 translate_freetype_cases([]) --> !.
3366 translate_freetype_cases([case(Name,Type)|Rest]) --> {nonvar(Type),Type=constant(_)},
3367 !,indent(Name),insert_comma(Rest),
3368 translate_freetype_cases(Rest).
3369 translate_freetype_cases([case(Name,Type)|Rest]) -->
3370 {pretty_type(Type,PT)},
3371 indent(Name),
3372 insertstr('('),insertstr(PT),insertstr(')'),
3373 insert_comma(Rest),
3374 translate_freetype_cases(Rest).
3375
3376 insert_comma([]) --> [].
3377 insert_comma([_|_]) --> insertstr(',').
3378
3379 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3380 % substitutions
3381
3382 translate_subst_or_bexpr(Stmt,String) :- get_texpr_type(Stmt,subst),!,
3383 translate_substitution(Stmt,String).
3384 translate_subst_or_bexpr(ExprOrPred,String) :-
3385 translate_bexpression(ExprOrPred,String).
3386
3387 translate_subst_or_bexpr_with_limit(Stmt,Limit,String) :-
3388 translate_subst_or_bexpr_with_limit(Stmt,Limit,report_errors,String).
3389 translate_subst_or_bexpr_with_limit(Stmt,_Limit,ReportErrors,String) :- get_texpr_type(Stmt,subst),!,
3390 translate_substitution(Stmt,String,ReportErrors). % TO DO: use limit
3391 translate_subst_or_bexpr_with_limit(ExprOrPred,Limit,ReportErrors,String) :-
3392 translate_bexpression_with_limit(ExprOrPred,Limit,ReportErrors,String).
3393
3394 print_subst(Stmt) :- translate_substitution(Stmt,T), write(T).
3395 translate_substitution(Stmt,String) :- translate_substitution(Stmt,String,report_errors).
3396 translate_substitution(Stmt,String,_) :-
3397 translate_subst_with_indention(Stmt,0,Codes,[]),
3398 (Codes = [10|C] -> true ; Codes=[13|C] -> true ; Codes=C), % peel off leading newline
3399 atom_codes_with_limit(String, C),!.
3400 translate_substitution(Stmt,String,report_errors) :-
3401 add_error(translate_substitution,'Could not translate substitution: ',Stmt),
3402 String='???'.
3403
3404 translate_subst_with_indention(TS,Indention,I,O) :-
3405 translate_subst(TS,(Indention,I),(_,O)).
3406 translate_subst_with_indention_and_label(TS,Indention,I,O) :-
3407 translate_subst_with_label(TS,(Indention,I),(_,O)).
3408
3409 translate_subst(TS) -->
3410 ( {get_texpr_expr(TS,S)} ->
3411 translate_subst2(S)
3412 ; translate_subst2(TS)).
3413
3414 translate_subst_with_label(TS) -->
3415 ( {get_texpr_expr(TS,S)} ->
3416 indent_rodin_label(TS), % pretty print substitution labels
3417 translate_subst2(S)
3418 ; translate_subst2(TS)).
3419
3420 % will print (first) rodin or pragma label indendent
3421 :- public indent_rodin_label/3.
3422 indent_rodin_label(_TExpr) --> {get_preference(translate_suppress_rodin_positions_flag,true),!}.
3423 indent_rodin_label(_TExpr) --> {get_preference(bugly_pp_scrambling,true),!}.
3424 indent_rodin_label(TExpr) --> {get_texpr_labels(TExpr,Names)},!, % note: this will only get the first label
3425 indent('/* @'),pp_ids_indent(Names),insertstr('*/ '). % this Camille syntax cannot be read back in by B parser
3426 indent_rodin_label(_TExpr) --> [].
3427
3428 pp_ids_indent([]) --> !, [].
3429 pp_ids_indent([ID]) --> !,pp_expr_indent(identifier(ID)).
3430 pp_ids_indent([ID|T]) --> !,pp_expr_indent(identifier(ID)), insertstr(' '),pp_ids_indent(T).
3431 pp_ids_indent(X) --> {add_error(pp_ids_indent,'Not a list of atoms: ',pp_ids_indent(X))}.
3432
3433 translate_subst2(Var) --> {var(Var)}, !, "_", {add_warning(translate_subst,'Variable subst:',Var)}.
3434 translate_subst2(skip) -->
3435 indent(skip).
3436 translate_subst2(operation(Id,Res,Params,Body)) --> translate_operation(Id,Res,Params,Body). % not really a substition that can appear normally
3437 translate_subst2(precondition(P,S)) -->
3438 indent('PRE '), pred_over_lines(2,'@grd',P), indent('THEN'), insert_subst(S), indent('END').
3439 translate_subst2(assertion(P,S)) -->
3440 indent('ASSERT '), pp_expr_indent(P), indent('THEN'), insert_subst(S), indent('END').
3441 translate_subst2(witness_then(P,S)) -->
3442 indent('WITNESS '), pp_expr_indent(P), indent('THEN'), insert_subst(S), indent('END').
3443 translate_subst2(block(S)) -->
3444 indent('BEGIN'), insert_subst(S), indent('END').
3445 translate_subst2(assign([L],[R])) --> !,
3446 indent,pp_expr_indent(L),insertstr(' := '),pp_expr_indent(R).
3447 translate_subst2(assign(L,R)) -->
3448 {(member(b(E,_,_),R), can_indent_expr(E)
3449 -> maplist(create_assign,L,R,ParAssigns))},!, % split into parallel assignments so that we can indent
3450 translate_subst2(parallel(ParAssigns)).
3451 translate_subst2(assign(L,R)) -->
3452 indent,pp_expr_indent_l(L),insertstr(' := '),pp_expr_indent_l(R).
3453 translate_subst2(assign_single_id(L,R)) -->
3454 translate_subst2(assign([L],[R])).
3455 translate_subst2(becomes_element_of(L,R)) -->
3456 indent,pp_expr_indent_l(L),insertstr(' :: '),pp_expr_indent(R).
3457 translate_subst2(becomes_such(L,R)) -->
3458 indent,pp_expr_indent_l(L),insertstr(' : '), insertstr('('),
3459 { add_optional_typing_predicates(L,R,R1) },
3460 pp_expr_indent(R1), insertstr(')').
3461 translate_subst2(evb2_becomes_such(L,R)) --> translate_subst2(becomes_such(L,R)).
3462 translate_subst2(if([Elsif|Rest])) -->
3463 { get_if_elsif(Elsif,P,S) },
3464 indent('IF '), pp_expr_indent(P), insertstr(' THEN'),
3465 insert_subst(S),
3466 translate_ifs(Rest).
3467 translate_subst2(if_elsif(P,S)) --> % not a legal top-level construct; but can be called in b_portray_hook
3468 indent('IF '), pp_expr_indent(P), insertstr(' THEN'),
3469 insert_subst(S),
3470 indent('END').
3471 translate_subst2(choice(Ss)) --> indent(' CHOICE'),
3472 split_over_lines(Ss,'OR'),
3473 indent('END'). % indentation seems too far
3474 translate_subst2(parallel(Ss)) -->
3475 split_over_lines(Ss,'||').
3476 translate_subst2(init_statement(S)) --> insert_subst(S).
3477 translate_subst2(sequence(Ss)) -->
3478 split_over_lines(Ss,';').
3479 translate_subst2(operation_call(Id,Rs,As)) -->
3480 indent,translate_op_results(Rs),
3481 pp_expr_indent(Id),
3482 translate_op_params(As).
3483 translate_subst2(identifier(op(Id))) --> % shouldn't normally appear
3484 indent,pp_expr_indent(identifier(Id)).
3485 translate_subst2(external_subst_call(Symbol,Args)) -->
3486 indent,
3487 pp_expr_indent(identifier(Symbol)),
3488 translate_op_params(Args).
3489 translate_subst2(any(Ids,Pred,Subst)) -->
3490 indent('ANY '), pp_expr_indent_l(Ids),
3491 indent('WHERE '),
3492 {add_optional_typing_predicates(Ids,Pred,Pred2)},
3493 pred_over_lines(2,'@grd',Pred2), indent('THEN'),
3494 insert_subst(Subst),
3495 indent('END').
3496 translate_subst2(select(Whens)) -->
3497 translate_whens(Whens,'SELECT '),
3498 indent('END').
3499 translate_subst2(select_when(Cond,Then)) --> % not a legal top-level construct; but can be called in b_portray_hook
3500 indent('WHEN'),
3501 pp_expr_indent(Cond),
3502 indent('THEN'),
3503 insert_subst(Then),
3504 indent('END').
3505 translate_subst2(select(Whens,Else)) -->
3506 translate_whens(Whens,'SELECT '),
3507 indent('ELSE'), insert_subst(Else),
3508 indent('END').
3509 translate_subst2(var(Ids,S)) -->
3510 indent('VAR '),
3511 pp_expr_indent_l(Ids),
3512 indent('IN'),insert_subst(S),
3513 indent('END').
3514 translate_subst2(let(Ids,P,S)) -->
3515 indent('LET '),
3516 pp_expr_indent_l(Ids),
3517 insertstr(' BE '), pp_expr_indent(P),
3518 indent('IN'), insert_subst(S),
3519 indent('END').
3520 translate_subst2(lazy_let_subst(TID,P,S)) -->
3521 indent('LET '),
3522 pp_expr_indent_l([TID]),
3523 insertstr(' BE '), pp_expr_indent(P), % could be expr or pred
3524 indent('IN'), insert_subst(S),
3525 indent('END').
3526 translate_subst2(case(Expression,Cases,ELSE)) -->
3527 % CASE E OF EITHER m THEN G OR n THEN H ... ELSE I END END
3528 indent('CASE '),
3529 pp_expr_indent(Expression), insertstr(' OF'),
3530 indent('EITHER '), translate_cases(Cases),
3531 indent('ELSE '), insert_subst(ELSE), % we could drop this if ELSE is skip ?
3532 indent('END END').
3533 translate_subst2(while(Pred,Subst,Inv,Var)) -->
3534 indent('WHILE '), pp_expr_indent(Pred),
3535 indent('DO'),insert_subst(Subst),
3536 indent('INVARIANT '),pp_expr_indent(Inv),
3537 indent('VARIANT '),pp_expr_indent(Var),
3538 indent('END').
3539 translate_subst2(while1(Pred,Subst,Inv,Var)) -->
3540 indent('WHILE /* 1 */ '), pp_expr_indent(Pred),
3541 indent('DO'),insert_subst(Subst),
3542 indent('INVARIANT '),pp_expr_indent(Inv),
3543 indent('VARIANT '),pp_expr_indent(Var),
3544 indent('END').
3545 translate_subst2(rlevent(Id,Section,Status,Parameters,Guard,Theorems,Actions,VWitnesses,PWitnesses,_Unmod,Refines)) -->
3546 indent,
3547 insert_status(Status),
3548 insertstr('EVENT '),
3549 ({Id = 'INITIALISATION'}
3550 -> [] % avoid BLexer error in ProB2-UI, BLexerException: Invalid combination of symbols: 'INITIALISATION' and '='.
3551 ; insertstr(Id), insertstr(' = ')),
3552 insertstr('/'), insertstr('* of machine '),
3553 insertstr(Section),insertstr(' */'),
3554 insert_variant(Status),
3555 ( {Parameters=[], get_texpr_expr(Guard,truth)} ->
3556 {NoGuard=true} % indent('BEGIN ')
3557 ; {Parameters=[]} ->
3558 indent('WHEN '),
3559 pred_over_lines(2,'@grd',Guard)
3560 ;
3561 indent('ANY '),pp_expr_indent_l(Parameters),
3562 indent('WHERE '),
3563 pred_over_lines(2,'@grd',Guard)
3564 ),
3565 ( {VWitnesses=[],PWitnesses=[]} ->
3566 []
3567 ;
3568 {append(VWitnesses,PWitnesses,Witnesses)},
3569 indent('WITH '),pp_witness_l(Witnesses)
3570 ),
3571 {( Actions=[] ->
3572 create_texpr(skip,subst,[],Subst)
3573 ;
3574 create_texpr(parallel(Actions),subst,[],Subst)
3575 )},
3576 ( {Theorems=[]} -> {true}
3577 ;
3578 indent('THEOREMS '),
3579 preds_over_lines(2,'@thm',Theorems)
3580 ),
3581 ({NoGuard==true}
3582 -> indent('BEGIN ') % avoid BLexer errors in ProB2-UI Syntax highlighting
3583 ; indent('THEN ')
3584 ),
3585 insert_subst(Subst),
3586 pp_refines_l(Refines,Id),
3587 indent('END').
3588
3589 % translate cases of a CASE statement
3590 translate_cases([]) --> !,[].
3591 translate_cases([CaseOr|T]) -->
3592 {get_texpr_expr(CaseOr,case_or(Exprs,Subst))},!,
3593 pp_expr_indent_l(Exprs),
3594 insertstr(' THEN '),
3595 insert_subst(Subst),
3596 ({T==[]} -> {true}
3597 ; indent('OR '), translate_cases(T)).
3598 translate_cases(L) -->
3599 {add_internal_error('Cannot translate CASE list: ',translate_cases(L,_,_))}.
3600
3601 insert_status(TStatus) -->
3602 {get_texpr_expr(TStatus,Status),
3603 status_string(Status,String)},
3604 insertstr(String).
3605 status_string(ordinary,'').
3606 status_string(anticipated(_),'ANTICIPATED ').
3607 status_string(convergent(_),'CONVERGENT ').
3608
3609 insert_variant(TStatus) -->
3610 {get_texpr_expr(TStatus,Status)},
3611 insert_variant2(Status).
3612 insert_variant2(ordinary) --> !.
3613 insert_variant2(anticipated(Variant)) --> insert_variant3(Variant).
3614 insert_variant2(convergent(Variant)) --> insert_variant3(Variant).
3615 insert_variant3(Variant) -->
3616 indent('USING VARIANT '),pp_expr_indent(Variant).
3617
3618 pp_refines_l([],_) --> [].
3619 pp_refines_l([Ref|Rest],Id) -->
3620 pp_refines(Ref,Id),pp_refines_l(Rest,Id).
3621 pp_refines(Refined,_Id) -->
3622 % indent(Id), insertstr(' REFINES '),
3623 indent('REFINES '),
3624 insert_subst(Refined).
3625
3626 pp_witness_l([]) --> [].
3627 pp_witness_l([Witness|WRest]) -->
3628 pp_witness(Witness),pp_witness_l(WRest).
3629 pp_witness(Expr) -->
3630 indention_level(I1,I2),
3631 {get_texpr_expr(Expr,witness(Id,Pred)),
3632 I2 is I1+2},
3633 indent, pp_expr_indent(Id), insertstr(': '),
3634 pp_expr_indent(Pred),
3635 pp_description_pragma_of(Pred),
3636 indention_level(_,I1).
3637
3638
3639 translate_whens([],_) --> !.
3640 translate_whens([When|Rest],T) -->
3641 {get_texpr_expr(When,select_when(P,S))},!,
3642 indent(T), pred_over_lines(2,'@grd',P),
3643 indent('THEN '),
3644 insert_subst(S),
3645 translate_whens(Rest,'WHEN ').
3646 translate_whens(L,_) -->
3647 {add_internal_error('Cannot translate WHEN: ',translate_whens(L,_,_,_))}.
3648
3649
3650
3651 create_assign(LHS,RHS,b(assign([LHS],[RHS]),subst,[])).
3652
3653 split_over_lines([],_) --> !.
3654 split_over_lines([S|Rest],Symbol) --> !,
3655 indention_level(I1,I2),{atom_codes(Symbol,X),length(X,N),I2 is I1+N+1},
3656 translate_subst_check(S),
3657 split_over_lines2(Rest,Symbol,I1,I2).
3658 split_over_lines(S,Symbol) --> {add_error(split_over_lines,'Illegal argument: ',Symbol:S)}.
3659
3660 split_over_lines2([],_,_,_) --> !.
3661 split_over_lines2([S|Rest],Symbol,I1,I2) -->
3662 indention_level(_,I1), indent(Symbol),
3663 indention_level(_,I2), translate_subst(S),
3664 split_over_lines2(Rest,Symbol,I1,I2).
3665
3666 % print a predicate over several lines, at most one conjunct per line
3667 % N is the increment that should be added to the indentation
3668 %pred_over_lines(N,Pred) --> pred_over_lines(N,'@pred',Pred).
3669 pred_over_lines(N,Lbl,Pred) -->
3670 {conjunction_to_list(Pred,List)},
3671 preds_over_lines(N,Lbl,List).
3672 section_pred_over_lines(N,Title,Pred) -->
3673 ({get_eventb_default_label(Title,Lbl)} -> [] ; {Lbl='@pred'}),
3674 pred_over_lines(N,Lbl,Pred).
3675 get_eventb_default_label(properties,'@axm').
3676 get_eventb_default_label(assertions,'@thm').
3677
3678 % print a list of predicates over several lines, at most one conjunct per line
3679 preds_over_lines(N,Lbl,Preds) --> preds_over_lines(N,Lbl,'& ',Preds).
3680 % preds_over_lines(IndentationIncrease,EventBDefaultLabel,ClassicalBSeperator,ListOfPredicates)
3681 preds_over_lines(N,Lbl,Sep,Preds) -->
3682 indention_level(I1,I2),{I2 is I1+N},
3683 preds_over_lines1(Preds,Lbl,1,Sep),
3684 indention_level(_,I1).
3685 preds_over_lines1([],Lbl,Nr,Sep) --> !,
3686 preds_over_lines1([b(truth,pred,[])],Lbl,Nr,Sep).
3687 preds_over_lines1([H|T],Lbl,Nr,Sep) -->
3688 indent(' '), pp_label(Lbl,Nr),
3689 %({T==[]} -> pp_expr_indent(H) ; pp_expr_m_indent(H,40)),
3690 ({T==[]} -> pp_pred_nested(H,conjunct,0) ; pp_pred_nested(H,conjunct,40)),
3691 pp_description_pragma_of(H),
3692 {N1 is Nr+1},
3693 preds_over_lines2(T,Lbl,N1,Sep).
3694 preds_over_lines2([],_,_,_Sep) --> !.
3695 preds_over_lines2([E|Rest],Lbl,Nr,Sep) -->
3696 ({force_eventb_rodin_mode} -> indent(' '), pp_label(Lbl,Nr) ; indent(Sep)),
3697 pp_pred_nested(E,conjunct,40),
3698 pp_description_pragma_of(E),
3699 {N1 is Nr+1},
3700 preds_over_lines2(Rest,Lbl,N1,Sep).
3701
3702 % print event-b label for Rodin/Camille:
3703 pp_label(Lbl,Nr) -->
3704 ({force_eventb_rodin_mode}
3705 -> {atom_codes(Lbl,C1), number_codes(Nr,NC), append(C1,NC,AC), atom_codes(A,AC)},
3706 pp_atom_indent(A), pp_atom_indent(' ')
3707 ; []).
3708
3709 % a version of nested_print_bexpr / nbp that does not directly print to stream conjunct
3710 pp_pred_nested(TExpr,CurrentType,_) --> {TExpr = b(E,pred,_)},
3711 {get_binary_connective(E,NewType,Ascii,LHS,RHS), binary_infix(NewType,Ascii,Prio,left)},
3712 !,
3713 pp_rodin_label_indent(TExpr), % print any label
3714 inc_lvl(CurrentType,NewType),
3715 pp_pred_nested(LHS,NewType,Prio),
3716 {translate_in_mode(NewType,Ascii,Symbol)},
3717 indent(' '),pp_atom_indent(Symbol),
3718 indent(' '),
3719 {(is_associative(NewType) -> NewTypeR=NewType % no need for parentheses if same operator on right
3720 ; NewTypeR=right(NewType))},
3721 pp_pred_nested(RHS,NewTypeR,Prio),
3722 dec_lvl(CurrentType,NewType).
3723 pp_pred_nested(TExpr,_,_) --> {is_nontrivial_negation(TExpr,NExpr,InnerType,Prio)},
3724 !,
3725 pp_rodin_label_indent(TExpr), % print any label
3726 {translate_in_mode(negation,'not',Symbol)},
3727 pp_atom_indent(Symbol),
3728 inc_lvl(other,negation), % always need parentheses for negation
3729 pp_pred_nested(NExpr,InnerType,Prio),
3730 dec_lvl(other,negation).
3731 pp_pred_nested(TExpr,_,_) --> {TExpr = b(exists(Ids,RHS),pred,_)},
3732 !,
3733 pp_rodin_label_indent(TExpr), % print any label
3734 {translate_in_mode(exists,'#',FSymbol)},
3735 %indent(' '),
3736 pp_atom_indent(FSymbol),
3737 pp_expr_ids_in_mode_indent(Ids),pp_atom_indent('.'),
3738 inc_lvl(other,conjunct), % always need parentheses here
3739 {add_normal_typing_predicates(Ids,RHS,RHST), Prio=40}, % Prio of conjunction
3740 pp_pred_nested(RHST,conjunct,Prio),
3741 dec_lvl(other,conjunct).
3742 pp_pred_nested(TExpr,_,_) --> {TExpr = b(forall(Ids,LHS,RHS),pred,_)},
3743 !,
3744 pp_rodin_label_indent(TExpr), % print any label
3745 {translate_in_mode(forall,'!',FSymbol)},
3746 %indent(' '),
3747 pp_atom_indent(FSymbol),
3748 pp_expr_ids_in_mode_indent(Ids),pp_atom_indent('.'),
3749 inc_lvl(other,implication), % always need parentheses here
3750 {add_normal_typing_predicates(Ids,LHS,LHST), Prio=30}, % Prio of implication
3751 pp_pred_nested(LHST,implication,Prio),
3752 {translate_in_mode(implication,'=>',Symbol)},
3753 indent(' '),pp_atom_indent(Symbol),
3754 indent(' '),
3755 pp_pred_nested(RHS,right(implication),Prio),
3756 dec_lvl(other,implication).
3757 pp_pred_nested(TExpr,_,_) -->
3758 {\+ eventb_translation_mode,
3759 TExpr = b(let_predicate(Ids,Exprs,Body),pred,_)
3760 }, %Ids=[_]}, % TODO: enable printing with more than one id; see below
3761 !,
3762 pp_let_nested(Ids,Exprs,Body).
3763 pp_pred_nested(b(BOP,pred,_),_CurrentType,CurMinPrio) -->
3764 {indent_binary_predicate(BOP,LHS,RHS,OpStr),
3765 get_texpr_id(LHS,_),can_indent_texpr(RHS)},!,
3766 pp_expr_m_indent(LHS,CurMinPrio),
3767 insertstr(OpStr),
3768 increase_indentation_level(2),
3769 indent(''),
3770 pp_expr_indent(RHS), % only supports %, {}, bool which do not need parentheses
3771 decrease_indentation_level(2).
3772 pp_pred_nested(Expr,_CurrentType,CurMinPrio) --> {can_indent_texpr(Expr)},!,
3773 pp_expr_m_indent(Expr,CurMinPrio).
3774 pp_pred_nested(Expr,_CurrentType,CurMinPrio) --> pp_expr_m_indent(Expr,CurMinPrio).
3775
3776 indent_binary_predicate(equal(LHS,RHS),LHS,RHS,' = ').
3777 indent_binary_predicate(member(LHS,RHS),LHS,RHS,' : ').
3778
3779 pp_let_nested(Ids,Exprs,Body) -->
3780 indent('LET '),
3781 pp_expr_indent_l(Ids),
3782 insertstr(' BE '),
3783 {maplist(create_equality,Ids,Exprs,Equalities)},
3784 preds_over_lines(2,'@let_eq',Equalities),
3785 indent(' IN '),
3786 increase_indentation_level(2),
3787 pp_pred_nested(Body,let_predicate,40),
3788 decrease_indentation_level(2),
3789 indent(' END').
3790 pp_let_expr_nested(Ids,Exprs,Body) -->
3791 insertstr('LET '),
3792 pp_expr_indent_l(Ids),
3793 insertstr(' BE '),
3794 {maplist(create_equality,Ids,Exprs,Equalities)},
3795 preds_over_lines(2,'@let_eq',Equalities),
3796 indent('IN '),
3797 increase_indentation_level(2),
3798 pp_expr_indent(Body),
3799 decrease_indentation_level(2),
3800 indent('END').
3801
3802 is_nontrivial_negation(b(negation(NExpr),pred,_),NExpr,NewType,Prio) :-
3803 get_texpr_expr(NExpr,E),
3804 (E=negation(_) -> NewType=other,Prio=0
3805 ; get_binary_connective(E,NewType,Ascii,_,_),
3806 binary_infix(NewType,Ascii,Prio,_Assoc)).
3807
3808 pp_rodin_label_indent(b(_,_,Infos),(I,S),(I,T)) :- pp_rodin_label(Infos,S,T).
3809 % note: below we will print unnecessary parentheses in case of Atelier-B mode; but for readability it maye be better to add them
3810 inc_lvl(Old,New) --> {New=Old}, !,[].
3811 inc_lvl(_,_) --> pp_atom_indent('('), % not strictly necessary if higher_prio
3812 increase_indentation_level, indent(' ').
3813 dec_lvl(Old,New) --> {New=Old}, !,[].
3814 dec_lvl(_,_) --> decrease_indentation_level, indent(' '),pp_atom_indent(')').
3815
3816 is_associative(conjunct).
3817 is_associative(disjunct).
3818
3819 %higher_prio(conjunct,implication).
3820 %higher_prio(disjunct,implication).
3821 % priority of equivalence changes in Rodin vs Atelier-B, maybe better add parentheses
3822
3823 translate_ifs([]) --> !,
3824 indent('END').
3825 translate_ifs([Elsif]) -->
3826 {get_if_elsif(Elsif,P,S),
3827 optional_type(P,truth)},!,
3828 indent('ELSE'), insert_subst(S), indent('END').
3829 translate_ifs([Elsif|Rest]) -->
3830 {get_if_elsif(Elsif,P,S)},!,
3831 indent('ELSIF '), pp_expr_indent(P), insertstr(' THEN'),
3832 insert_subst(S),
3833 translate_ifs(Rest).
3834 translate_ifs(ElseList) -->
3835 {functor(ElseList,F,A),add_error(translate_ifs,'Could not translate IF-THEN-ELSE: ',F/A-ElseList),fail}.
3836
3837 get_if_elsif(Elsif,P,S) :-
3838 (optional_type(Elsif,if_elsif(P,S)) -> true
3839 ; add_internal_error('Is not an if_elsif:',get_if_elsif(Elsif,P,S)), fail).
3840
3841 insert_subst(S) -->
3842 indention_level(I,I2),{I2 is I+2},
3843 translate_subst_check(S),
3844 indention_level(_,I).
3845
3846 translate_subst_check(S) --> translate_subst(S),!.
3847 translate_subst_check(S) -->
3848 {b_functor(S,F,A),add_error(translate_subst,'Could not translate substitution: ',F/A-S),fail}.
3849
3850 b_functor(b(E,_,_),F,A) :- !,functor(E,F,A).
3851 b_functor(E,F,A) :- functor(E,F,A).
3852
3853 pp_description_pragma_of(enumerated_set_def(_,_)) --> !, "".
3854 pp_description_pragma_of(Expr) -->
3855 ({get_texpr_description(Expr,Desc)}
3856 -> insert_atom(' /*@desc '), insert_atom(Desc), insert_atom(' */')
3857 ; {true}).
3858 indent_expr(Expr) -->
3859 indent, pp_expr_indent(Expr),
3860 pp_description_pragma_of(Expr).
3861 %indent_expr_l([]) --> !.
3862 %indent_expr_l([Expr|Rest]) -->
3863 % indent_expr(Expr), indent_expr_l(Rest).
3864 indent_expr_l_sep([],_) --> !.
3865 indent_expr_l_sep([Expr|Rest],Sep) -->
3866 indent_expr(Expr),
3867 {(Rest=[] -> RealSep='' ; RealSep=Sep)},
3868 insert_atom(RealSep), % the threaded argument is a pair, not directly a string !
3869 indent_expr_l_sep(Rest,Sep).
3870 %indention_level(L) --> indention_level(L,L).
3871 increase_indentation_level --> indention_level(L,New), {New is L+1}.
3872 increase_indentation_level(N) --> indention_level(L,New), {New is L+N}.
3873 decrease_indentation_level --> indention_level(L,New), {New is L-1}.
3874 decrease_indentation_level(N) --> indention_level(L,New), {New is L-N}.
3875 indention_level(Old,New,(Old,S),(New,S)).
3876 indention_codes(Old,New,(Indent,Old),(Indent,New)).
3877 indent --> indent('').
3878 indent(M,(I,S),(I,T)) :- indent2(I,M,S,T).
3879 indent2(Level,Msg) -->
3880 "\n",do_indention(Level),ppatom(Msg).
3881
3882 insert_atom(Sep,(I,S),(I,T)) :- ppatom(Sep,S,T).
3883
3884 insertstr(M,(I,S),(I,T)) :- ppterm(M,S,T).
3885 insertcodes(M,(I,S),(I,T)) :- ppcodes(M,S,T).
3886
3887 do_indention(0,T,R) :- !, R=T.
3888 do_indention(N,[32|I],O) :-
3889 N>0,N2 is N-1, do_indention(N2,I,O).
3890
3891 optional_type(Typed,Expr) :- get_texpr_expr(Typed,E),!,Expr=E.
3892 optional_type(Expr,Expr).
3893
3894 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3895 % expressions and predicates
3896
3897 % pretty-type an expression in an indent-environment
3898 % currently, the indent level is just thrown away
3899 % TODO: pp_expr_indent dom( comprehension_set ) / union ( ...)
3900 pp_expr_indent(b(comprehension_set(Ids,Body),_,_)) -->
3901 {\+ eventb_translation_mode, % TODO: also print in Event-B mode:
3902 detect_lambda_comprehension(Ids,Body, FrontIDs,LambdaBody,ToExpr)},
3903 {add_normal_typing_predicates(FrontIDs,LambdaBody,TLambdaBody)},
3904 !,
3905 insertstr('%('), % to do: use lambda_symbol and improve layout below
3906 pp_expr_indent_l(FrontIDs),
3907 insertstr(') . ('),
3908 pred_over_lines(2,'@body',TLambdaBody),
3909 indent(' | '), increase_indentation_level(2),
3910 indent(''), pp_expr_indent(ToExpr), decrease_indentation_level(2),
3911 indent(')').
3912 pp_expr_indent(b(comprehension_set(Ids,Body),_,Info),(I,S),(I,T)) :-
3913 pp_comprehension_set5(Ids,Body,Info,_,special(_Kind),S,T),
3914 % throw away indent and check if a special pp rule is applicable
3915 !.
3916 pp_expr_indent(b(comprehension_set(Ids,Body),_,_)) -->
3917 !,
3918 insertstr('{'), pp_expr_indent_l(Ids),
3919 insertstr(' | '),
3920 pred_over_lines(2,'@body',Body),
3921 indent('}').
3922 pp_expr_indent(b(convert_bool(Body),_,_)) -->
3923 !,
3924 insertstr('bool('),
3925 pred_over_lines(2,'@bool',Body),
3926 indent(')').
3927 pp_expr_indent(b(if_then_else(Test,Then,Else),_,_)) -->
3928 !,
3929 insertstr('IF'),
3930 pred_over_lines(2,'@test',Test),
3931 indent('THEN'),increase_indentation_level(2),
3932 indent(''),pp_expr_indent(Then),decrease_indentation_level(2),
3933 indent('ELSE'),increase_indentation_level(2),
3934 indent(''),pp_expr_indent(Else),decrease_indentation_level(2),
3935 indent('END').
3936 pp_expr_indent(b(let_expression(Ids,Exprs,Body),_,_)) -->
3937 !,
3938 pp_let_expr_nested(Ids,Exprs,Body).
3939 % TODO: support a few more like dom/ran(comprehension_set) SIGMA, PI, \/ (union), ...
3940 pp_expr_indent(Expr,(I,S),(I,T)) :-
3941 %get_texpr_expr(Expr,F), functor(F,FF,NN), format(user_output,'Cannot indent: ~w/~w~n',[FF,NN]),
3942 pp_expr(Expr,_,_LimitReached,S,T). % throw away indent
3943
3944 can_indent_texpr(b(E,_,_)) :- can_indent_expr(E).
3945 can_indent_expr(comprehension_set(_,_)).
3946 can_indent_expr(convert_bool(_)).
3947 can_indent_expr(if_then_else(_,_,_)).
3948 can_indent_expr(let_expression(_,_,_)).
3949
3950 pp_expr_indent_l([E]) --> !, pp_expr_indent(E).
3951 pp_expr_indent_l(Exprs,(I,S),(I,T)) :-
3952 pp_expr_l(Exprs,_LR,S,T). % throw away indent
3953 pp_expr_m_indent(Expr,MinPrio,(I,S),(I,T)) :-
3954 pp_expr_m(Expr,MinPrio,_LimitReached,S,T).
3955 pp_atom_indent(A,(I,S),(I,T)) :- ppatom(A,S,T).
3956 pp_expr_ids_in_mode_indent(Ids,(I,S),(I,T)) :- pp_expr_ids_in_mode(Ids,_,S,T).
3957
3958
3959
3960
3961 is_boolean_value(b(B,boolean,_),BV) :- boolean_aux(B,BV).
3962 boolean_aux(boolean_true,pred_true).
3963 boolean_aux(boolean_false,pred_false).
3964 boolean_aux(value(V),BV) :- nonvar(V),!,BV=V.
3965
3966
3967 constants_in_mode(F,S) :-
3968 constants(F,S1), translate_in_mode(F,S1,S).
3969
3970 constants(pred_true,'TRUE').
3971 constants(pred_false,'FALSE').
3972 constants(boolean_true,'TRUE').
3973 constants(boolean_false,'FALSE').
3974 constants(max_int,'MAXINT').
3975 constants(min_int,'MININT').
3976 constants(empty_set,'{}').
3977 constants(bool_set,'BOOL').
3978 constants(float_set,'FLOAT').
3979 constants(real_set,'REAL').
3980 constants(string_set,'STRING').
3981 constants(empty_sequence,'[]').
3982 constants(event_b_identity,'id').
3983
3984 constants(truth,Res) :- eventb_translation_mode,!,Res=true.
3985 constants(truth,Res) :- animation_minor_mode(tla),!,Res='TRUE'.
3986 constants(truth,Res) :- atelierb_mode(_),!,Res='(TRUE:BOOL)'. % __truth; we could also do TRUE=TRUE
3987 constants(truth,'btrue').
3988 constants(falsity,Res) :- eventb_translation_mode,!,Res=false.
3989 constants(falsity,Res) :- animation_minor_mode(tla),!,Res='FALSE'.
3990 constants(falsity,Res) :- atelierb_mode(_),!,Res='(TRUE=FALSE)'.
3991 constants(falsity,'bfalse'). % __falsity
3992 constants(unknown_truth_value(Msg),Res) :- % special internal constant
3993 ajoin(['?(',Msg,')'],Res).
3994
3995 function_like_in_mode(F,S) :-
3996 function_like(F,S1),
3997 translate_in_mode(F,S1,S).
3998
3999 function_like(convert_bool,bool).
4000 function_like(convert_real,real). % cannot be used on its own: dom(real) is not accepted by Atelier-B
4001 function_like(convert_int_floor,floor). % ditto
4002 function_like(convert_int_ceiling,ceiling). % ditto
4003 function_like(successor,succ). % can also be used on its own; e.g., dom(succ)=INTEGER is ok
4004 function_like(predecessor,pred). % ditto
4005 function_like(max,max).
4006 function_like(max_real,max).
4007 function_like(min,min).
4008 function_like(min_real,min).
4009 function_like(card,card).
4010 function_like(pow_subset,'POW').
4011 function_like(pow1_subset,'POW1').
4012 function_like(fin_subset,'FIN').
4013 function_like(fin1_subset,'FIN1').
4014 function_like(identity,id).
4015 function_like(first_projection,prj1).
4016 function_like(first_of_pair,'prj1'). % used to be __first_of_pair, will be dealt with separately to generate parsable representation
4017 function_like(second_projection,prj2).
4018 function_like(second_of_pair,'prj2'). % used to be __second_of_pair, will be dealt with separately to generate parsable representation
4019 function_like(iteration,iterate).
4020 function_like(event_b_first_projection_v2,prj1).
4021 function_like(event_b_second_projection_v2,prj2).
4022 function_like(reflexive_closure,closure).
4023 function_like(closure,closure1).
4024 function_like(domain,dom).
4025 function_like(range,ran).
4026 function_like(seq,seq).
4027 function_like(seq1,seq1).
4028 function_like(iseq,iseq).
4029 function_like(iseq1,iseq1).
4030 function_like(perm,perm).
4031 function_like(size,size).
4032 function_like(first,first).
4033 function_like(last,last).
4034 function_like(front,front).
4035 function_like(tail,tail).
4036 function_like(rev,rev).
4037 function_like(general_concat,conc).
4038 function_like(general_union,union).
4039 function_like(general_intersection,inter).
4040 function_like(trans_function,fnc).
4041 function_like(trans_relation,rel).
4042 function_like(tree,tree).
4043 function_like(btree,btree).
4044 function_like(const,const).
4045 function_like(top,top).
4046 function_like(sons,sons).
4047 function_like(prefix,prefix).
4048 function_like(postfix,postfix).
4049 function_like(sizet,sizet).
4050 function_like(mirror,mirror).
4051 function_like(rank,rank).
4052 function_like(father,father).
4053 function_like(son,son).
4054 function_like(subtree,subtree).
4055 function_like(arity,arity).
4056 function_like(bin,bin).
4057 function_like(left,left).
4058 function_like(right,right).
4059 function_like(infix,infix).
4060
4061 function_like(rec,rec).
4062 function_like(struct,struct).
4063
4064 function_like(negation,not).
4065 function_like(bag_items,items).
4066
4067 function_like(finite,finite). % from Event-B, TO DO: if \+ eventb_translation_mode then print as S:FIN(S)
4068 function_like(witness,'@witness'). % from Event-B
4069
4070 function_like(floored_div,'FDIV') :- \+ animation_minor_mode(tla). % using external function
4071
4072 unary_prefix(unary_minus,'\x2212\',210) :- unicode_mode, eventb_translation_mode, !.
4073 unary_prefix(unary_minus,-,210).
4074 unary_prefix(unary_minus_real,-,210).
4075 unary_prefix(mu,'MU',210) :- animation_minor_mode(z).
4076
4077 unary_prefix_parentheses(compaction,'compaction').
4078 unary_prefix_parentheses(bag_items,'bag_items').
4079 unary_prefix_parentheses(mu,'MU') :- \+ animation_minor_mode(z). % write with () for external function
4080
4081 unary_postfix(reverse,'~',230). % relational inverse
4082
4083
4084 always_surround_by_parentheses(parallel_product).
4085 always_surround_by_parentheses(composition).
4086
4087 binary_infix_symbol(b(T,_,_),Symbol) :- functor(T,F,2), binary_infix_in_mode(F,Symbol,_,_).
4088
4089 % EXPR * EXPR --> EXPR
4090 binary_infix(composition,';',20,left).
4091 binary_infix(overwrite,'<+',160,left).
4092 binary_infix(direct_product,'><',160,left). % Rodin requires parentheses
4093 binary_infix(parallel_product,'||',20,left).
4094 binary_infix(concat,'^',160,left).
4095 binary_infix(relations,'<->',125,left).
4096 binary_infix(partial_function,'+->',125,left).
4097 binary_infix(total_function,'-->',125,left).
4098 binary_infix(partial_injection,'>+>',125,left).
4099 binary_infix(total_injection,'>->',125,left).
4100 binary_infix(partial_surjection,'+->>',125,left).
4101 binary_infix(total_surjection,Symbol,125,left) :-
4102 (eventb_translation_mode -> Symbol = '->>'; Symbol = '-->>').
4103 binary_infix(total_bijection,'>->>',125,left).
4104 binary_infix(partial_bijection,'>+>>',125,left).
4105 binary_infix(total_relation,'<<->',125,left). % only in Event-B
4106 binary_infix(surjection_relation,'<->>',125,left). % only in Event-B
4107 binary_infix(total_surjection_relation,'<<->>',125,left). % only in Event-B
4108 binary_infix(insert_front,'->',160,left).
4109 binary_infix(insert_tail,'<-',160,left).
4110 binary_infix(domain_restriction,'<|',160,left).
4111 binary_infix(domain_subtraction,'<<|',160,left).
4112 binary_infix(range_restriction,'|>',160,left).
4113 binary_infix(range_subtraction,'|>>',160,left).
4114 binary_infix(intersection,'/\\',160,left).
4115 binary_infix(union,'\\/',160,left).
4116 binary_infix(restrict_front,'/|\\',160,left).
4117 binary_infix(restrict_tail,'\\|/',160,left).
4118 binary_infix(couple,'|->',160,left).
4119 binary_infix(interval,'..',170,left).
4120 binary_infix(add,+,180,left).
4121 binary_infix(add_real,+,180,left).
4122 binary_infix(minus,-,180,left).
4123 binary_infix(minus_real,-,180,left).
4124 binary_infix(set_subtraction,'\\',180,left) :- eventb_translation_mode,!. % symbol is not allowed by Atelier-B
4125 binary_infix(set_subtraction,-,180,left).
4126 binary_infix(minus_or_set_subtract,-,180,left).
4127 binary_infix(multiplication,*,190,left).
4128 binary_infix(multiplication_real,*,190,left).
4129 binary_infix(cartesian_product,**,190,left) :- eventb_translation_mode,!.
4130 binary_infix(cartesian_product,*,190,left).
4131 binary_infix(mult_or_cart,*,190,left). % in case type checker not yet run
4132 binary_infix(div,/,190,left).
4133 binary_infix(div_real,/,190,left).
4134 binary_infix(floored_div,div,190,left) :- animation_minor_mode(tla).
4135 binary_infix(modulo,mod,190,left).
4136 binary_infix(power_of,**,200,right).
4137 binary_infix(power_of_real,**,200,right).
4138 binary_infix(typeof,oftype,120,right). % Event-B oftype operator; usually removed by btypechecker, technically has no associativity in our parser, but right associativity matches better
4139
4140 binary_infix(ring,'\x2218\',160,left). % our B Parser gives ring same priority as direct_product or overwrite
4141
4142 % PRED * PRED --> PRED
4143 binary_infix(implication,'=>',30,left).
4144 binary_infix(conjunct,'&',40,left).
4145 binary_infix(disjunct,or,40,left).
4146 binary_infix(equivalence,'<=>',Prio,left) :- % in Rodin this has the same priority as implication
4147 (eventb_translation_mode -> Prio=30 ; Prio=60).
4148
4149
4150 % EXPR * EXPR --> PRED
4151 binary_infix(equal,=,60,left).
4152 binary_infix(not_equal,'/=',160,left).
4153 binary_infix(less_equal,'<=',160,left).
4154 binary_infix(less,'<',160,left).
4155 binary_infix(less_equal_real,'<=',160,left).
4156 binary_infix(less_real,'<',160,left).
4157 binary_infix(greater_equal,'>=',160,left).
4158 binary_infix(greater,'>',160,left).
4159 binary_infix(member,':',60,left).
4160 binary_infix(not_member,'/:',160,left).
4161 binary_infix(subset,'<:',110,left).
4162 binary_infix(subset_strict,'<<:',110,left).
4163 binary_infix(not_subset,'/<:',110,left).
4164 binary_infix(not_subset_strict,'/<<:',110,left).
4165
4166 binary_infix(values_entry,'=',60,left).
4167
4168 % atelierb_mode(prover(_)): translation for AtelierB's PP/ML prover
4169 % atelierb_mode(native): translation to native B supported by AtelierB
4170 :- dynamic latex_mode/0, unicode_mode/0, atelierb_mode/1, force_eventb_rodin_mode/0.
4171
4172 %latex_mode.
4173 %unicode_mode.
4174 %force_eventb_rodin_mode. % Force Event-B output even if not in eventb minor mode
4175
4176 eventb_translation_mode :- animation_minor_mode(eventb),!.
4177 eventb_translation_mode :- animation_minor_mode(sequent_prover),!.
4178 eventb_translation_mode :- force_eventb_rodin_mode.
4179
4180 set_force_eventb_mode :- assertz(force_eventb_rodin_mode).
4181 unset_force_eventb_mode :-
4182 (retract(force_eventb_rodin_mode) -> true ; add_internal_error('Was not in forced Event-B mode: ',force_eventb_rodin_mode)).
4183
4184 set_unicode_mode :- assertz(unicode_mode).
4185 set_latex_mode :- assertz(latex_mode).
4186 unset_unicode_mode :-
4187 (retract(unicode_mode) -> true ; add_internal_error('Was not in Unicode mode: ',unset_unicode_mode)).
4188 unset_latex_mode :-
4189 (retract(latex_mode) -> true
4190 ; add_internal_error('Was not in Latex mode: ',unset_latex_mode)).
4191
4192 set_atelierb_mode(Mode) :- asserta(atelierb_mode(Mode)).
4193 unset_atelierb_mode :-
4194 (retract(atelierb_mode(_)) -> true ; add_internal_error('Was not in Atelier-B mode: ',unset_atelierb_mode)).
4195
4196 get_translation_mode(M) :- unicode_mode, !, M=unicode.
4197 get_translation_mode(M) :- latex_mode, !, M=latex.
4198 get_translation_mode(M) :- atelierb_mode(native), !, M=atelierb.
4199 get_translation_mode(M) :- atelierb_mode(prover(pp)), !, M=atelierb_pp.
4200 get_translation_mode(M) :- atelierb_mode(prover(ml)), !, M=atelierb_ml.
4201 get_translation_mode(ascii).
4202
4203 % TO DO: provide better stack-based setting/unsetting of modes or use options parameter
4204 set_translation_mode(ascii) :- !, retractall(unicode_mode), retractall(latex_mode), retractall(atelierb_mode(_)).
4205 set_translation_mode(unicode) :- !, set_unicode_mode.
4206 set_translation_mode(latex) :- !, set_latex_mode.
4207 set_translation_mode(atelierb) :- !, set_atelierb_mode(native).
4208 set_translation_mode(atelierb_pp) :- !, set_atelierb_mode(prover(pp)). % translation for PP/ML prover
4209 set_translation_mode(atelierb_ml) :- !, set_atelierb_mode(prover(ml)).
4210 set_translation_mode(Mode) :- add_internal_error('Illegal mode:',set_translation_mode(Mode)).
4211
4212 unset_translation_mode(ascii) :- !.
4213 unset_translation_mode(unicode) :- !,unset_unicode_mode.
4214 unset_translation_mode(latex) :- !,unset_latex_mode.
4215 unset_translation_mode(atelierb) :- !,unset_atelierb_mode.
4216 unset_translation_mode(atelierb_pp) :- !,unset_atelierb_mode.
4217 unset_translation_mode(atelierb_ml) :- !,unset_atelierb_mode.
4218 unset_translation_mode(Mode) :- add_internal_error('Illegal mode:',unset_translation_mode(Mode)).
4219
4220 with_translation_mode(Mode, Call) :-
4221 get_translation_mode(OldMode),
4222 (OldMode == Mode -> Call ;
4223 set_translation_mode(ascii), % Clear all existing translation mode settings first
4224 set_translation_mode(Mode),
4225 call_cleanup(Call, set_translation_mode(OldMode))
4226 % FIXME This might not restore all translation modes fully!
4227 % For example, if both unicode_mode and latex_mode are set,
4228 % then with_translation_mode(ascii, ...) will only restore unicode_mode.
4229 % Not sure if this might cause problems for some code.
4230 ).
4231
4232 % The language mode is currently linked to the animation minor mode,
4233 % so be careful when changing it!
4234 % TODO Allow overriding the language for translate without affecting the animation mode
4235
4236 get_language_mode(csp_and(Lang)) :-
4237 csp_with_bz_mode,
4238 !,
4239 (animation_minor_mode(Lang) -> true ; Lang = b).
4240 get_language_mode(Lang) :- animation_minor_mode(Lang), !.
4241 get_language_mode(Lang) :- animation_mode(Lang).
4242
4243 set_language_mode(csp_and(Lang)) :-
4244 !,
4245 set_animation_mode(csp_and_b),
4246 (Lang == b -> true ; set_animation_minor_mode(Lang)).
4247 set_language_mode(csp) :- !, set_animation_mode(csp).
4248 set_language_mode(xtl) :- !, set_animation_mode(xtl).
4249 set_language_mode(sequent_prover) :- !, set_animation_mode(xtl), set_animation_minor_mode(sequent_prover).
4250 set_language_mode(b) :- !, set_animation_mode(b).
4251 set_language_mode(Lang) :-
4252 set_animation_mode(b),
4253 set_animation_minor_mode(Lang).
4254
4255 with_language_mode(Lang, Call) :-
4256 get_language_mode(OldLang),
4257 (OldLang == Lang -> Call ;
4258 set_language_mode(Lang),
4259 call_cleanup(Call, set_language_mode(OldLang))
4260 % FIXME This might not restore all animation modes fully!
4261 % It's apparently possible to have multiple animation minor modes,
4262 % which get/set_language_mode doesn't handle.
4263 % (Are multiple animation minor modes actually used anywhere?)
4264 ).
4265
4266 exists_symbol --> {latex_mode},!, "\\exists ".
4267 exists_symbol --> {unicode_mode},!, pp_colour_code(magenta),[8707],pp_colour_code(reset).
4268 exists_symbol --> pp_colour_code(blue),"#",pp_colour_code(reset).
4269 forall_symbol --> {latex_mode},!, "\\forall ".
4270 forall_symbol --> {unicode_mode},!, pp_colour_code(magenta),[8704],pp_colour_code(reset).
4271 forall_symbol --> pp_colour_code(blue),"!",pp_colour_code(reset).
4272 dot_symbol --> {latex_mode},!, "\\cdot ".
4273 dot_symbol --> {unicode_mode},!, [183]. %"·". % dot also used in Rodin
4274 dot_symbol --> ".".
4275 dot_bullet_symbol --> {latex_mode},!, "\\cdot ".
4276 dot_bullet_symbol --> [183]. %"·". % dot also used in Rodin
4277 set_brackets(X,Y) :- latex_mode,!,X='\\{', Y='\\}'.
4278 set_brackets('{','}').
4279 left_set_bracket --> {latex_mode},!, "\\{ ".
4280 left_set_bracket --> "{".
4281 right_set_bracket --> {latex_mode},!, "\\} ".
4282 right_set_bracket --> "}".
4283 maplet_symbol --> {latex_mode},!, "\\mapsto ".
4284 maplet_symbol --> {unicode_mode},!, [8614].
4285 maplet_symbol --> "|->". % also provide option to use colours? pp_colour_code(blue) ,...
4286
4287 lambda_symbol --> {unicode_mode},!, [955]. % '\x3BB\'
4288 lambda_symbol --> {latex_mode},!, "\\lambda ".
4289 lambda_symbol --> pp_colour_code(blue),"%",pp_colour_code(reset).
4290
4291 and_symbol --> {unicode_mode},!, [8743]. % ''\x2227\''
4292 and_symbol --> {latex_mode},!, "\\wedge ".
4293 and_symbol --> "&".
4294
4295 hash_card_symbol --> {latex_mode},!, "\\# ".
4296 hash_card_symbol --> "#".
4297 ldots --> {latex_mode},!, "\\ldots ".
4298 ldots --> "...".
4299
4300 empty_set_symbol --> {get_preference(translate_print_all_sequences,true)},!, pp_empty_sequence.
4301 empty_set_symbol --> {unicode_mode},!, [8709].
4302 empty_set_symbol --> {latex_mode},!, "\\emptyset ".
4303 empty_set_symbol --> "{}".
4304
4305 underscore_symbol --> {latex_mode},!, "\\_".
4306 underscore_symbol --> "_".
4307
4308 string_start_symbol --> {latex_mode},!, "\\textnormal{``".
4309 string_start_symbol --> pp_colour_code(blue), """".
4310 string_end_symbol --> {latex_mode},!, "''}".
4311 string_end_symbol --> pp_colour_code(reset), """".
4312
4313
4314 unary_postfix_in_mode(Op,Trans2,Prio) :-
4315 unary_postfix(Op,Trans,Prio), % write(op(Op,Trans)),nl,
4316 translate_in_mode(Op,Trans,Trans2).
4317
4318 binary_infix_in_mode(Op,Trans2,Prio,Assoc) :-
4319 binary_infix(Op,Trans,Prio,Assoc), % write(op(Op,Trans)),nl,
4320 translate_in_mode(Op,Trans,Trans2).
4321
4322 latex_integer_set_translation('NATURAL', '\\mathbb N '). % \nat in bsymb.sty
4323 latex_integer_set_translation('NATURAL1', '\\mathbb N_1 '). % \natn
4324 latex_integer_set_translation('INTEGER', '\\mathbb Z '). % \intg
4325 latex_integer_set_translation('REAL', '\\mathbb R '). % \intg
4326
4327 latex_translation(empty_set, '\\emptyset ').
4328 latex_translation(implication, '\\mathbin\\Rightarrow ').
4329 latex_translation(conjunct,'\\wedge ').
4330 latex_translation(disjunct,'\\vee ').
4331 latex_translation(equivalence,'\\mathbin\\Leftrightarrow ').
4332 latex_translation(negation,'\\neg ').
4333 latex_translation(not_equal,'\\neq ').
4334 latex_translation(less_equal,'\\leq ').
4335 latex_translation(less_equal_real,'\\leq ').
4336 latex_translation(greater_equal,'\\geq ').
4337 latex_translation(member,'\\in ').
4338 latex_translation(not_member,'\\not\\in ').
4339 latex_translation(subset,'\\subseteq ').
4340 latex_translation(subset_strict,'\\subset ').
4341 latex_translation(not_subset,'\\not\\subseteq ').
4342 latex_translation(not_subset_strict,'\\not\\subset ').
4343 latex_translation(union,'\\cup ').
4344 latex_translation(intersection,'\\cap ').
4345 latex_translation(couple,'\\mapsto ').
4346 latex_translation(cartesian_product,'\\times').
4347 latex_translation(rec,'\\mathit{rec}').
4348 latex_translation(struct,'\\mathit{struct}').
4349 latex_translation(convert_bool,'\\mathit{bool}').
4350 latex_translation(max,'\\mathit{max}').
4351 latex_translation(max_real,'\\mathit{max}').
4352 latex_translation(min,'\\mathit{min}').
4353 latex_translation(min_real,'\\mathit{min}').
4354 latex_translation(modulo,'\\mod ').
4355 latex_translation(card,'\\mathit{card}').
4356 latex_translation(successor,'\\mathit{succ}').
4357 latex_translation(predecessor,'\\mathit{pred}').
4358 latex_translation(domain,'\\mathit{dom}').
4359 latex_translation(range,'\\mathit{ran}').
4360 latex_translation(size,'\\mathit{size}').
4361 latex_translation(first,'\\mathit{first}').
4362 latex_translation(last,'\\mathit{last}').
4363 latex_translation(front,'\\mathit{front}').
4364 latex_translation(tail,'\\mathit{tail}').
4365 latex_translation(rev,'\\mathit{rev}').
4366 latex_translation(seq,'\\mathit{seq}').
4367 latex_translation(seq1,'\\mathit{seq}_{1}').
4368 latex_translation(perm,'\\mathit{perm}').
4369 latex_translation(fin_subset,'\\mathit{FIN}').
4370 latex_translation(fin1_subset,'\\mathit{FIN}_{1}').
4371 latex_translation(first_projection,'\\mathit{prj}_{1}').
4372 latex_translation(second_projection,'\\mathit{prj}_{2}').
4373 latex_translation(pow_subset,'\\mathbb P\\hbox{}'). % POW \pow would require bsymb.sty
4374 latex_translation(pow1_subset,'\\mathbb P_1'). % POW1 \pown would require bsymb.sty
4375 latex_translation(concat,'\\stackrel{\\frown}{~}'). % was '\\hat{~}').
4376 latex_translation(relations,'\\mathbin\\leftrightarrow'). % <->, \rel requires bsymb.sty
4377 latex_translation(total_relation,'\\mathbin{\\leftarrow\\mkern-14mu\\leftrightarrow}'). % <<-> \trel requires bsymb.sty
4378 latex_translation(total_surjection_relation,'\\mathbin{\\leftrightarrow\\mkern-14mu\\leftrightarrow}'). % <<->> \strel requires bsymb.sty
4379 latex_translation(surjection_relation,'\\mathbin{\\leftrightarrow\\mkern-14mu\\rightarrow}'). % <->> \srel requires bsymb.sty
4380 latex_translation(partial_function,'\\mathbin{\\mkern6mu\\mapstochar\\mkern-6mu\\rightarrow}'). % +-> \pfun requires bsymb.sty, but \mapstochar is not supported by Mathjax
4381 latex_translation(partial_injection,'\\mathbin{\\mkern9mu\\mapstochar\\mkern-9mu\\rightarrowtail}'). % >+> \pinj requires bsymb.sty
4382 latex_translation(partial_surjection,'\\mathbin{\\mkern6mu\\mapstochar\\mkern-6mu\\twoheadrightarrow}'). % >+> \psur requires bsymb.sty
4383 latex_translation(total_function,'\\mathbin\\rightarrow'). % --> \tfun would require bsymb.sty
4384 latex_translation(total_surjection,'\\mathbin\\twoheadrightarrow'). % -->> \tsur requires bsymb.sty
4385 latex_translation(total_injection,'\\mathbin\\rightarrowtail'). % >-> \tinj requires bsymb.sty
4386 latex_translation(total_bijection,'\\mathbin{\\rightarrowtail\\mkern-18mu\\twoheadrightarrow}'). % >->> \tbij requires bsymb.sty
4387 latex_translation(domain_restriction,'\\mathbin\\lhd'). % <| domres requires bsymb.sty
4388 latex_translation(range_restriction,'\\mathbin\\rhd'). % |> ranres requires bsymb.sty
4389 latex_translation(domain_subtraction,'\\mathbin{\\lhd\\mkern-14mu-}'). % <<| domsub requires bsymb.sty
4390 latex_translation(range_subtraction,'\\mathbin{\\rhd\\mkern-14mu-}'). % |>> ransub requires bsymb.sty
4391 latex_translation(overwrite,'\\mathbin{\\lhd\\mkern-9mu-}'). % <+ \ovl requires bsymb.sty
4392 latex_translation(ring,'\\circ '). % not tested
4393 latex_translation(general_sum,'\\Sigma ').
4394 latex_translation(general_product,'\\Pi ').
4395 latex_translation(lambda,'\\lambda ').
4396 latex_translation(quantified_union,'\\bigcup\\nolimits'). % \Union requires bsymb.sty
4397 latex_translation(quantified_intersection,'\\bigcap\\nolimits'). % \Inter requires bsymb.sty
4398 %latex_translation(truth,'\\top').
4399 %latex_translation(falsity,'\\bot').
4400 latex_translation(truth,'{\\color{olive} \\top}'). % requires \usepackage{xcolor} in Latex
4401 latex_translation(falsity,'{\\color{red} \\bot}').
4402 latex_translation(boolean_true,'{\\color{olive} \\mathit{TRUE}}').
4403 latex_translation(boolean_false,'{\\color{red} \\mathit{FALSE}}').
4404 latex_translation(pred_true,'{\\color{olive} \\mathit{TRUE}}').
4405 latex_translation(pred_false,'{\\color{red} \\mathit{FALSE}}').
4406 latex_translation(reverse,'^{-1}').
4407
4408 ascii_to_unicode(Ascii,Unicode) :-
4409 translate_prolog_constructor(BAst,Ascii), % will not backtrack
4410 unicode_translation(BAst,Unicode).
4411
4412
4413 % can be used to translate Latex shortcuts to B Unicode operators for editors
4414 latex_to_unicode(LatexShortcut,Unicode) :-
4415 latex_to_b_ast(LatexShortcut,BAst),
4416 unicode_translation(BAst,Unicode).
4417 latex_to_unicode(LatexShortcut,Unicode) :- % allow to use B AST names as well
4418 unicode_translation(LatexShortcut,Unicode).
4419 latex_to_unicode(LatexShortcut,Unicode) :-
4420 greek_symbol(LatexShortcut,Unicode).
4421
4422 get_latex_keywords(List) :-
4423 findall(Id,latex_to_unicode(Id,_),Ids),
4424 sort(Ids,List).
4425
4426 get_latex_keywords_with_backslash(BList) :-
4427 get_latex_keywords(List),
4428 maplist(atom_concat('\\'),List,BList).
4429
4430 latex_to_b_ast(and,conjunct).
4431 latex_to_b_ast(bcomp,ring). % bsymb: backwards composition
4432 latex_to_b_ast(bigcap,quantified_intersection).
4433 latex_to_b_ast(bigcup,quantified_union).
4434 latex_to_b_ast(cap,intersection).
4435 latex_to_b_ast(cart,cartesian_product).
4436 latex_to_b_ast(cprod,cartesian_product).
4437 latex_to_b_ast(cdot,dot_symbol).
4438 latex_to_b_ast(cup,union).
4439 latex_to_b_ast(dprod,direct_product).
4440 latex_to_b_ast(dres,domain_restriction).
4441 latex_to_b_ast(dsub,domain_subtraction).
4442 latex_to_b_ast(emptyset,empty_set).
4443 latex_to_b_ast(exp,power_of).
4444 %latex_to_b_ast(fcomp,composition). % bsymb: forwards composition
4445 latex_to_b_ast(geq,greater_equal).
4446 latex_to_b_ast(implies,implication).
4447 latex_to_b_ast(in,member).
4448 latex_to_b_ast(int,'INTEGER').
4449 latex_to_b_ast(intg,'INTEGER'). % from bsymb
4450 latex_to_b_ast(lambda,lambda).
4451 latex_to_b_ast(land,conjunct).
4452 latex_to_b_ast(leq,less_equal).
4453 latex_to_b_ast(leqv,equivalence).
4454 latex_to_b_ast(lhd,domain_restriction).
4455 latex_to_b_ast(limp,implication).
4456 latex_to_b_ast(lor,disjunct).
4457 latex_to_b_ast(lnot,negation).
4458 latex_to_b_ast(mapsto,couple).
4459 latex_to_b_ast(nat,'NATURAL').
4460 latex_to_b_ast(natn,'NATURAL1').
4461 latex_to_b_ast(neg,negation).
4462 latex_to_b_ast(neq,not_equal).
4463 latex_to_b_ast(nin,not_member).
4464 latex_to_b_ast(not,negation).
4465 latex_to_b_ast(nsubseteq,not_subset).
4466 latex_to_b_ast(nsubset,not_subset_strict).
4467 latex_to_b_ast(or,disjunct).
4468 %latex_to_b_ast(ovl,overwrite).
4469 latex_to_b_ast(pfun,partial_function).
4470 latex_to_b_ast(pinj,partial_injection).
4471 latex_to_b_ast(psur,partial_surjection).
4472 latex_to_b_ast(pow,pow_subset).
4473 latex_to_b_ast(pown,pow1_subset).
4474 latex_to_b_ast(pprod,parallel_product).
4475 latex_to_b_ast(qdot,dot_symbol).
4476 latex_to_b_ast(real,'REAL').
4477 latex_to_b_ast(rel,relations).
4478 latex_to_b_ast(rhd,range_restriction).
4479 latex_to_b_ast(rres,range_restriction).
4480 latex_to_b_ast(rsub,range_subtraction).
4481 latex_to_b_ast(srel,surjection_relation).
4482 latex_to_b_ast(subseteq,subset).
4483 latex_to_b_ast(subset,subset_strict).
4484 latex_to_b_ast(tbij,total_bijection).
4485 latex_to_b_ast(tfun,total_function).
4486 latex_to_b_ast(tinj,total_injection).
4487 latex_to_b_ast(trel,total_relation).
4488 latex_to_b_ast(tsrel,total_surjection_relation).
4489 latex_to_b_ast(tsur,total_surjection).
4490 latex_to_b_ast(upto,interval).
4491 latex_to_b_ast(vee,disjunct).
4492 latex_to_b_ast(wedge,conjunct).
4493 latex_to_b_ast('INT','INTEGER').
4494 latex_to_b_ast('NAT','NATURAL').
4495 latex_to_b_ast('N','NATURAL').
4496 latex_to_b_ast('Pi',general_product).
4497 latex_to_b_ast('POW',pow_subset).
4498 latex_to_b_ast('REAL','REAL').
4499 latex_to_b_ast('Rightarrow',implication).
4500 latex_to_b_ast('Sigma',general_sum).
4501 latex_to_b_ast('Leftrightarrow',equivalence).
4502 latex_to_b_ast('Inter',quantified_intersection).
4503 latex_to_b_ast('Union',quantified_union).
4504 latex_to_b_ast('Z','INTEGER').
4505
4506 unicode_translation(implication, '\x21D2\').
4507 unicode_translation(conjunct,'\x2227\').
4508 unicode_translation(disjunct,'\x2228\').
4509 unicode_translation(negation,'\xAC\').
4510 unicode_translation(equivalence,'\x21D4\').
4511 unicode_translation(not_equal,'\x2260\').
4512 unicode_translation(less_equal,'\x2264\').
4513 unicode_translation(less_equal_real,'\x2264\').
4514 unicode_translation(greater_equal,'\x2265\').
4515 unicode_translation(member,'\x2208\').
4516 unicode_translation(not_member,'\x2209\').
4517 unicode_translation(subset,'\x2286\').
4518 unicode_translation(subset_strict,'\x2282\').
4519 unicode_translation(not_subset,'\x2288\').
4520 unicode_translation(not_subset_strict,'\x2284\').
4521 unicode_translation(supseteq,'\x2287\'). % ProB parser supports unicode symbol by reversing arguments
4522 unicode_translation(supset_strict,'\x2283\'). % ditto
4523 unicode_translation(not_supseteq,'\x2289\'). % ditto
4524 unicode_translation(not_supset_strict,'\x2285\'). % ditto
4525 unicode_translation(union,'\x222A\').
4526 unicode_translation(intersection,'\x2229\').
4527 unicode_translation(cartesian_product,'\xD7\'). % also 0x2217 in Camille or 0x2A2F (vector or cross product) in IDP
4528 unicode_translation(couple,'\x21A6\').
4529 unicode_translation(div,'\xF7\').
4530 unicode_translation(multiplication,'\x2217\') :- eventb_translation_mode. % Rodin asterisk operator
4531 unicode_translation(minus,'\x2212\') :- eventb_translation_mode. % Rodin minus
4532 unicode_translation(unary_minus,'\x2212\') :- eventb_translation_mode.
4533 unicode_translation(dot_symbol,'\xB7\'). % not a B AST operator, cf dot_symbol 183
4534 unicode_translation(floored_div,'\xF7\') :-
4535 animation_minor_mode(tla). % should we provide another Unicode character here for B?
4536 unicode_translation(power_of,'\x02C4\') :- \+ eventb_translation_mode. % version of ^, does not exist in Rodin ?!, upwards arrow x2191 used below for restrict front
4537 unicode_translation(power_of,'\x5E\') :- eventb_translation_mode.
4538 unicode_translation(power_of_real,'\x02C4\').
4539 unicode_translation(interval,'\x2025\').
4540 unicode_translation(domain_restriction,'\x25C1\').
4541 unicode_translation(domain_subtraction,'\x2A64\').
4542 unicode_translation(range_restriction,'\x25B7\').
4543 unicode_translation(range_subtraction,'\x2A65\').
4544 unicode_translation(relations,'\x2194\').
4545 unicode_translation(partial_function,'\x21F8\').
4546 unicode_translation(total_function,'\x2192\').
4547 unicode_translation(partial_injection,'\x2914\').
4548 unicode_translation(partial_surjection,'\x2900\').
4549 unicode_translation(total_injection,'\x21A3\').
4550 unicode_translation(total_surjection,'\x21A0\').
4551 unicode_translation(total_bijection,'\x2916\').
4552 unicode_translation('INTEGER','\x2124\').
4553 unicode_translation('NATURAL','\x2115\').
4554 unicode_translation('NATURAL1','\x2115\\x2081\') :- \+ eventb_translation_mode. % \x2081\ is subscript 1, not accepted by Rodin
4555 unicode_translation('NATURAL1','\x2115\\x31\') :- eventb_translation_mode. % N1
4556 unicode_translation('REAL','\x211D\'). % 8477 in decimal
4557 unicode_translation(real_set,'\x211D\').
4558 %unicode_translation(bool_set,'\x1D539\'). % conversion used by IDP, but creates SPIO_E_ENCODING_INVALID problem
4559 unicode_translation(pow_subset,'\x2119\').
4560 unicode_translation(pow1_subset,'\x2119\\x2081\') :- \+ eventb_translation_mode. % \x2081\ is subscript 1
4561 unicode_translation(pow1_subset,'\x2119\\x31\') :- eventb_translation_mode. % P1
4562 unicode_translation(lambda,'\x3BB\').
4563 unicode_translation(general_product,'\x220F\').
4564 unicode_translation(general_sum,'\x2211\').
4565 unicode_translation(quantified_union,'\x22C3\'). % 8899 in decimal
4566 unicode_translation(quantified_intersection,'\x22C2\'). % 8898 in decimal
4567 unicode_translation(empty_set,'\x2205\').
4568 unicode_translation(truth,'\x22A4\'). % 8868 in decimal
4569 unicode_translation(falsity,'\x22A5\'). % 8869 in decimal
4570 unicode_translation(direct_product,'\x2297\').
4571 unicode_translation(parallel_product,'\x2225\').
4572 unicode_translation(reverse,'\x207B\\xB9\') :- \+ eventb_translation_mode. % the one ¹ is ASCII 185, this symbol is not accepted by Rodin
4573 unicode_translation(reverse,'\x223c\') :- eventb_translation_mode. % tilde operator used by Rodin
4574 % unicode_translation(infinity,'\x221E\'). % 8734 in decimal
4575 unicode_translation(concat,'\x2312\'). % Arc character
4576 unicode_translation(insert_front,'\x21FE\').
4577 unicode_translation(insert_tail,'\x21FD\').
4578 unicode_translation(restrict_front,'\x2191\'). % up arrow
4579 unicode_translation(restrict_tail,'\x2192\').
4580 unicode_translation(forall, '\x2200\'). % usually forall_symbol used
4581 unicode_translation(exists, '\x2203\'). % usually exists_symbol used
4582 unicode_translation(eqeq,'\x225c\').
4583
4584 unicode_translation(total_relation,'\xE100\') :- force_eventb_rodin_mode. % use this custom symbol only in forced Rodin mode, cannot be displayed by most editors, but is important for BPR proof files
4585 unicode_translation(surjection_relation,'\xE101\') :- force_eventb_rodin_mode. % ditto
4586 unicode_translation(total_surjection_relation,'\xE102\') :- force_eventb_rodin_mode. % ditto
4587 unicode_translation(overwrite,'\xE103\') :- force_eventb_rodin_mode. % ditto, from kernel_lang_20.pdf
4588 unicode_translation(ring,'\x2218\'). % from Event-B
4589 unicode_translation(set_subtraction,'\x2216\'). % used by Rodin
4590 unicode_translation(typeof,'\x2982\'). % Event-B oftype operator
4591
4592 % see Chapter 3 of Atelier-B prover manual:
4593 %atelierb_pp_translation(E,PP,_) :- write(pp(PP,E)),nl,fail.
4594 atelierb_pp_translation(set_minus,pp,'_moinsE'). % is set_subtraction ??
4595 atelierb_pp_translation(cartesian_product,pp,'_multE').
4596 atelierb_pp_translation('INTEGER',_,'INTEGER').
4597 %atelierb_pp_translation('INT','(MININT..MAXINT)'). % does not seem necessary
4598 atelierb_pp_translation('NATURAL',_,'NATURAL').
4599 atelierb_pp_translation('NATURAL1',_,'(NATURAL - {0})').
4600 atelierb_pp_translation('NAT1',_,'(NAT - {0})').
4601 %atelierb_pp_translation('NAT','(0..MAXINT)'). % does not seem necessary
4602 %atelierb_pp_translation('NAT1','(1..MAXINT)'). % does not seem necessary
4603 atelierb_pp_translation(truth,_,btrue).
4604 atelierb_pp_translation(falsity,_,bfalse).
4605 atelierb_pp_translation(boolean_true,_,'TRUE').
4606 atelierb_pp_translation(boolean_false,_,'FALSE').
4607 atelierb_pp_translation(empty_sequence,_,'{}').
4608
4609
4610
4611 quantified_in_mode(F,S) :-
4612 quantified(F,S1), translate_in_mode(F,S1,S).
4613
4614 translate_in_mode(F,S1,Result) :-
4615 ? ( unicode_mode, unicode_translation(F,S) -> true
4616 ; latex_mode, latex_translation(F,S) -> true
4617 ; atelierb_mode(prover(PPML)), atelierb_pp_translation(F,PPML,S) -> true
4618 ; colour_translation(F,S1,S) -> true
4619 ; S1=S),
4620 (colour_translation(F,S,Res) -> Result=Res ; Result=S).
4621
4622 :- use_module(tools_printing,[get_terminal_colour_code/2, no_color/0]).
4623 use_colour_codes :- \+ no_color,
4624 get_preference(pp_with_terminal_colour,true).
4625 colour_translation(F,S1,Result) :- use_colour_codes,
4626 colour_construct(F,Colour),!,
4627 get_terminal_colour_code(Colour,R1),
4628 get_terminal_colour_code(reset,R2),
4629 ajoin([R1,S1,R2],Result).
4630 colour_construct(pred_true,green).
4631 colour_construct(pred_false,red).
4632 colour_construct(boolean_true,green).
4633 colour_construct(boolean_false,red).
4634 colour_construct(truth,green).
4635 colour_construct(falsity,red).
4636 colour_construct(_,blue).
4637
4638 % pretty print a colour code if colours are enabled:
4639 pp_colour_code(Colour) --> {use_colour_codes,get_terminal_colour_code(Colour,C), atom_codes(C,CC)},!,CC.
4640 pp_colour_code(_) --> [].
4641
4642
4643 quantified(general_sum,'SIGMA').
4644 quantified(general_product,'PI').
4645 quantified(quantified_union,'UNION').
4646 quantified(quantified_intersection,'INTER').
4647 quantified(lambda,X) :- atom_codes(X,[37]).
4648 quantified(forall,'!').
4649 quantified(exists,'#').
4650
4651
4652 translate_prolog_constructor(C,R) :- unary_prefix(C,R,_),!.
4653 translate_prolog_constructor(C,R) :- unary_postfix(C,R,_),!.
4654 translate_prolog_constructor(C,R) :- binary_infix_in_mode(C,R,_,_),!.
4655 translate_prolog_constructor(C,R) :- function_like_in_mode(C,R),!.
4656 translate_prolog_constructor(C,R) :- constants_in_mode(C,R),!.
4657 translate_prolog_constructor(C,R) :- quantified_in_mode(C,R),!.
4658
4659 % translate the Prolog constuctor of an AST node into a form for printing to the user
4660 translate_prolog_constructor_in_mode(Constructor,Result) :-
4661 unicode_mode,
4662 unicode_translation(Constructor,Unicode),!, Result=Unicode.
4663 translate_prolog_constructor_in_mode(Constructor,Result) :-
4664 latex_mode,
4665 latex_translation(Constructor,Latex),!, Result=Latex.
4666 translate_prolog_constructor_in_mode(C,R) :- translate_prolog_constructor(C,R).
4667
4668 translate_subst_or_bexpr_in_mode(Mode,TExpr,String) :-
4669 with_translation_mode(Mode, translate_subst_or_bexpr(TExpr,String)).
4670
4671
4672 translate_bexpression_to_unicode(TExpr,String) :-
4673 with_translation_mode(unicode, translate_bexpression(TExpr,String)).
4674
4675 translate_bexpression(TExpr,String) :-
4676 (pp_expr(TExpr,String) -> true
4677 ; add_error(translate_bexpression,'Could not translate bexpression: ',TExpr),String='???').
4678
4679 translate_bexpression_to_codes(TExpr,Codes) :-
4680 reset_pp,
4681 pp_expr(TExpr,_,_LimitReached,Codes,[]).
4682
4683 pp_expr(TExpr,String) :-
4684 translate_bexpression_to_codes(TExpr,Codes),
4685 atom_codes_with_limit(String, Codes).
4686
4687 translate_bexpression_with_limit(T,S) :- translate_bexpression_with_limit(T,200,report_errors,S).
4688 translate_bexpression_with_limit(TExpr,Limit,String) :-
4689 translate_bexpression_with_limit(TExpr,Limit,report_errors,String).
4690 translate_bexpression_with_limit(TExpr,Limit,report_errors,String) :- compound(String),!,
4691 add_internal_error('Result is instantiated to a compound term:',
4692 translate_bexpression_with_limit(TExpr,Limit,report_errors,String)),fail.
4693 translate_bexpression_with_limit(TExpr,Limit,ReportErrors,String) :-
4694 (catch_call(pp_expr_with_limit(TExpr,Limit,String)) -> true
4695 ; (ReportErrors=report_errors,
4696 add_error(translate_bexpression,'Could not translate bexpression: ',TExpr),String='???')).
4697
4698 pp_expr_with_limit(TExpr,Limit,String) :-
4699 set_up_limit_reached(Codes,Limit,LimitReached),
4700 reset_pp,
4701 pp_expr(TExpr,_,LimitReached,Codes,[]),
4702 atom_codes_with_limit(String, Limit, Codes).
4703
4704
4705
4706 % pretty-type an expression, if the expression has a priority >MinPrio, parenthesis
4707 % can be ommitted, if not the expression has to be put into parenthesis
4708 pp_expr_m(TExpr,MinPrio,LimitReached,S,Srest) :-
4709 add_outer_paren(Prio,MinPrio,S,Srest,X,Xrest), % use co-routine to instantiate S as soon as possible
4710 pp_expr(TExpr,Prio,LimitReached,X,Xrest).
4711
4712 :- block add_outer_paren(-,?,?,?,?,?).
4713 add_outer_paren(Prio,MinPrio,S,Srest,X,Xrest) :-
4714 ( Prio > MinPrio -> % was >=, but problem with & / or with same priority or with non associative operators !
4715 S=X, Srest=Xrest
4716 ;
4717 [Open] = "(", [Close] = ")",
4718 S = [Open|X], Xrest = [Close|Srest]).
4719 % warning: if prio not set we will have a pending co-routine and instantiation_error in atom_codes later
4720
4721 :- use_module(translate_keywords,[classical_b_keyword/1, translate_keyword_id/2]).
4722 translated_identifier('_zzzz_binary',R) :- !,
4723 (latex_mode -> R='z''''' ; R='z__'). % TO DO: could cause clash with user IDs
4724 translated_identifier('_zzzz_unary',R) :- !,
4725 (latex_mode -> R='z''' ; R='z_'). % TO DO: ditto
4726 translated_identifier('__RANGE_LAMBDA__',R) :- !,
4727 (latex_mode -> R='\\rho\'' ; unicode_mode -> R= '\x03c1\' % RHO
4728 ; R = 'RANGE_LAMBDA__'). %ditto, could clash with user IDs !!
4729 % TO DO: do we need to treat _prj_arg1__, _prj_arg2__, _lambda_result_ here ?
4730 translated_identifier(ID,Result) :-
4731 latex_mode,!,
4732 my_greek_latex_escape_atom(ID,Greek,Res), %print_message(translate_latex(ID,Greek,Res)),
4733 (Greek=greek -> Result = Res ; ajoin(['\\mathit{',Res,'}'],Result)).
4734 translated_identifier(X,X).
4735
4736 pp_identifier(Atom) --> {id_requires_escaping(Atom), \+ eventb_translation_mode, \+ latex_mode}, !,
4737 ({atelierb_mode(_)}
4738 -> pp_identifier_for_atelierb(Atom)
4739 ; pp_backquoted_identifier(Atom)
4740 ).
4741 pp_identifier(Atom) --> ppatom_opt_scramble(Atom).
4742
4743 % print atom using backquotes, we use same escaping rules as in a string
4744 % requires B parser version 2.9.30 or newer
4745 pp_backquoted_identifier(Atom) --> {atom_codes(Atom,Codes)}, pp_backquoted_id_codes(Codes,outer).
4746 pp_backquoted_id_codes(Codes,_) --> {append(Prefix,[0'. | Suffix],Codes), Suffix=[_|_]},
4747 !, % we need to split the id and quote each part separately; otherwise the parser will complain
4748 % see issue https://github.com/hhu-stups/prob-issues/issues/321
4749 % However, ids with dots are not accepted for constants and variables; so this does not solve all problems
4750 ({id_codes_requires_escaping(Prefix)}
4751 -> "`", pp_codes_opt_scramble(Prefix), "`"
4752 ; pp_codes_opt_scramble(Prefix)
4753 ), ".",
4754 pp_backquoted_id_codes(Suffix,inner).
4755 pp_backquoted_id_codes(Codes,inner) --> % last part of an id with dots
4756 {\+ id_codes_requires_escaping(Codes)},
4757 !, pp_codes_opt_scramble(Codes).
4758 pp_backquoted_id_codes(Codes,_) --> "`", pp_codes_opt_scramble(Codes), "`".
4759
4760 id_codes_requires_escaping(Codes) :- atom_codes(PA,Codes),id_requires_escaping(PA).
4761 :- use_module(tools_strings,[is_simple_classical_b_identifier/1]).
4762 id_requires_escaping(ID) :- classical_b_keyword(ID).
4763 id_requires_escaping(ID) :- \+ is_simple_classical_b_identifier(ID).
4764
4765 pp_identifier_for_atelierb(Atom) -->
4766 {atom_codes(Atom,Codes),
4767 strip_illegal_id_codes(Codes,Change,Codes2),
4768 Change==true},!,
4769 {atom_codes(A2,Codes2)},
4770 ppatom_opt_scramble(A2).
4771 pp_identifier_for_atelierb(Atom) --> ppatom_opt_scramble(Atom).
4772
4773 % remove illegal codes in an identifier (probably EventB or Z)
4774 strip_illegal_id_codes([0'_ | T ],Change,[946 | TR]) :- !, Change=true, strip_illegal_id_codes(T,_,TR).
4775 strip_illegal_id_codes(Codes,Change,Res) :- strip_illegal_id_codes2(Codes,Change,Res).
4776
4777 strip_illegal_id_codes2([],_,[]).
4778 strip_illegal_id_codes2([H|T],Change,Res) :- strip_code(H,Res,TR),!, Change=true, strip_illegal_id_codes2(T,_,TR).
4779 strip_illegal_id_codes2([H|T],Change,[H|TR]) :- strip_illegal_id_codes2(T,Change,TR).
4780
4781 strip_code(46,[0'_, 0'_ |T],T). % replace dot . by two underscores
4782 strip_code(36,[946|T],T) :- T \= [48]. % replace dollar $ by beta unless it is $0 at the end
4783 strip_code(92,[950|T],T). % replace dollar by zeta; probably from Zed
4784 % TODO: add more symbols and ensure that the new codes do not exist
4785
4786
4787
4788 :- use_module(tools,[latex_escape_atom/2]).
4789
4790 greek_or_math_symbol(Symbol) :- greek_symbol(Symbol,_).
4791 % other Latex math symbols
4792 greek_or_math_symbol('varepsilon').
4793 greek_or_math_symbol('varphi').
4794 greek_or_math_symbol('varpi').
4795 greek_or_math_symbol('varrho').
4796 greek_or_math_symbol('varsigma').
4797 greek_or_math_symbol('vartheta').
4798 greek_or_math_symbol('vdash').
4799 greek_or_math_symbol('models').
4800
4801 greek_symbol('Alpha','\x0391\').
4802 greek_symbol('Beta','\x0392\').
4803 greek_symbol('Chi','\x03A7\').
4804 greek_symbol('Delta','\x0394\').
4805 greek_symbol('Epsilon','\x0395\').
4806 greek_symbol('Eta','\x0397\').
4807 greek_symbol('Gamma','\x0393\').
4808 greek_symbol('Iota','\x0399\').
4809 greek_symbol('Kappa','\x039A\').
4810 greek_symbol('Lambda','\x039B\').
4811 greek_symbol('Mu','\x039C\').
4812 greek_symbol('Nu','\x039D\').
4813 greek_symbol('Phi','\x03A6\').
4814 greek_symbol('Pi','\x03A0\').
4815 greek_symbol('Psi','\x03A8\').
4816 greek_symbol('Rho','\x03A1\').
4817 greek_symbol('Omega','\x03A9\').
4818 greek_symbol('Omicron','\x039F\').
4819 greek_symbol('Sigma','\x03A3\').
4820 greek_symbol('Theta','\x0398\').
4821 greek_symbol('Upsilon','\x03A5\').
4822 greek_symbol('Xi','\x039E\').
4823 greek_symbol('alpha','\x03B1\').
4824 greek_symbol('beta','\x03B2\').
4825 greek_symbol('delta','\x03B4\').
4826 greek_symbol('chi','\x03C7\').
4827 greek_symbol('epsilon','\x03B5\').
4828 greek_symbol('eta','\x03B7\').
4829 greek_symbol('gamma','\x03B3\').
4830 greek_symbol('iota','\x03B9\').
4831 greek_symbol('kappa','\x03BA\').
4832 greek_symbol('lambda','\x03BB\').
4833 greek_symbol('mu','\x03BC\').
4834 greek_symbol('nu','\x03BD\').
4835 greek_symbol('omega','\x03C9\').
4836 greek_symbol('omicron','\x03BF\').
4837 greek_symbol('pi','\x03C0\').
4838 greek_symbol('phi','\x03C6\').
4839 greek_symbol('psi','\x03C8\').
4840 greek_symbol('rho','\x03C1\').
4841 greek_symbol('sigma','\x03C3\').
4842 greek_symbol('tau','\x03C4\').
4843 greek_symbol('theta','\x03B8\').
4844 greek_symbol('upsilon','\x03C5\').
4845 greek_symbol('xi','\x03BE\').
4846 greek_symbol('zeta','\x03B6\').
4847
4848
4849 my_greek_latex_escape_atom(A,greek,Res) :-
4850 greek_or_math_symbol(A),get_preference(latex_pp_greek_ids,true),!,
4851 atom_concat('\\',A,Res).
4852 my_greek_latex_escape_atom(A,no_greek,EA) :- latex_escape_atom(A,EA).
4853
4854 % ppatom + scramble if BUGYLY is TRUE
4855 ppatom_opt_scramble(Name) --> {get_preference(bugly_pp_scrambling,true)},
4856 % {\+ bmachine:b_top_level_operation(Name)}, % comment in to not change name of B operations
4857 !,
4858 {bugly_scramble_id(Name,ScrName)},
4859 ppatom(ScrName).
4860 ppatom_opt_scramble(Name) -->
4861 {primes_to_unicode(Name, UnicodeName)},
4862 pp_atom_opt_latex(UnicodeName).
4863
4864 % Convert ASCII primes (apostrophes) in identifiers to Unicode primes
4865 % so they can be parsed by the classical B parser.
4866 primes_to_unicode(Name, UnicodeName) :-
4867 atom_codes(Name, Codes),
4868 phrase(primes_to_unicode(Codes), UCodes),
4869 atom_codes(UnicodeName, UCodes).
4870 primes_to_unicode([0'\'|T]) --> !,
4871 "\x2032\",
4872 primes_to_unicode(T).
4873 primes_to_unicode([C|T]) --> !,
4874 [C],
4875 primes_to_unicode(T).
4876 primes_to_unicode([]) --> "".
4877
4878 :- use_module(tools,[b_string_escape_codes/2]).
4879 :- use_module(tools_strings,[convert_atom_to_number/2]).
4880 % a version of ppatom which encodes/quotes symbols inside strings such as quotes "
4881 ppstring_opt_scramble(Name) --> {var(Name)},!,ppatom(Name).
4882 ppstring_opt_scramble(Name) --> {compound(Name)},!,
4883 {add_internal_error('Not an atom: ',ppstring_opt_scramble(Name,_,_))},
4884 "<<" ,ppterm(Name), ">>".
4885 ppstring_opt_scramble(Name) --> {get_preference(bugly_pp_scrambling,true)},!,
4886 pp_bugly_composed_string(Name).
4887 ppstring_opt_scramble(Name) --> {atom_codes(Name,Codes),b_string_escape_codes(Codes,EscCodes)},
4888 pp_codes_opt_latex(EscCodes).
4889
4890 % a version of ppstring_opt_scramble with codes list
4891 pp_codes_opt_scramble(Codes) --> {get_preference(bugly_pp_scrambling,true)},!,
4892 pp_bugly_composed_string_codes(Codes,[]).
4893 pp_codes_opt_scramble(Codes) --> {b_string_escape_codes(Codes,EscCodes)},
4894 pp_codes_opt_latex(EscCodes).
4895
4896 pp_bugly_composed_string(Name) --> {atom_codes(Name,Codes)},
4897 !, % we can decompose the string; scramble each string separately; TODO: provide option to define separators
4898 % idea is that if we have a string with spaces or other special separators we preserve the separators
4899 pp_bugly_composed_string_codes(Codes,[]).
4900
4901 pp_bugly_composed_string_codes([],Acc) --> {atom_codes(Atom,Acc)}, pp_bugly_string(Atom).
4902 pp_bugly_composed_string_codes(List,Acc) --> {decompose_string(List,Seps,T)},!,
4903 {reverse(Acc,Rev),atom_codes(Atom,Rev)}, pp_bugly_string(Atom),
4904 ppcodes(Seps),
4905 pp_bugly_composed_string_codes(T,[]).
4906 pp_bugly_composed_string_codes([H|T],Acc) --> pp_bugly_composed_string_codes(T,[H|Acc]).
4907
4908 decompose_string([Sep|T],[Sep],T) :- bugly_separator(Sep).
4909 % comment in and adapt for domain specific separators:
4910 %decompose_string(List,Seps,T) :- member(Seps,["LEU","DEF","BAL"]), append(Seps,T,List).
4911 %bugly_separator(10).
4912 %bugly_separator(13).
4913 bugly_separator(32).
4914 bugly_separator(0'-).
4915 bugly_separator(0'_).
4916 bugly_separator(0',).
4917 bugly_separator(0'.).
4918 bugly_separator(0';).
4919 bugly_separator(0':).
4920 bugly_separator(0'#).
4921 bugly_separator(0'[).
4922 bugly_separator(0']).
4923 bugly_separator(0'().
4924 bugly_separator(0')).
4925
4926 % scramble and pretty print individual strings or components of strings
4927 pp_bugly_string('') --> !, [].
4928 pp_bugly_string(Name) -->
4929 {convert_atom_to_number(Name,_)},!, % do not scramble numbers; we could check if LibraryStrings is available
4930 pp_atom_opt_latex(Name).
4931 pp_bugly_string(Name) -->
4932 {bugly_scramble_id(Name,ScrName)},
4933 ppatom(ScrName).
4934
4935 % ------------
4936
4937 pp_atom_opt_latex(Name) --> {latex_mode},!,
4938 {my_greek_latex_escape_atom(Name,_,EscName)},
4939 % should we add \mathrm{.} or \mathit{.}?
4940 ppatom(EscName).
4941 pp_atom_opt_latex(Name) --> ppatom(Name).
4942
4943 % a version of pp_atom_opt_latex working with codes
4944 pp_codes_opt_latex(Codes) --> {latex_mode},!,
4945 {atom_codes(Name,Codes),my_greek_latex_escape_atom(Name,_,EscName)},
4946 ppatom(EscName).
4947 pp_codes_opt_latex(Codes) --> ppcodes(Codes).
4948
4949 pp_atom_opt_latex_mathit(Name) --> {latex_mode},!,
4950 {latex_escape_atom(Name,EscName)},
4951 "\\mathit{",ppatom(EscName),"}".
4952 pp_atom_opt_latex_mathit(Name) --> ppatom(Name).
4953
4954 pp_atom_opt_mathit(EscName) --> {latex_mode},!,
4955 % we assume already escaped
4956 "\\mathit{",ppatom(EscName),"}".
4957 pp_atom_opt_mathit(Name) --> ppatom(Name).
4958
4959 pp_space --> {latex_mode},!, "\\ ".
4960 pp_space --> " ".
4961
4962 opt_scramble_id(ID,Res) :- get_preference(bugly_pp_scrambling,true),!,
4963 bugly_scramble_id(ID,Res).
4964 opt_scramble_id(ID,ID).
4965
4966 :- use_module(probsrc(gensym),[gensym/2]).
4967 :- dynamic bugly_scramble_id_cache/2.
4968 bugly_scramble_id(ID,Res) :- var(ID),!, add_internal_error('Illegal call: ',bugly_scramble_id(ID,Res)), ID=Res.
4969 bugly_scramble_id(ID,Res) :- bugly_scramble_id_cache(ID,ScrambledID),!,
4970 Res=ScrambledID.
4971 bugly_scramble_id(ID,Res) :- %write(gen_id(ID,Res)),nl,
4972 genbuglynr(Nr),
4973 gen_bugly_id(Nr,ScrambledID),
4974 assertz(bugly_scramble_id_cache(ID,ScrambledID)),
4975 %format(user_output,'BUGLY scramble ~w --> ~w~n',[ID,ScrambledID]),
4976 Res = ScrambledID.
4977
4978 gen_bugly_id_codes(Nr,[Char|TC]) :- Char is 97+ Nr mod 26,
4979 (Nr> 25 -> N1 is Nr // 26, gen_bugly_id_codes(N1,TC) ; TC=[]).
4980 gen_bugly_id(Nr,ScrambledID) :- gen_bugly_id_codes(Nr,Codes), atom_codes(ScrambledID,[97,97|Codes]).
4981 %gen_bugly_id(Nr,ScrambledID) :- ajoin(['aa',Nr],ScrambledID). % old version using aaNr
4982
4983 :- dynamic bugly_count/1.
4984 bugly_count(0).
4985 genbuglynr(Nr) :-
4986 retract(bugly_count(Nr)), N1 is Nr + 1,
4987 assertz(bugly_count(N1)).
4988
4989
4990 is_lambda_result_id(b(identifier(ID),_,_INFO),Suffix) :- % _INFO=[lambda_result], sometiems _INFO=[]
4991 is_lambda_result_name(ID,Suffix).
4992 is_lambda_result_name(LAMBDA_RESULT,Suffix) :- atomic(LAMBDA_RESULT),
4993 atom_codes(LAMBDA_RESULT,[95,108,97,109,98,100,97,95,114,101,115,117,108,116,95|Suffix]). % _lambda_result_
4994
4995 pp_expr(TE,P) --> %{write('OBSOLETE'),nl,nl},
4996 pp_expr(TE,P,_LimitReached).
4997
4998 pp_expr(TExpr,Prio,_) --> {var(TExpr)},!,"_",{Prio=500}.
4999 pp_expr(_,Prio,LimitReached) --> {LimitReached==limit_reached},!,"...",{Prio=500}.
5000 pp_expr(b(Expr,Type,Info),Prio,LimitReached) --> !,
5001 pp_expr0(Expr,Type,Info,Prio,LimitReached).
5002 pp_expr([H|T],10,LimitReached) --> !, % also allow pp_expr to be used for lists of expressions
5003 pp_expr_l([H|T],LimitReached).
5004 pp_expr(Expr,Prio,LimitReached) -->
5005 pp_expr1(Expr,any,[],Prio,LimitReached).
5006
5007 pp_expr0(identifier(ID),_Type,_Info,Prio,_LimitReached) --> {is_lambda_result_name(ID,Suffix)},!, {Prio=500},
5008 {append("LAMBDA_RESULT___",Suffix,ASCII), atom_codes(R,ASCII)}, ppatom(R).
5009 pp_expr0(Expr,_Type,Info,Prio,_LimitReached) -->
5010 {eventb_translation_mode},
5011 pp_theory_operator(Expr,Info,Prio),!.
5012 pp_expr0(Expr,Type,Info,Prio,LimitReached) -->
5013 {check_info(Expr,Type,Info)},
5014 pp_rodin_label(Expr,Info),
5015 (pp_expr1(Expr,Type,Info,Prio,LimitReached) -> {true}
5016 ; {add_error(translate,'Could not translate:',Expr,Expr),fail}
5017 ).
5018
5019 check_info(Expr,_,Info) :- var(Info), add_error(translate,'Illegal variable info field for expression: ', Expr),fail.
5020 check_info(_,_,_).
5021
5022 pp_theory_operator(general_sum(_,Membercheck,_),_Info,500) -->
5023 {get_texpr_expr(Membercheck,member(_,Arg))},
5024 ppatom('SUM('),pp_expr(Arg,_),ppatom(')').
5025 pp_theory_operator(general_product(_,Membercheck,_),_Info,500) -->
5026 {get_texpr_expr(Membercheck,member(_Couple,Arg))},
5027 ppatom('PRODUCT('),pp_expr(Arg,_),ppatom(')').
5028 pp_theory_operator(function(_,Arg),Info,500) -->
5029 {memberchk_in_info(theory_operator(O,N),Info),decouplise_expr(N,Arg,Args)},
5030 ppatom(O),"(",pp_expr_l_sep(Args,",",_LR),")".
5031 pp_theory_operator(member(Arg,_),Info,500) -->
5032 {memberchk_in_info(theory_operator(O,N),Info),decouplise_expr(N,Arg,Args)},
5033 ppatom(O),"(",pp_expr_l_sep(Args,",",_LR),")".
5034
5035 decouplise_expr(1,E,R) :- !,R=[E].
5036 decouplise_expr(N,E,R) :-
5037 get_texpr_expr(E,couple(A,B)),!,
5038 N2 is N-1,
5039 decouplise_expr(N2,A,R1),append(R1,[B],R).
5040 decouplise_expr(N,E,[E]) :-
5041 print_message(call_failed(decouplise_expr(N,E,_))),nl.
5042
5043 % do not print labels for identifiers (can happen in ANY); not accepted by B parser
5044 pp_rodin_label(identifier(_),_Infos) --> {!}, [].
5045 pp_rodin_label(_Expr,Infos) --> pp_rodin_label(Infos).
5046
5047 % will pretty print (first) rodin or pragma label
5048 pp_rodin_label(_Infos) --> {preference(translate_suppress_rodin_positions_flag,true),!}.
5049 pp_rodin_label(_Infos) --> {preference(bugly_pp_scrambling,true),!}.
5050 pp_rodin_label(Infos) --> {var(Infos)},!, "/* ILLEGAL VARIABLE INFO FIELD */".
5051 pp_rodin_label(Infos) --> {get_info_labels(Infos,Label)},!,
5052 pp_start_label_pragma,
5053 ppatoms_opt_latex(Label),
5054 pp_end_label_pragma.
5055 pp_rodin_label(Infos) --> {preference(pp_wd_infos,true)},!, pp_wd_info(Infos).
5056 pp_rodin_label(_Infos) --> [].
5057
5058 % print infos about well-definedness attached to AST node:
5059 pp_wd_info(Infos) --> {member(discharged_wd_po,Infos)},!, "/*D",
5060 ({member(contains_wd_condition,Infos)} -> "-WD*/ " ; "*/ ").
5061 pp_wd_info(Infos) --> {member(contains_wd_condition,Infos)},!, "/*WD*/ ".
5062 pp_wd_info(_Infos) --> [].
5063
5064 pp_start_label_pragma -->
5065 {(atelierb_mode(prover(_))
5066 ; get_preference(translate_print_typing_infos,true))}, % proxy for parseable;
5067 % set by translate_bvalue_to_parseable_classicalb; important for parsertests with labels
5068 !,
5069 "/*@label ".
5070 pp_start_label_pragma --> "/* @". % shorter version, for viewing in UI
5071 pp_end_label_pragma --> " */ ".
5072
5073 ppatoms([]) --> !, [].
5074 ppatoms([ID|T]) --> !,ppatom(ID), " ", ppatoms(T).
5075 ppatoms(X) --> {add_error(ppatoms,'Not a list of atoms: ',ppatoms(X))}.
5076
5077 ppatoms_opt_latex([]) --> !, [].
5078 ppatoms_opt_latex([ID]) --> !,pp_atom_opt_latex(ID).
5079 ppatoms_opt_latex([ID|T]) --> !,pp_atom_opt_latex(ID), " ", ppatoms_opt_latex(T).
5080 ppatoms_opt_latex(X) --> {add_error(ppatoms_opt_latex,'Not a list of atoms: ',ppatoms_opt_latex(X))}.
5081
5082 %:- use_module(bsyntaxtree,[is_set_type/2]).
5083 :- load_files(library(system), [when(compile_time), imports([environ/2])]).
5084 pp_expr1(Expr,_,_,Prio,_) --> {var(Expr)},!,"_",{Prio=500}.
5085 pp_expr1(event_b_comprehension_set(Ids,E,P),Type,_Info,Prio,LimitReached) -->
5086 {\+ eventb_translation_mode, b_ast_cleanup:rewrite_event_b_comprehension_set(Ids,E,P,Type, NewExpression)},!,
5087 pp_expr(NewExpression,Prio,LimitReached).
5088 pp_expr1(union(b(event_b_identity,Type,_), b(closure(Rel),Type,_)),_,Info,500,LimitReached) -->
5089 /* closure(Rel) = id \/ closure1(Rel) */
5090 {member_in_info(was(reflexive_closure),Info)},!,
5091 "closure(",pp_expr(Rel,_,LimitReached),")".
5092 pp_expr1(comprehension_set([_],_),_,Info,500,_LimitReached) -->
5093 {memberchk_in_info(freetype(P),Info),!},ppatom(P).
5094 % used instead of constants(Expr,Symbol) case below:
5095 pp_expr1(greater_equal(A,Y),Type,Info,Prio,LimitReached) --> % x:NATURAL was rewritten to x>=0, see test 499, 498
5096 {memberchk_in_info(was(member(A,B)),Info), get_integer(Y,_)},
5097 pp_expr1(member(A,B),Type,Info,Prio,LimitReached).
5098 pp_expr1(comprehension_set([TID],b(B,_,_)),Type,Info,Prio,LimitReached) -->
5099 {memberchk_in_info(was(integer_set(S)),Info)},
5100 {S='INTEGER' -> B=truth
5101 ; get_texpr_id(TID,ID),
5102 B=greater_equal(TID2,Y), get_integer(Y,I),
5103 get_texpr_id(TID2,ID),
5104 (I=0 -> S='NATURAL' ; I=1,S='NATURAL1')}, % TO DO: check bounds
5105 !,
5106 pp_expr1(integer_set(S),Type,Info,Prio,LimitReached).
5107 pp_expr1(interval(b(A,_,_),B),Type,Info,Prio,LimitReached) -->
5108 {memberchk_in_info(was(integer_set(S)),Info)},
5109 {B=b(max_int,integer,_)}, % TO DO ? allow value(int(Mx))
5110 {A=min_int -> S='INT' ; A=integer(0) -> S='NAT' ; A=integer(1),S='NAT1'},
5111 !,
5112 pp_expr1(integer_set(S),Type,Info,Prio,LimitReached).
5113 pp_expr1(falsity,_,Info,Prio,LimitReached) --> {memberchk_in_info(was(Pred),Info)},!,
5114 ({(unicode_mode ; latex_mode)}
5115 -> {translate_in_mode(falsity,'falsity',Symbol)},
5116 ppatom(Symbol),
5117 ({get_preference(pp_propositional_logic_mode,true)} -> {true}
5118 ; " ", enter_comment, " ", pp_expr2(Pred,Prio,LimitReached), " ", exit_comment)
5119 ; enter_comment, " falsity ",exit_comment, " ",
5120 pp_expr2(Pred,Prio,LimitReached)). % Pred is not wrapped
5121 pp_expr1(truth,_,Info,Prio,LimitReached) --> {memberchk_in_info(was(Pred),Info)},!,
5122 ({(unicode_mode ; latex_mode)}
5123 -> {translate_in_mode(truth,'truth',Symbol)},
5124 ppatom(Symbol),
5125 ({get_preference(pp_propositional_logic_mode,true)} -> {true}
5126 ; " ",enter_comment, " ", pp_expr2(Pred,Prio,LimitReached), " ", exit_comment)
5127 ; enter_comment, " truth ", exit_comment, " ",
5128 pp_expr2(Pred,Prio,LimitReached)). % Pred is not wrapped
5129 % TO DO: do this for other expressions as well; but then we have to ensure that ast_cleanup generates complete was(_) infos
5130 % :- load_files(library(system), [when(compile_time), imports([environ/2])]). % directive moved above to avoid Spider warning
5131 pp_expr1(event_b_identity,Type,_Info,500,_LimitReached) -->
5132 {\+ eventb_translation_mode}, %{atelierb_mode(prover(_)},
5133 {is_set_type(Type,couple(ElType,ElType))},
5134 !,
5135 "id(", {pretty_normalized_type(ElType,S)},ppatom(S), ")".
5136 pp_expr1(typeset,SType,_Info,500,_LimitReached) --> % normally removed by ast_cleanup
5137 {is_set_type(SType,Type)},
5138 !,
5139 ({normalized_type_requires_outer_paren(Type)} -> "(" ; ""),
5140 {pretty_normalized_type(Type,S)},ppatom(S),
5141 ({normalized_type_requires_outer_paren(Type)} -> ")" ; "").
5142 :- if(environ(prob_safe_mode,true)).
5143 pp_expr1(exists(Parameters,_),_,Info,_Prio,_LimitReached) -->
5144 {\+ member_in_info(used_ids(_),Info),
5145 add_error(translate,'Missing used_ids Info for exists: ',Parameters:Info),fail}.
5146 %pp_expr1(exists(Ids,P1),_,Info,250) --> !, { member_in_info(used_ids(Used),Info)},
5147 % exists_symbol,pp_expr_ids_in_mode(Ids,LimitReached),
5148 % {add_normal_typing_predicates(Ids,P1,P)},
5149 % " /* Used = ", ppterm(Used), " */ ",
5150 % ".",pp_expr_m(P,221).
5151 :- endif.
5152 pp_expr1(Expr,Type,Info,Prio,LimitReached) --> {member_in_info(sharing(ID,Count,_,_),Info),number(Count),Count>1},!,
5153 "( ",enter_comment," CSE ",ppnumber(ID), ":#", ppnumber(Count),
5154 ({member_in_info(negated_cse,Info)} -> " (neg) " ; " "),
5155 ({member_in_info(contains_wd_condition,Info)} -> " (wd) " ; " "),
5156 exit_comment, " ",
5157 {delete(Info,sharing(_,_,_,_),Info2)},
5158 pp_expr1(Expr,Type,Info2,Prio,LimitReached), ")".
5159 %pp_expr1(Expr,_,Info,Prio) --> {member_in_info(contains_wd_condition,Info)},!,
5160 % "( /* (wd) */ ", pp_expr2(Expr,Prio), ")".
5161 % pp_expr1(Expr,subst,_Info,Prio) --> !, translate_subst2(Expr,Prio). % TO DO: also allow substitutions here
5162 pp_expr1(value(V),Type,_,Prio,LimitReached) --> !,
5163 {(nonvar(V),V=closure(_,_,_) -> Prio=300 ; Prio=500)}, pp_value_with_type(V,Type,LimitReached).
5164 pp_expr1(comprehension_set(Ids,P1),_,Info,500,LimitReached) --> !,
5165 pp_comprehension_set(Ids,P1,Info,LimitReached).
5166 %pp_expr1(Expr,_,Info,Prio,LimitReached) --> {pp_is_important_info_field(Expr,Info,_)},
5167 % !, pp_important_infos(Expr,Info), pp_expr2(Expr,Prio,LimitReached).
5168 pp_expr1(first_of_pair(X),_,Info,500,LimitReached) --> {was_eventb_destructor(Info,X,Op,Arg)},!,
5169 ppatom(Op), "(",pp_expr(Arg,_,LimitReached), ")".
5170 pp_expr1(second_of_pair(X),_,Info,500,LimitReached) --> {was_eventb_destructor(Info,X,Op,Arg)},!,
5171 ppatom(Op), "(",pp_expr(Arg,_,LimitReached), ")".
5172 %pp_expr1(let_expression(_Ids,Exprs,_P),_Type,Info,500,LimitReached) -->
5173 % % pretty print direct definition operator calls, which get translated using create_z_let
5174 % % However: the lets can get removed; in which case the translated direct definition will be pretty printed
5175 % % also: what if the body of the let has been modified ??
5176 % {member(was(extended_expr(DirectDefOp)),Info),
5177 % bmachine_eventb:stored_operator_direct_definition(DirectDefOp,_Proj,_Theory,Parameters,_Def,_WD,_TypeParas,_Kind),
5178 % %length(Exprs,Arity),,length(Parameters,Arity1), write(found_dd(DirectDefOp,Arity1,Arity2,Proj,Theory)),nl,
5179 % same_length(Parameters,ActualParas), %same_length(TypeParameters,TP),
5180 % append(ActualParas,_TP,Exprs)
5181 % },
5182 % !,
5183 % ppatom(DirectDefOp),
5184 % pp_expr_wrap_l('(',ActualParas,')',LimitReached).
5185 pp_expr1(Expr,_,_Info,Prio,LimitReached) --> pp_expr2(Expr,Prio,LimitReached).
5186
5187 was_eventb_destructor(Info,X,Op,Arg) :- eventb_translation_mode,
5188 member(was(extended_expr(Op)),Info),peel_projections(X,Arg).
5189 is_projection(first_of_pair(A),A).
5190 is_projection(second_of_pair(A),A).
5191 % peel projections constructed for Event-B destructor operator
5192 peel_projections(b(A,_,_),R) :-
5193 (is_projection(A,RA) -> peel_projections(RA,R)
5194 ; A = freetype_destructor(_,_,R)).
5195
5196
5197 :- public pp_important_infos/4. % debugging utility
5198 pp_important_infos(Expr,Info) -->
5199 {findall(PPI,pp_is_important_info_field(Expr,Info,PPI),PPInfos), PPInfos \= []},
5200 " ", enter_comment, ppterm(PPInfos), exit_comment, " ".
5201 pp_is_important_info_field(_,Infos,'DO_NOT_ENUMERATE'(X)) :- member(prob_annotation('DO_NOT_ENUMERATE'(X)),Infos).
5202 pp_is_important_info_field(exists(_,_),Infos,'LIFT') :- member(allow_to_lift_exists,Infos).
5203 pp_is_important_info_field(exists(_,_),Infos,used_ids(Used)) :- member(used_ids(Used),Infos).
5204 pp_is_important_info_field(exists(_,_),Infos,'(wd)') :- member(contains_wd_condition,Infos).
5205
5206
5207 pp_expr2(Expr,_,_LimitReached) --> {var(Expr)},!,"_".
5208 pp_expr2(_,_,LimitReached) --> {LimitReached==limit_reached},!,"...".
5209
5210 pp_expr2(atom_string(V),500,_) --> !,pp_atom_opt_latex_mathit(V). % hardwired_atom
5211 pp_expr2(global_set(V),500,_) --> !, pp_identifier(V).
5212 pp_expr2(freetype_set(V),500,_) --> !,{pretty_freetype(V,P)},ppatom_opt_scramble(P).
5213 pp_expr2(lazy_lookup_expr(I),500,_) --> !, pp_identifier(I).
5214 pp_expr2(lazy_lookup_pred(I),500,_) --> !, pp_identifier(I).
5215 pp_expr2(identifier(I),500,_) --> !,
5216 {( I=op(Id) -> true; I=Id)},
5217 ( {atomic(Id)} -> ({translated_identifier(Id,TId)},
5218 ({latex_mode} -> ppatom(TId) ; pp_identifier(TId)))
5219 ;
5220 "'",ppterm(Id), "'").
5221 pp_expr2(integer(N),500,_) --> !, ppnumber(N).
5222 pp_expr2(real(N),500,_) --> !, ppatom(N).
5223 pp_expr2(integer_set(S),500,_) --> !,
5224 {integer_set_mapping(S,T)},ppatom(T).
5225 pp_expr2(string(S),500,_) --> !, string_start_symbol, ppstring_opt_scramble(S), string_end_symbol.
5226 pp_expr2(set_extension(Ext),500,LimitReached) --> !, {set_brackets(L,R)},
5227 pp_expr_wrap_l(L,Ext,R,LimitReached).
5228 pp_expr2(sequence_extension(Ext),500,LimitReached) --> !,
5229 pp_begin_sequence,
5230 ({get_preference(translate_print_cs_style_sequences,true)} -> pp_expr_l_sep(Ext,"",LimitReached)
5231 ; pp_expr_l_sep(Ext,",",LimitReached)),
5232 pp_end_sequence.
5233 pp_expr2(assign(LHS,RHS),10,LimitReached) --> !,
5234 pp_expr_wrap_l(',',LHS,'',LimitReached), ":=", pp_expr_wrap_l(',',RHS,'',LimitReached).
5235 pp_expr2(assign_single_id(LHS,RHS),10,LimitReached) --> !, pp_expr2(assign([LHS],[RHS]),10,LimitReached).
5236 pp_expr2(parallel(RHS),10,LimitReached) --> !,
5237 pp_expr_wrap_l('||',RHS,'',LimitReached).
5238 pp_expr2(sequence(RHS),10,LimitReached) --> !,
5239 pp_expr_wrap_l(';',RHS,'',LimitReached).
5240 pp_expr2(event_b_comprehension_set(Ids,E,P1),500,LimitReached) --> !, % normally conversion above should trigger; this is if we call pp_expr for untyped expressions
5241 pp_event_b_comprehension_set(Ids,E,P1,LimitReached).
5242 pp_expr2(recursive_let(Id,S),500,LimitReached) --> !,
5243 ({eventb_translation_mode} -> "" % otherwise we get strange characters in Rodin
5244 ; enter_comment," recursive ID ", pp_expr(Id,_,LimitReached), " ", exit_comment),
5245 pp_expr(S,_,LimitReached).
5246 pp_expr2(image(A,B),300,LimitReached) --> !,
5247 pp_expr_m(A,249,LimitReached),"[", % was 0; but we may have to bracket A; e.g., f <| {2} [{2}] is not ok; 250 is priority of lambda
5248 pp_expr_m(B,0,LimitReached),"]". % was 500, now set to 0: we never need an outer pair of () !?
5249 pp_expr2(function(A,B),300,LimitReached) --> !,
5250 pp_expr_m(A,249,LimitReached), % was 0; but we may have to bracket A; e.g., f <| {2} (2) is not ok; 250 is priority of lambda
5251 pp_function_left_bracket,
5252 pp_expr_m(B,0,LimitReached), % was 500, now set to 0: we never need an outer pair of () !?
5253 pp_function_right_bracket.
5254 pp_expr2(definition(A,B),300,LimitReached) --> !, % definition call; usually inlined,...
5255 ppatom(A),
5256 pp_function_left_bracket,
5257 pp_expr_l_sep(B,",",LimitReached),
5258 pp_function_right_bracket.
5259 pp_expr2(operation_call_in_expr(A,B),300,LimitReached) --> !,
5260 pp_expr_m(A,249,LimitReached),
5261 pp_function_left_bracket,
5262 pp_expr_l_sep(B,",",LimitReached),
5263 pp_function_right_bracket.
5264 pp_expr2(enumerated_set_def(GS,ListEls),200,LimitReached) --> !, % for pretty printing enumerate set defs
5265 {reverse(ListEls,RLE)}, /* they have been inserted in inverse order */
5266 pp_identifier(GS), "=", pp_expr_wrap_l('{',RLE,'}',LimitReached).
5267 pp_expr2(forall(Ids,D1,P),Prio,LimitReached) --> !,
5268 ({eventb_translation_mode} -> {Prio=60} ; {Prio=250}), % in Rodin forall/exists cannot be mixed with &, or, <=>, ...
5269 ({eventb_translation_mode} -> "(" ; ""), % always put brackets around the forall in Rodin
5270 forall_symbol,pp_expr_ids_in_mode(Ids,LimitReached),
5271 {add_normal_typing_predicates(Ids,D1,D)},
5272 dot_symbol,pp_expr_m(b(implication(D,P),pred,[]),221,LimitReached),
5273 ({eventb_translation_mode} -> ")" ; "").
5274 pp_expr2(exists(Ids,P1),Prio,LimitReached) --> !,
5275 ({eventb_translation_mode} -> {Prio=60} ; {Prio=250}), % exists has Prio 250, but dot has 220
5276 ({eventb_translation_mode} -> "(" ; ""), % always put brackets around the exists in Rodin
5277 exists_symbol,pp_expr_ids_in_mode(Ids,LimitReached),
5278 {add_normal_typing_predicates(Ids,P1,P)},
5279 dot_symbol,
5280 ({eventb_translation_mode} -> {MinPrio=29} ; {MinPrio=500}),
5281 % used to be 221, but #x.x>7 or #x.not(...) are not parsed by Atelier-B or ProB, x.x and x.not are parsed as composed identifiers
5282 % In Event-B ∃y·y>x ∧ (y=x+1 ∨ y=x+2) is valid and requires no outer parenthesis (if not on the left side of another predicate!)
5283 pp_expr_m(P,MinPrio,LimitReached),
5284 ({eventb_translation_mode} -> ")" ; "").
5285 pp_expr2(record_field(R,I),250,LimitReached) --> !,
5286 pp_expr_m(R,251,LimitReached),"'",pp_identifier(I).
5287 pp_expr2(rec(Fields),500,LimitReached) --> !,
5288 {function_like_in_mode(rec,Symbol)},
5289 ppatom(Symbol), "(",pp_expr_fields(Fields,LimitReached),")".
5290 pp_expr2(struct(Rec),500,LimitReached) -->
5291 {get_texpr_expr(Rec,rec(Fields)),Val=false ; get_texpr_expr(Rec,value(rec(Fields)))},!,
5292 {function_like_in_mode(struct,Symbol)},
5293 ppatom(Symbol), "(",
5294 ({Val==false} -> pp_expr_fields(Fields,LimitReached)
5295 ; pp_value_l(Fields,',',LimitReached)),
5296 ")".
5297 pp_expr2(freetype_case(_FT,L,Expr),Prio,LimitReached) --> !,
5298 %{Prio=500}, pp_freetype_term('__is_ft_case',FT,L,Expr,LimitReached).
5299 % we now pretty-print it as Expr : ran(L) assuming there is a constant L generated for every case
5300 {FTCons = b(identifier(L),any,[]), RanFTCons = b(range(FTCons),any,[])},
5301 pp_expr(b(member(Expr,RanFTCons),pred,[]),Prio,LimitReached).
5302 pp_expr2(freetype_constructor(_FT,Case,Expr),Prio,LimitReached) --> !,
5303 {FTCons = b(identifier(Case),any,[])},
5304 pp_expr(b(function(FTCons,Expr),any,[]),Prio,LimitReached).
5305 % ppatom_opt_scramble(Case),ppatom('('),pp_expr(Expr,_,LimitReached),ppatom(')').
5306 pp_expr2(freetype_destructor(_FT,Case,Expr),Prio,LimitReached) --> !,
5307 % pretty print it as: Case~(Expr)
5308 {FTCons = b(identifier(Case),any,[]), Destr = b(reverse(FTCons),any,[])},
5309 pp_expr(b(function(Destr,Expr),any,[]),Prio,LimitReached).
5310 % ({unicode_mode}
5311 % -> {unicode_translation(reverse,PowMinus1Symbol)},
5312 % ppatom(Case),ppatom(PowMinus1Symbol), % Note: we do not print the freetype's name FT
5313 % "(",pp_expr_m(Expr,0,LimitReached),")"
5314 % ; pp_freetype_term('__ft~',FT,Case,Expr,LimitReached) % TODO: maybe find better print
5315 % ).
5316 pp_expr2(let_predicate(Ids,Exprs,P),1,LimitReached) --> !,
5317 pp_expr_let_exists(Ids,Exprs,P,LimitReached). % instead of pp_expr_let
5318 pp_expr2(let_expression(Ids,Exprs,P),1,LimitReached) --> !,
5319 pp_expr_let(Ids,Exprs,P,LimitReached).
5320 pp_expr2(let_expression_global(Ids,Exprs,P),1,LimitReached) --> !, " /", "* global *", "/ ",
5321 pp_expr_let(Ids,Exprs,P,LimitReached).
5322 pp_expr2(lazy_let_pred(Id,Expr,P),Pr,LimitReached) --> !, pp_expr2(lazy_let_expr(Id,Expr,P),Pr,LimitReached).
5323 pp_expr2(lazy_let_subst(Id,Expr,P),Pr,LimitReached) --> !, pp_expr2(lazy_let_expr(Id,Expr,P),Pr,LimitReached).
5324 pp_expr2(lazy_let_expr(Id,Expr,P),1,LimitReached) --> !,
5325 pp_expr_let([Id],[Expr],P,LimitReached).
5326 pp_expr2(norm_conjunct(Cond,[]),1,LimitReached) --> !, % norm_conjunct: flattened version generated by b_interpreter_check,...
5327 "( ",pp_expr(Cond,_,LimitReached), ")".
5328 pp_expr2(norm_conjunct(Cond,[H|T]),1,LimitReached) --> !,
5329 "( ",pp_expr(Cond,_,LimitReached), ") ", and_symbol, " (", pp_expr2(norm_conjunct(H,T),_,LimitReached), ")".
5330 pp_expr2(assertion_expression(Cond,Msg,Expr),1,LimitReached) --> !,
5331 " ASSERT_EXPR (",
5332 pp_expr_m(b(convert_bool(Cond),pred,[]),30,LimitReached), ",",
5333 pp_expr_m(string(Msg),30,LimitReached), ",",
5334 pp_expr_m(Expr,30,LimitReached),
5335 " )".
5336 %pp_expr2(assertion_expression(Cond,_Msg,Expr),1) --> !,
5337 % "__ASSERT ",pp_expr_m(Cond,30),
5338 % " IN ", pp_expr_m(Expr,30).
5339 pp_expr2(partition(S,Elems),500,LimitReached) -->
5340 {eventb_translation_mode ;
5341 \+ atelierb_mode(_), length(Elems,Len), Len>50 % we need to print a quadratic number of disjoints
5342 },!,
5343 "partition(",pp_expr(S,_,LimitReached),
5344 ({Elems=[]} -> ")" ; pp_expr_wrap_l(',',Elems,')',LimitReached)).
5345 pp_expr2(partition(S,Elems),500,LimitReached) --> !,
5346 "(",pp_expr(S,_,LimitReached), " = ",
5347 ({Elems=[]} -> "{})"
5348 ; pp_expr_l_sep(Elems,"\\/",LimitReached), pp_all_disjoint(Elems,LimitReached),")").
5349 pp_expr2(finite(S),Prio,LimitReached) --> {\+ eventb_translation_mode}, %{atelierb_mode(_)},
5350 !,
5351 pp_expr2(member(S,b(fin_subset(S),set(any),[])),Prio,LimitReached).
5352 pp_expr2(if_then_else(If,Then,Else),1,LimitReached) --> {animation_minor_mode(z)},!,
5353 "\\IF ",pp_expr_m(If,30,LimitReached),
5354 " \\THEN ",pp_expr_m(Then,30,LimitReached),
5355 " \\ELSE ",pp_expr_m(Else,30,LimitReached).
5356 %pp_expr2(if_then_else(If,Then,Else),1) --> {unicode_mode},!,
5357 % "if ",pp_expr_m(If,30), " then ",pp_expr_m(Then,30), " else ",pp_expr_m(Else,30).
5358 pp_expr2(if_then_else(If,Then,Else),Prio,LimitReached) --> {atelierb_mode(_)},!,
5359 % print IF-THEN-ELSE using a translation that Atelier-B can understand:
5360 {rewrite_if_then_else_expr_to_b(if_then_else(If,Then,Else), NExpr),
5361 get_texpr_type(Then,Type),
5362 NAst = b(NExpr,Type,[])},
5363 % construct {d,x| If => x=Then & not(if) => x=Else}(TRUE)
5364 pp_expr(NAst,Prio,LimitReached).
5365 pp_expr2(if_then_else(If,Then,Else),1,LimitReached) --> !,
5366 pp_atom_opt_mathit('IF'),pp_space, % "IF ",
5367 pp_expr_m(If,30,LimitReached),
5368 pp_space, pp_atom_opt_mathit('THEN'),pp_space, %" THEN ",
5369 pp_expr_m(Then,30,LimitReached),
5370 pp_space, pp_atom_opt_mathit('ELSE'),pp_space, %" ELSE ",
5371 pp_expr_m(Else,30,LimitReached),
5372 pp_space, pp_atom_opt_mathit('END'). %" END"
5373 pp_expr2(kodkod(Id,Identifiers),300,LimitReached) --> !,
5374 "KODKOD_CALL(",ppnumber(Id),": ",pp_expr_ids(Identifiers,LimitReached),")".
5375 pp_expr2(Expr,500,_) -->
5376 {constants_in_mode(Expr,Symbol)},!,ppatom(Symbol).
5377 pp_expr2(equal(A,B),Prio,LimitReached) -->
5378 {get_preference(pp_propositional_logic_mode,true), % a mode for printing propositional logic formuli
5379 is_boolean_value(B,BV),
5380 get_texpr_id(A,_)},!,
5381 ({BV=pred_true} -> pp_expr(A,Prio,LimitReached)
5382 ; pp_expr2(negation(b(equal(A,b(boolean_true,boolean,[])),pred,[])),Prio,LimitReached)).
5383 pp_expr2(Expr,Prio,LimitReached) -->
5384 {functor(Expr,F,1),
5385 unary_prefix(F,Symbol,Prio),!,
5386 arg(1,Expr,Arg),APrio is Prio+1},
5387 ppatom(Symbol),
5388 ({F=unary_minus, eventb_translation_mode} -> "" ; " "), % no space between unary minus and integer literal in Event-B
5389 pp_expr_m(Arg,APrio,LimitReached).
5390 pp_expr2(Expr,500,LimitReached) -->
5391 {functor(Expr,F,1),
5392 unary_prefix_parentheses(F,Symbol),!,
5393 arg(1,Expr,Arg)},
5394 pp_atom_opt_latex(Symbol), "(", pp_expr(Arg,_,LimitReached), ")".
5395 pp_expr2(Expr,Prio,LimitReached) -->
5396 {functor(Expr,F,1),
5397 unary_postfix_in_mode(F,Symbol,Prio),!,
5398 arg(1,Expr,Arg),APrio is Prio+1},
5399 pp_expr_m(Arg,APrio,LimitReached),ppatom(Symbol).
5400 pp_expr2(power_of(Left,Right),Prio,LimitReached) --> {latex_mode},!, % special case, as we need to put {} around RHS
5401 {Prio=200, LPrio is Prio+1, RPrio = Prio},
5402 pp_expr_m(Left,LPrio,LimitReached),
5403 "^{",
5404 pp_expr_m(Right,RPrio,LimitReached),
5405 "}".
5406 pp_expr2(power_of_real(Left,Right),Prio,LimitReached) --> !,
5407 ({get_texpr_expr(Right,convert_real(RI))}
5408 -> pp_expr2(power_of(Left,RI),Prio,LimitReached) % the Atelier-B power_of expects integer exponent
5409 ; pp_external_call('RPOW',[Left,Right],expression,Prio,LimitReached)
5410 ).
5411 pp_expr2(Expr,OPrio,LimitReached) -->
5412 {functor(Expr,F,2),
5413 binary_infix_in_mode(F,Symbol,Prio,Ass),!,
5414 arg(1,Expr,Left),
5415 arg(2,Expr,Right),
5416 ( Ass = left, binary_infix_symbol(Left,Symbol) -> LPrio is Prio-1, RPrio is Prio+1
5417 ; Ass = right, binary_infix_symbol(Right,Symbol) -> LPrio is Prio+1, RPrio is Prio-1
5418 ; LPrio is Prio+1, RPrio is Prio+1)},
5419 % Note: Prio+1 is actually not necessary, Prio would be sufficient, as pp_expr_m uses a strict comparison <
5420 ({always_surround_by_parentheses(F)} -> "(",{OPrio=1000} ; {OPrio=Prio}),
5421 pp_expr_m(Left,LPrio,LimitReached),
5422 " ", ppatom(Symbol), " ",
5423 pp_expr_m(Right,RPrio,LimitReached),
5424 ({always_surround_by_parentheses(F)} -> ")" ; []).
5425 pp_expr2(first_of_pair(X),500,LimitReached) --> {get_texpr_type(X,couple(From,To))},!,
5426 "prj1(", % TO DO: Latex version
5427 ({\+ atelierb_mode(_)} % eventb_translation_mode
5428 -> "" % no need to print types in Event-B or with new parser;
5429 % TODO: also with new parser no longer required; only print in Atelier-B mode
5430 ; {pretty_normalized_type(From,FromT),
5431 pretty_normalized_type(To,ToT)},
5432 pp_atom_opt_latex(FromT), ",", pp_atom_opt_latex(ToT),
5433 ")("
5434 ),
5435 pp_expr(X,_,LimitReached),")".
5436 pp_expr2(second_of_pair(X),500,LimitReached) --> {get_texpr_type(X,couple(From,To))},!,
5437 "prj2(", % TO DO: Latex version
5438 ({\+ atelierb_mode(_)} -> "" % no need to print types in Event-B or with new parser
5439 ; {pretty_normalized_type(From,FromT),
5440 pretty_normalized_type(To,ToT)},
5441 pp_atom_opt_latex(FromT), ",", pp_atom_opt_latex(ToT),
5442 ")("
5443 ),
5444 pp_expr(X,_,LimitReached),")".
5445 pp_expr2(Call,Prio,LimitReached) --> {external_call(Call,Kind,Symbol,Args)},!,
5446 pp_external_call(Symbol,Args,Kind,Prio,LimitReached).
5447 pp_expr2(card(A),500,LimitReached) --> {latex_mode, get_preference(latex_pp_greek_ids,true)},!,
5448 "|",pp_expr_m(A,0,LimitReached),"|".
5449 pp_expr2(Expr,500,LimitReached) -->
5450 {functor(Expr,F,_),
5451 function_like_in_mode(F,Symbol),!,
5452 Expr =.. [F|Args]},
5453 ppatom(Symbol),
5454 ({Args=[]}
5455 -> "" % some operators like pred and succ do not expect arguments
5456 ; pp_expr_wrap_l('(',Args,')',LimitReached)).
5457 pp_expr2(Expr,250,LimitReached) -->
5458 {functor(Expr,F,3),
5459 quantified_in_mode(F,Symbol),
5460 Expr =.. [F,Ids,P1,E],
5461 !,
5462 add_normal_typing_predicates(Ids,P1,P)},
5463 ppatom(Symbol),pp_expr_ids(Ids,LimitReached),".(",
5464 pp_expr_m(P,11,LimitReached),pp_such_that_bar(E),
5465 pp_expr_m(E,11,LimitReached),")".
5466 pp_expr2(Expr,Prio,LimitReached) -->
5467 {functor(Expr,F,N),
5468 (debug_mode(on)
5469 -> format('**** Unknown functor ~w/~w in pp_expr2~n expression: ~w~n',[F,N,Expr])
5470 ; format('**** Unknown functor ~w/~w in pp_expr2~n',[F,N])
5471 ),
5472 %add_internal_error('Unknown Expression: ',pp_expr2(Expr,Prio)),
5473 Prio=20},
5474 ppterm_with_limit_reached(Expr,LimitReached).
5475
5476 :- use_module(external_function_declarations,[synonym_for_external_predicate/2]).
5477
5478 pp_external_call('MEMOIZE_STORED_FUNCTION',[TID],_,500,LimitReached) -->
5479 {get_integer(TID,ID),memoization:get_registered_function_name(ID,Name)},!,
5480 pp_expr_m(atom_string(Name),20,LimitReached),
5481 " /*@memo ", pp_expr_m(TID,20,LimitReached), "*/".
5482 pp_external_call('STRING_LENGTH',[Arg],_,Prio,LimitReached) -->
5483 {get_preference(allow_sequence_operators_on_strings,true)},!,
5484 pp_expr2(size(Arg),Prio,LimitReached).
5485 pp_external_call('STRING_APPEND',[Arg1,Arg2],_,Prio,LimitReached) -->
5486 {get_preference(allow_sequence_operators_on_strings,true)},!,
5487 pp_expr2(concat(Arg1,Arg2),Prio,LimitReached).
5488 pp_external_call('STRING_CONC',[Arg1],_,Prio,LimitReached) -->
5489 {get_preference(allow_sequence_operators_on_strings,true)},!,
5490 pp_expr2(general_concat(Arg1),Prio,LimitReached).
5491 % we could also pretty-print RMUL, ...
5492 pp_external_call(PRED,Args,pred,Prio,LimitReached) -->
5493 {get_preference(translate_ids_to_parseable_format,true),
5494 synonym_for_external_predicate(PRED,FUNC)},
5495 !, % print external predicate as function, as parser can only parse the latter without access to DEFINITIONS
5496 pp_expr2(equal(b(external_function_call(FUNC,Args),boolean,[]),
5497 b(boolean_true,boolean,[])),Prio,LimitReached).
5498 pp_external_call(Symbol,Args,_,Prio,LimitReached) -->
5499 ({invisible_external_pred(Symbol)}
5500 -> pp_expr2(truth,Prio,LimitReached),
5501 " /* ",pp_expr_m(atom_string(Symbol),20,LimitReached),pp_expr_wrap_l('(',Args,') */',LimitReached)
5502 ; {Prio=500},pp_expr_m(atom_string(Symbol),20,LimitReached),
5503 pp_expr_wrap_l('(',Args,')',LimitReached) % pp_expr_wrap_l('/*EXT:*/(',Args,')')
5504 ).
5505
5506 invisible_external_pred('LEQ_SYM').
5507 invisible_external_pred('LEQ_SYM_BREAK'). % just for symmetry breaking foralls,...
5508 external_call(external_function_call(Symbol,Args),expression,Symbol,Args).
5509 external_call(external_pred_call(Symbol,Args),pred,Symbol,Args).
5510 external_call(external_subst_call(Symbol,Args),subst,Symbol,Args).
5511
5512 pp_all_disjoint([H1,H2],LimitReached) --> !, " ",and_symbol," ", pp_disjoint(H1,H2,LimitReached).
5513 pp_all_disjoint([H1|T],LimitReached) --> pp_all_disjoint_aux(T,H1,LimitReached), pp_all_disjoint(T,LimitReached).
5514 pp_all_disjoint([],_) --> "".
5515
5516 pp_all_disjoint_aux([],_,_) --> "".
5517 pp_all_disjoint_aux([H2|T],H1,LimitReached) --> " ",and_symbol," ",
5518 pp_disjoint(H1,H2,LimitReached), pp_all_disjoint_aux(T,H1,LimitReached).
5519
5520 pp_disjoint(H1,H2,LimitReached) --> pp_expr(H1,_), "/\\", pp_expr(H2,_,LimitReached), " = {}".
5521
5522
5523 % given a list of predicates and an ID either extract ID:Set and return Set or return its type as string
5524 select_membership([],TID,[],atom_string(TS)) :- % atom_string used as wrapper for pp_expr2
5525 get_texpr_type(TID,Type), pretty_type(Type,TS).
5526 select_membership([Pred|Rest],TID,Rest,Set) :-
5527 Pred = b(member(TID2,Set),pred,_),
5528 same_id(TID2,TID,_),!.
5529 select_membership([Pred|Rest],TID,Rest,Set) :-
5530 Pred = b(equal(TID2,EqValue),pred,_),
5531 same_id(TID2,TID,_),!, get_texpr_type(TID,Type),
5532 Set = b(set_extension([EqValue]),set(Type),[]).
5533 select_membership([Pred|T],TID,[Pred|Rest],Set) :-
5534 select_membership(T,TID,Rest,Set).
5535
5536 % pretty print prj1/prj2
5537 pp_prj12(Prj,Set1,Set2,LimitReached) -->
5538 ppatom(Prj),"(",pp_expr(Set1,_,LimitReached),",",pp_expr(Set2,_),")".
5539
5540 %:- use_module(bsyntaxtree,[is_a_disjunct/3, get_integer/2]).
5541
5542 pp_comprehension_set(Ids,P1,Info,LimitReached) -->
5543 pp_comprehension_set5(Ids,P1,Info,LimitReached,_).
5544
5545 % the extra argument of pp_comprehension_set5 indicates whether a special(Rule) was applied or not
5546 %pp_comprehension_set(IDs,Body,Info,LimitReached,_) --> {write(pp(IDs,Body,Info)),nl,fail}.
5547 pp_comprehension_set5([TID1,TID2,TID3],Body,_Info,LimitReached,special(Proj)) -->
5548 /* This comprehension set was a projection function (prj1/prj2) */
5549 % %(z_,z__).(z__ : NATURAL|z_) -> prj1(INTEGER,NATURAL)
5550 {get_texpr_id(TID1,ID1), % sometimes _zzzz_unary or _prj_arg1__
5551 get_texpr_id(TID2,ID2), % sometimes _zzzz_binary or _prj_arg2__
5552 get_texpr_id(TID3,LambdaID),
5553 get_lambda_equality(Body,LambdaID,RestBody,ResultExpr),
5554 get_texpr_id(ResultExpr,ResultID),
5555 (ResultID = ID1 -> Proj = prj1 ; ResultID = ID2, Proj = prj2),
5556 flatten_conjunctions(RestBody,Rest1),
5557 select_membership(Rest1,TID1,Rest2,Set1),
5558 select_membership(Rest2,TID2,[],Set2)},
5559 !,
5560 pp_prj12(Proj,Set1,Set2,LimitReached).
5561 pp_comprehension_set5([ID1|T],Body,Info,LimitReached,special(disjunct)) --> {is_a_disjunct(Body,B1,B2),
5562 get_last(T,ID1,_FrontIDs,LastID),
5563 is_lambda_result_id(LastID,_Suffix)},!, % we seem to have the union of two lambda expressions
5564 "(", pp_comprehension_set([ID1|T],B1,Info,LimitReached),
5565 " \\/ ", pp_comprehension_set([ID1|T],B2,Info,LimitReached), ")".
5566 pp_comprehension_set5([b(identifier('_pred_'),integer,_),
5567 b(identifier(LAMBDARES),integer,_)],Body,_,_,special(pred)) --> % '_lambda_result_'
5568 {Body = b(equal(LR,T),pred,_),
5569 LR = b(identifier(LAMBDARES),integer,_),
5570 T = b(minus(ARG,One),integer,_),
5571 get_integer(One,1),
5572 ARG = b(identifier('_pred_'),integer,_)},
5573 !,
5574 "pred".
5575 pp_comprehension_set5([b(identifier('_succ_'),integer,_),
5576 b(identifier(LAMBDARES),integer,_)],Body,_,_,special(succ)) --> % '_lambda_result_'
5577 {Body = b(equal(LR,T),pred,_),
5578 LR = b(identifier(LAMBDARES),integer,_),
5579 T = b(add(ARG,One),integer,_),
5580 get_integer(One,1),
5581 ARG = b(identifier('_succ_'),integer,_)},
5582 !,
5583 "succ".
5584 pp_comprehension_set5(Paras,Body,Info,LimitReached,special(lambda)) -->
5585 {detect_lambda_comprehension(Paras,Body, FrontIDs,LambdaBody,ToExpr)},
5586 !,
5587 {add_normal_typing_predicates(FrontIDs,LambdaBody,TLambdaBody)},
5588 ({eventb_translation_mode} -> "(" ; ""), % put brackets around the lambda in Rodin
5589 pp_annotations(Info,Body),
5590 lambda_symbol, % "%"
5591 pp_lambda_identifiers(FrontIDs,LimitReached),
5592 ".",
5593 ({eventb_translation_mode} -> {IPrio=30} ; {IPrio=11}, "("), % In Rodin it is not ok to write (P|E)
5594 pp_expr_m(TLambdaBody,IPrio,LimitReached), % Check 11 against prio of . and |
5595 pp_such_that_bar(ToExpr),
5596 pp_expr_m(ToExpr,IPrio,LimitReached),
5597 ")".
5598 pp_comprehension_set5(TIds,Body,Info,LimitReached,special(event_b_comprehension_set)) -->
5599 % detect Event-B style set comprehensions and use bullet • or Event-B notation such as {x·x ∈ 1 ‥ 3|x * 10}
5600 % gets translated to {`__comp_result__`|∃x·(x ∈ 1 ‥ 3 ∧ `__comp_result__` = x * 10)}
5601 {is_eventb_comprehension_set(TIds,Body,Info,Ids,P1,EXPR), \+ atelierb_mode(_)},!, % print rewritten version for AtelierB
5602 pp_annotations(Info,P1),
5603 left_set_bracket,
5604 pp_expr_l_pair_in_mode(Ids,LimitReached),
5605 {add_normal_typing_predicates(Ids,P1,P)},
5606 dot_bullet_symbol,
5607 pp_expr_m(P,11,LimitReached),
5608 pp_such_that_bar(P),
5609 pp_expr_m(EXPR,11,LimitReached),
5610 right_set_bracket.
5611 pp_comprehension_set5(Ids,P1,_Info,LimitReached,normal) --> {atelierb_mode(prover(ml))},!,
5612 "SET(",
5613 pp_expr_l_pair_in_mode(Ids,LimitReached),
5614 ").(",
5615 {add_normal_typing_predicates(Ids,P1,P)},
5616 pp_expr_m(P,11,LimitReached),
5617 ")".
5618 pp_comprehension_set5(Ids,P1,Info,LimitReached,normal) -->
5619 pp_annotations(Info,P1),
5620 left_set_bracket,
5621 pp_expr_l_pair_in_mode(Ids,LimitReached),
5622 {add_normal_typing_predicates(Ids,P1,P)},
5623 pp_such_that_bar(P),
5624 pp_expr_m(P,11,LimitReached),
5625 right_set_bracket.
5626
5627
5628 detect_lambda_comprehension([ID1|T],Body, FrontIDs,LambdaBody,ToExpr) :-
5629 get_last(T,ID1,FrontIDs,LastID),
5630 FrontIDs=[_|_], % at least one identifier for the lambda
5631 is_lambda_result_id(LastID,Suffix),
5632 % nl, write(lambda(Body,T,ID1)),nl,
5633 (is_an_equality(Body,From,ToExpr) -> LambdaBody = b(truth,pred,[])
5634 ; is_a_conjunct(Body,LambdaBody,Equality),
5635 is_an_equality(Equality,From,ToExpr)),
5636 is_lambda_result_id(From,Suffix).
5637
5638 pp_annotations(V,_) --> {var(V), format('Illegal variable info field in pp_annotations: ~w~n',[V])},!,
5639 "/* ILLEGAL VARIABLE INFO FIELD */".
5640 pp_annotations(INFO,_) --> {member(prob_annotation('SYMBOLIC'),INFO)},!,
5641 "/*@symbolic*/ ".
5642 ?pp_annotations(_,b(_,_,INFO)) --> {nonvar(INFO),member(prob_annotation('SYMBOLIC'),INFO)},!,
5643 "/*@symbolic*/ ".
5644 % TO DO: maybe also print other annotations like memoize, recursive ?
5645 pp_annotations(_,_) --> "".
5646
5647 % in Event-B style: { x,y . P | E }
5648 pp_event_b_comprehension_set(Ids,E,P1,LimitReached) -->
5649 left_set_bracket,pp_expr_l(Ids,LimitReached), % use comma separated list; maplet is not accepted by Rodin
5650 {add_normal_typing_predicates(Ids,P1,P)},
5651 dot_symbol,pp_expr_m(P,11,LimitReached),
5652 pp_such_that_bar(P),pp_expr_m(E,11,LimitReached),right_set_bracket.
5653
5654 pp_lambda_identifiers([H1,H2|T],LimitReached) --> {\+ eventb_translation_mode},!,
5655 "(",pp_expr_l([H1,H2|T],LimitReached),")".
5656 pp_lambda_identifiers(L,LimitReached) --> pp_expr_l_pair_in_mode(L,LimitReached).
5657
5658 pp_such_that_bar(_) --> {latex_mode},!, "\\mid ".
5659 pp_such_that_bar(_) --> {unicode_mode},!, "\x2223\". % used by Rodin
5660 pp_such_that_bar(b(unary_minus(_),_,_)) --> !, " | ". % otherwise AtelierB complains about illegal token |-
5661 pp_such_that_bar(b(unary_minus_real(_),_,_)) --> !, " | ". % otherwise AtelierB complains about illegal token |-
5662 pp_such_that_bar(_Next) --> "|".
5663 pp_such_that_bar --> {latex_mode},!, "\\mid ".
5664 pp_such_that_bar --> {unicode_mode},!, "\x2223\".
5665 pp_such_that_bar --> "|".
5666
5667 is_an_equality(b(equal(A,B),_,_),A,B).
5668
5669 integer_set_mapping(A,B) :- integer_set_mapping(A,_,B).
5670 ?integer_set_mapping(A,integer_set,B) :- unicode_mode, unicode_translation(A,B),!.
5671 integer_set_mapping(A,integer_set,B) :- latex_mode, latex_integer_set_translation(A,B),!.
5672 integer_set_mapping(A,integer_set,B) :- atelierb_mode(prover(PPML)),
5673 atelierb_pp_translation(A,PPML,B),!.
5674 integer_set_mapping(A,integer_set,B) :-
5675 eventb_translation_mode, eventb_integer_mapping(A,B),!.
5676 integer_set_mapping(ISet,user_set,Res) :- atomic(ISet),!,Res=ISet.
5677 integer_set_mapping(_ISet,unknown_set,'integer_set(??)').
5678
5679 eventb_integer_mapping('INTEGER','INT').
5680 eventb_integer_mapping('NATURAL','NAT').
5681 eventb_integer_mapping('NATURAL1','NAT1').
5682
5683 real_set_mapping(A,B) :- unicode_mode, unicode_translation(A,B),!.
5684 real_set_mapping(X,X). % TO DO: unicode_mode,...
5685
5686 :- dynamic comment_level/1.
5687 reset_pp :- retractall(comment_level(_)).
5688 enter_comment --> {retract(comment_level(N))},!, "(*", {N1 is N+1, assertz(comment_level(N1))}.
5689 enter_comment --> "/*", {assertz(comment_level(1))}.
5690 exit_comment --> {retract(comment_level(N))},!,
5691 ({N>1} -> "*)", {N1 is N-1, assertz(comment_level(N1))} ; "*/").
5692 exit_comment --> "*/", {add_internal_error('Unmatched closing comment:',exit_comment)}.
5693 % TO DO: ensure reset_pp is called when starting to pretty print, in case timeout occurs in previous pretty prints
5694
5695 %get_last([b(identifier(_lambda_result_10),set(couple(integer,set(couple(integer,integer)))),[])],b(identifier(i),integer,[]),[b(identifier(i),integer,[])],b(identifier(_lambda_result_10),set(couple(integer,set(couple(integer,integer)))),[]))
5696
5697 get_last([],Last,[],Last).
5698 get_last([H2|T],H1,[H1|LT],Last) :- get_last(T,H2,LT,Last).
5699
5700 pp_expr_wrap_l(Pre,Expr,Post,LimitReached) -->
5701 ppatom(Pre),pp_expr_l(Expr,LimitReached),ppatom(Post).
5702 %pp_freetype_term(Term,FT,L,Expr,LimitReached) -->
5703 % {pretty_freetype(FT,P)},
5704 % ppatom(Term),"(",ppatom_opt_scramble(P),",",
5705 % ppatom(L),",",pp_expr_m(Expr,500,LimitReached),")".
5706
5707 % print a list of expressions, seperated by commas
5708 pp_expr_l_pair_in_mode(List,LimitReached) --> {eventb_translation_mode},!,
5709 {maplet_symbol(MapletStr,[])},
5710 pp_expr_l_sep(List,MapletStr,LimitReached).
5711 pp_expr_l_pair_in_mode(List,LimitReached) --> pp_expr_l_sep(List,",",LimitReached).
5712 pp_expr_l(List,LimitReached) --> pp_expr_l_sep(List,",",LimitReached).
5713
5714 pp_expr_l_sep([Expr],_,LimitReached) --> !,
5715 pp_expr_m(Expr,0,LimitReached).
5716 pp_expr_l_sep(List,Sep,LimitReached) --> pp_expr_l2(List,Sep,LimitReached).
5717 pp_expr_l2([],_Sep,_) --> !.
5718 pp_expr_l2([Expr|Rest],Sep,LimitReached) -->
5719 {get_sep_prio(Sep,Prio)},
5720 pp_expr_m(Expr,Prio,LimitReached),
5721 pp_expr_l3(Rest,Sep,LimitReached).
5722 pp_expr_l3([],_Sep,_) --> !.
5723 pp_expr_l3(Rest,Sep,LimitReached) -->
5724 Sep,pp_expr_l2(Rest,Sep,LimitReached).
5725
5726 get_sep_prio(",",Prio) :- !, Prio=116. % Prio of , is 115
5727 get_sep_prio("\\/",Prio) :- !, Prio=161.
5728 get_sep_prio("/\\",Prio) :- !, Prio=161.
5729 get_sep_prio("|->",Prio) :- !, Prio=161.
5730 get_sep_prio([8614],Prio) :- !, Prio=161.
5731 %
5732 get_sep_prio(_,161).
5733
5734 % print the fields of a record
5735 pp_expr_fields([field(Name,Expr)],LimitReached) --> !,
5736 pp_identifier(Name),":",pp_expr_m(Expr,120,LimitReached).
5737 pp_expr_fields(Fields,LimitReached) -->
5738 pp_expr_fields2(Fields,LimitReached).
5739 pp_expr_fields2([],_) --> !.
5740 pp_expr_fields2([field(Name,Expr)|Rest],LimitReached) -->
5741 pp_identifier(Name),":",
5742 pp_expr_m(Expr,116,LimitReached),
5743 pp_expr_fields3(Rest,LimitReached).
5744 pp_expr_fields3([],_) --> !.
5745 pp_expr_fields3(Rest,LimitReached) -->
5746 ",",pp_expr_fields2(Rest,LimitReached).
5747
5748 % TO DO: test more fully; identifiers seem to be wrapped in brackets
5749 pp_expr_let_exists(Ids,Exprs,P,LimitReached) -->
5750 exists_symbol,
5751 ({eventb_translation_mode} -> % otherwise we get strange characters in Rodin, no (.) allowed in Rodin
5752 pp_expr_ids_in_mode(Ids,LimitReached),
5753 ".("
5754 ; " /* LET */ (",
5755 pp_expr_l_pair_in_mode(Ids,LimitReached),
5756 ").("
5757 ),
5758 pp_expr_let_pred_exprs(Ids,Exprs,LimitReached),
5759 ({is_truth(P)} -> ""
5760 ; " ",and_symbol," ", pp_expr_m(P,40,LimitReached)),
5761 ")".
5762
5763 pp_expr_let_pred_exprs([],[],_) --> !.
5764 pp_expr_let_pred_exprs([Id|Irest],[Expr|Erest],LimitReached) -->
5765 " ",pp_expr_let_id(Id,LimitReached),
5766 "=",pp_expr_m(Expr,400,LimitReached),
5767 ( {Irest=[]} -> [] ; " ", and_symbol),
5768 pp_expr_let_pred_exprs(Irest,Erest,LimitReached).
5769
5770 % print a LET expression
5771 pp_expr_let(_Ids,Exprs,P,LimitReached) -->
5772 {eventb_translation_mode,
5773 P=b(_,_,I), member(was(extended_expr(Op)),I)},!, % let was created by direct_definition for a theory operator call
5774 ppatom(Op),
5775 pp_function_left_bracket,
5776 pp_expr_l_sep(Exprs,",",LimitReached),
5777 %pp_expr_let_pred_exprs(Ids,Exprs,LimitReached) % write entire predicate with parameter names
5778 pp_function_right_bracket.
5779 pp_expr_let(Ids,Exprs,P,LimitReached) -->
5780 "LET ", pp_expr_ids_no_parentheses(Ids,LimitReached),
5781 " BE ", pp_expr_let_pred_exprs(Ids,Exprs,LimitReached),
5782 " IN ",pp_expr_m(P,5,LimitReached),
5783 " END".
5784
5785 pp_expr_let_id(ID,LimitReached) --> {atomic(ID),!, write(unwrapped_let_id(ID)),nl},
5786 pp_expr_m(identifier(ID),500,LimitReached).
5787 pp_expr_let_id(ID,LimitReached) --> pp_expr_m(ID,499,LimitReached).
5788
5789 % print a list of identifiers
5790 pp_expr_ids_in_mode([],_) --> !.
5791 pp_expr_ids_in_mode(Ids,LimitReached) --> {eventb_translation_mode ; Ids=[_]},!,
5792 pp_expr_l(Ids,LimitReached). % no (.) allowed in Event-B; not necessary in B if only one id
5793 pp_expr_ids_in_mode(Ids,LimitReached) --> "(",pp_expr_l(Ids,LimitReached),")".
5794
5795 pp_expr_ids([],_) --> !.
5796 pp_expr_ids(Ids,LimitReached) -->
5797 % ( {Ids=[Id]} -> pp_expr_m(Id,221)
5798 % ;
5799 "(",pp_expr_l(Ids,LimitReached),")".
5800
5801 pp_expr_ids_no_parentheses(Ids,LimitReached) --> pp_expr_l(Ids,LimitReached).
5802
5803
5804 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5805 % pretty print types for error messages
5806
5807 %:- use_module(probsrc(typing_tools), [normalize_type/2]).
5808 % replace seq(.) types before pretty printing:
5809 pretty_normalized_type(Type,String) :- typing_tools:normalize_type(Type,NT),!,
5810 pretty_type(NT,String).
5811 pretty_normalized_type(Type,String) :-
5812 add_internal_error('Cannot normalize type:',pretty_normalized_type(Type,String)),
5813 pretty_type(Type,String).
5814
5815 % for pp_expr: do we potentially have to add parentheses
5816 normalized_type_requires_outer_paren(couple(_,_)).
5817 % all other types are either identifiers or use prefix notation (POW(.), seq(.), struct(.))
5818
5819 pretty_type(Type,String) :-
5820 pretty_type_l([Type],[String]).
5821
5822 pretty_type_l(Types,Strings) :-
5823 extract_vartype_names(Types,N),
5824 pretty_type2_l(Types,N,Strings).
5825 pretty_type2_l([],_,[]).
5826 pretty_type2_l([T|TRest],Names,[S|SRest]) :-
5827 pretty_type2(T,Names,noparen,S),
5828 pretty_type2_l(TRest,Names,SRest).
5829
5830 extract_vartype_names(Types,names(Variables,Names)) :-
5831 term_variables(Types,Variables),
5832 name_variables(Variables,1,Names).
5833
5834 pretty_type2(X,names(Vars,Names),_,Name) :- var(X),!,exact_member_lookup(X,Name,Vars,Names).
5835 pretty_type2(any,_,_,'?').
5836 pretty_type2(set(T),N,_,Text) :- nonvar(T),T=couple(A,B),!,
5837 pretty_type2(A,N,paren,AT), pretty_type2(B,N,paren,BT),
5838 binary_infix_in_mode(relations,Symbol,_,_), % <->
5839 ajoin(['(',AT,Symbol,BT,')'],Text).
5840 pretty_type2(set(T),N,_,Text) :-
5841 pretty_type2(T,N,noparen,TT), function_like_in_mode(pow_subset,POW),
5842 ajoin([POW,'(',TT,')'],Text).
5843 pretty_type2(seq(T),N,_,Text) :-
5844 pretty_type2(T,N,noparen,TT), ajoin(['seq(',TT,')'],Text).
5845 pretty_type2(couple(A,B),N,Paren,Text) :-
5846 pretty_type2(A,N,paren,AT),pretty_type2(B,N,paren,BT),
5847 binary_infix_in_mode(cartesian_product,Cart,_,_),
5848 ajoin([AT,Cart,BT],Prod),
5849 ( Paren == noparen ->
5850 Text = Prod
5851 ;
5852 ajoin(['(',Prod,')'],Text)).
5853 pretty_type2(string,_,_,'STRING').
5854 pretty_type2(integer,_,_,Atom) :- integer_set_mapping('INTEGER',Atom).
5855 pretty_type2(real,_,_,Atom) :- real_set_mapping('REAL',Atom).
5856 pretty_type2(boolean,_,_,'BOOL').
5857 pretty_type2(global(G_Id),_,_,A) :- opt_scramble_id(G_Id,G), ajoin([G],A).
5858 pretty_type2(freetype(Id),N,_,A) :- pretty_freetype2(Id,N,A).
5859 pretty_type2(pred,_,_,predicate).
5860 pretty_type2(subst,_,_,substitution).
5861 pretty_type2(constant(List),_,_,A) :-
5862 (var(List) -> ['{??VAR??...}'] % should not happen
5863 ; ajoin_with_sep(List,',',P), ajoin(['{',P,'}'],A)).
5864 pretty_type2(record(Fields),N,_,Text) :-
5865 pretty_type_fields(Fields,N,FText),
5866 ajoin(['struct(',FText,')'],Text).
5867 pretty_type2(op(Params,Results),N,_,Text) :-
5868 pretty_type_l(Params,N,PText),
5869 ( nonvar(Results),Results=[] ->
5870 ajoin(['operation(',PText,')'],Text)
5871 ;
5872 pretty_type_l(Results,N,RText),
5873 ajoin([RText,'<--operation(',PText,')'],Text) ).
5874 pretty_type2(definition(DefType,_,_),_,_,DefType).
5875 pretty_type2(witness,_,_,witness).
5876 pretty_type2([],_,_,'[]') :- add_error(pretty_type,'Illegal list in type:','[]').
5877 pretty_type2([H|T],_,_,'[_]') :- add_error(pretty_type,'Illegal list in type:',[H|T]).
5878 pretty_type2(b(E,T,I),_,_,'?') :- add_error(pretty_type,'Illegal b/3 term in type:',b(E,T,I)).
5879
5880 pretty_type_l(L,_,'...') :- var(L),!.
5881 pretty_type_l([],_,'') :- !.
5882 pretty_type_l([E|Rest],N,Text) :-
5883 pretty_type2(E,N,noparen,EText),
5884 ( nonvar(Rest),Rest=[] ->
5885 EText=Text
5886 ;
5887 pretty_type_l(Rest,N,RText),
5888 ajoin([EText,',',RText],Text)).
5889
5890 pretty_type_fields(L,_,'...') :- var(L),!.
5891 pretty_type_fields([],_,'') :- !.
5892 pretty_type_fields([field(Name,Type)|FRest],N,Text) :- !,
5893 pretty_type2(Type,N,noparen,TText),
5894 ptf_seperator(FRest,Sep),
5895 pretty_type_fields(FRest,N,RestText),
5896 opt_scramble_id(Name,ScrName),
5897 ajoin([ScrName,':',TText,Sep,RestText],Text).
5898 pretty_type_fields(Err,N,Text) :-
5899 add_internal_error('Illegal field type: ',pretty_type_fields(Err,N,Text)), Text='??'.
5900 ptf_seperator(L,', ') :- var(L),!.
5901 ptf_seperator([],'') :- !.
5902 ptf_seperator(_,', ').
5903
5904 pretty_freetype(Id,A) :-
5905 extract_vartype_names(Id,N),
5906 pretty_freetype2(Id,N,A).
5907 pretty_freetype2(Id,_,A) :- var(Id),!,A='_'.
5908 pretty_freetype2(Id,_,A) :- atomic(Id),!,Id=A.
5909 pretty_freetype2(Id,N,A) :-
5910 Id=..[Name|TypeArgs],
5911 pretty_type2_l(TypeArgs,N,PArgs),
5912 ajoin_with_sep(PArgs,',',P),
5913 ajoin([Name,'(',P,')'],A).
5914
5915 name_variables([],_,[]).
5916 name_variables([_|VRest],Index,[Name|NRest]) :-
5917 (nth1(Index,"ABCDEFGHIJKLMNOPQRSTUVWXYZ",C) -> SName = [C] ; number_codes(Index,SName)),
5918 append("_",SName,CName),atom_codes(Name,CName),
5919 Next is Index+1,
5920 name_variables(VRest,Next,NRest).
5921
5922 ppatom(Var) --> {var(Var)},!, ppatom('$VARIABLE').
5923 ppatom(Cmp) --> {compound(Cmp)},!, ppatom('$COMPOUND_TERM').
5924 ppatom(Atom) --> {safe_atom_codes(Atom,Codes)}, ppcodes(Codes).
5925
5926 ppnumber(Number) --> {var(Number)},!,pp_clpfd_variable(Number).
5927 ppnumber(inf) --> !,"inf".
5928 ppnumber(minus_inf) --> !,"minus_inf".
5929 ppnumber(Number) --> {number(Number),number_codes(Number,Codes)},!, ppcodes(Codes).
5930 ppnumber(Number) --> {add_internal_error('Not a number: ',ppnumber(Number,_,_))}, "<<" ,ppterm(Number), ">>".
5931
5932 pp_numberedvar(N) --> "_",ppnumber(N),"_".
5933
5934 pp_clpfd_variable(X) --> "?:",{fd_dom(X,Dom)},write_to_codes(Dom), pp_frozen_info(X).
5935
5936 pp_frozen_info(_X) --> {get_preference(translate_print_frozen_infos,false)},!,[].
5937 pp_frozen_info(X) -->
5938 ":(",{frozen(X,Goal)},
5939 write_goal_with_max_depth(Goal),
5940 ")".
5941
5942 write_goal_with_max_depth((A,B)) --> !, "(",write_goal_with_max_depth(A),
5943 ", ", write_goal_with_max_depth(B), ")".
5944 write_goal_with_max_depth(Term) --> write_with_max_depth(3,Term).
5945
5946 write_with_max_depth(Depth,Term,S1,S2) :- write_term_to_codes(Term,S1,S2,[max_depth(Depth)]).
5947
5948 ppterm(Term) --> write_to_codes(Term).
5949
5950 ppcodes([],S,S).
5951 ppcodes([C|Rest],[C|In],Out) :- ppcodes(Rest,In,Out).
5952
5953 ppterm_with_limit_reached(Term,LimitReached) -->
5954 {write_to_codes(Term,Codes,[])}, ppcodes_with_limit_reached(Codes,LimitReached).
5955
5956 ppcodes_with_limit_reached([C|Rest],LimitReached,[C|In],Out) :- var(LimitReached), !,
5957 ppcodes_with_limit_reached(Rest,LimitReached,In,Out).
5958 ppcodes_with_limit_reached(_,_LimitReached,S,S).
5959
5960 % for debugging:
5961 :- public b_portray_hook/1.
5962 b_portray_hook(X) :-
5963 nonvar(X),
5964 (is_texpr(X), ground(X) -> write('{# '),print_bexpr_or_subst(X),write(' #}')
5965 ; X=avl_set(_), ground(X) -> write('{#avl '), print_bvalue(X), write(')}')
5966 ; X=wfx(WF0,_,WFE,Info) -> format('wfx(~w,$mutable,~w,~w)',[WF0,WFE,Info]) % to do: short summary of prios & call stack
5967 ).
5968
5969 install_b_portray_hook :- % register portray hook mainly for the Prolog debugger
5970 assertz(( user:portray(X) :- translate:b_portray_hook(X) )).
5971 remove_b_portray_hook :-
5972 retractall( user:portray(_) ).
5973
5974
5975 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5976 % Pretty-print of Event-B models as classical B
5977
5978 translate_eventb_to_classicalb(EBMachine,AddInfo,Rep) :-
5979 ( conversion_check(EBMachine) ->
5980 convert_eventb_classicalb(EBMachine,CBMachine),
5981 call_cleanup(( set_animation_mode(b), % clear minor mode "eventb"
5982 translate_machine(CBMachine,Rep,AddInfo),!),
5983 set_animation_minor_mode(eventb))
5984 ; \+ animation_minor_mode(eventb) -> add_error(translate,'Conversion only applicable to Event-B models')
5985 ;
5986 add_error_and_fail(translate,'Conversion not applicable, check if you limited the number of abstract level to 0')
5987 ).
5988
5989 convert_eventb_classicalb(EBMachine,CBMachine) :-
5990 select_section(operation_bodies,In,Out,EBMachine,CBMachine1),
5991 maplist(convert_eventop,In,Out),
5992 select_section(initialisation,IIn,IOut,CBMachine1,CBMachine),
5993 convert_event(IIn,[],IOut).
5994 convert_eventop(EBOp,CBOp) :-
5995 get_texpr_expr(EBOp,operation(Id,[],Args,EBBody)),
5996 get_texpr_info(EBOp,Info),
5997 convert_event(EBBody,Args,CBBody),
5998 % Remove the arguments
5999 create_texpr(operation(Id,[],[],CBBody),op([],[]),Info,CBOp).
6000 convert_event(TEvent,Parameters,TSubstitution) :-
6001 get_texpr_expr(TEvent,rlevent(_Id,_Section,_Status,_Parameters,Guard,_Theorems,Actions,_VariableWitnesses,_ParameterWitnesses,_Ums,_Refined)),
6002 in_parallel(Actions,PAction),
6003 convert_event2(Parameters,Guard,PAction,TSubstitution).
6004 convert_event2([],Guard,Action,Action) :-
6005 is_truth(Guard),!.
6006 convert_event2([],Guard,Action,Select) :-
6007 !,create_texpr(select([When]),subst,[],Select),
6008 create_texpr(select_when(Guard,Action),subst,[],When).
6009 convert_event2(Parameters,Guard,Action,Any) :-
6010 create_texpr(any(Parameters,Guard,Action),subst,[],Any).
6011 in_parallel([],Skip) :- !,create_texpr(skip,subst,[],Skip).
6012 in_parallel([A],A) :- !.
6013 in_parallel(Actions,Parallel) :- create_texpr(parallel(Actions),subst,[],Parallel).
6014
6015 conversion_check(Machine) :-
6016 animation_mode(b),
6017 animation_minor_mode(eventb),
6018 get_section(initialisation,Machine,Init),
6019 get_texpr_expr(Init,rlevent(_Id,_Sec,_St,_Par,_Grd,_Thms,_Act,_VW,_PW,_Ums,[])).
6020
6021 % ------------------------------------------------------------
6022
6023 % divide a B typed expression into columns for CSV export or Table viewing of its values
6024 get_bexpression_column_template(b(couple(A,B),_,_),(AVal,BVal),ColHeaders,Columns) :- !,
6025 get_bexpression_column_template(A,AVal,AHeaders,AColumns),
6026 get_bexpression_column_template(B,BVal,BHeaders,BColumns),
6027 append(AHeaders,BHeaders,ColHeaders),
6028 append(AColumns,BColumns,Columns).
6029 get_bexpression_column_template(TypedExpr,Value,[ColHeader],[Value]) :-
6030 translate:translate_bexpression_with_limit(TypedExpr,100,ColHeader).
6031
6032
6033 % a version of member that creates an error when info list not instantiated
6034 member_in_info(X,T) :- var(T),!, add_internal_error('Illegal info field:', member_in_info(X,T)),fail.
6035 member_in_info(X,[X|_]).
6036 member_in_info(X,[_|T]) :- member_in_info(X,T).
6037
6038 memberchk_in_info(X,T) :- var(T),!, add_internal_error('Illegal info field:', memberchk_in_info(X,T)),fail.
6039 memberchk_in_info(X,[X|_]) :- !.
6040 memberchk_in_info(X,[_|T]) :- memberchk_in_info(X,T).
6041
6042 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6043
6044 :- use_module(library(clpfd)).
6045
6046 % print also partially instantiated variables with CLP(FD) Info
6047 print_value_variable(X) :- var(X), !, write(X).
6048 print_value_variable(int(X)) :- write('int('), print_clpfd_variable(X), write(')').
6049 print_value_variable(fd(X,T)) :- write('fd('), print_clpfd_variable(X), write(','), write(T),write(')').
6050 print_value_variable(X) :- write(X).
6051
6052 print_clpfd_variable(X) :- var(X),!,write(X), write(':'), fd_dom(X,Dom), write(Dom), print_frozen_info(X).
6053 print_clpfd_variable(X) :- write(X).
6054
6055 %print_clpfd_variables([]).
6056 %print_clpfd_variables([H|T]) :- write('CLPFD: '),print_clpfd_variable(H), nl, print_clpfd_variables(T).
6057
6058
6059 :- public l_print_frozen_info/1.
6060 l_print_frozen_info([]).
6061 l_print_frozen_info([H|T]) :- write(H), write(' '),
6062 (var(H) -> print_frozen_info(H) ;
6063 H=fd_var(V,_) -> print_frozen_info(V) ; true), l_print_frozen_info(T).
6064
6065 print_frozen_info(X) :- frozen(X,Goal), print_frozen_goal(Goal).
6066 print_frozen_goal((A,B)) :- !, print_frozen_goal(A), write(','), print_frozen_goal(B).
6067 print_frozen_goal(prolog:trig_nondif(_A,_B,R,_S)) :- !, frozen(R,G2), print_frozen_goal2(G2).
6068 print_frozen_goal(G) :- print_frozen_goal2(G).
6069 print_frozen_goal2(V) :- var(V),!, write(V).
6070 print_frozen_goal2(true) :- !.
6071 print_frozen_goal2((A,B)) :- !, print_frozen_goal2(A), write(','), print_frozen_goal2(B).
6072 print_frozen_goal2(G) :- write(' :: '), tools_printing:print_term_summary(G).
6073
6074
6075 /* Event-B operators */
6076 translate_eventb_operators([]) --> !.
6077 translate_eventb_operators([Name-Call|Rest]) -->
6078 translate_eventb_operator(Call,Name),
6079 translate_eventb_operators(Rest).
6080
6081 translate_eventb_operator(Module:Call,Name) -->
6082 insertcodes("\n "),
6083 indention_codes(In,Out),
6084 {Call =.. [Functor|Args],
6085 translate_eventb_operator2(Functor,Args,Module,Call,Name,In,Out)}.
6086
6087
6088 translate_eventb_operator2(direct_definition,[Args,_RawWD,RawBody,TypeParas|_],_Module,_Call,Name) -->
6089 pp_eventb_direct_definition_header(Name,Args),!,
6090 ppcodes(" direct_definition ["),
6091 pp_eventb_operator_args(TypeParas),
6092 ppcodes("] "),
6093 {translate_in_mode(eqeq,'==',EqEqStr)}, ppatom(EqEqStr),
6094 ppcodes(" "),
6095 pp_raw_formula(RawBody). % TO DO: use indentation
6096 translate_eventb_operator2(axiomatic_definition,[Tag|_],_Module,_Call,Name) --> !,
6097 ppterm(Name),
6098 ppcodes(": Operator implemented by axiomatic definition using "),
6099 ppatom(Tag).
6100 translate_eventb_operator2(Functor,_,Module,_Call,Name) -->
6101 ppterm(Name),
6102 ppcodes(": Operator implemented by "),
6103 ppatom(Module),ppcodes(":"),ppatom(Functor).
6104
6105 % example direct definition:
6106 %direct_definition([argument(curM,integer_set(none)),argument(curH,integer_set(none))],truth(none),add(none,identifier(none,curM),multiplication(none,identifier(none,curH),integer(none,60))),[])
6107
6108 pp_eventb_direct_definition_header(Name,Args) -->
6109 ppterm(Name), ppcodes("("),
6110 pp_eventb_operator_args(Args), ppcodes(")").
6111
6112 translate_eventb_direct_definition_header(Name,Args,ResAtom) :-
6113 (pp_eventb_direct_definition_header(Name,Args,C,[])
6114 -> atom_codes(ResAtom,C) ; ResAtom='<<UNABLE TO PRETTY-PRINT OPERATOR HEADER>>').
6115 translate_eventb_direct_definition_body(RawBody,ResAtom) :-
6116 (pp_raw_formula(RawBody,C,[]) -> atom_codes(ResAtom,C) ; ResAtom='<<UNABLE TO PRETTY-PRINT OPERATOR BODY>>').
6117
6118 pp_raw_formula(RawExpr) --> {transform_raw(RawExpr,TExpr)},!, pp_expr(TExpr,_,_LR).
6119 pp_raw_formula(_) --> ppcodes("<<UNABLE TO PRETTY-PRINT>>").
6120
6121
6122 pp_eventb_operator_args([]) --> [].
6123 pp_eventb_operator_args([Arg]) --> !, pp_argument(Arg).
6124 pp_eventb_operator_args([Arg|T]) --> pp_argument(Arg), ppcodes(","),
6125 pp_eventb_operator_args(T).
6126 pp_argument(argument(ID,_RawType)) --> !, ppatom(ID).
6127 pp_argument(identifier(_,ID)) --> !, "<",ppatom(ID),">".
6128 pp_argument(Atom) --> ppatom(Atom).
6129
6130 % ---------------------------------------
6131
6132 % translate a predicate into B machine for manipulation
6133 translate_predicate_into_machine(Pred,MchName,ResultAtom) :-
6134 with_language_mode(b, translate_predicate_into_machine_aux(Pred,MchName,ResultAtom)). % clear minor mode "eventb"
6135 translate_predicate_into_machine_aux(Pred,MchName,ResultAtom) :-
6136 get_global_identifiers(Ignored,ignore_promoted_constants),
6137 find_typed_identifier_uses(Pred, Ignored, TUsedIds),
6138 get_texpr_ids(TUsedIds,UsedIds),
6139 add_typing_predicates(TUsedIds,Pred,TPred),
6140 set_print_type_infos(all,CHNG),
6141 call_pp_with_no_limit_and_parseable(pred_over_lines(2,_Lbl,TPred,(0,CPrettyPred),(_,[]))), % split predicate into one conjunct per line
6142 atom_codes(PrettyPred,CPrettyPred),
6143 reset_print_type_infos(CHNG),
6144 convert_and_ajoin_ids_line_break(UsedIds,AllIds,10),
6145 bmachine:get_full_b_machine(Name,BMachine),
6146 include(relevant_section,BMachine,RelevantSections),
6147 % TO DO: we could filter out enumerate/deferred sets not occuring in Pred
6148 translate_section_list(RelevantSections,SetsParas),
6149 atom_codes(ASP,SetsParas),
6150 pp_identifier(MchName,CMchName,[]), atom_codes(EMchName,CMchName),
6151 ajoin(['MACHINE ', EMchName, ' /* generated from ',Name,' */\n',ASP,'CONSTANTS\n ',AllIds,'\nPROPERTIES',PrettyPred,'\nEND\n'],ResultAtom).
6152
6153 relevant_section(deferred_sets/_).
6154 relevant_section(enumerated_elements/_).
6155 relevant_section(parameters/_).
6156
6157 :- use_module(library(system),[ datime/1]).
6158 :- use_module(specfile,[currently_opened_file/1]).
6159 :- use_module(probsrc(version), [format_prob_version/1]).
6160 % print a Proof Obligation aka Sequent as a B machine
6161 % Rodin disprover can print this to tmp/ProB_Rodin_PO_SelectedHyps.mch
6162 nested_print_sequent_as_classicalb(Stream,HypsList,Goal,AllHypsList,MchName,ProofInfos) :-
6163 set_suppress_rodin_positions(false,Chng), % ensure we print Rodin labels if available
6164 call_cleanup(nested_print_sequent_as_classicalb_aux(Stream,HypsList,Goal,AllHypsList,MchName,ProofInfos),
6165 reset_suppress_rodin_positions(Chng)).
6166
6167 % convert identifier by adding backquote if necessary for unicode, reserved keywords, ...
6168 convert_id(b(identifier(ID),_,_),CAtom) :- !, convert_id(ID,CAtom).
6169 convert_id(Atom,CAtom) :- atom(Atom),!,pp_identifier(Atom,Codes,[]), atom_codes(CAtom,Codes).
6170 convert_id(E,CAtom) :- add_internal_error('Illegal id: ',E), CAtom = '?'.
6171 convert_and_ajoin_ids(UsedIds,AllIdsWithCommas) :-
6172 maplist(convert_id,UsedIds,ConvUsedIds),
6173 ajoin_with_sep(ConvUsedIds,', ',AllIdsWithCommas).
6174 convert_and_ajoin_ids_line_break(UsedIds,AllIdsWithLineBreak,Threshold) :-
6175 (length(UsedIds,Len), Len>Threshold -> Sep=',\n ' ; Sep=', ' ),
6176 maplist(convert_id,UsedIds,ConvUsedIds),
6177 ajoin_with_sep(ConvUsedIds,Sep,AllIdsWithLineBreak).
6178
6179 nested_print_sequent_as_classicalb_aux(Stream,HypsList,Goal,AllHypsList,MchName,ProofInfos) :-
6180 conjunct_predicates(HypsList,HypsPred),
6181 conjunct_predicates([Goal|HypsList],Pred),
6182 get_global_identifiers(Ignored,ignore_promoted_constants), % the sets section below will not print the promoted enumerated set constants, they may also not be valid for the selected hyps only
6183 find_typed_identifier_uses(Pred, Ignored, TUsedIds),
6184 get_texpr_ids(TUsedIds,UsedIds),
6185 convert_and_ajoin_ids(UsedIds,AllIds),
6186 bmachine:get_full_b_machine(_Name,BMachine),
6187 include(relevant_section,BMachine,RelevantSections),
6188 % TO DO: we could filter out enumerate/deferred sets not occuring in Pred
6189 translate_section_list(RelevantSections,SetsParas),
6190 set_print_type_infos(all,CHNG),
6191 datime(datime(Yr,Mon,Day,Hr,Min,_Sec)),
6192 format(Stream,'MACHINE ~w~n /* Exported: ~w/~w/~w ~w:~w */~n',[MchName,Day,Mon,Yr,Hr,Min]),
6193 (currently_opened_file(File), bmachine:b_machine_name(Name)
6194 -> format(Stream,' /* Origin: ~w : ~w */~n',[Name,File]) ; true),
6195 write(Stream,' /* '),format_prob_version(Stream), format(Stream,' */~n',[]),
6196 format(Stream,' /* Use static asssertion checking to look for counter examples: */~n',[]),
6197 format(Stream,' /* - probcli -cbc_assertions ProB_Rodin_PO_SelectedHyps.mch */~n',[]),
6198 format(Stream,' /* - in ProB2-UI: Verifications View -> Symbolic Tab -> Static Assertion Checking */~n',[]),
6199 maplist(format_proof_infos(Stream),ProofInfos),
6200 format(Stream,'~sCONSTANTS~n ~w~nPROPERTIES /* Selected Hypotheses: */~n',[SetsParas,AllIds]),
6201 add_typing_predicates(TUsedIds,HypsPred,HypsT),
6202 current_output(OldStream),
6203 set_output(Stream),
6204 nested_print_bexpr_as_classicalb2(HypsT,s(0)), % TODO: pass stream to this predicate
6205 format(Stream,'~nASSERTIONS /* Proof Goal: */~n',[]),
6206 nested_print_bexpr_as_classicalb2(Goal,s(0)), % TODO: pass stream to this predicate
6207 (AllHypsList = [] -> true
6208 ; sort(AllHypsList,SAL), sort(HypsList,SL),
6209 ord_subtract(SAL,SL,RemainingHypsList), % TODO: we could preserve order
6210 conjunct_predicates(RemainingHypsList,AllHypsPred),
6211 find_typed_identifier_uses(AllHypsPred, Ignored, TAllUsedIds),
6212 get_texpr_ids(TAllUsedIds,AllUsedIds),
6213 ord_subtract(AllUsedIds,UsedIds,NewIds), % compute new ids not used in selected hyps and goal
6214 (NewIds = []
6215 -> format(Stream,'OPERATIONS~n CheckRemainingHypotheses = SELECT~n',[])
6216 ; ajoin_with_sep(NewIds,', ',NIdLst),
6217 format(Stream,'OPERATIONS~n CheckRemainingHypotheses(~w) = SELECT~n',[NIdLst])
6218 ),
6219 add_typing_predicates(TAllUsedIds,AllHypsPred,AllHypsT),
6220 nested_print_bexpr_as_classicalb2(AllHypsT,s(0)), % TODO: pass stream to this predicate
6221 format(Stream,' THEN skip~n END /* CheckRemainingHypotheses */~n',[])
6222 ),
6223 set_output(OldStream),
6224 reset_print_type_infos(CHNG),
6225 format(Stream,'DEFINITIONS~n SET_PREF_DISPROVER_MODE == TRUE~n ; SET_PREF_TRY_FIND_ABORT == FALSE~n',[]),
6226 format(Stream,' ; SET_PREF_ALLOW_REALS == FALSE~n',[]),
6227 % The Rodin DisproverCommand.java usually enables CHR;
6228 % TODO: we could also look for options(List) in ProofInfos and check use_chr_solver/true in List, ...
6229 (get_preference(use_clpfd_solver,false) -> format(Stream,' ; SET_PREF_CHR == FALSE~n',[]) ; true),
6230 (get_preference(use_chr_solver,true) -> format(Stream,' ; SET_PREF_CHR == TRUE~n',[]) ; true),
6231 (get_preference(use_smt_mode,true) -> format(Stream,' ; SET_PREF_SMT == TRUE~n',[]) ; true),
6232 (get_preference(use_common_subexpression_elimination,true) -> format(Stream,' ; SET_PREF_CSE == TRUE~n',[]) ; true),
6233 (get_preference(smt_supported_interpreter,true) -> format(Stream,' ; SET_PREF_SMT_SUPPORTED_INTERPRETER == TRUE~n',[]) ; true),
6234 format(Stream,'END~n',[]).
6235
6236 format_proof_infos(_,Var) :- var(Var),!.
6237 format_proof_infos(Stream,disprover_result(Prover,Hyps,Result)) :- nonvar(Result),functor(Result,FR,_),!,
6238 format(Stream,' /* ProB Disprover ~w result on ~w : ~w */~n',[Prover,Hyps,FR]).
6239 format_proof_infos(Stream,E) :- format(Stream,' /* ~w */~n',[E]).
6240
6241
6242 % ---------------------------------------
6243
6244
6245 % show non obvious functors
6246 get_texpr_top_level_symbol(TExpr,Symbol,2,infix) :-
6247 translate:binary_infix_symbol(TExpr,Symbol),!.
6248 get_texpr_top_level_symbol(b(E,_,_),Symbol,1,postfix) :-
6249 functor(E,F,1), translate:unary_postfix_in_mode(F,Symbol,_),!.
6250 get_texpr_top_level_symbol(b(E,_,_),Symbol,3,prefix) :-
6251 functor(E,F,Arity), (Arity=3 ; Arity=2), % 2 for exists
6252 quantified_in_mode(F,Symbol),!.
6253 get_texpr_top_level_symbol(b(E,_,_),Symbol,N,prefix) :-
6254 functor(E,F,N),
6255 function_like_in_mode(F,Symbol).
6256
6257 % ---------------
6258
6259 % feedback to user about values
6260 translate_bvalue_kind([],Res) :- !, Res='EMPTY-Set'.
6261 translate_bvalue_kind([_|_],Res) :- !, Res='LIST-Set'.
6262 translate_bvalue_kind(avl_set(A),Res) :- !, avl_size(A,Size), ajoin(['AVL-Set:',Size],Res).
6263 translate_bvalue_kind(int(_),Res) :- !, Res = 'INTEGER'.
6264 translate_bvalue_kind(term(floating(_)),Res) :- !, Res = 'FLOAT'.
6265 translate_bvalue_kind(string(_),Res) :- !, Res = 'STRING'.
6266 translate_bvalue_kind(pred_true,Res) :- !, Res = 'TRUE'.
6267 translate_bvalue_kind(pred_false,Res) :- !, Res = 'FALSE'.
6268 translate_bvalue_kind(fd(_,T),Res) :- !, Res = T.
6269 translate_bvalue_kind((_,_),Res) :- !, Res = 'PAIR'.
6270 translate_bvalue_kind(rec(_),Res) :- !, Res = 'RECORD'.
6271 translate_bvalue_kind(freeval(Freetype,_Case,_),Res) :- !, Res = Freetype.
6272 translate_bvalue_kind(CL,Res) :- custom_explicit_sets:is_interval_closure(CL,_,_),!, Res= 'INTERVAL'.
6273 translate_bvalue_kind(CL,Res) :- custom_explicit_sets:is_infinite_explicit_set(CL),!, Res= 'INFINITE-Set'.
6274 translate_bvalue_kind(closure(_,_,_),Res) :- !, Res= 'SYMBOLIC-Set'.
6275
6276
6277 % ---------------------------------------
6278
6279 :- use_module(tools_printing,[better_write_canonical_to_codes/3]).
6280 pp_xtl_value(Value) --> better_write_canonical_to_codes(Value).
6281
6282 translate_xtl_value(Value,Output) :-
6283 pp_xtl_value(Value,Codes,[]),
6284 atom_codes_with_limit(Output,Codes).