1 % (c) 2009-2026 Lehrstuhl fuer Softwaretechnik und Programmiersprachen,
2 % Heinrich Heine Universitaet Duesseldorf
3 % This software is licenced under EPL 1.0 (http://www.eclipse.org/org/documents/epl-v10.html)
4
5
6 :- module(bvisual2,[ %reset_bvisual/0,
7 bv_get_top_level/1
8 , bv_expand_formula/3 % also used to get string of entry
9 , bv_get_values/3
10 , bv_get_values/2
11 , bv_insert_formula/3
12 %, bv_include_variables/0
13 , bv_get_value_unlimited/3
14 , bv_get_values_unlimited/3
15 , bv_is_explanation_node/1
16 , bv_print_to_file/3
17 , bv_write_all_variables_and_constants/2
18 , set_bvisual2_translation_mode/1
19 , bvisual2_translation_mode/1
20 , bv_get_top_level_formula/4
21 , bv_get_top_level_formula/5
22 , bv_value_to_atom/2
23 , bv_formula_origin/2
24 , bv_show_formula_origin/2
25 , bv_formula_description/2
26 , bv_formula_extended_description/2
27 , bv_formula_discharged_info/2
28 , bv_get_formula_functor_symbol/2
29 , bv_get_btvalue/4
30 , bv_is_child_formula/1
31 , bv_formula_labels/2
32 , bv_is_typed_formula/1
33 , bv_is_typed_predicate/1
34 , bv_is_typed_identifier/2
35 , bv_get_stored_formula_expr/2
36 , tcltk_register_new_user_formula/1
37 , bv_re_register_definitions_if_necessary/0
38 , html_debug_operation_for_stateid/4
39 , bv_top_level_set/2
40 ]).
41
42 :- meta_predicate bv_time_out_call(0,-,-,-).
43 :- meta_predicate call_with_temp_preference(*,*,0).
44 :- meta_predicate with_bvisual2_translation_mode(0).
45
46 :- use_module(probsrc(module_information),[module_info/2]).
47 :- module_info(group,ast).
48 :- module_info(description,'This module contains functionality to analyse B expressions and predicates by evaluating and decomposing them into substructures.').
49
50 :- use_module(library(lists)).
51 :- use_module(library(codesio)).
52 %:- use_module(library(timeout)).
53
54 :- use_module(probsrc(bmachine), [ b_machine_has_constants_or_properties/0
55 , b_get_machine_variables_in_original_order/1
56 , b_get_machine_constants/1
57 , b_get_machine_operation/4
58 , b_get_properties_from_machine/1
59 , b_machine_additional_property/1
60 , get_invariant_list_with_proof_info/1
61 , number_of_ignored_invariants/1
62 , b_get_static_assertions_from_machine/1
63 , b_get_assertions_from_main_machine/2
64 , b_get_dynamic_assertions_from_machine/1
65 , b_get_assertion_count/3
66 , b_get_operation_variant/3
67 , b_get_operation_description/2
68 ]).
69 :- use_module(probsrc(specfile),[ state_corresponds_to_initialised_b_machine/2
70 , state_corresponds_to_set_up_constants/2, property/2, xtl_mode/0
71 ]).
72 :- use_module(probsrc(state_space), [ visited_expression/2
73 , current_state_id/1
74 , invariant_not_yet_checked/1
75 , invariant_violated/1
76 , transition/3
77 ]).
78 :- use_module(probsrc(store),[ empty_state/1
79 , normalise_store/2
80 ]).
81 :- use_module(probsrc(b_interpreter),[
82 b_not_test_boolean_expression_cs/5
83 , b_test_boolean_expression_for_ground_state/5
84 , b_test_boolean_expression/4
85 , b_compute_expression_nowf/6
86 , b_execute_statement_nowf/5
87 , set_up_typed_localstate/6
88 , properties_were_filtered/1
89 ]).
90 :- use_module(probsrc(translate),[ translate_bexpression_to_codes/2
91 , translate_subst_or_bexpr_with_limit/3
92 , get_texpr_top_level_symbol/4
93 , translate_bvalue_for_expression_with_limit/4
94 , translate_bvalue_for_expression/3
95 , translate_bvalue_with_type_and_limit/4
96 , translate_bvalue/2
97 , translate_any_state/2
98 , translate_b_state_to_comma_list/3
99 , translate_prolog_exception/2
100 , translate_eventb_direct_definition_header/3
101 , translate_eventb_direct_definition_body/2
102 ]).
103 :- use_module(probsrc(bsyntaxtree),[ get_texpr_expr/2
104 , get_texpr_type/2
105 , get_texpr_info/2
106 , get_texpr_id/2
107 , is_texpr/1
108 , create_texpr/4
109 , safe_create_texpr/3
110 , syntaxtraversion/6
111 , conjunction_to_list/2
112 , conjunct_predicates/2
113 , create_exists/3
114 , create_or_merge_exists/3
115 , create_forall/3
116 , texpr_contains_wd_condition/1
117 %, find_identifier_uses/3
118 ]).
119 :- use_module(probsrc(b_ast_cleanup),[ clean_up_pred/3
120 ]).
121 :- use_module(probsrc(error_manager),[ get_all_errors_and_clear/1
122 , get_perror/4
123 , time_out_with_enum_warning_one_solution/3
124 ]).
125 :- use_module(probsrc(preferences), [ get_preference/2
126 , set_preference/2
127 ]).
128 :- use_module(probsrc(kernel_waitflags), [ init_wait_flags_with_call_stack/2
129 , ground_wait_flags/1]).
130 :- use_module(probsrc(b_enumerate),[ b_tighter_enumerate_values_in_ctxt/3 ]).
131 :- use_module(probsrc(tools_files),[ put_codes/2]).
132 :- use_module(probsrc(tools_strings),[ strip_newlines/2, atom_prefix/2]).
133 :- use_module(probltlsrc(ltl),[get_ltl_formula_strings/2]). % did lead to SPIO_E_TOO_MANY_OPEN_FILES
134 :- use_module(probltlsrc(ltl_translate),[pp_ltl_formula/2, get_ltl_sub_formulas/3]).
135 :- use_module(probltlsrc(ltl_propositions), [is_atomic_ltl_property/1, check_atomic_property_formula/2]).
136 :- use_module(probltlsrc(ltl_tools),[temporal_parser/3]).
137
138 :- use_module(probsrc(bmachine_eventb), [ stored_operator_direct_definition/8]).
139 :- use_module(probsrc(debug), [debug_format/3, debug_mode/1]).
140
141 :- set_prolog_flag(double_quotes, codes).
142
143 :- dynamic top_level_node/1, subnode/2, supernode/2.
144 :- dynamic stored_formula/2, expanded/1, explanation_parent/1.
145 :- dynamic id_counter/1.
146 :- dynamic formula_cache/3.
147 % We store the information whether a node can introduce new values as an (counter-)example
148 % in example_node/1; the value is computed in explore_node/2.
149 % For such nodes, the found examples are stored in local_state/3.
150 :- dynamic example_node/1,local_state/3.
151
152
153
154 bv_value_to_atom(p(P),R) :- !, % a predicate value
155 (P=true -> R='TRUE' ; P=false -> R='FALSE' ; R=P).
156 bv_value_to_atom(e(_),R) :- !, R='ERROR'.
157 bv_value_to_atom(v(V),R) :- !, R=V. % a value, already pretty-printed !??
158 bv_value_to_atom(i,R) :- !, R='INACTIVE'.
159 bv_value_to_atom(bv_info(V),R) :- !, R=V.
160 bv_value_to_atom(V,V).
161
162
163 :- dynamic variables_should_be_included/0.
164 variables_should_be_included.
165 %bv_include_variables :-
166 % (variables_should_be_included -> true ; assertz(variables_should_be_included)).
167
168 clear_bvisual :-
169 retractall(top_level_node(_)),
170 retractall(subnode(_,_)), % TO DO: do not delete user formulas for a simple re-load ?!
171 retractall(supernode(_,_)),
172 retractall(stored_formula(_,_)),
173 retractall(expanded(_)),
174 retractall(explanation_parent(_)),
175 retractall(formula_cache(_,_,_)),
176 retractall(example_node(_)),
177 retractall(local_state(_,_,_)),
178 retractall(specfile_property_cache(_,_)),
179 retractall(specfile_property_cached_state(_)).
180 % This predicate should be called after loading a new specification
181 % and before using the other predicates below
182 reset_bvisual :-
183 clear_bvisual,
184 register_top_level.
185
186 :- public portray_bvisual/0. % debugging utility
187 portray_bvisual :- %listing(top_level_node/1), listing(subnode/2), listing(stored_formula/2)
188 top_level_node(N), bv_portray(N,1),fail.
189 portray_bvisual.
190 bv_portray(ID,Level) :-
191 (stored_formula(ID,Form) -> functor(Form,F,_) ; F = '??'),
192 (expanded(ID) -> E=expanded ; E = 'not_expanded'),
193 indent(Level),format('~w -> ~w [~w]~n',[ID,F,E]),
194 L1 is Level+1,
195 subnode(ID,ID2),
196 bv_portray(ID2,L1).
197 indent(0) :- !.
198 indent(N) :- N>0,!, N1 is N-1, print(' '), indent(N1).
199
200 % returns the top-level nodes as a list of IDs
201 bv_get_top_level(Tops) :-
202 findall( Id, top_level_node(Id), Tops ).
203
204 % bv_expand_formula(+FormulaID,-LabelAtom,-)
205 % Input: Id
206 % Output: Label of Formula, list of children
207 bv_expand_formula(Id,Label,Children) :-
208 expanded(Id),!,
209 get_node_label(Id,Label),
210 findall(C, subnode(Id,C), Children).
211 bv_expand_formula(Id,Label,Children) :-
212 get_node_label(Id,Label),
213 explore_node(Id,Children).
214
215 % bv_get_values in current state
216 bv_get_values(Ids,Values) :-
217 current_state_id(StateId),
218 bv_get_values(Ids,StateId,Values).
219
220 :- use_module(probsrc(specfile),[prepare_state_for_specfile_trans/3]).
221 :- use_module(probsrc(tools),[start_ms_timer/1,get_elapsed_runtime/2]).
222
223 % bv_get_values(+IdsOfFormulas,+CurrentStateID,-ValuesOfFormulas)
224 bv_get_values(Ids,StateId,Values) :-
225 visited_expression(StateId,State),!,
226 prepare_state_for_specfile_trans(State,StateId,PreparedState),
227 bv_get_default_formula_timeout(Timeout),
228 start_ms_timer(Timer),
229 bv_get_values2(Ids,PreparedState,StateId,Timeout,Timer,Values).
230 bv_get_values(Ids,_StateId,Values) :-
231 % in case that the state ID cannot be resolved, we
232 % return an error for each formula
233 same_length(Ids,Values),
234 same_value(Values,e('unknown state')).
235 same_value([],_).
236 same_value([V|Rest],V) :- same_value(Rest,V).
237
238 % get timeout for standard formula evaluation of an entry in the bvisual2 table:
239 bv_get_default_formula_timeout(BVTimeout) :-
240 get_preference(time_out,Timeout),
241 (Timeout =< 1200 -> BVTimeout = Timeout
242 ; BVTimeout is 1200 + (Timeout-1000)// 5).
243
244 % Insert a new formula
245 % bv_insert_formula(+TypeCheckedExpression,+ParentID,-IDofNewFormula)
246 bv_insert_formula(TExpr,ParentId,Id) :- %print(bv_insert_formula(TExpr,ParentId)),nl,
247 get_new_id(Id),
248 assertz( stored_formula(Id,TExpr) ),
249 assertz( subnode(ParentId,Id) ),
250 assertz( supernode(Id,ParentId) ),
251 (ParentId == top -> assertz(top_level_node(Id)) ; true).
252
253 bv_time_out_call(Call,ValueFromCall,Timeout,Value) :-
254 catch(bv_time_out_call2(Call,ValueFromCall,Timeout,Value),Exception,
255 (Value=e(ErrStr),translate_prolog_exception(Exception,ErrStr))).
256 bv_time_out_call2(Call,ValueFromCall,Timeout,Value) :-
257 (time_out_with_enum_warning_one_solution( Call,
258 Timeout,
259 Result)
260 ->
261 ( Result == success -> Value = ValueFromCall
262 ; Result = virtual_time_out(failure_enumeration_warning(_Info,_,_,_,critical)) -> Value = e('?(\x221E\)') % Value = e('\x22A5\?(\x221E\)') % 8734 in decimal % Feedback for user : increasing MAXINT,... could mean ProB can find a solution; However, increasing TIMEOUT value will not help
263 ; Result = virtual_time_out(_) -> Value = e('?(\x221E\)') % infinity symbol: Feedback to the user : probably no way to solve the issue apart from ensuring that set comprehensions are finite ...
264 ; Value = e(timeout))
265 ).
266
267 % compute a local timeout based on how long the bv_get_values command has been running thus far:
268 get_local_formula_timeout(DefaultTimeout,TimerSinceStart,FormulaTimeout) :-
269 get_elapsed_runtime(TimerSinceStart,Delta),
270 (Delta > 2*DefaultTimeout % then reduce timeout
271 -> Factor is Delta / DefaultTimeout,
272 debug_format(19,'Reducing timeout by factor of ~w (runtime thus far: ~w, default timeout: ~w)~n',[Factor,Delta,DefaultTimeout]),
273 FormulaTimeout is integer(DefaultTimeout / Factor)
274 ; FormulaTimeout=DefaultTimeout).
275
276 bv_get_values2([],_,_,_,_,[]).
277 bv_get_values2([Id|Irest],State,StateId,Timeout,TimerSinceStart,[Value|Vrest]) :-
278 get_local_formula_timeout(Timeout,TimerSinceStart,FormulaTimeout),
279 bv_get_value(Id,State,StateId,FormulaTimeout,Value),
280 bv_get_values2(Irest,State,StateId,Timeout,TimerSinceStart,Vrest).
281
282 bv_get_value(Id,State1,StateId,Timeout,FinalValue) :-
283 bv_get_value_unprocessed(Id,State1,StateId,Timeout,Value),
284 value_post_processing(Value,FinalValue).
285 bv_get_value_unprocessed(Id,State1,StateId,Timeout,Value) :-
286 is_active(Id,StateId,State1,State,LocalState),!, %print(is_active(Id)), debug:nl_time,
287 ( is_cached(Id,StateId,State1,Value1) ->
288 FromCache = true
289 ; bv_get_value1(Id,StateId,State,LocalState,Timeout,Value1) ->
290 FromCache = false %, print(evaluated),debug:nl_time
291 ;
292 Value1 = e('evaluation failed'),
293 FromCache = false),
294 handle_errors(Value1,Value),
295 (FromCache==false, should_be_cached(Id) -> write_to_cache(Id,StateId,State1,Value) ; true).
296 bv_get_value_unprocessed(_Id,_State1,_StateId,_Timeout,i). % INACTIVE
297
298 bv_get_value1(Id,StateId,State,LocalState,Timeout,Value) :-
299 stored_formula(Id,Formula),
300 bv_get_value2(Formula,Id,StateId,State,LocalState,Timeout,Value).
301
302 bv_get_value2(Formula,Id,_,State,LocalState,Timeout,Value) :-
303 is_texpr(Formula),!,
304 get_texpr_type(Formula,Type),
305 bv_get_texpr_value3(Type,Id,Formula,LocalState,State,Timeout,Value).
306 bv_get_value2(named_subformula(_,Formula,Error),Id,StateId,State,LocalState,Timeout,Value) :- !,
307 (Error='$no_error' -> bv_get_value2(Formula,Id,StateId,State,LocalState,Timeout,Value)
308 ; Value = e(Error)).
309 bv_get_value2(bind(_ID,Value),_,_,_,_,_,btvalue(Value)).
310 bv_get_value2(textnode(Value,_),_,_,_State,_LS,_,v(Value)).
311 bv_get_value2(textnode3(_,Value,_),_,_,_State,_LS,_,ResValue) :-
312 (color_based_on_value(Value,_) -> ResValue = Value % already using encoding
313 ; ResValue = v(Value)).
314 %bv_get_value2(cbc_path(_LastStateID,Path,_Last),_,_,_State,_LS,_,v(NrOfPaths)) :-
315 % append(Path,_,Prefix),
316 % findall(Last,sap:cb_path(_,Prefix,Last),AllPathsWithPrefix),
317 % length(AllPathsWithPrefix,NrOfPaths).
318 bv_get_value2(included_machine(_,Value,_),_,_,_State,_LS,_,v(Value)).
319 bv_get_value2(variant(_Name,_ConvOrAnt,Variant),Id,StateId,State,LocalState,Timeout,Value) :- !,
320 bv_get_value2(Variant,Id,StateId,State,LocalState,Timeout,Value).
321 bv_get_value2(ltl_named_formula(_Name,String),_Id,_StateId,_,_LocalState,_Timeout,Value) :- !,
322 strip_newlines(String,String2), Value=v(String2).
323 bv_get_value2(ltl_formula(_,Tree),_Id,StateId,_,_LocalState,_Timeout,Value) :- !,
324 (is_atomic_ltl_property(Tree) ->
325 (check_atomic_property_formula(Tree,StateId) -> Value = p(true) ; Value=p(false))
326 ; Value = v('-')). % TO DO: we could evaluate the formulas on back and forward history; ap(enabled(E))
327 bv_get_value2(guard(_Name,Parameters,Guard),Id,StateId,State,LocalState,Timeout,Value) :- !,
328 get_guard_formula(Parameters,Guard,Expr),
329 bv_get_value2(Expr,Id,StateId,State,LocalState,Timeout,Value).
330 bv_get_value2(guard_theorems(_Name,Parameters,Guard,Theorems),FID,StateId,State,LocalState,Timeout,Value) :- !,
331 get_guard_theorems_formula(Parameters,Guard,Theorems,Expr),
332 bv_get_value2(Expr,FID,StateId,State,LocalState,Timeout,Value).
333 bv_get_value2(raw_state_pp,_,_,State,_LS,_,v(Value)) :- !, % raw pretty printed stated
334 translate_any_state(State,Value).
335 bv_get_value2(property_pp(Nr),_,_,State,_LS,_,v(Value)) :- !, % xtl or csp properties
336 get_specfile_property(State,Nr,Value).
337 bv_get_value2(Node,_,_,_State,_LS,_,e(unknown)) :-
338 format('Unknown bvisual2 node: ~w~n',[Node]).
339
340
341 :- dynamic specfile_property_cache/2, specfile_property_cached_state/1.
342 get_specfile_property(State,Nr,PVal) :- recompute_specfile_property_cache(State),
343 specfile_property_cache(Nr,PVal).
344
345 recompute_specfile_property_cache(State) :- \+ specfile_property_cached_state(State),
346 retractall(specfile_property_cached_state(_)),
347 assert(specfile_property_cached_state(State)),
348 retractall(specfile_property_cache(_,_)),
349 findall(P,specfile:property(State,P),List),
350 is_property_atom(_Atom,Nr),
351 nth1(Nr,List,PVal),
352 write_to_codes(PVal,C), atom_codes(Value,C),
353 assert(specfile_property_cache(Nr,Value)),fail.
354 recompute_specfile_property_cache(_).
355
356
357
358 bv_get_texpr_value3(pred,Id,Formula,LocalState,State,Timeout,Value) :-
359 !,
360 % print('CHECKING PREDICATE: '), translate:print_bexpr(Formula),nl,
361 CS = 'state view table',
362 (bv_time_out_call(b_test_boolean_expression_for_ground_state(Formula,LocalState,State,CS,Id),true,Timeout,ValuePos) -> true ; ValuePos=false),
363 ( get_preference(double_evaluation_when_analysing,true) ->
364 CS2 = 'state view table (negated)',
365 (bv_time_out_call(b_not_test_boolean_expression_cs(Formula,LocalState,State,CS2,Id),true,Timeout,ValueNeg)
366 -> true ; ValueNeg=false),
367 combine_predicate_values(ValuePos,ValueNeg,Value)
368 ; encode_predicate_value(ValuePos,Value)).
369 bv_get_texpr_value3(subst,_Id,Formula,LocalState,State,Timeout,Res) :- !,
370 (bv_time_out_call(b_execute_statement_nowf(Formula,LocalState,State,OutState,prob_command_context),
371 OutState,Timeout,Value)
372 -> ( Value = e(E) -> Res = e(E) % e for exception or timeout
373 ; Res = btstate(Value)
374 )
375 ; Res = e(infeasible)
376 ).
377 bv_get_texpr_value3(_,Id,Formula,LocalState,State,Timeout,Res) :-
378 CS = 'state view table',
379 (bv_time_out_call(b_compute_expression_nowf(Formula,LocalState,State,FValue,CS,Id),FValue,Timeout,Value)
380 -> ( Value = e(E) -> Res = e(E) % e for exception or timeout
381 ; Res = btvalue(Value,Formula)
382 )
383 ; texpr_contains_wd_condition(Formula) -> Res = e(undefined)
384 ).
385
386
387
388 encode_predicate_value(e(Error),e(Error)).
389 encode_predicate_value(bv_info(Error),bv_info(Error)).
390 encode_predicate_value(true,p(true)).
391 encode_predicate_value(false,p(false)).
392 combine_predicate_values(e(_Error),true,p(false)) :-
393 print('### Ignoring Timeout in Positive Case of DOUBLE_EVALUATION as Negated Predicate is TRUE'),nl.
394 combine_predicate_values(e(_Error),false,p(true)) :-
395 print('### Ignoring Timeout in Positive Case of DOUBLE_EVALUATION as Negated Predicate is FALSE'),nl.
396 combine_predicate_values(e(Error),e(_),e(Error)).
397 combine_predicate_values(true,false,p(true)).
398 combine_predicate_values(false,true,p(false)).
399 combine_predicate_values(true,true,e('both true and false')).
400 combine_predicate_values(false,false,e(undefined)).
401 combine_predicate_values(true,e(_Error),p(true)) :-
402 print('### Ignoring Timeout in Negative Case of DOUBLE_EVALUATION'),nl.
403 combine_predicate_values(false,e(_Error),p(false)) :-
404 print('### Ignoring Timeout in Negative Case of DOUBLE_EVALUATION'),nl.
405
406 :- dynamic get_unlimited_value/0.
407
408
409 value_post_processing(In,Out) :-
410 with_bvisual2_translation_mode(
411 (value_post_processing2(In,O) -> O=Out
412 ; Out=e('internal error: failed to post-process value'))).
413 value_post_processing2(v(Value),v(Value)).
414 value_post_processing2(p(Value),p(Value)).
415 value_post_processing2(e(Error),e(Error)).
416 value_post_processing2(i,i).
417 value_post_processing2(bv_info(I),bv_info(I)).
418 value_post_processing2(btvalue(BValue,Expr),v(Value)) :-
419 (get_unlimited_value -> translate_bvalue_for_expression(BValue,Expr,Value)
420 ; translate_bvalue_for_expression_with_limit(BValue,Expr,600,Value) % one can always use bv_get_value_unlimited
421 ).
422 value_post_processing2(btvalue(BValue),v(Value)) :-
423 % TO DO: get type for variables and constants
424 (get_unlimited_value -> translate_bvalue(BValue,Value)
425 ; translate_bvalue_with_type_and_limit(BValue,any,600,Value)).
426 value_post_processing2(btstate(Updates),v(Value)) :-
427 (Updates=[] -> Value='skip'
428 ; get_unlimited_value -> translate_b_state_to_comma_list(Updates,100000,Value) % TODO: not unlimited
429 ; translate_b_state_to_comma_list(Updates,600,Value)).
430
431
432
433
434 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
435 % caching of some values
436 :- use_module(probsrc(store),[no_value_for_variable/2]).
437
438 % we do not compute the theorems node, only its subnodes
439 is_cached(theoremsc,_,_,v('')) :- !.
440 is_cached(theoremscm,_,_,v('')) :- !.
441 is_cached(theoremsv,_,_,v('')) :- !.
442 is_cached(theoremsvm,_,_,v('')) :- !.
443 is_cached(goal,_,_,v('')) :- !.
444 % we use the invariant_violated flag of the state space as a kind of cache
445 is_cached(inv,StateId,_State,Value) :-
446 \+ invariant_not_yet_checked(StateId),
447 (invariant_violated(StateId) -> Value=p(false)
448 ; number_of_ignored_invariants(Nr), Nr>0 ->
449 get_node_label(invariants,INV),
450 ajoin(['some ', INV,' (',Nr,') were ignored'],Msg), Value = e(Msg)
451 ; Value=p(true)).
452 % axioms are usually true in the constants' state
453 is_cached(axioms,StateId,concrete_constants(State),R) :- !,
454 is_cached_constants_state(StateId,State,R).
455 is_cached(axioms,StateId,[_|_],R) :- !, % states consisting of just a list are either constructed by
456 % find valid state, state-based MC,... or when there are no constants
457 % We assume PROPERTIES/axioms to be true; by constructing these commands have to satisfy the properties
458 is_cached_constants_state(StateId,[],R).
459 % this formula was already computed and cached for the given state
460 is_cached(FormulaId,StateId,_State,Value) :-
461 formula_cache(FormulaId,StateId,Value),!.
462 % in case of axiom, we can look at the reverences constants' state
463 is_cached(FormulaId,_,expanded_const_and_vars(ConstId,_,_,_),Value) :- is_cached_axiom(FormulaId,ConstId,Value).
464 is_cached(FormulaId,_,const_and_vars(ConstId,_),Value) :- is_cached_axiom(FormulaId,ConstId,Value).
465 is_cached_axiom(FormulaId,ConstId,Value) :-
466 is_axiom_or_static_theorem(FormulaId),
467 visited_expression(ConstId,ConstState),
468 % we could avoid unpacking, but constants state are not packed anyway; full value is only used for partial_setup_constants below; TO DO avoid extracting ConstState
469 is_cached(FormulaId,ConstId,ConstState,Value),!.
470
471 is_cached_constants_state(StateId,State,R) :-
472 % only if a partial_setup_constants event led to that
473 % state, then something did not work
474 ( transition(root,'$partial_setup_constants',StateId) ->
475 (member(bind(_,Val),State), no_value_for_variable(Val,_)
476 -> R = e('some constants have no values')
477 ; properties_were_filtered(Nr), Nr>0
478 -> get_node_label(axioms,Axioms),
479 ajoin(['some ',Axioms, ' (',Nr,') were ignored'],Msg), R = e(Msg)
480 ; animation_minor_mode(eventb) -> R = e('some axioms may be false')
481 ; R = e('some properties may be false')
482 )
483 ; R = p(true)).
484
485 should_be_cached(FormulaId) :-
486 is_axiom_or_static_theorem(FormulaId).
487
488 write_to_cache(FormulaId,_StateId,const_and_vars(ConstId,_),Value) :-
489 is_axiom_or_static_theorem(FormulaId),!,
490 visited_expression(ConstId,ConstState),
491 write_to_cache(FormulaId,ConstId,ConstState,Value).
492 write_to_cache(FormulaId,StateId,_State,Value) :-
493 assertz(formula_cache(FormulaId,StateId,Value)).
494
495 is_axiom_or_static_theorem(axioms) :- !.
496 is_axiom_or_static_theorem(theoremsc) :- !.
497 is_axiom_or_static_theorem(theoremscm) :- !.
498 is_axiom_or_static_theorem(FormulaId) :-
499 supernode(FormulaId,Parent),is_axiom_or_static_theorem(Parent).
500
501 :- use_module(probsrc(tools_strings),[atom_codes_with_limit/3]).
502 handle_errors(_,e(ErrorMsg)) :-
503 get_all_errors_and_clear([Error|_]),!, % TO DO: use proper error scoping and do not trigger when error stored before calling bvisual2
504 write_to_codes(Error,ErrorCodes),
505 atom_codes_with_limit(ErrorMsg,200,ErrorCodes).
506 handle_errors(Value,Value).
507
508
509 % is_active(+FormulaID,+InState,-PossiblyExpandedState)
510 % says if a FormulaID makes sense for the current state, if not: it will be grayed out
511 is_active(user,StateId,InState,OutState,LocalState) :- !,
512 is_active(inv,StateId,InState,OutState,LocalState).
513 is_active(inv,_StateId,InState,OutState,LocalState) :- !,
514 empty_state(LocalState),
515 state_corresponds_to_initialised_b_machine(InState,OutState).
516 is_active(axioms,_StateId,InState,OutState,LocalState) :- !,
517 InState \= root,
518 empty_state(LocalState),
519 state_corresponds_to_set_up_constants(InState,OutState),
520 !.
521 is_active(variants,StateId,InState,OutState,LocalState) :- !,
522 is_active(inv,StateId,InState,OutState,LocalState).
523 is_active(theoremsc,StateId,InState,OutState,LocalState) :- !,
524 is_active(axioms,StateId,InState,OutState,LocalState).
525 is_active(theoremscm,StateId,InState,OutState,LocalState) :- !,
526 is_active(axioms,StateId,InState,OutState,LocalState).
527 is_active(theoremsv,StateId,InState,OutState,LocalState) :- !,
528 is_active(inv,StateId,InState,OutState,LocalState).
529 is_active(theoremsvm,StateId,InState,OutState,LocalState) :- !,
530 is_active(inv,StateId,InState,OutState,LocalState).
531 is_active(guards_top_level,StateId,InState,OutState,LocalState) :- !,
532 is_active(inv,StateId,InState,OutState,LocalState).
533 is_active(guards_subsidiary,StateId,InState,OutState,LocalState) :- !,
534 is_active(inv,StateId,InState,OutState,LocalState).
535 is_active(guard_theorems,StateId,InState,OutState,LocalState) :- !,
536 is_active(inv,StateId,InState,OutState,LocalState).
537 is_active(variables,StateId,InState,OutState,LocalState) :- !,
538 is_active(inv,StateId,InState,OutState,LocalState).
539 is_active(constants,StateId,InState,OutState,LocalState) :- !,
540 is_active(axioms,StateId,InState,OutState,LocalState).
541 is_active(sets,_StateId,InState,OutState,LocalState) :- !, b_or_z_successsful_mode,
542 empty_state(LocalState), InState=OutState.
543 is_active(freetypes,_StateId,InState,OutState,LocalState) :- !,
544 empty_state(LocalState), InState=OutState.
545 is_active(goal,StateId,InState,OutState,LocalState) :- !,
546 is_active(inv,StateId,InState,OutState,LocalState).
547 is_active(user_formulas,StateId,InState,OutState,LocalState) :- !,
548 is_active(inv,StateId,InState,OutState,LocalState).
549 is_active(definitions,StateId,InState,OutState,LocalState) :- !,
550 is_active(inv,StateId,InState,OutState,LocalState).
551 is_active(included_machines,_StateId,_InState,_OutState,_LocalState) :- !,
552 b_or_z_mode.
553 is_active(ltl_formulas,StateId,InState,OutState,LocalState) :- !,
554 is_active(inv,StateId,InState,OutState,LocalState).
555 is_active(channels,_StateId,S,OutState,LocalState) :- !,
556 csp_mode, OutState=S,empty_state(LocalState).
557 is_active(datatypes,_StateId,S,OutState,LocalState) :- !,
558 csp_mode, OutState=S,empty_state(LocalState).
559 is_active(subtypes,_StateId,S,OutState,LocalState) :- !,
560 csp_mode, OutState=S,empty_state(LocalState).
561 is_active(raw_state,_StateId,S,OutState,LocalState) :- !,
562 \+ b_or_z_mode, % maybe we should extract it for csp_and_b ?
563 OutState=S,empty_state(LocalState).
564 %is_active(cbc_tests,_StateId,_InState,_OutState,_LocalState) :- !,
565 % sap:cb_path(_,[_],_). % show cbc_tests if we ran CBC test-case generator
566 is_active(Atom,_StateId,S,OutState,LocalState) :- is_property_atom(Atom,Nr),!,
567 get_specfile_property(S,Nr,_),
568 OutState=S,empty_state(LocalState).
569 is_active(NodeId,StateId,InState,OutState,LocalState) :-
570 supernode(NodeId,Super),
571 is_active(Super,StateId,InState,OutState,InLocalState),
572 % check if the parent node introduces example values (e.g. in a exists-clause)
573 % If yes, the local state is extended by the values
574 ( example_node(Super) ->
575 find_example(Super,StateId,OutState,InLocalState,LocalState)
576 ;
577 InLocalState = LocalState).
578
579 find_example(NodeId,StateId,_State,_InLocalState,LocalState) :-
580 % check if the value was computed and cached before
581 local_state(NodeId,StateId,Result),!,
582 % if Result is not of the form example/1, no example
583 % was found. Just fail to show inactive state
584 Result = example(LocalState).
585 find_example(NodeId,StateId,State,InLocalState,LocalState) :-
586 % how the example is computed depends on the expression (e.g. exists)
587 % and the outcome of the parent node (true or false)
588 bv_get_values([NodeId],StateId,[V]),!,
589 stored_formula(NodeId,Formula),
590 ( find_example1(Formula,V,State,InLocalState,LocalState) ->
591 Result = example(LocalState)
592 ;
593 Result = not_found),
594 % currently we just ignore errors
595 handle_errors(_,_),
596 assertz( local_state(NodeId,StateId,Result) ),
597 Result = example(_).
598 find_example1(TExpr,V,State,InLocalState,LocalState) :-
599 is_texpr(TExpr),!,
600 get_texpr_expr(TExpr,Expr),
601 find_example2(Expr,V,State,InLocalState,LocalState).
602 find_example1(guard(_Name,Parameters,Guard),V,State,InLocalState,LocalState) :-
603 create_or_merge_exists(Parameters,Guard,Exists),
604 find_example1(Exists,V,State,InLocalState,LocalState).
605 find_example1(guard_theorems(_Name,Parameters,Guard,Theorems),V,State,InLocalState,LocalState) :-
606 create_forall(Parameters,b(implication(Guard,Theorems),pred,[]),ForAll),
607 find_example1(ForAll,V,State,InLocalState,LocalState).
608 find_example2(let_predicate(Ids,AssignmentExprs,Pred),Status,State,InLocalState,LocalState) :-
609 % translate LET back to exists
610 create_exists_for_let(Ids,AssignmentExprs,Pred,_,ExistsPred),
611 find_example2(exists(Ids,ExistsPred),Status,State,InLocalState,LocalState).
612 find_example2(let_expression(Ids,AssignmentExprs,_Expr),_Status,State,InLocalState,LocalState) :-
613 % translate LET expression to exists to find solution for assignments
614 % _Status is typically v(Value)
615 create_exists_for_let(Ids,AssignmentExprs,b(truth,pred,[]),_,ExistsPred),
616 find_example2(exists(Ids,ExistsPred),p(true),State,InLocalState,LocalState).
617 find_example2(exists(Ids,Predicate),p(true),State,InLocalState,LocalState) :-
618 find_solution_for_predicate(Ids,Predicate,State,InLocalState,LocalState).
619 find_example2(exists(Ids,Predicate),p(false),State,InLocalState,LocalState) :-
620 Predicate = b(conjunct(_,_),pred,_), % we have a false conjunct: try and find maximal prefix that is satisfiable
621 conjunction_to_list(Predicate,PredList),
622 maxsat_conjunct(Ids,PredList,[],State,InLocalState,no_solution,LocalState),
623 LocalState \= no_solution,
624 !.
625 find_example2(exists(Ids,Predicate),p(false),State,InLocalState,LocalState) :-
626 safe_create_texpr(negation(Predicate),pred,Negation),
627 find_solution_for_predicate(Ids,Negation,State,InLocalState,LocalState).
628 find_example2(forall(Ids,Pre,Condition),p(false),State,InLocalState,LocalState) :-
629 safe_create_texpr(negation(Condition),pred,Negation),
630 conjunct_predicates([Pre,Negation],Predicate),
631 find_solution_for_predicate(Ids,Predicate,State,InLocalState,LocalState).
632 find_example2(forall(Ids,Pre,Condition),p(true),State,InLocalState,LocalState) :-
633 conjunct_predicates([Pre,Condition],Predicate),
634 find_solution_for_predicate(Ids,Predicate,State,InLocalState,LocalState),
635 !.
636 find_example2(forall(Ids,Pre,_),p(true),State,InLocalState,LocalState) :-
637 % the LHS of the forall is never true; try find maximal subsequence for it:
638 find_example2(exists(Ids,Pre),p(false),State,InLocalState,LocalState).
639 find_example2(comprehension_set(Ids,Predicate),v(VS),State,InLocalState,LocalState) :-
640 (empty_val_str(VS) -> PVal=false ; PVal=true),
641 find_example2(exists(Ids,Predicate),p(PVal),State,InLocalState,LocalState).
642 empty_val_str(VS) :- (VS = '{}' -> true ; atom(VS),atom_codes(VS,[8709])). % unicode empty set
643
644 find_solution_for_predicate(Ids,Predicate,State,InLocalState,NormedLocalState) :-
645 get_preference(time_out,Timeout),
646 set_up_typed_localstate(Ids,_FreshVars,TypedVals,InLocalState,LocalState,positive),
647 (time_out_with_enum_warning_one_solution(
648 (init_wait_flags_with_call_stack(WF,[prob_command_context(find_solution_for_predicate,unknown)]),
649 b_tighter_enumerate_values_in_ctxt(TypedVals,Predicate,WF),
650 b_test_boolean_expression(Predicate,LocalState,State,WF),
651 ground_wait_flags(WF)),
652 Timeout,Result)
653 -> Result == success,
654 normalise_store(LocalState,NormedLocalState)
655 ).
656
657 create_exists_for_let(Ids,AssignmentExprs,Pred,Equalities,ExistsPred) :-
658 maplist(create_equality,Ids,AssignmentExprs,Equalities),
659 append(Equalities,[Pred],L),
660 conjunct_predicates(L,ExistsPred).
661
662 create_equality(ID,Expr,Equality) :-
663 safe_create_texpr(equal(ID,Expr),pred,Equality).
664
665 % find maximal subsequence of conjuncts that are satisfiable and return LocalState solution found
666 maxsat_conjunct(Ids,[LHS|Rest],ConSoFar,State,InLocalState,_BestLocalStateSoFar,ResLocalState) :-
667 append(ConSoFar,[LHS],NewConjunctList),
668 %print('TRY: '), translate:print_bexpr(LHS),nl,
669 conjunct_predicates(NewConjunctList,NewConjunct),
670 find_solution_for_predicate(Ids,NewConjunct,State,InLocalState,LocalState),
671 !, % print('+'),
672 maxsat_conjunct(Ids,Rest,NewConjunctList,State,InLocalState, LocalState,ResLocalState).
673 maxsat_conjunct(Ids,[_|Rest],ConSoFar,State,InLocalState,BestLocalStateSoFar,Res) :- !, % allows skipping conjuncts
674 maxsat_conjunct(Ids,Rest,ConSoFar,State,InLocalState,BestLocalStateSoFar,Res).
675 maxsat_conjunct(_Ids,_Rest,_ConSoFar,_State,_InLocalState,BestLocalStateSoFar,Res) :-
676 Res = BestLocalStateSoFar.
677
678 explore_node(Id,Children) :-
679 stored_formula(Id,Formula),
680 check_if_example_node(Id,Formula),
681 get_subformulas(Formula,Subs,Kind),
682 assertz( expanded(Id) ),
683 (Kind = explanation(Operator)
684 -> assert_as_explanation(Id,ExplId,Operator), Children = [ExplId|Children1],
685 register_formulas(Subs,Id,Children1)
686 ; register_formulas(Subs,Id,Children)).
687
688 assert_as_explanation(Parent,Id,Operator) :-
689 assertz(explanation_parent(Parent)),
690 % add a text line; showing that we did an equivalence rewrite
691 get_new_id(Id),
692 assertz( stored_formula(Id,textnode3('\x21D4\',bv_info(Operator),[])) ), % unicode translation
693 assertz( subnode(Parent,Id) ),
694 assertz( supernode(Id,Parent) ).
695
696 bv_is_explanation_node(NodeId) :-
697 supernode(NodeId,ParentId),
698 explanation_parent(ParentId).
699
700 % the following predicates determine whether the node introduces
701 % example values and stores that information in example_node/1.
702 check_if_example_node(Id,Formula) :-
703 is_example_node(Formula),!,
704 assertz( example_node(Id) ).
705 check_if_example_node(_Id,_Formula).
706 is_example_node(TFormula) :-
707 is_texpr(TFormula),!,
708 get_texpr_expr(TFormula,Formula),
709 is_example_node2(Formula).
710 is_example_node(guard(_Name,[_|_],_Guard)).
711 is_example_node(guard_theorems(_Name,[_|_],_Guard,_Theorems)).
712 is_example_node2(exists(_Ids,_Cond)).
713 is_example_node2(forall(_Ids,_Pre,_Cond)).
714 is_example_node2(let_predicate(_Ids,_E,_Cond)).
715 is_example_node2(let_expression(_Ids,_E,_Cond)).
716 is_example_node2(comprehension_set(_Ids,_Cond)).
717
718 % subformula_rule/4 defines wich subformulas should be
719 % shown in an evaluation tree for a formula. If no
720 % rule matches, the standard subformulas are used
721 subformula_rule(forall(Ids,P,R),_,Subs,quantifier) :-
722 append(Ids,[P,R],Subs).
723 subformula_rule(let_predicate(Ids,AssignmentExprs,Pred),_,Subs,quantifier) :-
724 % see create_exists_for_let(Ids,AssignmentExprs,Pred,Equalities,_ExistsPred),
725 maplist(create_equality,Ids,AssignmentExprs,Equalities),
726 append(Equalities,[Pred],EP), % show equalities and then one subnode for the predicate
727 append(Ids,EP,Subs).
728 subformula_rule(exists(Ids,P),_,Subs,quantifier) :-
729 conjunction_to_list(P,PL),
730 append(Ids,PL,Subs).
731 subformula_rule(comprehension_set(Ids,P),_,Subs,quantifier) :-
732 conjunction_to_list(P,PL),
733 append(Ids,PL,Subs).
734 subformula_rule(conjunct(A,B),_,Subs,normal) :-
735 safe_create_texpr(conjunct(A,B),pred,E),
736 conjunction_to_list(E,Subs).
737 subformula_rule(disjunct(A,B),_,Subs,normal) :-
738 disjunction_to_list(A,B,Subs).
739 subformula_rule(let_expression(Ids,AssignmentExprs,Expr),_,Subs,quantifier) :-
740 maplist(create_equality,Ids,AssignmentExprs,Equalities),
741 append(Equalities,[Expr],EP), % show equalities and then one subnode for the expression
742 append(Ids,EP,Subs).
743 subformula_rule(Formula,TFormula,Subs,Kind) :-
744 get_preference(show_bvisual_formula_explanations,true),
745 subformula_explanation_rule(Formula,TFormula,Subs,Kind).
746
747 subformula_explanation_rule(equal(A,B),TF,[Sub1,Sub2],explanation(equal)) :-
748 get_texpr_type(A,T),T=set(SType),
749 \+ (kernel_objects:max_cardinality(SType,Max),number(Max),Max<10), % sets can never be very large
750 is_not_bvexpr(TF), % do not apply to explanation expressions
751 no_empty_set(A),no_empty_set(B),
752 !,
753 create_bvexpr(set_subtraction(A,B),T,AminusB),
754 create_bvexpr(set_subtraction(B,A),T,BminusA),
755 create_bvexpr(empty_set,T,Empty),
756 create_bvexpr(equal(AminusB,Empty),pred,Sub1),
757 create_bvexpr(equal(BminusA,Empty),pred,Sub2).
758 subformula_explanation_rule(subset(A,B),_,[Equals],explanation(subset)) :-
759 subset_rule(A,B,Equals).
760 subformula_explanation_rule(subset_strict(A,B),_,[AmBEmpty,NotEquals],explanation(subset_strict)) :-
761 subset_rule(A,B,AmBEmpty),
762 get_texpr_type(A,T),
763 create_bvexpr(set_subtraction(B,A),T,BminusA),
764 create_bvexpr(empty_set,T,Empty),
765 create_bvexpr(not_equal(BminusA,Empty),pred,NotEquals).
766 subformula_explanation_rule(not_subset(A,B),_,Subs,Kind) :-
767 subformula_explanation_rule(subset(A,B),_,Subs,Kind). % it is an equivalent rewrite to the negation
768 subformula_explanation_rule(not_subset_strict(A,B),_,Subs,Kind) :-
769 subformula_explanation_rule(subset_strict(A,B),_,Subs,Kind). % it is an equivalent rewrite to the negation
770 subformula_explanation_rule(member(TM,TS),_,Children,explanation(Func)) :-
771 get_texpr_expr(TS,S),
772 subformula_member_rule(S,TM,TS,Children),!,
773 functor(S,Func,_).
774
775 subset_rule(A,B,Equals) :- % A<:B <=> A\B = {}
776 get_texpr_type(A,T),
777 create_bvexpr(set_subtraction(A,B),T,AminusB),
778 create_bvexpr(empty_set,T,Empty),
779 create_bvexpr(equal(AminusB,Empty),pred,Equals1),
780 add_texpr_description(Equals1,'Subset check: find elements in left not in right set',Equals).
781
782 :- use_module(probsrc(bsyntaxtree),[is_just_type/1,add_texpr_description/3]).
783
784 subformula_member_rule(partial_function(TDom,TRan),TM,TS,[TDoubles]) :-
785 is_just_type(TDom),
786 is_just_type(TRan),
787 !,
788 % this rule describes the plain function check, without
789 % checking domains
790 texpr_function_type(TS,FunctionType,DomType,RanType),
791 create_texpr(identifier(d),DomType,[],TDomId),
792 create_texpr(identifier(r1),RanType,[],TRanId1),
793 create_texpr(identifier(r2),RanType,[],TRanId2),
794 create_texpr(comprehension_set([TDomId,TRanId1],TExists),FunctionType,[],TDoubles0),
795 % create_exists([TRanId2],TPred,TExists1), % moved below so that used ids can be computed
796 create_texpr(couple(TDomId,TRanId1),couple(DomType,RanType),[],TCouple1),
797 create_texpr(couple(TDomId,TRanId2),couple(DomType,RanType),[],TCouple2),
798 safe_create_texpr(not_equal(TRanId1,TRanId2),pred,Unequal),
799 create_texpr(member(TCouple1,TM),pred,[],TMember1),
800 create_texpr(member(TCouple2,TM),pred,[],TMember2),
801 conjunct_predicates([Unequal,TMember1,TMember2],TPred),
802 create_exists([TRanId2],TPred,TExists1), % now also computes used identifiers; cleanup no longer required ?
803 % This is necessary to register the used identifiers
804 clean_up_pred(TExists1,[],TExists),
805 add_texpr_description(TDoubles0,'Partial function check: find counter-example pairs for functionality',TDoubles).
806 subformula_member_rule(partial_function(TA,TB),TM,TS,[TFunCheck|SetChecks]) :-
807 create_function_check(TM,TS,TFunCheck,TDomain,TRange),
808 create_domain_range_checks(TDomain,TRange,TA,TB,SetChecks).
809 subformula_member_rule(total_function(TA,TB),TM,TS,[TFunCheck|SetChecks]) :-
810 create_function_check(TM,TS,TFunCheck,TDomain,TRange),
811 create_total_domain_range_checks(TDomain,TRange,TA,TB,SetChecks).
812 subformula_member_rule(partial_injection(TA,TB),TM,TS,[TFunCheck,TInjCheck|SetChecks]) :-
813 create_function_check(TM,TS,TFunCheck,TDomain,TRange),
814 create_injection_check(TM,TS,TInjCheck),
815 create_domain_range_checks(TDomain,TRange,TA,TB,SetChecks).
816 subformula_member_rule(total_injection(TA,TB),TM,TS,[TFunCheck,TInjCheck|SetChecks]) :-
817 create_function_check(TM,TS,TFunCheck,TDomain,TRange),
818 create_injection_check(TM,TS,TInjCheck),
819 create_total_domain_range_checks(TDomain,TRange,TA,TB,SetChecks).
820 subformula_member_rule(partial_surjection(TA,TB),TM,TS,Children) :-
821 create_function_check(TM,TS,TFunCheck,TDomain,TRange),
822 create_texpr(equal(TRange,TB),pred,[],TRanCheck),
823 create_optional_subset_check(TDomain,TA,DomChecks),
824 append([TFunCheck|DomChecks],[TRanCheck],Children).
825 subformula_member_rule(total_surjection(TA,TB),TM,TS,[TFunCheck,TDomCheck,TRanCheck]) :-
826 create_function_check(TM,TS,TFunCheck,TDomain,TRange),
827 create_texpr(equal(TDomain,TA),pred,[],TDomCheck),
828 create_texpr(equal(TRange,TB),pred,[],TRanCheck).
829 subformula_member_rule(total_bijection(TA,TB),TM,TS,[TFunCheck,TInjCheck,TDomCheck,TRanCheck]) :-
830 create_function_check(TM,TS,TFunCheck,TDomain,TRange),
831 create_injection_check(TM,TS,TInjCheck),
832 create_texpr(equal(TDomain,TA),pred,[],TDomCheck),
833 create_texpr(equal(TRange,TB),pred,[],TRanCheck).
834 subformula_member_rule(partial_bijection(TA,TB),TM,TS,Children) :-
835 create_function_check(TM,TS,TFunCheck,TDomain,TRange),
836 create_injection_check(TM,TS,TInjCheck),
837 create_optional_subset_check(TDomain,TA,DomChecks),
838 create_texpr(equal(TRange,TB),pred,[],TRanCheck),
839 append([TFunCheck,TInjCheck|DomChecks],[TRanCheck],Children).
840 subformula_member_rule(pow_subset(TA),TM,_TS,[NewCheck]) :-
841 % TM : POW(TA) <=> TM \ TA = {}
842 get_texpr_type(TM,Type),
843 create_bvexpr(set_subtraction(TM,TA),Type,Diff),
844 create_bvexpr(empty_set,Type,Empty),
845 create_bvexpr(equal(Diff,Empty),pred,NewCheck).
846 % TO DO: add rules for <:, <<:, FIN, POW1, FIN1
847
848 :- use_module(probsrc(typing_tools),[create_maximal_type_set/2]).
849
850 create_function_check(TM,TS,TFunCheck,TDomain,TRange) :-
851 texpr_function_type(TS,FunctionType,DomType,RanType),
852 create_maximal_type_set(DomType,TDom), % we could use typeset/0, but requires going through ast_cleanup
853 create_maximal_type_set(RanType,TRan), % ditto
854 create_bvexpr(partial_function(TDom,TRan),set(FunctionType),TFun),
855 create_bvexpr(member(TM,TFun),pred,TFunCheck0),
856 create_bvexpr(domain(TM),set(DomType),TDomain),
857 create_bvexpr(range(TM),set(RanType),TRange),
858 add_texpr_description(TFunCheck0,'Check partial function',TFunCheck).
859
860 create_domain_range_checks(TDomain,TRange,TA,TB,SetChecks) :-
861 create_optional_subset_check(TDomain,TA,DomCheck),
862 create_optional_subset_check(TRange,TB,RanCheck),
863 append(DomCheck,RanCheck,SetChecks).
864 create_total_domain_range_checks(TDomain,TRange,TA,TB,[TIsTotal|RanCheck]) :-
865 create_texpr(equal(TDomain,TA),pred,[description('Check domain of total function')],TIsTotal),
866 create_optional_subset_check(TRange,TB,RanCheck).
867 create_optional_subset_check(TSubset,TSuperset,Check) :-
868 ( is_just_type(TSuperset) ->
869 Check = []
870 ;
871 create_texpr(subset(TSubset,TSuperset),pred,[description('Check domain/range of function')],TPred),
872 Check = [TPred]).
873
874 create_injection_check(TM,TS,TCheckExpr) :-
875 texpr_function_type(TS,FunctionType,DomType,RanType),
876 create_texpr(identifier(d1),DomType,[],TDomId1),
877 create_texpr(identifier(d2),DomType,[],TDomId2),
878 create_texpr(identifier(r1),RanType,[],TRanId),
879 create_texpr(comprehension_set([TDomId1,TRanId],TExists),FunctionType,[bv_function_check(TM)],TCheckExpr),
880 %create_exists([TDomId2],TPred,TExists1),
881 create_texpr(couple(TDomId1,TRanId),couple(DomType,RanType),[],TCouple1),
882 create_texpr(couple(TDomId2,TRanId),couple(DomType,RanType),[],TCouple2),
883 create_texpr(not_equal(TDomId1,TDomId2),pred,[],Unequal),
884 create_texpr(member(TCouple1,TM),pred,[],TMember1),
885 create_texpr(member(TCouple2,TM),pred,[],TMember2),
886 conjunct_predicates([Unequal,TMember1,TMember2],TPred),
887 create_exists([TDomId2],TPred,TExists1), % now also computes used identifiers; cleanup no longer required ?
888 % This is necessary to register the used identifiers
889 clean_up_pred(TExists1,[],TExists2),
890 add_texpr_description(TExists2,'Find counter-example pairs for injectivity',TExists).
891
892 texpr_function_type(TFun,FunType,DomType,RanType) :-
893 get_texpr_type(TFun,set(FunType)),
894 FunType = set(couple(DomType,RanType)).
895
896
897 create_bvexpr(Expr,Type,TExpr) :-
898 create_texpr(Expr,Type,[bvisual],TExpr).
899 is_not_bvexpr(TExpr) :-
900 get_texpr_info(TExpr,Infos),nonmember(bvisual,Infos).
901 %is_bvexpr(TExpr) :- get_texpr_info(TExpr,Infos),memberchk(bvisual,Infos).
902
903 no_empty_set(E) :- \+ is_empty_set(E).
904
905 is_empty_set(b(E,_,_)) :- is_empty_set_aux(E).
906 is_empty_set_aux(empty_set).
907 is_empty_set_aux(empty_sequence).
908 is_empty_set_aux(value(X)) :- X==[].
909
910 get_subformulas(TFormula,Subs,Kind) :-
911 is_texpr(TFormula),
912 get_texpr_expr(TFormula,Formula),
913 subformula_rule(Formula,TFormula,Subs,Kind),!.
914 get_subformulas(TFormula,Subs,syntaxtraversion) :-
915 is_texpr(TFormula),!,
916 bv_syntaxtraversion(TFormula,Subs).
917 get_subformulas(named_subformula(_,TFormula,_),Subs,Kind) :- !,
918 (get_subformulas(TFormula,TSubs,Kind),TSubs \= []
919 -> Subs = [TFormula]
920 ; Kind=syntaxtraversion, Subs=[]).
921 get_subformulas(textnode(_,Subs),Subs,textnode) :- !.
922 get_subformulas(textnode3(_,_,Subs),Subs,textnode) :- !.
923 get_subformulas(included_machine(_,_,Subs),Subs,textnode) :- !.
924 get_subformulas(guard(_Name,Parameters,Guard),Subs,guard) :- !,
925 conjunction_to_list(Guard,GuardSubs),
926 append(Parameters,GuardSubs,Subs).
927 get_subformulas(variant(_Name,_ConvOrAnt,Variant),Subs,variant) :- !,
928 Subs = [Variant].
929 get_subformulas(ltl_named_formula(_,String),Subs,ltl_formula) :- !,
930 temporal_parser(String,ltl,Formula),
931 Subs = [ltl_formula(String,Formula)].
932 get_subformulas(ltl_formula(_,Formula),Subs,ltl_formula) :- !,
933 get_bv_ltl_sub_formulas(Formula,Subs).
934 get_subformulas(guard_theorems(_Name,[],_Guard,Theorems),Subs,guard_theorems) :- !,
935 conjunction_to_list(Theorems,Subs).
936 get_subformulas(guard_theorems(_Name,Parameters,Guard,Theorems),Subs,guard_theorems) :- !,
937 append(Parameters,[b(implication(Guard,Theorems),pred,[])],Subs).
938 %get_subformulas(cbc_path(_,Path,_),Subs) :- !,
939 % append(Path,[X],XPath),
940 % findall(cbc_path(LastStateID,XPath,X), sap:cb_path(LastStateID,XPath,_), Subs).
941 get_subformulas(bind(_,_),S,K) :- !,S=[], K=none.
942 get_subformulas(Node,[],none) :- format('No subformulas for ~w~n',[Node]).
943
944 bv_syntaxtraversion(b(rec(Fields),_,_),Subs) :-
945 maplist(transform_field,Fields,Subs1),!,
946 Subs = Subs1. % Note: there are no new quantified names/identifiers; just field names
947 bv_syntaxtraversion(TFormula,Subs) :-
948 syntaxtraversion(TFormula,_,_,_,Subs1,Names),
949 (Names = [] -> filter_trivial_expressions(Subs1,Subs) ; Subs = []).
950
951 transform_field(field(Name,TVal),named_subformula(Name,TVal,'$no_error')).
952
953 :- use_module(probsrc(preferences),[get_prob_application_type/1]).
954 filter_trivial_expressions([],[]).
955 filter_trivial_expressions([TI|Irest],Out) :-
956 get_texpr_expr(TI,I),get_texpr_info(TI,Info),
957 ( get_preference(show_bvisual_formula_functor_from,Lim), Lim>=0 -> Out = [TI|Orest]
958 % only functor shown when expanding; we should show all sub formulas; otherwise it is confusing to the user
959 ; get_prob_application_type(Type), Type \= tcltk -> Out = [TI|Orest] % in ProB2 we always just show the functor now
960 ; is_trivial(I,Info) -> Out = Orest
961 ; Out = [TI|Orest]),
962 filter_trivial_expressions(Irest,Orest).
963
964 % is_trivial/2 is true for expressions that should not appear as single nodes
965 % in the evaluation tree, because they are too simple.
966 % The first argument is the expression, the second its information list
967 is_trivial(integer(_),_).
968 is_trivial(integer_set(_),_).
969 is_trivial(empty_set,_).
970 is_trivial(boolean_true,_).
971 is_trivial(boolean_false,_).
972 is_trivial(bool_set,_).
973 is_trivial(truth,_).
974 is_trivial(falsity,_).
975 is_trivial(identifier(_),Info) :-
976 memberchk(given_set,Info).
977 is_trivial(identifier(_),Info) :-
978 memberchk(enumerated_set_element,Info).
979 % TO DO: treat value(_) ?
980
981 :- use_module(probsrc(tools_strings),[ajoin/2]).
982 :- use_module(probsrc(specfile),[animation_minor_mode/1, csp_mode/0, b_or_z_mode/0,
983 get_specification_description/2]).
984 get_node_label(inv,invariants) :- animation_minor_mode(eventb),!.
985 get_node_label(inv,I):- !, get_specification_description(invariant,I).
986 get_node_label(invariants,invariants) :- animation_minor_mode(eventb),!.
987 get_node_label(invariants,I):- !, get_specification_description(invariants,I).
988 get_node_label(axioms,axioms) :- animation_minor_mode(eventb),!.
989 get_node_label(axioms,S):- !, get_specification_description(properties,S).
990 get_node_label(variants,variants) :- animation_minor_mode(eventb),!.
991 get_node_label(variants,'VARIANT') :- !. % not used there
992 get_node_label(theoremsc,'theorems (on constants)') :- animation_minor_mode(eventb),!.
993 get_node_label(theoremsc,'ASSUME (on constants)') :- animation_minor_mode(tla),!.
994 get_node_label(theoremsc,'ALL ASSERTIONS (on CONSTANTS)') :- b_get_assertion_count(static,AllNr,MainNr), MainNr < AllNr, !.
995 get_node_label(theoremsc,'ASSERTIONS (on CONSTANTS)') :- !.
996 get_node_label(theoremscm,'MAIN ASSERTIONS (on CONSTANTS)') :- !. % should only trigger in B mode at the moment
997 get_node_label(theoremsv,'theorems (on variables)') :- animation_minor_mode(eventb),!.
998 get_node_label(theoremsv,'ALL ASSERTIONS (on VARIABLES)') :- b_get_assertion_count(dynamic,AllNr,MainNr), MainNr < AllNr, !.
999 get_node_label(theoremsv,'ASSERTIONS (on VARIABLES)') :- !.
1000 get_node_label(theoremsvm,'MAIN ASSERTIONS (on VARIABLES)') :- !. % should only trigger in B mode at the moment
1001 get_node_label(sets,'sets') :- animation_minor_mode(eventb),!.
1002 get_node_label(sets,'SETS') :- !.
1003 get_node_label(freetypes,'inductive datatypes') :- animation_minor_mode(eventb),!.
1004 get_node_label(freetypes,'FREETYPES') :- !.
1005 get_node_label(goal,'GOAL') :- !.
1006 get_node_label(variables,'variables') :- animation_minor_mode(eventb),!.
1007 get_node_label(variables,'VARIABLES') :- !.
1008 get_node_label(constants,'constants') :- animation_minor_mode(eventb),!.
1009 get_node_label(constants,'CONSTANTS') :- !.
1010 get_node_label(guards_top_level,'event guards') :- animation_minor_mode(eventb),!.
1011 get_node_label(guards_top_level,'ACTIONS (guards)') :- animation_minor_mode(tla),!.
1012 get_node_label(guards_top_level,'OPERATIONS (guards/preconditions)') :- !. % GUARDS/PRE
1013 get_node_label(guards_subsidiary,'subsidiary event guards') :- animation_minor_mode(eventb),!.
1014 get_node_label(guards_subsidiary,'SUBSIDIARY ACTIONS (guards)') :- animation_minor_mode(tla),!.
1015 get_node_label(guards_subsidiary,'SUBSIDIARY OPERATIONS (guards/preconditions)') :- !. % GUARDS/PRE
1016 get_node_label(guard_theorems,'theorems (in guards)') :- !.
1017 get_node_label(user_formulas,'USER FORMULAS') :- !.
1018 get_node_label(definitions,'theory operators') :- animation_minor_mode(eventb),!.
1019 get_node_label(definitions,'DEFINITIONS') :- !.
1020 get_node_label(included_machines,'INCLUDED MACHINES') :- !.
1021 get_node_label(ltl_formulas,'LTL Formulas') :- !.
1022 %get_node_label(cbc_tests,'CBC_TESTS') :- !.
1023 get_node_label(channels,'channel') :- !.
1024 get_node_label(datatypes,'datatype') :- !.
1025 get_node_label(subtypes,'subtype') :- !.
1026 get_node_label(raw_state,'Raw State') :- !.
1027 get_node_label(Atom,Label) :- is_property_atom(Atom,Nr),!,
1028 (xtl_mode -> ajoin(['XTL Property ',Nr],Label) ; Label='property').
1029 get_node_label(Id,Label) :-
1030 stored_formula(Id,Formula),!,
1031 get_node_label2(Formula,Label).
1032 get_node_label(Id,'??') :- format('Unknown node: ~w~n',[Id]).
1033
1034 get_node_label2(textnode(Text,_),Text) :- !.
1035 get_node_label2(textnode3(Text,_,_),Text) :- !.
1036 get_node_label2(included_machine(Text,_,_),Text) :- !.
1037 get_node_label2(bind(TID,_),ID) :- !,
1038 (is_texpr(TID),get_texpr_id(TID,R) -> ID = R ; ID = TID).
1039 get_node_label2(named_subformula(Name,_,_),Res) :- !, Res=Name.
1040 get_node_label2(guard(Name,_Parameters,_Guard),Name) :- !.
1041 get_node_label2(guard_theorems(Name,_Parameters,_Guard,_Theorems),Name) :- !.
1042 get_node_label2(variant(OpName,ConvOrAnt,_Variant),Name) :- !,
1043 ajoin([OpName,' |-> ',ConvOrAnt],Name).
1044 get_node_label2(ltl_named_formula(Name,_),Res) :- !, Res=Name.
1045 get_node_label2(ltl_formula(String,_Tree),Name) :- !, strip_newlines(String,Name).
1046 get_node_label2(TExpr,Label) :-
1047 is_texpr(TExpr),!,
1048 % Mode = unicode, latex or ascii; but unicode_mode makes Viewer slower
1049 with_bvisual2_translation_mode(get_node_label3(TExpr,Label)).
1050 get_node_label2(Term,Label) :-
1051 functor(Term,F,Arity),
1052 ajoin(['unknown node type: ',F,'/',Arity],Label).
1053
1054 get_node_label3(TExpr,Label) :-
1055 with_bvisual2_translation_mode(translate_subst_or_bexpr_with_limit(TExpr,500,TS)),
1056 ( get_preference(show_bvisual_formula_functor_from,Lim), Lim>=0,
1057 get_texpr_top_level_symbol(TExpr,Functor,_,_OpType),
1058 %(OpType=infix ; OpType=postfix),
1059 (Lim=0 -> true ; atom_length(TS,Len), Len>=Lim)
1060 -> %ajoin([Functor,' \x25AB\ ',TS],Label) % unicode small block / box
1061 ajoin(['[',Functor, '] ',TS],Label0)
1062 ; Label0=TS
1063 ),
1064 (animation_minor_mode(eventb), %maybe in future we also have proof info for Classical B
1065 get_preference(show_bvisual_proof_info_icons,true),
1066 get_discharged_info(TExpr,Proven,_)
1067 -> (Proven=proven, _SHOW=full
1068 -> ajoin([' \x2713\ ',Label0],Label) % alternatives \x2705\ \x2713\ (10003) \x2714\ \x22A2\ (turnstile)
1069 % \x2705\ (crashes sometimes on macOS Tk)
1070 ; ajoin([' \x25EF\ ',Label0],Label) % x2B55 \x22AC\ (turnstile with bar)
1071 )
1072 ; Label=Label0).
1073
1074
1075 :- dynamic bvisual2_translation_mode/1.
1076 bvisual2_translation_mode(unicode). % any mode accepted by translate:set_translation_mode
1077 set_bvisual2_translation_mode(X) :- retractall(bvisual2_translation_mode(_)),
1078 assertz(bvisual2_translation_mode(X)).
1079
1080 :- use_module(probsrc(translate), [with_translation_mode/2]).
1081 :- use_module(probsrc(preferences), [temporary_set_preference/3,reset_temporary_preference/2]).
1082 with_bvisual2_translation_mode(Call) :-
1083 bvisual2_translation_mode(Mode),
1084 temporary_set_preference(expand_avl_upto,5000,Chng),
1085 call_cleanup(with_translation_mode(Mode,Call),
1086 reset_temporary_preference(expand_avl_upto,Chng)).
1087
1088
1089 register_top_level :- register_top_level(_).
1090 register_top_level(Id) :-
1091 ? top_level2(Id,Formula,Subs), % print(register(Id,Subs)),nl,
1092 register_top_level_formula(Id,Formula,Subs),
1093 fail.
1094 register_top_level(_Id).
1095
1096 % useful to re-register e.g. definitions after b_load_additional_definitions_file/1 or similar is called
1097 % Note: it should not be done in Tk while the State View dialog is open
1098
1099 bv_re_register_definitions_if_necessary :-
1100 additional_defs_loaded,!, re_register_top_level(definitions).
1101 bv_re_register_definitions_if_necessary.
1102
1103 re_register_top_level(Id) :-
1104 delete_top_level_formula(Id),
1105 register_top_level(Id).
1106
1107
1108 :- use_module(probsrc(specfile),[b_or_z_mode/0, classical_b_mode/0,spec_file_has_been_successfully_loaded/0]).
1109 :- use_module(probsrc(b_global_sets),[all_elements_of_type/2, b_get_fd_type_bounds/3]).
1110 :- use_module(probsrc(bmachine),[b_get_machine_goal/1]).
1111 b_or_z_successsful_mode :- b_or_z_mode,
1112 true. %spec_file_has_been_successfully_loaded. % make unit test fail
1113
1114 % top_level2(SectionID, ValueNode, ListOfSubs)
1115 top_level2(variables,textnode('',Variables),Variables) :- b_or_z_successsful_mode,
1116 variables_should_be_included,
1117 b_get_machine_variables_in_original_order(Variables).
1118 top_level2(constants,textnode('',Constants),Constants) :- b_or_z_successsful_mode,
1119 variables_should_be_included,
1120 b_get_machine_constants(Constants).
1121 top_level2(sets,textnode('',Sets),Sets) :- b_or_z_successsful_mode,
1122 findall(bind(S,All),
1123 bv_top_level_set(S,All),Sets), Sets \= [].
1124 top_level2(freetypes,textnode('',Sets),Sets) :- b_or_z_successsful_mode,
1125 findall(All, top_level_freetype(_Set,All),Sets), Sets \= [].
1126 top_level2(goal,Goal,Subs) :- b_or_z_successsful_mode,
1127 b_get_machine_goal(Goal),
1128 conjunction_to_list(Goal,Subs).
1129 top_level2(inv,Invariant,Subs) :- b_or_z_successsful_mode,
1130 get_invariant_list_with_proof_info(Subs),
1131 conjunct_predicates(Subs,Invariant).
1132 top_level2(variants,textnode('',Variants),Variants) :- animation_minor_mode(eventb),
1133 spec_file_has_been_successfully_loaded,
1134 findall(variant(Name,ConvOrAnt,Variant),
1135 b_get_operation_variant(Name,ConvOrAnt,Variant),
1136 Variants),
1137 Variants = [_|_].
1138 top_level2(axioms,Props,AllSubs) :- b_or_z_successsful_mode,
1139 b_machine_has_constants_or_properties,
1140 b_get_properties_from_machine(Props),
1141 conjunction_to_list(Props,Subs),
1142 findall(AddPred,b_machine_additional_property(AddPred),VSubs), % could be VALUES clause
1143 (VSubs=[] -> AllSubs = Subs ; append(Subs,VSubs,AllSubs)).
1144 top_level2(theoremsc,Pred,Assertions) :- b_or_z_successsful_mode,
1145 b_get_static_assertions_from_machine(Assertions),Assertions\=[],
1146 conjunct_predicates(Assertions,Pred).
1147 top_level2(theoremscm,Pred,Assertions) :- b_or_z_successsful_mode,
1148 b_get_assertions_from_main_machine(static,Assertions),
1149 %Assertions\=[], % in case we have less: show that there are none
1150 ? b_get_assertion_count(static,AllNr,MainNr), MainNr < AllNr,
1151 conjunct_predicates(Assertions,Pred).
1152 top_level2(theoremsv,Pred,Assertions) :- b_or_z_successsful_mode,
1153 b_get_dynamic_assertions_from_machine(Assertions),Assertions\=[],
1154 conjunct_predicates(Assertions,Pred).
1155 top_level2(theoremsvm,Pred,Assertions) :- b_or_z_successsful_mode,
1156 b_get_assertions_from_main_machine(dynamic,Assertions),
1157 %Assertions\=[], % in case we have less: show that there are none
1158 b_get_assertion_count(dynamic,AllNr,MainNr), MainNr < AllNr,
1159 conjunct_predicates(Assertions,Pred).
1160 top_level2(guards_top_level,textnode('',EventGuards),EventGuards) :- b_or_z_successsful_mode,
1161 findall(guard(Name,Params,Guard),
1162 get_top_level_guard(Name,Params,Guard),EventGuards),
1163 EventGuards = [_|_].
1164 top_level2(guards_subsidiary,textnode('',EventGuards),EventGuards) :- b_or_z_successsful_mode,
1165 findall(guard(Name,Params,Guard),
1166 get_subsidiary_guard(Name,Params,Guard),EventGuards),
1167 EventGuards = [_|_].
1168 top_level2(guard_theorems,textnode('',EventGuards),EventGuards) :- b_or_z_successsful_mode,
1169 findall(guard_theorems(Name,Params,Guard,GuardTheorems),
1170 ( b_get_machine_operation(Name,_,_,TBody),
1171 get_texpr_expr(TBody,Body),
1172 Body = rlevent(_Name,_Section,_Status,Params,Guard,Theorems,
1173 _Actions,_VWitnesses,_PWitnesses,_Unmod,_AbstractEvents),
1174 Theorems \= [],
1175 conjunct_predicates(Theorems,GuardTheorems)
1176 ),
1177 EventGuards),
1178 EventGuards = [_|_].
1179 top_level2(user_formulas,textnode('',UserPredicates),UserPredicates) :- b_or_z_successsful_mode,
1180 findall(UP,subnode(user_formulas,UP),UserPredicates), UserPredicates\=[].
1181 top_level2(definitions,textnode('',Defs),Defs) :- b_or_z_successsful_mode,
1182 pre_expand_typing_scope([variables_and_additional_defs],ExpandedScope),
1183 get_definitions_section(DefSection), % get it only once
1184 full_b_machine(Machine),
1185 findall(Node,get_definition(Node,pre_ctxt(ExpandedScope,DefSection,Machine)),Defs),
1186 Defs\=[].
1187 top_level2(included_machines,textnode('',Subs),Subs) :- b_or_z_successsful_mode,
1188 main_machine_name(Main),
1189 get_machine_inclusions(Main,Subs).
1190 top_level2(ltl_formulas,textnode('',Defs),Defs) :- b_or_z_successsful_mode,
1191 findall(Node,get_ltl_named_formula(Node),Defs), Defs\=[].
1192 top_level2(channels,textnode('',Channels),Channels) :- csp_mode, spec_file_has_been_successfully_loaded,
1193 findall(Node,get_csp_channel(Node),Cs), Cs\=[], sort(Cs,Channels).
1194 top_level2(datatypes,textnode('',DT),DT) :- csp_mode, spec_file_has_been_successfully_loaded,
1195 findall(Node,get_csp_datatype(Node),Cs), Cs\=[], sort(Cs,DT).
1196 top_level2(subtypes,textnode('',DT),DT) :- csp_mode, spec_file_has_been_successfully_loaded,
1197 findall(Node,get_csp_subtype(Node),Cs), Cs\=[], sort(Cs,DT).
1198 top_level2(raw_state,raw_state_pp,[]) :- \+ b_or_z_mode,
1199 spec_file_has_been_successfully_loaded.
1200 top_level2(Atom,property_pp(Nr),[]) :- xtl_mode, % probably also useful for CSP mode
1201 spec_file_has_been_successfully_loaded,
1202 is_property_atom(Atom,Nr).
1203 % we are now using a custom tree_inspector for the CBC Tests
1204 /* top_level2(cbc_tests,textnode('',Paths),Paths) :- b_or_z_mode,
1205 get_preference(user_is_an_expert_with_accessto_source_distribution,true),
1206 %sap:cb_path(_,[_],_), % we have generated at least one test-case
1207 % TO DO: we need a way to refresh this information after test-cases have been generated
1208 % currently the only way seems to close the evaluation view, reload, generate the tests and then open the view
1209 Paths = [cbc_path(root,[],'INITIALISATION')].
1210 */
1211
1212 :- use_module(library(between),[between/3]).
1213 :- use_module(probsrc(xtl_interface),[xtl_nr_state_properties/1]).
1214 % Tcl/Tk requires atoms as top_level properties
1215 is_property_atom(Name,Nr) :-
1216 (xtl_mode, xtl_nr_state_properties(MaxNr) % use value provided in XTL file
1217 -> true
1218 ; MaxNr = 8), % default
1219 ? between(1,MaxNr,Nr),
1220 ajoin([property,Nr],Name).
1221
1222 :- use_module(probcspsrc(haskell_csp),[channel_type_list/2, dataTypeDef/2, subTypeDef/2]).
1223 :- use_module(probsrc(translate),[translate_cspm_expression/2]).
1224 get_csp_channel(bind(Channel,string(TypeString))) :-
1225 channel_type_list(Channel,TypeList), % something like [dataType('SubSubMsg'),intType,boolType]
1226 translate_cspm_expression(dotTuple(TypeList),TypeString).
1227 get_csp_datatype(bind(DT,string(TypeString))) :-
1228 dataTypeDef(DT,TypeList), % something like [dataType('SubSubMsg'),intType,boolType]
1229 translate_cspm_expression(dataTypeDef(TypeList),TypeString).
1230 get_csp_subtype(bind(DT,string(TypeString))) :-
1231 subTypeDef(DT,TypeList), % something like [dataType('SubSubMsg'),intType,boolType]
1232 translate_cspm_expression(dataTypeDef(TypeList),TypeString).
1233
1234 :- use_module(probsrc(b_machine_hierarchy),[main_machine_name/1, machine_references/2, get_machine_short_desc/2]).
1235 get_machine_inclusions(M,Subs) :-
1236 findall(included_machine(SubMachine,Text,Subs),
1237 (machine_references(M,Refs),
1238 member(ref(Kind,SubMachine,Prefix),Refs),
1239 (Prefix='' -> Text=Kind ; ajoin([Kind,' with prefix ',Prefix],Text)),
1240 get_machine_inclusions(SubMachine,Subs)),
1241 Subs).
1242
1243 :- use_module(probsrc(bmachine)).
1244 :- use_module(probsrc(pref_definitions),[b_get_set_pref_definition/3]).
1245 get_definition(textnode('GOAL',Subs),_) :- b_get_machine_goal(Goal), conjunction_to_list(Goal,Subs).
1246 get_definition(bind('SCOPE',string(S)),_) :- b_get_machine_searchscope(S).
1247 get_definition(textnode('HEURISTIC_FUNCTION',[F]),_) :- b_get_machine_heuristic_function(F).
1248 get_definition(textnode(STR,[F]),_) :- b_get_machine_animation_expression(STR,F).
1249 get_definition(textnode(Name,[F]),_) :- b_get_machine_animation_function(F,Nr),
1250 number_codes(Nr,NC), append("ANIMATION_FUNCTION",NC,C), atom_codes(Name,C).
1251 ?get_definition(textnode(Name,[Term]),_) :- b_get_machine_setscope(SetName,Term),
1252 atom_codes(SetName,NC), append("scope_",NC,C), atom_codes(Name,C).
1253 get_definition(textnode(Name,[b(integer(Nr),integer,[])]),_) :- b_get_machine_operation_max(OpName,Nr),
1254 atom_codes(OpName,NC), append("MAX_OPERATIONS_",NC,C), atom_codes(Name,C).
1255 get_definition(textnode(PrefixCategory,Subs),PreExpandContext) :-
1256 ? special_def_category(PrefixCategory), % group special prefixes separately
1257 findall(named_subformula(DefName,Body,Error),
1258 (special_def_prefix(Prefix,Match,PrefixCategory),
1259 (Match=perfect -> DefName=Prefix ; true),
1260 get_useful_definition_for_bvisual2(expression,Prefix,DefName,PreExpandContext,_Pos,Body,Error)),
1261 Subs), Subs \= [].
1262 get_definition(named_subformula(DefName,Body,Error),PreExpandContext) :-
1263 get_useful_definition_for_bvisual2(_,'',DefName,PreExpandContext,_Pos,Body,Error).
1264 get_definition(textnode3(Header,DefBodyStr,
1265 [textnode3('Theory',FullThName,[]),
1266 textnode3('Body',DefBodyStr,[]) | Tail
1267 ]),_PreExpandContext) :-
1268 stored_operator_direct_definition(Name,ProjName,TheoryName,Parameters,RawDefBody,RawWD,ParaTypes,_Kind),
1269 ajoin([ProjName,'.',TheoryName],FullThName),
1270 translate_eventb_direct_definition_header(Name,Parameters,Header),
1271 with_bvisual2_translation_mode(translate_eventb_direct_definition_body(RawDefBody,DefBodyStr)),
1272 (ParaTypes = [] -> Tail = Tail2
1273 ; debug_mode(off), \+ ground(ParaTypes) -> Tail=Tail2 % non-ground types are for destructors, ... are not useful for average user
1274 ; Tail = [textnode3('Type Parameters',TypeParaStr,[])|Tail2],
1275 write_term_to_codes(ParaTypes,PC,[]),
1276 atom_codes(TypeParaStr,PC)
1277 ),
1278 (RawWD=truth(_) -> Tail2=[]
1279 ; Tail2 = [textnode3('WD Condition',WDStr,[])],
1280 with_bvisual2_translation_mode(translate_eventb_direct_definition_body(RawWD,WDStr))
1281 ).
1282 % TODO: show mapped Event-B and recursive operators; also: above still shown as string and type paras not shown
1283 % TO DO: we could try and get more complex definitions if get_preference(type_check_definitions,true), see type_check_definitions; or we could just pretty print the RAW ast using transform_raw;
1284 % NOTE: special definitions are also declared in
1285 % - procDoSyntaxColouring in main_prob_tcltk_gui.tcl
1286 % - isProBSpecialDefinitionName in Utils.java in de.be4.classicalb.core.parser.util
1287
1288 special_def_category('CUSTOM_GRAPH').
1289 special_def_category('VISB').
1290 special_def_category('PREFERENCES').
1291 special_def_category('UML_SEQUENCE').
1292 special_def_category('SIMB').
1293 % TODO: add ANIMATION function, ASSERT_CTL, ASSERT_LTL, GAME_MCTS, ...
1294 special_def_prefix('CUSTOM_GRAPH_EDGES',prefix,'CUSTOM_GRAPH').
1295 special_def_prefix('CUSTOM_GRAPH_NODES',prefix,'CUSTOM_GRAPH').
1296 special_def_prefix('CUSTOM_GRAPH',prefix,'CUSTOM_GRAPH'). % perfect match required
1297 special_def_prefix('VISB_DEFINITIONS_FILE',perfect,'VISB'). % perfect match required
1298 special_def_prefix('VISB_JSON_FILE',perfect,'VISB'). % perfect match required
1299 special_def_prefix('VISB_SVG_BOX',perfect,'VISB'). % perfect match required
1300 special_def_prefix('VISB_SVG_CONTENTS',prefix,'VISB').
1301 special_def_prefix('VISB_SVG_OBJECTS',prefix,'VISB').
1302 special_def_prefix('VISB_SVG_UPDATES',prefix,'VISB').
1303 special_def_prefix('VISB_SVG_EVENTS',prefix,'VISB').
1304 special_def_prefix('VISB_SVG_HOVERS',prefix,'VISB').
1305 special_def_prefix('SET_PREF_',prefix,'PREFERENCES').
1306 special_def_prefix('scope_',prefix,'PREFERENCES').
1307 special_def_prefix('MAX_OPERATIONS_',prefix,'PREFERENCES').
1308 special_def_prefix('OPERATION_REUSE_OFF_',prefix,'PREFERENCES').
1309 special_def_prefix('SEQUENCE_CHART_',prefix,'UML_SEQUENCE').
1310 % these definitions are already displayed by bvisual2 separately (via b_get_machine_animation_expression, ...)
1311 special_def_prefix('FORCE_SYMMETRY_',prefix,special).
1312 special_def_prefix('GOAL',perfect,special).
1313 special_def_prefix('SCOPE',perfect,special).
1314 special_def_prefix('HEURISTIC_FUNCTION',perfect,special).
1315 special_def_prefix('ANIMATION_FUNCTION',prefix,special).
1316 special_def_prefix('ANIMATION_EXPRESSION',prefix,special).
1317 special_def_prefix('DESCRIPTION_FOR_',prefix,special).
1318 special_def_prefix('GAME_PLAYER',perfect,special).
1319 special_def_prefix('GAME_OVER',perfect,special).
1320 special_def_prefix('GAME_VALUE',perfect,special).
1321 special_def_prefix('GAME_MCTS_RUNS',perfect,special).
1322 special_def_prefix('GAME_MCTS_TIMEOUT',perfect,special).
1323 special_def_prefix('GAME_MCTS_CACHE_LAST_TREE',perfect,special).
1324 special_def_prefix('SIMB_JSON_FILE',perfect,'SIMB').
1325
1326 ?is_special_def_name(DefName) :- special_def_prefix(SPrefix,Match,_),
1327 (Match=prefect -> DefName=SPrefix ; atom_prefix(SPrefix,DefName)).
1328
1329 % only get definitions matching prefix; or if Prefix='' only show those not matching any special prefix
1330 get_useful_definition_for_bvisual2(Type,Prefix,DefName,pre_ctxt(ExpandedScope,DefSection,Machine), DefPos,Body,Error) :-
1331 ? b_sorted_b_definition_prefixed(Type,Prefix,DefName,DefPos),
1332 ? (Prefix='' -> \+ is_special_def_name(DefName) ; true),
1333 ? (b_get_typed_definition_with_error_list(Machine,DefSection,DefName,ExpandedScope,Body,
1334 ErrorsAndWarnings,Success)
1335 -> \+ dangerous_body(Body),
1336 (Success=true -> Error='$no_error'
1337 ; member(Err1,ErrorsAndWarnings),
1338 get_perror(Err1,_Kind,Msg,_Pos) -> Error=Msg
1339 )
1340 ; % the definition has parameters, TODO: pretty print a bit like theory operators
1341 fail, Error=failure,
1342 b_get_definition(DefName,_DefType,_Paras,RawDefBody,_Deps),
1343 with_bvisual2_translation_mode(translate_eventb_direct_definition_body(RawDefBody,DefBodyStr)),
1344 Body = b(string(DefBodyStr),string,[])
1345 ).
1346
1347 % we do not want to perform IO while expanding the state view
1348 % e.g., if you include LibraryIO.def you would have GET_CODE_STDIN whose evaluation waits for user input
1349 % TODO: we should probably also check during evaluation that we do not perform io or that IO is possible?
1350 :- use_module(probsrc(external_functions), [ performs_io/1]).
1351 dangerous_body(b(external_function_call(FUNCTION,_),_,_)) :- performs_io(FUNCTION).
1352
1353 % ASSERT_LTL
1354 % at the top-level we generate named formulas; we only parse when expanding later
1355 % solves issues e.g., in CSP-B mode where the formulas use CSP constructs but the csp-guide has not been added yet
1356 % see tests 1257, 1259, 1644, 1647
1357 get_ltl_named_formula(ltl_named_formula(FullName,FormulaAsString)) :-
1358 get_ltl_formula_strings(Names,Strings),
1359 nth1(Nr,Names,Name),
1360 ajoin(['ASSERT_LTL',Name],FullName), % in CSP mode this would actually be assert Main |= LTL "formula"
1361 (nth1(Nr,Strings,FormulaAsString) -> true).
1362
1363 gen_ltl_bv_sub(Tree,ltl_formula(String,Tree)) :-
1364 pp_ltl_formula(Tree,String).
1365
1366 get_bv_ltl_sub_formulas(ap(bpred(Pred)),Subs) :- !, Subs = [Pred].
1367 get_bv_ltl_sub_formulas(LTLTee,BV_Subs) :-
1368 get_ltl_sub_formulas(LTLTee,_,LTLSubs),!,
1369 maplist(gen_ltl_bv_sub,LTLSubs,BV_Subs).
1370 get_bv_ltl_sub_formulas(_,[]).
1371
1372
1373 :- use_module(probsrc(b_operation_guards),[get_unsimplified_operation_enabling_condition/5]).
1374 :- use_module(probsrc(bmachine),[b_top_level_operation/1]).
1375 get_top_level_guard(OpName,Params,Guard) :-
1376 ? b_top_level_operation(OpName),
1377 \+ is_initialisation_op(OpName),
1378 get_unsimplified_operation_enabling_condition(OpName,Params,Guard,_BecomesSuchVars,_Precise).
1379 get_subsidiary_guard(OpName,Params,Guard) :- % get guards for non-top-level operations
1380 ? get_unsimplified_operation_enabling_condition(OpName,Params,Guard,_BecomesSuchVars,_Precise),
1381 \+ b_top_level_operation(OpName),
1382 \+ is_initialisation_op(OpName).
1383
1384 is_initialisation_op('$setup_constants').
1385 is_initialisation_op('$initialise_machine').
1386
1387 :- use_module(probsrc(custom_explicit_sets),[construct_interval_closure/3]).
1388 :- use_module(probsrc(kernel_freetypes),[registered_freetype/2]).
1389 :- use_module(probsrc(bmachine),[b_get_machine_set/2]).
1390 :- use_module(probsrc(b_global_sets),[b_replaced_global_set/2]).
1391 bv_top_level_set(TSet,AllEls) :- b_or_z_mode,
1392 ? b_get_machine_set(Set,TSet),
1393 (b_get_fd_type_bounds(Set,_Low,inf)
1394 -> %L1 is Low+1, L2 is Low+2,
1395 %AllEls= [fd(Low,Set),fd(L1,Set),fd(L2,Set),string('...')] % TODO: avoid type error
1396 AllEls = string('infinite deferred set')
1397 ; all_elements_of_type(Set,AllEls)).
1398 bv_top_level_set(b(identifier(ID),any,[]),string(TS)) :- % replaced by record construction /detection
1399 b_replaced_global_set(ID,NewTypeExpr),
1400 with_bvisual2_translation_mode(translate_subst_or_bexpr_with_limit(NewTypeExpr,500,TS)).
1401 bv_top_level_set(b(identifier('INT'),set(integer),[]),INTVAL) :- % we can return both typed ids and atomic ids
1402 ? classical_b_mode, % INT only exists in pure B mode
1403 get_preference(maxint,MAXINT), get_preference(minint,MININT),
1404 construct_interval_closure(MININT,MAXINT,INTVAL).
1405
1406 top_level_freetype(Set,textnode3(TopSet,'',AllCases)) :-
1407 animation_minor_mode(eventb), % here types contain Prolog variables (polymorphism) + we have the constant(_) type
1408 registered_freetype(SetP,Cases),
1409 (SetP =.. [Set|TypeParas] -> true ; add_error(bvisual2,'Cannot instantiate type paras:',SetP)),
1410 get_freetype_params(Set,TypeParaNames,TypeParas),
1411 (TypeParaNames = [] -> TopSet = Set ; ajoin_with_sep(TypeParaNames,',',Ps),ajoin([Set,'(',Ps,')'],TopSet)),
1412 findall(textnode3(Case,CaseString,Subs),
1413 (member(case(CaseP,T),Cases),functor(CaseP,Case,_),
1414 bv_pretty_type(T,TS), ajoin([Case,':',TS],CaseString),
1415 findall(textnode3(Destructor,TD,[]),get_destructor_for_freetype(SetP,Case,Destructor,TD),Subs)
1416 ),AllCases).
1417 top_level_freetype(Set,AllCases) :- b_or_z_mode, \+ animation_minor_mode(eventb),
1418 registered_freetype(Set,Cases),
1419 % TO DO: improve presentation, maybe use translate:type_set(_Type,TDom)
1420 findall(field(Case,Value),
1421 (member(case(Case,T),Cases),gen_case_value(T,Value)),
1422 Fields),
1423 AllCases = bind(Set,struct(Fields)).
1424
1425 get_freetype_params(Set,TypeParaNames,VirtualParaTypes) :-
1426 (stored_operator_direct_definition(Set,_Proj,_Theory,TypeParaNames,_Def,_WD,_TypeParas, datatype_definition)
1427 -> maplist(convert_name_to_type,TypeParaNames,VirtualParaTypes)
1428 ; add_error(bvisual2,'Unknown freetype:',Set),TypeParaNames=[]).
1429 convert_name_to_type(ParaName,VirtualType) :-
1430 (VirtualType=global(ParaName) -> true ; VirtualType=any),
1431 atom(ParaName),!. % so that pretty_type works
1432 convert_name_to_type(Type,_) :- add_error(bvisual,'Cannot convert parameter type:',Type).
1433
1434 get_destructor_for_freetype(Set,Case,Name,TypeAsString) :-
1435 stored_operator_direct_definition(Name,_Proj,_Theory,[argument(Case,_CType)],_Def,_WD,_TypeParameters,
1436 destructor(Set,ReturnType)),
1437 bv_pretty_type(ReturnType,TypeAsString).
1438
1439 :- use_module(probsrc(b_global_sets),[b_type2_set/2]).
1440 gen_case_value(constant(List),Res) :- List=[_], !, Res=[]. % the constant has no arguments, provide empty set
1441 gen_case_value(Type,Value) :- b_type2_set(Type,Value).
1442
1443 bv_pretty_type(Type,Res) :- with_bvisual2_translation_mode(pretty_type(Type,TS)),!,Res=TS.
1444 bv_pretty_type(Type,Res) :- add_error(bvisual2,'Cannot convert type:',Type), Res='?'.
1445
1446 register_top_level_formula(Id,Formula,Subs) :-
1447 assertz( stored_formula(Id,Formula) ),
1448 register_formulas(Subs,Id,_),
1449 assertz( top_level_node(Id) ),
1450 assertz( expanded(Id) ).
1451
1452 delete_top_level_formula(Id) :-
1453 retract( top_level_node(Id) ),
1454 delete_formula(Id),
1455 fail.
1456 delete_top_level_formula(_).
1457
1458 delete_formula(Id) :-
1459 retractall( expanded(Id) ),
1460 retractall( stored_formula(Id,_Formula) ),
1461 retract( subnode(Id,ChildId)),
1462 retractall( supernode(ChildId,Id)),
1463 delete_formula(ChildId),
1464 fail.
1465 delete_formula(_).
1466
1467
1468 register_formulas([],_Parent,[]).
1469 register_formulas([Formula|Frest],Parent,[SubId|Srest]) :-
1470 register_formula(Formula,Parent,SubId),
1471 register_formulas(Frest,Parent,Srest).
1472 register_formula(Formula,Parent,Id) :-
1473 get_new_id(Id),
1474 assertz( stored_formula(Id,Formula) ),
1475 assertz( subnode(Parent,Id) ),
1476 assertz( supernode(Id,Parent) ).
1477
1478 get_new_id(Id) :-
1479 (retract(id_counter(Old)) -> true ; Old = 0),
1480 Id is Old+1,
1481 assertz( id_counter(Id) ).
1482
1483
1484 disjunction_to_list(A,B,Out) :-
1485 disjunction_to_list2(A,[B],Out,[]).
1486 disjunction_to_list2(Expr,Rest) -->
1487 {get_texpr_expr(Expr,disjunct(A,B)),!},
1488 disjunction_to_list2(A,[B|Rest]).
1489 disjunction_to_list2(Expr,Rest) -->
1490 [Expr],disjunction_to_list3(Rest).
1491 disjunction_to_list3([]) --> !.
1492 disjunction_to_list3([H|T]) --> disjunction_to_list2(H,T).
1493
1494
1495 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1496 % some predicates for the support of the Tcl/Tk UI
1497
1498 call_with_temp_preference(Prefs,Values,Call) :-
1499 maplist(get_preference,Prefs,OldValues),
1500 maplist(set_preference,Prefs,Values),
1501 call_cleanup(Call,maplist(set_preference,Prefs,OldValues)),!.
1502
1503 bv_get_values_unlimited(Ids,StateId,Values) :-
1504 assertz(get_unlimited_value),
1505 call_cleanup(call_with_temp_preference([expand_avl_upto],[-1],bv_get_values(Ids,StateId,Values)),
1506 retractall(get_unlimited_value)).
1507
1508 bv_get_value_unlimited(Id,StateId,Value) :-
1509 visited_expression(StateId,State),!,
1510 get_preference(time_out,Timeout),
1511 assertz(get_unlimited_value),
1512 call_cleanup(call_with_temp_preference([expand_avl_upto],[-1],bv_get_value(Id,State,StateId,Timeout,Value)),
1513 retractall(get_unlimited_value)).
1514 bv_get_value_unlimited(_Id,_StateId,e('unknown state')).
1515
1516 bv_get_value_unlimited_and_unprocessed(Id,StateId,Value) :-
1517 visited_expression(StateId,State),!,
1518 get_preference(time_out,Timeout),
1519 call_with_temp_preference([expand_avl_upto],[-1],bv_get_value_unprocessed(Id,State,StateId,Timeout,Value)).
1520 bv_get_value_unlimited_and_unprocessed(_Id,_StateId,e('unknown state')).
1521
1522 % get the raw value in Prolog encoding
1523 bv_get_btvalue(Id,StateId,BExpr,BValue) :-
1524 bv_get_value_unlimited_and_unprocessed(Id,StateId,V),!,
1525 (V=btvalue(Value,E)
1526 -> BExpr=E,BValue=Value
1527 ; V=i -> add_error(bvisual2,'Cannot obtain value of inactive entry:',Id),fail
1528 ; add_error(bvisual2,'Cannot extract value of entry:',Id:V),fail).
1529 bv_get_btvalue(Id,_,_,_) :-
1530 add_error(bvisual2,'Unknown identifier or section, cannot find value:',Id),fail.
1531
1532
1533 :- use_module(probsrc(tools_commands),[edit_file/2]).
1534 :- use_module(probsrc(error_manager),[extract_file_line_col/6,extract_span_description_with_opts/3]).
1535 bv_formula_origin(Id,Desc) :-
1536 bv_get_stored_formula_expr(Id,Formula),
1537 (extract_span_description_with_opts(Formula,Desc,[compact_pos_info,tail_file_name])
1538 -> true ; Desc = '').
1539
1540 bv_show_formula_origin(Id,Desc) :-
1541 bv_get_stored_formula_expr(Id,Formula),
1542 extract_file_line(Formula,FILE,LINE),
1543 (extract_span_description_with_opts(Formula,Desc,[]) -> true ; Desc = ''),
1544 edit_file(FILE,LINE).
1545 :- use_module(probsrc(bmachine),[get_machine_file_number/4]).
1546 extract_file_line(included_machine(Machine,_,_),FILE,LINE) :- !,
1547 get_machine_file_number(Machine,_Ext,_Nr,FILE),
1548 LINE=1.
1549 extract_file_line(Formula,FILE,LINE) :- extract_file_line_col(Formula,FILE,LINE,_COL,_Erow,_Ecol).
1550
1551 :- use_module(probsrc(bsyntaxtree),[get_texpr_description/2, get_texpr_labels/2]).
1552 bv_formula_description(Id,Desc) :-
1553 bv_get_stored_formula_expr(Id,Formula),
1554 get_desc(Formula,Desc).
1555 get_desc(included_machine(Machine,_,_),Desc) :- !,
1556 get_machine_short_desc(Machine,Desc).
1557 get_desc(Formula,Desc) :-
1558 is_texpr(Formula),
1559 get_texpr_description(Formula,Desc).
1560
1561 % Extended description with additional information,
1562 % such as the formula label and the number of sub-elements.
1563 % Tcl/Tk displays this description in the context menu in the evaluation view.
1564 % The ProB 2 UI displays similar information in tooltips in the state view,
1565 % but those tooltips are constructed on the Java side.
1566 bv_formula_extended_description(Id,Desc) :-
1567 bvisual2_translation_mode(Mode),
1568 with_translation_mode(Mode, bv_formula_extended_description_aux(Id,Desc)).
1569 bv_formula_extended_description_aux(Id,Desc) :-
1570 bv_get_stored_formula_expr(Id,Formula),
1571 bv_ext_desc2(Id,Formula,Desc).
1572 bv_ext_desc2(Id,Formula,Desc) :-
1573 is_texpr(Formula), !,
1574 (get_texpr_description(Formula,Desc0)
1575 -> (get_first_label(Formula,Label) -> ajoin(['@',Label,': ',Desc0],Desc1)
1576 ; Desc1=Desc0
1577 )
1578 ; get_first_label(Formula,Desc1)
1579 % ; Desc = 'No description available via @desc pragma')
1580 ; Desc1 = ''
1581 ),
1582 (bv_formula_discharged_description(Id,DInfo)
1583 -> ajoin_with_nl(Desc1,DInfo,Desc2)
1584 ; Desc2 = Desc1),
1585 (bv_formula_size_description(Id,SzeD)
1586 -> ajoin_with_nl(Desc2,SzeD,Desc3)
1587 ; Desc3 = Desc2),
1588 (texpr_contains_wd_condition(Formula)
1589 -> ajoin_with_nl(Desc3,'Formula has wd condition',Desc4)
1590 ; Desc4 = Desc3),
1591 (display_type(Formula,TS)
1592 -> ajoin(['Type: ',TS],TTS), ajoin_with_nl(Desc4,TTS,Desc)
1593 ; Desc = Desc4),
1594 Desc \= ''.
1595 bv_ext_desc2(_Id,Formula,Desc) :- get_desc(Formula,Desc). % use default
1596
1597 :- use_module(probsrc(translate),[pretty_type/2]).
1598 display_type(b(_,T,_),TS) :- \+ do_not_show_type(T), bv_pretty_type(T,TS).
1599 % ltl
1600 do_not_show_type(pred).
1601 do_not_show_type(subst).
1602 do_not_show_type(op(_)).
1603
1604 ajoin_with_nl('',D2,Res) :- !, Res=D2.
1605 ajoin_with_nl(D1,D2,Res) :- ajoin([D1,'\n',D2],Res).
1606
1607
1608 bv_formula_discharged_info(Id,DischargedInfo) :-
1609 bv_formula_discharged_info(Id,_,DischargedInfo).
1610 bv_formula_discharged_info(Id,Proven,DischargedInfo) :-
1611 bv_get_stored_formula_expr(Id,Formula),
1612 get_discharged_info(Formula,Proven,DischargedInfo).
1613
1614 get_discharged_info(Formula,Proven,DischargedInfo) :-
1615 is_texpr(Formula),
1616 get_texpr_info(Formula,I),
1617 member(proof_status(Proven,DischargedInfo),I),!.
1618
1619 :- use_module(probsrc(tools_strings),[ajoin_with_sep/3]).
1620 bv_formula_discharged_description(Id,Desc) :-
1621 bv_formula_discharged_info(Id,Proven,L),
1622 ajoin_with_sep(L,',',DInfo),
1623 ajoin(['Proof status is ',Proven,': ',DInfo],Desc).
1624
1625 % extract some info about size and top-level function symbol of an entry:
1626 bv_formula_size_description(Id,Desc) :-
1627 get_nr_of_subformulas(Id,Nr), Nr>=1,
1628 (stored_formula(Id,F), is_texpr(F)
1629 -> SubName = 'subformulas',
1630 (get_texpr_top_level_symbol(F,Symbol,_,_)
1631 -> ajoin(['Formula (',Symbol,')'],Entry) ; Entry='Formula')
1632 ; atom(Id),get_node_label(Id,SubName)
1633 -> Entry = 'Entry'
1634 ; SubName = 'children', Entry = 'Entry'
1635 ),
1636 ajoin([Entry, ' contains ', Nr, ' ', SubName],Desc).
1637
1638 % compute number of subformulas/entries of a node:
1639 get_nr_of_subformulas(Id,Nr) :- expanded(Id),!,
1640 findall(1, subnode(Id,_), L),
1641 length(L,Nr).
1642 get_nr_of_subformulas(Id,Nr) :-
1643 bv_get_stored_formula_expr(Id,Formula),
1644 findall(1,get_subformulas(Formula,_,_Kind),L),
1645 length(L,Nr).
1646
1647 % get the top-level symbol of a formula
1648 bv_get_formula_functor_symbol(Id,Symbol) :-
1649 stored_formula(Id,F), is_texpr(F),
1650 \+ top_level_node(Id),
1651 bvisual2_translation_mode(Mode),
1652 with_translation_mode(Mode, get_texpr_top_level_symbol(F,Symbol,_,_)).
1653
1654 bv_is_child_formula(Id) :- \+ bv_is_topmost_formula(Id).
1655 % check if node is topmost formula, attached to a top_level_node
1656 bv_is_topmost_formula(Id) :-
1657 supernode(Id,Super),
1658 top_level_node(Super).
1659
1660
1661 bv_formula_labels(Id,Labels) :-
1662 bv_get_stored_formula_expr(Id,Formula),
1663 is_texpr(Formula),
1664 (get_texpr_labels(Formula,Labels) -> true).
1665
1666 get_first_label(Formula,Desc) :- get_texpr_labels(Formula,Lbls) -> [Desc|_]=Lbls.
1667
1668 bv_is_typed_formula(Id) :- bv_get_stored_formula_expr(Id,Formula), is_texpr(Formula).
1669 bv_is_typed_predicate(Id) :- bv_get_stored_formula_expr(Id,Formula), Formula = b(_,pred,_).
1670 bv_is_typed_identifier(Id,IDName) :- bv_get_stored_formula_expr(Id,Formula), Formula = b(identifier(IDName),_,_).
1671
1672 bv_get_stored_formula_expr(Id,Formula) :-
1673 stored_formula(Id,Stored),
1674 extract_typed_formua(Stored,Formula).
1675
1676 extract_typed_formua(bind(LHS,_),Formula) :- !, Formula=LHS.
1677 extract_typed_formua(guard(Name,Parameters,Guard),Formula) :- !,
1678 get_guard_formula(Parameters,Guard,GF),
1679 (b_get_operation_description(Name,Desc) -> add_texpr_description(GF,Desc,Formula)
1680 ; Formula = GF).
1681 extract_typed_formua(variant(_Name,_ConvOrAnt,Variant),Formula) :- !, Formula=Variant.
1682 extract_typed_formua(named_subformula(_,Formula,_),Res) :- !, Res=Formula.
1683 extract_typed_formua(TF,TF).
1684
1685 get_guard_formula([],Guard,Res) :- !, Res=Guard.
1686 get_guard_formula(Parameters,Guard,Expr) :- create_or_merge_exists(Parameters,Guard,Expr).
1687 get_guard_theorems_formula([],_Guard,Theorems,Res) :- !, Res=Theorems.
1688 get_guard_theorems_formula(Parameters,Guard,Theorems,Expr) :-
1689 create_forall(Parameters,b(implication(Guard,Theorems),pred,[]),Expr).
1690
1691 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1692 %
1693 :- use_module(probsrc(tools_io)).
1694 bv_print_to_file(IdList,StateId,Filename) :-
1695 call_with_temp_preference([expand_avl_upto],[-1],bv_print_to_file_aux(IdList,StateId,Filename)).
1696 bv_print_to_file_aux(IdList,StateId,Filename) :-
1697 safe_open_file(Filename,write,S,[encoding(utf8)]),
1698 bv_print_to_stream_l(IdList,S,StateId,false),
1699 close(S).
1700
1701 bv_print_to_stream_l([],_S,_StateId,_WriteAnd).
1702 bv_print_to_stream_l([Id|Rest],S,StateId,WriteAnd) :-
1703 bv_print_to_stream(Id,S,StateId,OutType,WriteAnd),
1704 (OutType=predicate -> WriteAnd2=true ; WriteAnd2=WriteAnd),
1705 bv_print_to_stream_l(Rest,S,StateId,WriteAnd2).
1706
1707 bv_print_to_stream(Id,S,StateId,predicate,WriteAnd) :-
1708 stored_formula(Id,Formula),
1709 bv_print_formula_to_stream(Formula,Id,S,StateId,WriteAnd),!,nl(S).
1710 bv_print_to_stream(Id,S,StateId,other,_WriteAnd) :-
1711 get_node_label(Id,Label),
1712 bv_get_values([Id],StateId,[Value]),
1713 write(S,'/* '),write(S,Label),write(S,': '),
1714 bv_print_std_value(Value,S),write(S,' */\n').
1715 bv_print_std_value(e(Msg),S) :- write(S,'ERROR( '), write(S,Msg), write(S,' )').
1716 bv_print_std_value(p(Value),S) :- write(S,Value).
1717 bv_print_std_value(v(Value),S) :- write(S,Value).
1718 bv_print_std_value(i,S) :- write(S,'INACTIVE').
1719 bv_print_std_value(bv_info(I),S) :- write(S,I).
1720
1721 bv_print_formula_to_stream(Formula,Id,S,StateId,WriteAnd) :-
1722 is_texpr(Formula),
1723 get_texpr_type(Formula,Type),
1724 bv_get_value_unlimited_and_unprocessed(Id,StateId,Value),
1725 bv_print_formula_to_stream2(Type,Formula,Value,S,WriteAnd).
1726
1727 bv_print_formula_to_stream2(pred,Formula,p(Value),S,WriteAnd) :- !,
1728 bv_print_predicate_to_stream(Value,Formula,S,WriteAnd).
1729 bv_print_formula_to_stream2(Type,Formula,btvalue(Value,_),S,WriteAnd) :- !,
1730 create_texpr(value(Value),Type,[],TValue),
1731 create_texpr(equal(Formula,TValue),pred,[],Equal),
1732 write_bexpression(S,Equal,WriteAnd).
1733
1734 bv_print_predicate_to_stream(true,Formula,S,WriteAnd) :-
1735 write_bexpression(S,Formula,WriteAnd).
1736 bv_print_predicate_to_stream(false,Formula,S,WriteAnd) :-
1737 create_texpr(negation(Formula),pred,[],NegatedFormula),
1738 write(S,' /* FALSE */ '),write_bexpression(S,NegatedFormula,WriteAnd).
1739
1740 write_bexpression(Stream,Expression,WriteAnd) :-
1741 (WriteAnd = true -> write(Stream,' & \n') ; true),
1742 translate_bexpression_to_codes(Expression,Codes),
1743 put_codes(Codes,Stream).
1744
1745 bv_write_all_variables_and_constants(StateId,Filename) :-
1746 call_with_temp_preference([expand_avl_upto],[-1],bv_write_all_variables_and_constants_aux(StateId,Filename)).
1747 bv_write_all_variables_and_constants_aux(StateId,Filename) :-
1748 bv_expand_formula(constants,_CLabel,ConstNodes),
1749 bv_expand_formula(variables,_VLabel,VarNodes),
1750 append(ConstNodes,VarNodes,Subnodes),expand_all(Subnodes),
1751 append([constants|ConstNodes],[variables|VarNodes],Nodes),
1752 bv_print_to_file(Nodes,StateId,Filename).
1753 expand_all(Subnodes) :-
1754 member(Node,Subnodes),
1755 bv_expand_formula(Node,_,_),
1756 fail.
1757 expand_all(_Subnodes).
1758
1759
1760 :- use_module(probsrc(eventhandling),[register_event_listener/3]).
1761 :- register_event_listener(clear_specification,clear_bvisual,
1762 'Clear module bvisual2.').
1763 :- register_event_listener(specification_initialised,reset_bvisual,
1764 'Initialise module bvisual2.').
1765 :- register_event_listener(change_of_animation_mode,reset_bvisual,
1766 'Initialise module bvisual2.').
1767
1768 :- use_module(probsrc(self_check)).
1769 % a very small use-case:
1770 :- assert_must_succeed((
1771 reset_bvisual,
1772 bv_get_top_level(T), %print(top(T)),nl,
1773 %T == [variables,constants,inv],
1774 member(inv,T),
1775 bv_expand_formula(inv,Text,Sub),
1776 %print(expand(Text,Sub)),nl,
1777 Text == 'INVARIANT',
1778 Sub = [H|_],
1779 bv_expand_formula(H,_Text1,_Sub1),
1780 %print(expand1(Text1,Sub1)),nl,
1781 bv_get_values(Sub,root,Values),
1782 %print(values(Values)),nl, % should be [i,...] or e('unknown state')
1783 Values = [VI|_], (VI=i ; VI=e(_)),
1784 reset_bvisual
1785 )).
1786
1787 % example bv_get_top_level_formula(theoremsc,Txt,Labels,Vals)
1788
1789 bv_get_top_level_formula(Category,CatText,Labels,Values) :-
1790 current_state_id(StateId),
1791 bv_get_top_level_formula(Category,CatText,StateId,Labels,Values).
1792 bv_get_top_level_formula(Category,CatText,StateId,Labels,Values) :-
1793 bv_get_top_level(TL),
1794 member(Category,TL),
1795 bv_expand_formula(Category,CatText,Subs),
1796 maplist(get_node_label,Subs,Labels),
1797 bv_get_values(Subs,StateId,Values).
1798
1799 % ------------------------------------
1800
1801 % provide HTML output of operation guards upto MaxLevel
1802 % TO DO: provide option to provide parameter values, output to Latex,...
1803 html_debug_operation_for_stateid(Stream,OpName,StateId,MaxLevel) :-
1804 stored_formula(Id,guard(OpName,_,_)),
1805 enter_level(Stream,0),
1806 traverse(Stream,StateId,0,MaxLevel,Id),
1807 exit_level(Stream,0).
1808
1809 :- use_module(probsrc(tools),[html_escape/2]).
1810 traverse(_,_StateId,Level,MaxLevel,_Id) :- Level>MaxLevel,!.
1811 traverse(Stream,StateId,Level,MaxLevel,Id) :-
1812 bv_expand_formula(Id,Label,Children), html_escape(Label,ELabel),
1813 bv_get_values([Id],StateId,[Value]),
1814 translate_value(Value,ValueS), html_escape(ValueS,EValueS),
1815 % format('~w : ~w = ~w [~w] (level ~w)~n',[Id,Label,Value,ValueS,Level]),
1816 stored_formula(Id,Formula),
1817 (color_based_on_value(Value,Col) -> format(Stream,'<font color="~w">',[Col]) ; true),
1818 (Formula = guard(_,_,_)
1819 -> format(Stream,'<li>Guard of ~w = ~w</li>~n',[ELabel,EValueS])
1820 ; format(Stream,'<li>~w = ~w</li>~n',[ELabel,EValueS])
1821 ),
1822 (color_based_on_value(Value,_) -> format(Stream,'</font>',[]) ; true),
1823 (Level < MaxLevel
1824 -> L1 is Level+1,
1825 enter_level(Stream,L1),
1826 maplist(traverse(Stream,StateId,L1,MaxLevel),Children),
1827 exit_level(Stream,L1)
1828 ; true).
1829
1830 color_based_on_value(p(false),'INDIANRED').
1831 color_based_on_value(p(true),'DARKGREEN').
1832 color_based_on_value(e(_),red).
1833 color_based_on_value(i,gray).
1834 color_based_on_value(bv_info(_),gray).
1835
1836 enter_level(Stream,L1) :- indent_ws(Stream,L1), format(Stream,'<ul>~n',[]).
1837 exit_level(Stream,L1) :- indent_ws(Stream,L1), format(Stream,'</ul>~n',[]).
1838
1839 indent_ws(_,X) :- X<1,!.
1840 indent_ws(Stream,X) :- write(Stream,' '), X1 is X-1, indent_ws(Stream,X1).
1841
1842
1843 translate_value(p(V),R) :- !,R=V.
1844 translate_value(v(V),R) :- R=V.
1845 translate_value(e(M),R) :- !, ajoin(['ERROR(',M,')'],R).
1846 translate_value(i,R) :- !, R='INACTIVE'.
1847 translate_value(bv_info(I),R) :- !, R=I.
1848 translate_value(V,V).
1849
1850 % --------------------------------
1851
1852 :- use_module(probsrc(error_manager),[add_error/3]).
1853 :- use_module(probsrc(bmachine),[b_parse_machine_predicate_from_codes_open/5, b_parse_machine_formula_from_codes/7]).
1854 tcltk_register_new_user_formula(F) :- tcltk_register_new_user_formula(formula,F).
1855 tcltk_register_new_user_formula(Kind,F) :-
1856 atom_codes(F,Codes),
1857 TypingScope=[prob_ids(visible),variables],
1858 (Kind=open_predicate
1859 -> b_parse_machine_predicate_from_codes_open(exists,Codes,[],TypingScope,Typed)
1860 ; b_parse_machine_formula_from_codes(Kind,Codes,TypingScope,Typed,_Type,true,Error)
1861 ),
1862 (Error=none -> true
1863 ; add_error(tcltk_register_new_user_formula,'Error occured while parsing formula: ',Error),fail),
1864 bv_insert_formula(Typed,user_formulas,_),
1865 (top_level_node(user_formulas) -> true ; register_top_level(user_formulas) ).
1866 %retractall(expanded(user_formulas)),
1867 %bv_insert_formula(Typed,user,_),.
1868
1869 % --------------------------------
1870
1871