1 % (c) 2009-2026 Lehrstuhl fuer Softwaretechnik und Programmiersprachen,
2 % Heinrich Heine Universitaet Duesseldorf
3 % This software is licenced under EPL 1.0 (http://www.eclipse.org/org/documents/epl-v10.html)
4
5 :- module(specfile, [
6 specfile_possible_trans_name/2, specfile_possible_trans_name_for_successors/2,
7 specfile_trans/6,
8 prepare_state_for_specfile_trans/3, prepare_state_for_specfile_trans/4,
9 extract_infos_from_prepared_state/2,
10
11 property/2, elementary_prop/2,
12
13 partial_trans/4, specfile_trans_or_partial_trans/7,
14
15 animation_mode/1, set_animation_mode/1, add_animation_minor_mode/1,
16 animation_minor_mode/1, set_animation_minor_mode/1, remove_animation_minor_mode/0,
17 unset_animation_minor_modes/1, reset_animation_minor_modes/1,
18 b_mode/0, z_mode/0, b_or_z_mode/0, b_or_z_mode/1,
19 eventb_mode/0,
20 classical_b_mode/0,
21 csp_mode/0, csp_mode/1,
22 csp_with_bz_mode/0, z_or_tla_minor_mode/0,
23 process_algebra_mode/0,
24 xtl_mode/0,
25 sequent_prover_mode/0,
26 b_syntax_available_for_source/0,
27
28 type_check_csp_and_b/0,
29
30 currently_opened_file/1, % only true for successfully opened files
31 currently_opened_file_status/2, % second parameter: success or failure
32 currently_opened_specification_name/1,
33 %reset_currently_opened_file/0,
34 set_currently_opened_file/1, set_currently_opened_b_file/1,
35 set_failed_to_open_file/1,
36 set_minor_animation_mode_from_file/1,
37 set_currently_opening_file/1,
38 set_currently_opened_package/1,
39 spec_file_has_been_successfully_loaded/0,
40 b_absolute_file_name_relative_to_opened_file/2,
41
42 state_corresponds_to_initialised_b_machine/1, state_corresponds_to_initialised_b_machine/2,
43 state_corresponds_to_fully_setup_b_machine/1, state_corresponds_to_fully_setup_b_machine/2,
44 state_corresponds_to_set_up_constants/1,
45 state_corresponds_to_set_up_constants/2, state_corresponds_to_set_up_constants_only/2,
46 extract_variables_from_state/2, extract_variables_from_state_as_primed/2,
47 current_b_expression/1,
48 get_current_state_for_b_formula/2, get_state_for_b_formula/3,
49
50 get_operation_name/2, get_operation_internal_name/2,
51 translate_operation_name/2,
52 get_operation_arguments/2, get_operation_return_values_and_arguments/3,
53 get_operation_description/2, get_operation_description_for_transition/4,
54 get_operation_description_for_transition_id/3,
55 build_up_b_real_operation/2,
56 expand_const_and_vars/3, expand_const_and_vars_to_full_store/2,
57 expand_to_constants_and_variables/3,
58
59 get_specific_time_out_for_operation/2,
60 get_specific_max_operations_for_operation/2,
61 max_operations_zero_for_operation/1,
62 max_operations_zero_for_setup_constants/0, max_operations_zero_for_initialisation/0,
63 compute_operation_effect_max/6, % has replaced compute_operation_effect/5
64 create_setup_constants_operation/3,
65 get_internal_representation/1, get_internal_representation/4,
66 get_possible_event/1, get_feasible_event/1,
67 get_possible_language_specific_top_level_event/3,
68 get_specification_description/2,
69 get_local_state_for_operation_transition/3,
70 get_local_states_for_operation_transition/4, create_local_store_for_operation/4
71 ]).
72
73 :- use_module(tools).
74
75 :- use_module(module_information).
76 :- module_info(group,animator).
77 :- module_info(description,'This module computes transitions and properties depending on the current animator mode.').
78
79 :- use_module(library(lists)).
80 %:- use_module(library(avl)).
81 %:- use_module(library(clpfd)).
82
83 :- use_module(debug).
84 :- use_module(b_interpreter).
85 :- use_module(preferences).
86 :- use_module(self_check).
87 :- use_module(error_manager).
88 :- use_module(translate).
89 :- use_module(succeed_max,[succeed_max_call_id/3]).
90
91 :- use_module(store,[empty_state/1]).
92 :- use_module(bmachine).
93 :- use_module(bsyntaxtree).
94 :- use_module(xtl_interface).
95
96
97 %% :- use_module(delay).
98
99 :- use_module(value_persistance, [load_constants/2]).
100
101 :- set_prolog_flag(double_quotes, codes).
102
103 /* --------------------------------- */
104
105
106
107
108 /* --------------------------------- */
109 /* ANIMATION & TEMPORAL VERIFICATION */
110 /* --------------------------------- */
111
112
113 :- dynamic animation_mode/1, animation_minor_mode/1.
114 % not marking as volatile; otherwise we startup with no animation_mode set
115 % not all parts of ProB can deal with this at the moment
116 %:- volatile animation_mode/1, animation_minor_mode/1.
117
118 animation_mode(b).
119 % possible values: b, csp, xtl, z, csp_and_b, promela
120 % animation_minor_mode: possible values alloy, event_b, tla, z (classical B if no animation_minor_mode fact)
121
122 b_mode :- animation_mode(X), (X=b ; X=csp_and_b).
123 z_mode :- animation_mode(X), (X=b ; X=csp_and_b), animation_minor_mode(z).
124 ?classical_b_mode :- b_mode, (animation_minor_mode(Mode) -> classical_minor_mode(Mode) ; true).
125 classical_minor_mode(rules_dsl).
126
127 eventb_mode :-
128 animation_mode(X), (X=b ; X=csp_and_b),
129 animation_minor_mode(eventb).
130 csp_with_bz_mode :- animation_mode(csp_and_b).
131 z_or_tla_minor_mode :- animation_minor_mode(X), (X=tla ; X=z).
132
133 csp_mode :- animation_mode(X), csp_mode(X).
134 csp_mode(cspm).
135 csp_mode(csp_and_b).
136
137 xtl_mode :- animation_mode(xtl).
138 sequent_prover_mode :- animation_mode(xtl), animation_minor_mode(sequent_prover).
139
140 b_or_z_mode :-
141 (animation_mode(X) -> (X=b -> true ; X=csp_and_b)). % basically the same as b_mode
142 b_or_z_mode(b). b_or_z_mode(csp_and_b).
143
144 % is B syntax (ASCII/Unicode available for source files)
145 % currently used in Tcl/Tk to decide whether Unicode conversion is available
146 b_syntax_available_for_source :-
147 b_or_z_mode,
148 (animation_minor_mode(X) -> b_syntax_minor_mode(X) ; true).
149
150 b_syntax_minor_mode(eventb).
151 b_syntax_minor_mode(rules_dsl).
152 % Z and TLA are not supported
153
154
155 process_algebra_mode :- %Note: also covers xtl (Promela, CCS interpreters; see test 756); used for translating events (tau, tick, io(...))
156 animation_mode(X), X \= b,
157 \+ ((X=xtl, xtl_get_definition_string('PROCESS_ALGEBRA_MODE',false))).
158
159 set_animation_mode(X) :-
160 retractall(animation_mode(_)),
161 remove_animation_minor_mode,
162 assertz(animation_mode(X)).
163
164 set_animation_minor_mode(X) :-
165 remove_animation_minor_mode,
166 assertz(animation_minor_mode(X)).
167 remove_animation_minor_mode :-
168 retractall(animation_minor_mode(_)).
169
170 % use to temporarily unset animation minor mode:
171 :- use_module(library(lists),[maplist/2]).
172 unset_animation_minor_modes(L) :- findall(X,retract(animation_minor_mode(X)),L).
173 reset_animation_minor_modes(L) :- maplist(add_animation_minor_mode,L).
174
175 add_animation_minor_mode(X) :-
176 (animation_minor_mode(X) -> true ; assertz(animation_minor_mode(X))).
177
178
179 :- dynamic currently_opened_file_status/2.
180 :- volatile currently_opened_file_status/2.
181 %currently_opened_file(none).
182
183 :- use_module(bmachine,[b_machine_name/1]).
184 currently_opened_specification_name(Name) :-
185 (b_or_z_mode -> b_machine_name(Name)
186 ; currently_opened_file(File),
187 get_modulename_filename(File,Name)
188 ).
189
190 reset_currently_opened_file :- retractall(currently_opened_file_status(_,_)).
191 reset_specfile :- reset_currently_opened_file,
192 retractall(animation_minor_mode(_)).
193 :- use_module(eventhandling,[register_event_listener/3]).
194 :- register_event_listener(clear_specification,reset_specfile,
195 'Reset Specfile Currently Opened File.').
196
197 % call if a spec was not loaded from file but from package (e.g., via socket / prob2)
198 set_currently_opened_package(Type) :-
199 set_currently_opened_file(package(Type)).
200
201 % indicates we are in the process of opening a file
202 set_currently_opening_file(File) :-
203 reset_currently_opened_file,
204 assertz(currently_opened_file_status(File,opening)),
205 set_minor_animation_mode_from_file(File).
206
207 :- use_module(tools,[get_filename_extension/2]).
208 set_minor_animation_mode_from_file(File) :-
209 get_filename_extension(File,Extension),
210 file_extension_mode(Extension,Major,ExpectedMinor),!,
211 animation_mode(CurMajor),
212 (CurMajor=Major
213 -> (animation_minor_mode(CurMinor)
214 -> (CurMinor=ExpectedMinor -> true
215 ; member(CurMinor,ExpectedMinor) -> true % is amongst the valid options
216 ; add_warning(specfile,'Minor animation mode incompatible with file: ',CurMinor:File)
217 )
218 ; ExpectedMinor=none -> true
219 ; member(none,ExpectedMinor) -> true
220 ; set_animation_minor_mode(ExpectedMinor)
221 )
222 ; add_warning(specfile,'Major animation mode incompatible with file: ',CurMajor:File)
223 ).
224 set_minor_animation_mode_from_file(File) :- add_warning(specfile,'Unknown file extension: ',File).
225
226 :- use_module(probsrc(tools_platform), [host_platform/1]).
227 file_extension_mode(mch,b,none). % classical b, could also be csp_and_b
228 file_extension_mode(ref,b,none).
229 file_extension_mode(imp,b,none).
230 file_extension_mode(def,b,none).
231 file_extension_mode(sys,b,none).
232 file_extension_mode(rmch,b,rules_dsl).
233 file_extension_mode(prob,b,[none,tla]). % Here we cannot infer the mode; TODO: in future store this info in the .prob file
234 file_extension_mode(tla,b,tla).
235 file_extension_mode(fuzz,b,z).
236 file_extension_mode(tex,b,z).
237 file_extension_mode(eventb,b,eventb).
238 file_extension_mode(pl,xtl,none). % can be proof obligation files (sequent_prover minor mode)
239 file_extension_mode(probpo,xtl,sequent_prover). % proof obligation files (sequent_prover minor mode)
240 file_extension_mode('P',xtl,none).
241 file_extension_mode('p',xtl,none) :- host_platform(windows),
242 add_message(specfile,'Lower-case file extension .p treated as .P for XTL mode').
243 file_extension_mode(als,b,alloy).
244 file_extension_mode(cnf,b,none).
245 file_extension_mode(csp,cspm,none).
246 file_extension_mode(cspm,cspm,none).
247
248 % store currently openend specification file and set B minor animation mode
249 set_currently_opened_b_file(File) :-
250 set_currently_opened_file(File),
251 set_minor_animation_mode_from_file(File).
252
253 set_currently_opened_file(File) :-
254 reset_currently_opened_file,
255 assertz(currently_opened_file_status(File,success)).
256
257 set_failed_to_open_file(File) :-
258 reset_currently_opened_file,
259 assertz(currently_opened_file_status(File,failure)).
260
261 currently_opened_file(File) :- currently_opened_file_status(File,success).
262
263
264 :- use_module(tools,[safe_absolute_file_name/3, get_parent_directory/2]).
265 % open a file relateive to the main B file
266 b_absolute_file_name_relative_to_opened_file(File,AbsFileName) :-
267 currently_opened_file_status(MainFileName,_Status), % _Status=failure happens when error during loading
268 atom(MainFileName), % not for a package like package(event_b_project)
269 get_parent_directory(MainFileName,Directory),
270 safe_absolute_file_name(File,AbsFileName,[relative_to(Directory)]).
271
272 spec_file_has_been_successfully_loaded :- currently_opened_file(_).
273
274 :- use_module(bmachine,[b_machine_has_variables/0]).
275 % true if state corresponds to an initialised B machine
276 state_corresponds_to_initialised_b_machine(const_and_vars(_,_)) :- !,
277 b_or_z_mode.
278 state_corresponds_to_initialised_b_machine(expanded_const_and_vars(_,_,_,_)) :- !,
279 b_or_z_mode.
280 state_corresponds_to_initialised_b_machine(expanded_vars(_,_)) :- !,
281 b_or_z_mode.
282 state_corresponds_to_initialised_b_machine([]).
283 state_corresponds_to_initialised_b_machine([_|_]).
284 state_corresponds_to_initialised_b_machine(csp_and_b(_,BState)) :-
285 csp_with_bz_mode,
286 state_corresponds_to_initialised_b_machine(BState).
287
288
289 % check if state corresponds to an initialised B machine, and if so return expanded State
290 state_corresponds_to_initialised_b_machine(const_and_vars(ConstID,Vars),State) :- !,
291 b_or_z_mode, expand_const_and_vars(ConstID,Vars,State).
292 state_corresponds_to_initialised_b_machine(expanded_const_and_vars(_ConstID,_Vars,FullStore,_),State) :- !,
293 % generated by prepare_state_for_specfile_trans
294 State=FullStore.
295 state_corresponds_to_initialised_b_machine(expanded_vars(FullStore,_),State) :- !,
296 % generated by prepare_state_for_specfile_trans
297 State=FullStore.
298 state_corresponds_to_initialised_b_machine([],[]).
299 state_corresponds_to_initialised_b_machine([H|T],[H|T]).
300 state_corresponds_to_initialised_b_machine(csp_and_b(_,BState),State) :-
301 csp_with_bz_mode,
302 state_corresponds_to_initialised_b_machine(BState,State).
303
304 % check if all variables, constants setup; also allows concrete_constants if no variables exist
305 state_corresponds_to_fully_setup_b_machine(concrete_constants(_)) :- !, b_or_z_mode,
306 \+ b_machine_has_variables.
307 state_corresponds_to_fully_setup_b_machine(X) :- state_corresponds_to_initialised_b_machine(X).
308
309 state_corresponds_to_fully_setup_b_machine(concrete_constants(Constants),State) :- !, b_or_z_mode,
310 \+ b_machine_has_variables, State=Constants.
311 state_corresponds_to_fully_setup_b_machine(X,State) :- state_corresponds_to_initialised_b_machine(X,State).
312
313 % check if we only have constants, no variable values yet
314 state_corresponds_to_set_up_constants_only(root,State) :- !, animation_mode(b),
315 \+ b_machine_has_constants_or_properties, State=[]. % relevant for test 960
316 state_corresponds_to_set_up_constants_only(concrete_constants(Constants),State) :- !,
317 b_or_z_mode, State=Constants.
318 state_corresponds_to_set_up_constants_only(csp_and_b(_,BState),State) :- !,
319 csp_with_bz_mode,
320 state_corresponds_to_set_up_constants_only(BState,State).
321
322 state_corresponds_to_set_up_constants(const_and_vars(_,_)) :- !. % avoid expansion
323 state_corresponds_to_set_up_constants(csp_and_b(_,BState)) :- !, state_corresponds_to_set_up_constants(BState).
324 state_corresponds_to_set_up_constants(X) :- state_corresponds_to_set_up_constants(X,_).
325
326 state_corresponds_to_set_up_constants(const_and_vars(ConstID,Vars),State) :- !,
327 b_or_z_mode, expand_const_and_vars(ConstID,Vars,State).
328 state_corresponds_to_set_up_constants(expanded_const_and_vars(_ConstID,_Vars,FullStore,_),State) :- !,
329 % generated by prepare_state_for_specfile_trans
330 State=FullStore.
331 state_corresponds_to_set_up_constants(expanded_vars(FullStore,_),State) :- !,
332 % generated by prepare_state_for_specfile_trans
333 State=FullStore.
334 state_corresponds_to_set_up_constants(concrete_constants(Constants),State) :- !,
335 b_or_z_mode, State=Constants.
336 state_corresponds_to_set_up_constants(csp_and_b(_,BState),State) :- !,
337 csp_with_bz_mode,
338 state_corresponds_to_set_up_constants(BState,State).
339 state_corresponds_to_set_up_constants([],State) :- !, State=[],
340 b_or_z_mode.
341 state_corresponds_to_set_up_constants(State,RState) :-
342 b_or_z_mode,
343 (State = root -> \+ b_machine_has_constants_or_properties,RState=[] ; RState=State).
344
345 :- use_module(state_space,[current_expression/2]).
346 current_b_expression(CurBState) :-
347 current_expression(_CurID,CurState),
348 state_corresponds_to_set_up_constants(CurState,CurBState).
349
350
351
352 :- use_module(state_space, [current_state_id/1]).
353 :- use_module(bmachine, [determine_type_of_formula/2]).
354 % get a state sufficient to evaluate a given formula (expression or predicate)
355 % it will give an error is the machine is not sufficiently initialised
356 get_current_state_for_b_formula(TypedExpr,BState) :-
357 current_state_id(CurID),
358 get_state_for_b_formula(CurID,TypedExpr,BState).
359 get_state_for_b_formula(StateID,TypedExpr,BState) :-
360 (formula_class(TypedExpr) -> Type=TypedExpr % user already provided formula class
361 ; determine_type_of_formula(TypedExpr,Type)), % TODO: extract used ids and only expand those that are required
362 get_state(Type,StateID,TypedExpr,BState).
363
364 formula_class(requires_nothing).
365 formula_class(requires_constants).
366 formula_class(requires_variables).
367
368 :- use_module(probsrc(state_space),[get_constants_state_for_id/3]).
369 get_state(requires_nothing,_ID,_TE,[]).
370 get_state(requires_constants,StateID,TypedExpr,BState) :-
371 (get_constants_state_for_id(StateID,[allow_variable_list],BState) -> true
372 ; ajoin(['Please setup constants first, cannot evaluate (in state ',StateID,') :'],Msg),
373 add_error(get_state,Msg,TypedExpr,TypedExpr),fail).
374 get_state(requires_variables,StateID,TypedExpr,BState) :-
375 my_visited_expression(StateID,State),
376 (state_corresponds_to_initialised_b_machine(State,BState) -> true ;
377 ajoin(['Please initialise machine first, cannot evaluate (in state ',StateID,') :'],Msg),
378 add_error(get_state,Msg,TypedExpr,TypedExpr),fail).
379 % TO DO: we could try and get only those identifiers that are really used
380
381 :- use_module(state_space,[visited_expression/2]).
382 my_visited_expression(root,S) :- !,
383 S=root. % ensure we succeed even if no state space set (e.g. when no machine provided to probcli)
384 my_visited_expression(StateID,State) :- visited_expression(StateID,State).
385
386 /* TRANSITIONS */
387
388 :- use_module(tools_meta,[call_residue/2]).
389 % ---------------------------------------------------
390
391
392 % Tries to find successor state for Op in State DB. Fails, if no solution is possible
393 % Input: State DB, Operation Op
394 % Output: DB2 the sucessor state
395 % Residue: Unfinished co-routines (should not happen!)
396 %specfile_trans(DB,Op,DB2,[]) :- !, specfile:i_transition(DB,Op,DB2).
397 specfile_trans(DB,OpName,Op,DB2,TransInfo,Residue) :-
398 % i_transition(DB,OpName,Op,DB2,TransInfo), Residue = []. % is faster, but no residue checking
399 animation_mode(MODE),
400 ? call_residue(i_transition(MODE,DB,OpName,Op,DB2,TransInfo),Residue).
401
402 i_transition(b,State,OpName,Op,NewState,TransInfo) :- !,
403 ? b_trans(State,OpName,Op,NewState,TransInfo).
404 i_transition(xtl,State,OpName,Op,NewState,TransInfo) :- !,
405 xtl_transition(State,Op,NewState,TransInfo), functor(Op,OpName,_).
406 i_transition(cspm,State,_OpName,Op,NewState,[]) :- !,
407 % Note: the OpName is ignored: we cannot make use of it
408 cspm_transition(State,Op,NewState).
409 %i_transition(promela,State,_OpName,Op,NewState,[]) :- !,
410 % promela_transition(State,Op,NewState).
411 % ; MODE=smv -> smv_transition(State,Op,NewState),TransInfo=[]
412 i_transition(csp_and_b,State,OpName,Op,NewState,TransInfo) :- !,
413 % Note: the OpName is either the name of a B operation or '$CSP' (or a variable)
414 csp_and_b_transition(State,OpName,Op,NewState,TransInfo).
415
416
417 % if you call specfile_trans multiple times for the same state, it is
418 % more efficient to call this predicate first once
419 prepare_state_for_specfile_trans(const_and_vars(ConstID,Vars),CurID,R) :- !, %print(expanding_const(ConstID)),nl,
420 R = expanded_const_and_vars(ConstID,Vars,FullStore,Infos),
421 expand_const_and_vars(ConstID,Vars,FullStore), %, print(full(FullStore)),nl.
422 prepare_infos(const_and_vars(ConstID,Vars),CurID,Infos).
423 prepare_state_for_specfile_trans(FullStore,CurID,R) :- FullStore=[_|_],
424 prepare_infos(FullStore,CurID,Infos), Infos \=[],
425 !,
426 R = expanded_vars(FullStore,Infos).
427 prepare_state_for_specfile_trans(csp_and_b(CSP,BStore),CurID,csp_and_b(CSP,Res)) :- !,
428 prepare_state_for_specfile_trans(BStore,CurID,Res).
429 prepare_state_for_specfile_trans(X,_,X).
430
431 % a version with a memo argument to store expanded constants
432 prepare_state_for_specfile_trans(const_and_vars(ConstID,Vars),CurID,MEMO,R) :- !, %print(expanding_const(ConstID)),nl,
433 R = expanded_const_and_vars(ConstID,Vars,FullStore,Infos),
434 (nonvar(MEMO),MEMO = [ConstID/ConstantsStore]
435 % constants have already been previously extracted; e.g., in model_checker loop
436 -> %tools_printing:print_term_summary(memo(ConstID,ConstantsStore)),nl,
437 append(Vars,ConstantsStore,FullStore) % simply reuse expanded constants in MEMO
438 % used to be constants first, but has to be compatible with b_initialise_machine2 and expand_const_and_vars/3
439 ; % expand_const_and_vars/3 inlined (so as to obtain ConstantsStore
440 visited_expression(ConstID,concrete_constants(ConstantsStore)), %% This can take a while if the Constants are big !
441 % print(no_memo(ConstID,MEMO)),tools_printing:print_term_summary(ConstantsStore),nl,
442 append(Vars,ConstantsStore,FullStore),
443 (var(MEMO)
444 -> MEMO=[ConstID/ConstantsStore] % store expanded constants to avoid unpacking again
445 ; true)
446 ), %, print(full(FullStore)),nl.
447 prepare_infos(const_and_vars(ConstID,Vars),CurID,Infos).
448 prepare_state_for_specfile_trans(FullStore,CurID,_,R) :- FullStore=[_|_],
449 prepare_infos(FullStore,CurID,Infos), Infos \=[], !,
450 R = expanded_vars(FullStore,Infos).
451 prepare_state_for_specfile_trans(csp_and_b(CSP,BStore),CurID,MEMO,csp_and_b(CSP,Res)) :- !,
452 prepare_state_for_specfile_trans(BStore,CurID,MEMO,Res).
453 prepare_state_for_specfile_trans(X,_,_,X). % TODO: prepare infos as well if we have no constants
454
455 :- use_module(state_packing,[pack_state/2, get_packed_b_state_with_uncompressed_lists/2]).
456 % store some operation independent infos which are useful
457 % State is the unexpanded state (const_and_vars(ConstID,Vars) or a list of bindings), as stored in the state space
458 prepare_infos(State,CurID,Infos) :- CurID \= root,
459 (get_preference(operation_reuse_setting,false)
460 -> Infos = []
461 % TO DO: check if we should enable this: get_preference(use_state_packing,true)
462 % TODO: we could check if there are operations which do not write all variables
463 ; Infos=[packed_state/PackedList, state_id/CurID],
464 (CurID \= unknown, state_space:packed_visited_expression(CurID,PS),
465 get_packed_b_state_with_uncompressed_lists(PS,PackedList) % replace '$cst_vars' and compressed lists
466 -> true %,write('+')
467 ; (CurID \= unknown % unknown: state not (yet) stored in the state space, e.g., for custom operations
468 -> debug_format(19,'prepare_infos for state ~w: need to pack_values~n',[CurID]) % should ideally not happen
469 ; true),
470 pack_state(State,PS), % precompute state packing; will be re-used multiple times
471 get_packed_b_state_with_uncompressed_lists(PS,PackedList)
472 )
473 ).
474
475 % extract pre-computed infos
476 extract_infos_from_prepared_state(expanded_const_and_vars(_,_,_,Infos),Res) :- !, Res=Infos.
477 extract_infos_from_prepared_state(expanded_vars(_,Infos),Res) :- !, Res=Infos.
478 extract_infos_from_prepared_state(_,[]).
479
480 remove_infos_from_prepared_state(expanded_const_and_vars(C,V,_,_),Res) :- !, Res=const_and_vars(C,V).
481 remove_infos_from_prepared_state(expanded_vars(V,_),R) :- !, R=V.
482 remove_infos_from_prepared_state(S,S).
483
484 % extract initialised variables from a state
485 extract_variables_from_state(const_and_vars(_,V),R) :- !,R=V.
486 extract_variables_from_state(expanded_const_and_vars(_,V,_,_),R) :- !,R=V.
487 extract_variables_from_state(expanded_vars(V,_),R) :- !,R=V.
488 extract_variables_from_state([],[]).
489 extract_variables_from_state([H|T],[H|T]).
490
491 :- use_module(probsrc(btypechecker),[prime_atom0/2]). % add $0 at end of variable
492 prime_binding(bind(ID,V),bind(PID,V)) :- prime_atom0(ID,PID).
493
494 extract_variables_from_state_as_primed(SrcState,PrimedVarBindings) :-
495 extract_variables_from_state(SrcState,VarVals),
496 maplist(prime_binding,VarVals,PrimedVarBindings).
497
498
499 /* can be attempted if trans fails to attempt to use partially computed transitions */
500 partial_trans(DB,Op,DB2,Residue) :-
501 call_residue(partial_i_transition(DB,Op,DB2),Residue).
502
503 partial_i_transition(root,'$partial_setup_constants',concrete_constants(ConstantsStore)) :-
504 animation_mode(b),
505 b_interpreter:b_partial_set_up_concrete_constants(ConstantsStore).
506
507 % a predicate which automatically calls partial_trans :
508 specfile_trans_or_partial_trans(DB,OpName,Op,DB2,TransInfo,Residue,Partial) :-
509 if(specfile_trans(DB,OpName,Op,DB2,TransInfo,Residue),Partial=false,
510 (OpName='$setup_constants', % OpName is often an input argument here
511 TransInfo=[], Partial = true,
512 partial_trans(DB,Op,DB2,Residue))).
513
514 /* build up skeletons of possible operations; to be used before time_out call is made */
515 % currently only used in zmq worker.pl
516 specfile_possible_trans_name(State,OpName) :- animation_mode(MODE),
517 (MODE=b -> b_possible_trans_name(State,OpName)
518 ; MODE = csp_and_b -> csp_b_possible_trans_name(State,OpName)
519 ; setup_transition_skeleton(MODE,OpName) /* TO DO: add more possible skeletons */
520 ).
521
522 % only set up skeletons if it is worth it for computing all successors
523 % e.g., currently it is not worth it to setup skeletons for either CSP or CSP||B (due to renaming,... we cannot predict which parts of a CSP process can be excluded)
524 specfile_possible_trans_name_for_successors(State,OpName) :- animation_mode(MODE),
525 (MODE=b
526 ? -> b_possible_trans_name(State,OpName)
527 ; setup_transition_skeleton(MODE,OpName)
528 ).
529
530 setup_transition_skeleton(_,_). /* TO DO: add more possible skeletons ? */
531
532 :- use_module(library(random)).
533 b_possible_trans_name(root,OpName) :- !,
534 (b_machine_has_constants_or_properties
535 -> OpName = '$setup_constants'
536 ; OpName = '$initialise_machine').
537 b_possible_trans_name(concrete_constants(_),OpName) :- !, OpName = '$initialise_machine'.
538 b_possible_trans_name(_State,OpName) :-
539 (preferences:preference(randomise_operation_order,true),random(1,3,1)
540 -> b_machine_operation_names_in_reverse_order(OpName) ; true),
541 %b_get_machine_operation(OpName,_Res,_Params,_,_,_).
542 ? b_top_level_feasible_operation(OpName). % TO DO: we could link also with cbc feasibility analysis if carried out
543 % we could check: b_operation_cannot_modify_state(OpName) given a preference, in particular for SAFETY_MODEL_CHECK when operation already covered
544
545 csp_b_possible_trans_name(csp_and_b_root,OpName) :- !,
546 (b_machine_has_constants_or_properties
547 -> OpName = tau % tau('$setup_constants')
548 ; OpName = tau % tau('$initialise_machine')
549 ).
550 csp_b_possible_trans_name(concrete_constants(_),OpName) :- !,
551 OpName = tau. % tau('$initialise_machine')
552 csp_b_possible_trans_name(State,OpName) :-
553 b_possible_trans_name(State,OpName).
554 % TO DO: add non-synchronised channels
555
556 % --------------
557
558 :- use_module(probcspsrc(haskell_csp),[channel_type_list/2, symbol/4]).
559 :- use_module(debug).
560 % check whether a currently open CSP is type compatible with a loaded B machine
561 type_check_csp_and_b :- animation_mode(csp_and_b), debug_println(9,type_check_csp_and_b),
562 b_get_machine_operation(ChannelOpName,Res,Params,_,_,_), append(Params,Res,ParamsRes),
563 channel_type_list(ChannelOpName,CSPTypeList),
564 % TO DO: also check B variable names which match channel name
565 debug_println(9,checking_synchronised_channel(ChannelOpName,ParamsRes,CSPTypeList)),
566 l_check_compatibility(ParamsRes,CSPTypeList,ChannelOpName),
567 fail.
568 type_check_csp_and_b.
569
570 %convert_csp_type_to_list(type(T),R) :- convert_csp_type_to_list2(T,R).
571 %convert_csp_type_to_list2(dotUnitType,[]).
572 %convert_csp_type_to_list2(dotTupleType(T),T).
573
574 l_check_compatibility([],[],_) :- !.
575 l_check_compatibility([B|BT],[CSP|CSPT],ChannelOpName) :- !, check_compatibility(B,CSP,ChannelOpName),
576 l_check_compatibility(BT,CSPT,ChannelOpName).
577 l_check_compatibility([],CSP,ChannelOpName) :- !,
578 add_error(type_check_csp_and_b,'CSP Channel has too many parameters: ', ChannelOpName:CSP).
579 l_check_compatibility(B,[],ChannelOpName) :- !,
580 (maplist(get_texpr_id,B,BArgs) -> true ; BArgs=B), % B: typed parameter/result identifiers
581 ajoin(['CSP Channel has too few parameters, B arguments ',BArgs,' will be ignored for: '],Msg),
582 add_message(type_check_csp_and_b,Msg,ChannelOpName).
583
584 check_compatibility(b(identifier(ID),TYPE,INFO),CSP_TYPE,ChannelOpName) :- !,
585 (type_ok(TYPE,CSP_TYPE) -> true ;
586 % TO DO: pretty print B and CSP Types
587 add_error(type_check_csp_and_b,'Incompatible types between B and CSP: ',ChannelOpName:ID:(TYPE:CSP_TYPE),b(identifier(ID),TYPE,INFO))
588 ).
589 check_compatibility(B,CSP,ChannelOpName) :- !,
590 add_internal_error('Illegal types: ',check_compatibility(B,CSP,ChannelOpName)).
591
592 % TO DO: needs to be refined much more;
593 type_ok(Type,CSPType) :-
594 (type_ok2(Type,CSPType) -> true
595 ; add_error(type_check_csp_and_b,'Cannot be converted (B:CSP): ',Type:CSPType),fail).
596 type_ok2(boolean,X) :- !, is_csp_boolean_type(X).
597 type_ok2(integer,X) :- !, is_csp_integer_type(X).
598 type_ok2(string,CSP) :- !,is_csp_global_set_type(CSP,string).
599 type_ok2(set(T),CSP) :- !, is_csp_set_type(CSP,T).
600 % TO DO: what if we have set(couple(integer,T)) instead of seq(T) ?
601 % rye
602 % check set(couple(integer,T)) (seq type) in B
603 type_ok2(seq(T),CSP) :- !, is_csp_seq_type(CSP,T).
604 type_ok2(couple(A,B),CSP) :- !, is_csp_couple_type(CSP,A,B).
605 type_ok2(global(GS),CSP) :- !,is_csp_global_set_type(CSP,GS).
606 type_ok2(X,Y) :- % e.g., record or freetype; we accept the type but cannot check it
607 add_message(type_check_csp_and_b,'Cannot check compatibility of B type with CSP type: ',X:Y).
608
609 is_csp_couple_type(setValue([na_tuple([A|Rest])|_]),TA,TB) :- type_ok(TA,setValue([A])),
610 check_rest_couple_els(Rest,TB).
611 is_csp_couple_type(typeTuple([A|Rest]),TA,TB) :- type_ok(TA,A), check_rest_couple(Rest,TB).
612
613 check_rest_couple_els([B],TB) :- !, type_ok(TB,setValue([B])).
614 check_rest_couple_els([B1|BRest],couple(TB1,TBRest)) :- type_ok(TB1,setValue([B1])),
615 check_rest_couple_els(BRest,TBRest).
616 check_rest_couple([B],TB) :- !, type_ok(TB,B).
617 check_rest_couple([B1|BRest],couple(TB1,TBRest)) :- type_ok(TB1,B1),
618 check_rest_couple(BRest,TBRest).
619
620 % rye
621 % check if it is couple(integer, T) type and return T in 2nd arguments
622 couple_int(couple(integer, T), T). % possible sequence
623
624 % rye: it's not correct to check only Head
625 % % for example, for CSP type, {{}, {(1,2),(2,3)}}. Its head is emptyset, but the second one is set of tuple
626 is_csp_set_type('Set'([H|_]),Type) :- type_ok(Type,H). % just check Head
627 % check the 2nd element in set in case the 1st is emptyset
628 is_csp_set_type(setValue([_|T]),Type) :-
629 T = [H1 | _], !,
630 % if it is set(couple(integer, T)), it can be a sequence type
631 (couple_int(Type, TH1) -> is_csp_seq_type(setValue(T), TH1)
632 ; type_ok(Type,H1)
633 ). % check the 2nd
634 % if it is only one element in set, just check the head
635 is_csp_set_type(setValue([H|T]),Type) :-
636 (couple_int(Type, TH1) -> is_csp_seq_type(setValue([H|T]), TH1)
637 ; type_ok(Type,H)
638 ). % just check Head
639
640 is_csp_seq_type('Seq'(H),Type) :- type_ok(Type,H).
641 is_csp_seq_type(setValue([H|_]),Type) :- H=list(L), is_csp_list(L,Type).
642 is_csp_list([],_).
643 is_csp_list([H|_],Type) :- type_ok(Type,setValue([H])).
644
645 is_csp_boolean_type(boolType).
646 is_csp_boolean_type(setValue([H|_])) :- is_csp_boolean_value(H).
647 is_csp_boolean_value(true).
648 is_csp_boolean_value(false).
649
650 is_csp_integer_type(intType).
651 is_csp_integer_type(setFromTo(_,_)).
652 is_csp_integer_type(setFrom(_)).
653 is_csp_integer_type(setValue([int(_)|_])).
654
655 is_csp_global_set_type(dataType(_),_GS). % TO DO CHECK MEMBERS
656 is_csp_global_set_type(setValue([]),_GS). % TO DO CHECK MEMBERS
657 is_csp_global_set_type(setValue([_|_]),_GS). % TO DO CHECK MEMBERS
658
659 % --------------
660
661
662 :- use_module(bmachine,[b_get_machine_operation/4]).
663 :- use_module(kernel_relations,[tuple_of/3]).
664 % compute transitions for a CSP || B specification
665 % Note: the OpName is either the name of a B machine operation (even if hidden) or '$CSP' (useful for ltsmin)
666 csp_and_b_transition(root,OpName,Op,NewState,TransInfo) :- !,
667 Op = start_cspm_MAIN, OpName=Op, NewState = csp_and_b_root,TransInfo = [].
668 csp_and_b_transition(csp_and_b_root,OpName,Trans,NewState,TransInfo) :- !,
669 Trans = tau(Op), OpName = '$CSP',
670 b_trans(root,_,Op,InitialBState,TransInfo),
671 (InitialBState = concrete_constants(_)
672 -> NewState = InitialBState
673 ; csp_initialisation_for_b(InitialCSPState),
674 NewState = csp_and_b(InitialCSPState,InitialBState)
675 ).
676 csp_and_b_transition(concrete_constants(C),OpName,Trans,NewState,TransInfo) :- !,
677 Trans = tau(Op), OpName = '$CSP',
678 b_trans(concrete_constants(C),_,Op,InitialBState,TransInfo),
679 csp_initialisation_for_b(InitialCSPState),
680 NewState = csp_and_b(InitialCSPState,InitialBState).
681 csp_and_b_transition(csp_and_b(CSPState,BState),OpName,EventVisibleToUser,csp_and_b(NewCSP,NewB),TransInfo) :-
682 !,
683 csp_transition_for_b(CSPState,ChOpName,ChArgs,CSPAction,NewCSP),
684 % TO DO: split up csp_transition_for_b: into two parts; second part converts datatypes and only called if linking op exists
685 %% print(csp_trans(ChOpName,ChArgs)),nl, %%
686 length(ChArgs,Len), length(OpArgs,Len),
687 b_transition_for_csp(ChOpName,CSPAction,ChArgs,OpArgs,BState,OpName,Operation,NewB,TransInfo),
688 hide_csp_event(CSPAction,Operation,EventVisibleToUser).
689 csp_and_b_transition(State,'$CSP',Op,NewState,[]) :-
690 % we have a pure CSP state: use CSP-M transition
691 cspm_transition(State,Op,NewState).
692
693 :- use_module(bmachine,[b_is_variable/1,b_is_constant/1]).
694 % compute the B counterpart for a CSP transition:
695 b_transition_for_csp(ChOpName,_CSPAction,ChArgs,OpArgs,BState,OpName,Operation,NewB,TransInfo) :-
696 build_up_b_operation_for_csp(ChOpName,OpArgs,Operation),
697 !,
698 OpName = ChOpName,
699 %% print_message(trying_b_trans(Operation,ChArgs,OpArgs)), %%
700 (get_preference(operation_reuse_setting,false)
701 -> generate_b_operationargs_from_csp(ChArgs,OpArgs),
702 b_trans(BState,OpName,Operation,NewB,TransInfo)
703 ;
704 % compute B transition without parameters so that we can cache
705 b_trans(BState,OpName,Operation,NewB,TransInfo),
706 % and unify afterwards
707 generate_b_operationargs_from_csp(ChArgs,OpArgs)
708 ).
709 %%, print_message(csp_btrans(Operation)), %%.
710 b_transition_for_csp(ChOpName,_CSPAction,ChArgs,OpArgs,BState,OpName,Operation,NewB,TransInfo) :-
711 OpName = ChOpName,
712 (b_is_variable(ChOpName) ; b_is_constant(ChOpName)),
713 %% print(probing(ChOpName)),nl,
714 expand_const_and_vars_to_full_store(BState,FBState),
715 try_lookup_value_in_store_and_global_sets(ChOpName,FBState,IdValue),
716 !,
717 remove_infos_from_prepared_state(BState,NewB),
718 TransInfo=[],
719 /* interpret as probing operation: get value of var,cst,SET */
720 generate_b_operationargs_from_csp(ChArgs,OpArgs),
721 (OpArgs=[SingleVal]
722 -> SingleVal = IdValue, Operation='-->'(ChOpName,[IdValue])
723 ; OpArgs=[Val1,Val2],
724 tuple_of(Val1,Val2,IdValue), /* from kernel_relations, use exact_element_of ??? */
725 OpCall =.. [ChOpName,Val1],
726 Operation='-->'(OpCall,[Val2])
727 ).
728 b_transition_for_csp(_ChOpName,CSPAction,_ChArgs,_OpArgs,BState,OpName,Operation,NewB,TransInfo) :-
729 OpName = '$CSP', % all CSP events grouped together
730 remove_infos_from_prepared_state(BState,NewB),
731 TransInfo=[],
732 /* no B operation of the name; could be tau or tick or extra comm channel */
733 %Op1 =.. [ChOpName|OpArgs],
734 Operation = CSPAction. % removed csp(.) wrapper [for LTL model checker]
735
736 hide_csp_event(CSPAction,_,EventVisibleToUser) :-
737 symbol('HIDE_CSPB',_,Span,_),!, % if there is a HIDE_CSPB definition then we do MAIN [{| HIDE_CSPB |}] B_MACHINE \ {| HIDE_CSPB |}
738 haskell_csp:evaluate_argument(val_of('HIDE_CSPB',Span),EvCList),
739 % print(hide(EvCList,CSPAction)),nl,
740 haskell_csp:expand_channel_pattern_expression(EvCList,ECList,no_loc_info_available),
741 haskell_csp:cspm_hide_action(CSPAction,omega,ECList, Span, EventVisibleToUser,_).
742 hide_csp_event(_CSPAction,Operation,EventVisibleToUser) :-
743 % get_preference(csp_event_visible_to_user,true) -> EventVisibleToUser = CSPAction
744 EventVisibleToUser = Operation.
745
746 build_up_b_operation_for_csp(OpName,OpArgs,Operation) :- %print(csp(OpName,OpArgs,Operation)),nl,
747 nonvar(OpName),
748 b_get_machine_operation(OpName,Res,Params,_), %print(get_machine_op(OpName,Res,Params)),nl,
749 specfile:generate_args(OpArgs,Params,Res, BArgs,BResults,OpName), %print(gen_args(BArgs,BResults)),nl,
750 % TO DO: either also extract types from Params, Res or better write a static checker that tests that the B operation and CSP channel types are compatible
751 safe_univ(Op1,[OpName|BArgs]), %print(op1(Op1)),nl,
752 (BResults = [] -> Operation = Op1 ; Operation = '-->'(Op1,BResults)).
753
754 :- use_module(bmachine,[b_get_machine_operation_for_animation/6]).
755 build_up_b_real_operation(OpName,Operation) :-
756 b_get_machine_operation_for_animation(OpName,Res,Params,_,_,true), %true==TopLevel
757 length(Params,Len), length(OpArgs,Len),
758 length(Res,RLen), length(BResults,RLen),
759 safe_univ(Op1,[OpName|OpArgs]),
760 (BResults = [] -> Operation = Op1 ; Operation = '-->'(Op1,BResults)).
761
762 generate_args([],[],Res,[],BRes,_) :- length(Res,L), length(BRes,L).
763 generate_args([],[_Param|T],Res,[_|TA],BRes,OpName) :-
764 generate_args([],T,Res,TA,BRes,OpName).
765 generate_args([CSPArg|TC],[Param|T],Res,[CSPArg|TA],BRes,OpName) :- check_type_for_id(CSPArg,Param,OpName),
766 generate_args(TC,T,Res,TA,BRes,OpName).
767 generate_args([CSPArg|TC],[],[ReturnParam|TRes],[],[CSPArg|TBRes],OpName) :-
768 check_type_for_id(CSPArg,ReturnParam,OpName),
769 generate_args(TC,[],TRes,[],TBRes,OpName).
770 generate_args([H|T],[],[],[],[],OpName) :-
771 add_error(specfile,'CSP provides extra arguments: ',OpName:[H|T]).
772
773 check_type_for_id(Val,TID,OpName) :- get_texpr_type(TID,Type),
774 check_type(Type,Val,OpName:TID).
775
776 :- block check_type(?,-,?).
777 check_type(boolean,pred_true,_) :- !.
778 check_type(boolean,pred_false,_) :- !.
779 check_type(integer,int(_),_) :- !.
780 check_type(string,string(_),_) :- !.
781 check_type(global(GS),fd(_,GS),_) :- !.
782 check_type(couple(A,B),(VA,VB),OID) :- !, check_type(A,VA,OID), check_type(B,VB,OID).
783 check_type(set(_T),[],_) :- !.
784 check_type(set(_T),closure(_,_,_),_) :- !. % TO DO: check type signature of closure
785 check_type(set(T),[H|_],OID) :- !, check_type(T,H,OID).
786 check_type(set(T),avl_set((node(Y,_,_,_L,_R))),OID) :- !, check_type(T,Y,OID).
787 check_type(seq(T),V,OID) :- !, check_type(set(couple(integer,T)),V,OID).
788 % TO DO: record, freetype ; but do not appear in translation from CSP to B yet
789 check_type(Type,Value,OpName:TID) :-
790 def_get_texpr_id(TID,ID),
791 add_error(check_type_for_id,'Illegal CSP value for argument: ',OpName:ID:type(Type):value(Value)),
792 fail.
793
794 get_operation_internal_name(Op,Name) :- var(Op),!,
795 add_error(get_operation_internal_name,'Variable Transition: ',Op), Name=Op.
796 get_operation_internal_name('$initialise_machine',N) :- !, N='$initialise_machine'.
797 get_operation_internal_name('$setup_constants',N) :- !, N='$setup_constants'.
798 get_operation_internal_name('$partial_setup_constants',N) :- !, N='$partial_setup_constants'.
799 get_operation_internal_name(Op,Name) :- get_operation_name(Op,Name).
800
801 is_setup_constants_internal_name('$setup_constants').
802 is_setup_constants_internal_name('$partial_setup_constants').
803
804 % tool to get the basic operation/channel/event name of a transition:
805 get_operation_name(Op,Name) :- var(Op),!, add_error(get_operation_name,'Variable Transition: ',Op), Name=Op.
806 get_operation_name('-->'(F,_),N) :- !, functor(F,N,_).
807 %get_operation_name(io([Action|_],proc(_Nr,_PROC),_SPAN),F) :-
808 % animation_mode(promela),!,functor(Action,F,_).
809 get_operation_name(io(_V,Ch,_SPAN),N) :- csp_mode,!, functor(Ch,N,_).
810 get_operation_name(FullOperation,Name) :- functor(FullOperation,Functor,_),
811 translate_operation_name(Functor,Name).
812
813 get_operation_arguments(Op,Args) :- var(Op),!, add_error(get_operation_arguments,'Variable Transition: ',Op), Args=[].
814 get_operation_arguments('-->'(F,_ReturnValues),Args) :- !, F =.. [_|Args].
815 %get_operation_arguments(io([_Action|V],proc(_Nr,_PROC),_SPAN),Args) :-
816 % animation_mode(promela),!,Args=V.
817 get_operation_arguments(io(V,_Ch,_SPAN),Args) :- csp_mode,!, Args = V.
818 get_operation_arguments(FullOperation,Args) :- FullOperation =.. [_|Args].
819
820 get_operation_return_values_and_arguments('-->'(F,ReturnValues),ReturnValues,Args) :- !,
821 F =.. [_|Args].
822 get_operation_return_values_and_arguments(Op,[],Args) :- get_operation_arguments(Op,Args).
823
824 translate_operation_name(Var,T) :- var(Var),!,T='_?Operation?_'.
825 translate_operation_name('$initialise_machine',T) :- !,T='INITIALISATION'.
826 translate_operation_name('$setup_constants',T) :- !,T='SETUP_CONSTANTS'.
827 translate_operation_name('$partial_setup_constants',T) :- !,T='PARTIAL_SETUP_CONSTANTS'.
828 %translate_operation_name(InternalName,Res) :- !, Res=InternalName.
829 translate_operation_name(X,X).
830
831 :- use_module(bmachine,[b_get_operation_description/2]).
832 % get a textual description of a transition, based on description pragma
833 get_operation_description(OpTerm,Desc) :- b_or_z_mode,
834 get_operation_name(OpTerm,OpName),
835 b_get_operation_description(OpName,Desc).
836 % in future we could possible use operation parameter values to adapt description
837
838 :- use_module(state_space,[transition/4,transition_info/2]).
839 get_operation_description_for_transition_id(StateId,TransitionId,Desc) :-
840 transition(StateId,OpTerm,TransitionId,NextId), !,
841 (xtl_mode % TODO: allow other modes?
842 -> transition_info(TransitionId,description(Desc))
843 ; get_operation_description_for_transition(StateId,OpTerm,NextId,Desc)
844 ).
845 get_operation_description_for_transition_id(StateId,TransitionId,_) :-
846 debug_format(19,'No transition from state ~w with id ~w!~n', [StateId,TransitionId]),
847 fail.
848
849 :- use_module(probsrc(b_interpreter),[b_compute_explicit_epression_no_wf/6]).
850 :- use_module(probsrc(bmachine),[get_operation_description_template_expr/2]).
851 % get a description for operation from a given state; to do: we could provide destination state
852 get_operation_description_for_transition(StateId,OpTerm,NextId,Desc) :- xtl_mode, !,
853 transition(StateId,OpTerm,TransitionId,NextId),
854 transition_info(TransitionId,description(Desc)).
855 get_operation_description_for_transition(StateId,OpTerm,NextId,Desc) :- b_or_z_mode,
856 get_operation_internal_name(OpTerm,OpName),
857 get_operation_description_template_expr(OpName,TemplateStringExpr),
858 visited_expression(NextId,State),
859 state_for_op(OpName,State,BState1),
860 visited_expression(StateId,BeforeState),
861 ( no_before_state(OpName) -> BState=BState1
862 ; state_for_op(OpName,BeforeState,BState0)
863 -> extract_variables_from_state_as_primed(BState0,PrimedBefore),
864 append(PrimedBefore,BState1,BState)
865 ; add_warning(specfile,'Could not get initialised before state for: ',StateId),
866 BState = BState1
867 ),
868 % TODO: use NextId and add StateId as $0 vars
869 % maplist(create_primed_binding,InVariablesState,PrimedVars),
870 % append(PrimedVars,Out,PredState),
871 !,
872 get_local_state_for_operation_transition(OpName,OpTerm,LocalState),
873 %write(local_state(OpName,OpTerm,LocalState)),nl,
874 % TODO: process errors better
875 if(b_compute_explicit_epression_no_wf(TemplateStringExpr,LocalState,BState,string(SD),
876 operation_description(OpName),0),
877 Desc=SD,
878 (get_operation_description(OpTerm,D) -> ajoin(['Error evaluating template: ',D],Desc)
879 ; % no plain description pragma: we must not fail after the cut above
880 ajoin(['Error evaluating description template for ',OpName],Desc))
881 ).
882 get_operation_description_for_transition(_StateId,OpTerm,_NextStateId,Desc) :-
883 get_operation_description(OpTerm,Desc).
884
885 state_for_op('$setup_constants',State,BState) :- !,
886 state_corresponds_to_set_up_constants(State,BState).
887 state_for_op(_,State,BState) :-
888 state_corresponds_to_initialised_b_machine(State,BState).
889
890 no_before_state('$setup_constants').
891 no_before_state('$initialise_machine').
892
893 get_local_state_for_operation_transition(OpName,OperationTerm,LocalState) :-
894 get_local_states_for_operation_transition(OpName,OperationTerm,ParaStore,ResultStore),
895 append(ResultStore,ParaStore,LocalState).
896
897 % get local states for parameter values and result values extracted from a B operation transition term
898 get_local_states_for_operation_transition(OpName,OperationTerm,ParaStore,ResultStore) :- b_or_z_mode,!,
899 get_operation_return_values_and_arguments(OperationTerm,ReturnValues,ParaValues),
900 (b_get_machine_operation_for_animation(OpName,Results,Parameters,_Body,_OType,_TopLevel,OpPos)
901 -> true
902 ; is_setup_constants_internal_name(OpName) -> Results=[], Parameters=[], OpPos=unknown
903 ; add_error(specfile,'Unrecognized operation: ',OpName),
904 Results=[], Parameters=[], OpPos=unknown
905 ),
906 def_get_texpr_ids(Parameters,OpParameterNames), % also includes virtual parameters (show_eventb_any_arguments)
907 (ParaValues=[]
908 -> ParaStore=[]
909 ; (OpParameterNames=[], ParaValues=[_|_]
910 -> (get_preference(show_eventb_any_arguments,true)
911 -> add_message(specfile,'Ignoring additional parameters for:',OpName,OpPos)
912 ; add_warning(specfile,'Ignoring additional parameters for:',OpName,OpPos)
913 ),
914 ParaStore = []
915 ; create_local_store_for_operation(OpParameterNames,ParaValues,OpName,ParaStore)
916 )
917 ),
918 (ReturnValues=[]
919 -> ResultStore = []
920 ; %b_get_machine_operation_result_names(OpName,OpResults),
921 def_get_texpr_ids(Results,OpResults),
922 create_local_store_for_operation(OpResults,ReturnValues,OpName,ResultStore)
923 ).
924 get_local_states_for_operation_transition(OpName,OperationTerm,ParaStore,ResultStore) :- xtl_mode,
925 xtl_transition_parameters(OpName,ParaNames), !, % user-defined parameters found, otherwise use default params below
926 get_operation_return_values_and_arguments(OperationTerm,_ReturnValues,ParaValues), % ReturnValues should always be []
927 ResultStore = [],
928 maplist(gen_id_bind,ParaNames,ParaValues,ParaStore).
929 get_local_states_for_operation_transition(_OpName,OperationTerm,ParaStore,ResultStore) :- % not B mode or no user-defined params available in XTL mode
930 ResultStore = [],
931 get_operation_return_values_and_arguments(OperationTerm,ReturnValues,ParaValues),
932 ReturnValues = [], % should always succeed
933 gen_para_bind(ParaValues,1,ParaStore). % create [bind(para1,Val1),...]
934
935 gen_id_bind(ID,Val,bind(ID,Val)).
936
937 gen_para_bind([],_,[]).
938 gen_para_bind([H|T],Nr,[bind(ParaNr,H)|BT]) :- ajoin([para,Nr],ParaNr),
939 N1 is Nr+1,
940 gen_para_bind(T,N1,BT).
941
942 % create a store from parameter names and values:
943 create_local_store_for_operation([],[],_,Store) :- !, Store=[].
944 create_local_store_for_operation([Name|NT],[Val|VT],OpName,[bind(Name,Val)|StoreT]) :- !,
945 create_local_store_for_operation(NT,VT,OpName,StoreT).
946 create_local_store_for_operation([Name|NT],[],OpName,Store) :- !,
947 ajoin(['Missing values for operation ',OpName,':'],Msg),
948 add_error(specfile,Msg,[Name|NT]),
949 Store = [].
950 create_local_store_for_operation(_,V,OpName,Store) :-
951 ajoin(['Too many values for operation ',OpName,':'],Msg),
952 add_error(specfile,Msg,V),
953 Store = [].
954
955 :- use_module(library(between),[between/3]).
956
957 b_trans(State,OpName,Operation,NewState,PathInfo) :-
958 ? compute_operation_effect_max(State,OpName,Operation,NewState,PathInfo,_Max).
959
960 :- use_module(store,[store_updates_and_normalise/3]).
961
962 compute_operation_effect_max([],OpName,Operation,NewState,PathInfo,Max) :-
963 ? compute_operation_updates_on_expanded_store([],OpName,Operation,Updates,PathInfo,Max),
964 store_updates_and_normalise(Updates,[],NewState).
965 compute_operation_effect_max([H|T],OpName,Operation,NewState,PathInfo,Max) :-
966 compute_operation_updates_on_expanded_store([H|T],OpName,Operation,Updates,PathInfo,Max),
967 store_updates_and_normalise(Updates,[H|T],NewState).
968 compute_operation_effect_max(const_and_vars(ConstID,Vars),OpName,Operation,ResultingStore,PathInfo,Max) :-
969 prepare_state_for_specfile_trans(const_and_vars(ConstID,Vars),unknown,R),
970 %print(expanded_const_and_vars(ConstID)),nl,
971 compute_operation_effect_max(R,OpName,Operation,ResultingStore,PathInfo,Max).
972 compute_operation_effect_max(expanded_const_and_vars(ID,Vars,FullStore,Infos),OpName,Operation,NewState,PathInfo,Max) :-
973 ? compute_operation_updates_on_expanded_store(expanded_const_and_vars(ID,Vars,FullStore,Infos),
974 OpName,Operation,Updates,PathInfo,Max),
975 NewState = const_and_vars(ID,NewVars),
976 store_updates_and_normalise(Updates,Vars,NewVars).
977 compute_operation_effect_max(expanded_vars(FullStore,Infos),OpName,Operation,NewState,PathInfo,Max) :-
978 ? compute_operation_updates_on_expanded_store(expanded_vars(FullStore,Infos),
979 OpName,Operation,Updates,PathInfo,Max),
980 store_updates_and_normalise(Updates,FullStore,NewState).
981
982 compute_operation_effect_max(concrete_constants(ConstantsStore),'$initialise_machine',OpInit,ResultingStore,PathInfo,Max) :-
983 get_max_enablings_for_init(Max,'$initialise_machine',MaxForCall),
984 ? succeed_max_call_id('$initialise_machine',
985 b_interpreter:b_initialise_machine(ConstantsStore,InitialVars,InitialStore,PathInfo),MaxForCall),
986 create_initialisation_operation(InitialVars,OpInit),
987 (\+ preferences:preference(symmetry_mode,flood), /* TO DO : improve permutation to allow using const_and_vars optimisation */
988 ? visited_expression(ConstID,concrete_constants(ConstantsStore))
989 -> ResultingStore = const_and_vars(ConstID,InitialVars) /* avoid storing constant values again */
990 ; ResultingStore = InitialStore).
991 compute_operation_effect_max(root,'$setup_constants',OpSetup,concrete_constants(FilteredConstantsStore),[],Max) :-
992 b_machine_has_constants_or_properties,
993 get_max_enablings_for_init(Max,'$setup_constants',MaxForCall),
994 ? compute_constants(ConstantsStore,MaxForCall,Complete),
995 create_setup_constants_operation(ConstantsStore,Complete,OpSetup),
996 %%print_message('FOUND_CONSTANTS'(OpName)),
997 (get_preference(filter_unused_constants,true) % possibly disable if VisB or Custom Graph Definitions exist
998 -> exclude(unused_binding,ConstantsStore,FilteredConstantsStore)
999 ; FilteredConstantsStore=ConstantsStore).
1000 compute_operation_effect_max(root,'$initialise_machine',OpInit,InitialStore,PathInfo,Max) :-
1001 \+ b_machine_has_constants_or_properties,
1002 get_max_enablings_for_init(Max,'$initialise_machine',MaxForCall),
1003 empty_state(EmptyState),
1004 succeed_max_call_id('$initialise_machine',
1005 b_interpreter:b_initialise_machine(EmptyState,InitialVars,InitialStore,PathInfo),MaxForCall),
1006 create_initialisation_operation(InitialVars,OpInit).
1007
1008
1009 unused_binding(bind(C,_)) :- bmachine:b_is_unused_constant(C).
1010
1011 compute_constants(ConstantsStore,_Max,C) :-
1012 % check if values have been computed and stored into a file before -- if yes, use them
1013 load_constants(Stores,MaxReached),!,
1014 C = complete_properties, % TODO: check that only complete properties stored or that we store this info
1015 choose_loaded_solution(Stores,ConstantsStore,MaxReached).
1016 compute_constants(ConstantsStore,Max,Complete) :-
1017 % print('Searching for valid CONSTANTS'),nl,!,
1018 ? succeed_max_call_id('$setup_constants',b_interpreter:b_set_up_concrete_constants(ConstantsStore,Complete),Max).
1019
1020 choose_loaded_solution(Stores,ConstantsStore,_MaxReached) :-
1021 member(ConstantsStore,Stores).
1022 choose_loaded_solution(_Stores,_ConstantsStore,true) :-
1023 % provoke the setting of the "maximum reached flag"
1024 succeed_max_call_id('$setup_constants',member(_,_),1),fail.
1025
1026 expand_const_and_vars_to_full_store(root,R) :- !,R=[].
1027 expand_const_and_vars_to_full_store(concrete_constants(ConstantsStore),FullStore) :- !,
1028 ConstantsStore = FullStore.
1029 expand_const_and_vars_to_full_store(const_and_vars(ConstID,Vars),FullStore) :- !,
1030 expand_const_and_vars(ConstID,Vars,FullStore).
1031 expand_const_and_vars_to_full_store(expanded_const_and_vars(_ConstID,_Vars,ExpStore,_),FullStore) :- !,
1032 FullStore = ExpStore.
1033 expand_const_and_vars_to_full_store(expanded_vars(ExpStore,_),FullStore) :- !,
1034 FullStore = ExpStore.
1035 expand_const_and_vars_to_full_store(csp_and_b(_,BState),FullStore) :- !,
1036 expand_const_and_vars_to_full_store(BState,FullStore).
1037 expand_const_and_vars_to_full_store(R,R).
1038
1039 expand_const_and_vars(ConstID,Vars,FullStore) :-
1040 visited_expression(ConstID,concrete_constants(ConstantsStore)),
1041 %% This can take a while if the Constants are big !
1042 % print('exp: '),tools_printing:print_term_summary(ConstantsStore),nl,
1043 append(Vars,ConstantsStore,FullStore). % used to be Constants first, now consistent with b_initialise_machine2,
1044 % b_initialise_machine2 puts constants at end to make sharing of complete tail-list easier for successor states
1045
1046 expand_to_constants_and_variables(root,[],[]).
1047 expand_to_constants_and_variables(concrete_constants(ConstStore),ConstStore,[]).
1048 expand_to_constants_and_variables(const_and_vars(ConstID,VarStore),ConstStore,VarStore) :-
1049 visited_expression(ConstID,concrete_constants(ConstStore)).
1050 expand_to_constants_and_variables([],[],[]).
1051 expand_to_constants_and_variables([H|T],[],[H|T]).
1052 expand_to_constants_and_variables(csp_and_b(_,BState),ConstStore,VarStore) :-
1053 expand_to_constants_and_variables(BState,ConstStore,VarStore).
1054
1055 :- use_module(extrasrc(b_operation_cache),[compute_operation_on_expanded_store_cache/6]).
1056
1057 :- use_module(bmachine,[b_machine_temp_predicate/1]).
1058 % compute operation effect on a store where all constants have been put into the environment
1059 compute_operation_updates_on_expanded_store(InState,OpName,Operation,NewState,PathInfo,Max) :-
1060 get_preference(operation_reuse_setting,V), V \= false,
1061 b_or_z_mode, % now works with CSP || B
1062 \+ b_machine_temp_predicate(_), % otherwise caching may not respect the additional guard; relevant for Rodin Execute with additional Guard Constraint command
1063 %\+ animation_minor_mode(eventb), % projection used to confuse Event-B interpreter, see test 2138
1064 !,
1065 get_max_enablings_per_operation(Max,OpName,maxNrOfEnablingsPerOperation,MaxForCall),
1066 ? compute_operation_on_expanded_store_cache(OpName,Operation,InState,NewState,PathInfo,MaxForCall).
1067 compute_operation_updates_on_expanded_store(Store,OpName,Operation,Updates,PathInfo,Max) :-
1068 extract_full_store(Store,FullStore),!,
1069 compute_operation_on_expanded_store2(FullStore,OpName,Operation,Updates,PathInfo,Max).
1070 compute_operation_updates_on_expanded_store(InState,OpName,Operation,Updates,PathInfo,Max) :-
1071 ? compute_operation_on_expanded_store2(InState,OpName,Operation,Updates,PathInfo,Max).
1072
1073 compute_operation_on_expanded_store2(InState,OpName,Operation,Updates,PathInfo,Max) :-
1074 get_max_enablings_per_operation(Max,OpName,maxNrOfEnablingsPerOperation,MaxForCall),
1075 ? succeed_max_call_id(OpName,
1076 b_interpreter:b_execute_top_level_operation_update(OpName,Operation,InState,Updates,PathInfo),
1077 MaxForCall).
1078
1079 extract_full_store(expanded_const_and_vars(_,_,FS,_),Res) :- !, Res=FS.
1080 extract_full_store(expanded_vars(FS,_),Res) :- !, Res=FS.
1081
1082 create_initialisation_operation(_InitialVars,'$initialise_machine').
1083
1084 create_setup_constants_operation(_ConstantVars,complete_properties,R) :- !, R='$setup_constants'.
1085 create_setup_constants_operation(_ConstantVars,_,'$partial_setup_constants').
1086
1087 :- use_module(bmachine,[b_get_machine_operation_max/2]). % MAX_OPERATIONS_... DEFINITIONS
1088 get_max_enablings_per_operation_aux(OpName,RMax,_,RandomisedRestart) :-
1089 % TODO: decompose prefixed operation names with . write(get_mach_op_max(OpName)),nl,
1090 b_get_machine_operation_max(OpName,Max),
1091 !,
1092 (Max<0 -> RMax is -Max, RandomisedRestart=true ; RMax=Max, RandomisedRestart=false).
1093 get_max_enablings_per_operation_aux(_OpName,Max,DefaultPref,false) :-
1094 preferences:preference(DefaultPref,Max).
1095
1096 max_operations_zero_for_operation(OpName) :-
1097 b_or_z_mode,
1098 b_top_level_operation(OpName),
1099 (b_get_machine_operation_max(OpName,0) -> true
1100 ; get_preference(maxNrOfEnablingsPerOperation,0)).
1101
1102 max_operations_zero_for_setup_constants :-
1103 max_operations_zero_for_init_step('$setup_constants').
1104 max_operations_zero_for_initialisation :-
1105 max_operations_zero_for_init_step('$initialise_machine').
1106
1107 % the DEFINITIONS MAX_OPERATIONS_SETUP_CONSTANTS and MAX_OPERATIONS_INITIALISATION are stored
1108 % under the translated names SETUP_CONSTANTS and INITIALISATION (as in get_max_enablings_for_init)
1109 max_operations_zero_for_init_step(InternalName) :-
1110 b_or_z_mode,
1111 translate_operation_name(InternalName,OpName),
1112 (b_get_machine_operation_max(OpName,0) -> true
1113 ; get_preference(maxNrOfInitialisations,0)).
1114
1115 get_max_enablings_per_operation(Max,OpName,DefaultPref,MaxForCall) :-
1116 (var(Max) -> get_max_enablings_per_operation_aux(OpName,Max,DefaultPref,RandomisedRestart)
1117 ; RandomisedRestart=false),
1118 (RandomisedRestart=true,
1119 get_preference(randomise_enumeration_order,true)
1120 -> % we succeed Max times with the value 1, thus forcing a randomised restart
1121 MaxForCall=1, between(1,Max,Retry), format('Randomised Restart ~w for ~w~n',[Retry,OpName])
1122 ; % we succeed once with the value Max
1123 MaxForCall is Max).
1124
1125 get_max_enablings_for_init(Max,OpName,MaxForCall) :-
1126 translate_operation_name(OpName,IOp),
1127 get_max_enablings_per_operation(Max,IOp,maxNrOfInitialisations,MaxForCall).
1128
1129 get_specific_max_operations_for_operation(OpName,MaxOp) :-
1130 b_or_z_mode,
1131 b_get_machine_operation_max(OpName,MaxOp).
1132
1133 :- use_module(bmachine,[b_get_machine_operation_time_out/2]).
1134 get_specific_time_out_for_operation(OpName,TimeOut) :-
1135 b_or_z_mode,
1136 b_get_machine_operation_time_out(OpName,TimeOut).
1137
1138 /* PROPERTIES */
1139
1140 property(const_and_vars(ConstID,Vars),Property) :- b_or_z_mode,
1141 expand_const_and_vars(ConstID,Vars,FullStore),!,
1142 property(FullStore,Property).
1143 property(csp_and_b(CSPState,BState),Property) :- animation_mode(csp_and_b),!,
1144 %% print(checking_csp_and_b(CSPState)),nl,
1145 expand_const_and_vars_to_full_store(BState,FBState),
1146 (xtl_interface:cspm_property(CSPState,Property) ;
1147 b_property(FBState,Property)).
1148 property(State,Property) :- animation_mode(AM),
1149 (AM = b -> b_property(State,Property)
1150 ; AM=xtl -> xtl_interface:xtl_property(State,Property)
1151 ; AM=cspm -> xtl_interface:cspm_property(State,Property)
1152 ; AM=csp_and_b -> b_property(State,Property) % State should be root
1153 ).
1154
1155 %b_property(const_and_vars(ConstID,Vars),Prop) :- !,
1156 % expand_const_and_vars(ConstID,Vars,FullStore),
1157 % b_property(FullStore,Prop).
1158 b_property(root,Prop) :- !, b_preference_par(PARA,Op,VAL), Prop=.. [Op,PARA,VAL].
1159 b_property([H|T],non_ground(Var)) :- debug:debug_mode(on),
1160 member(bind(Var,Val),[H|T]),\+(ground(Val)).
1161 b_property([H|T],Prop) :- elementary_prop([H|T],Prop).
1162 b_property([],Prop) :- elementary_prop([],Prop).
1163 b_property(concrete_constants(DB),Prop) :- elementary_prop(DB,Prop).
1164
1165
1166
1167 :- use_module(preferences).
1168 :- use_module(b_global_sets,[b_global_set/1, b_fd_card/2,inferred_minimum_global_set_cardinality/2,
1169 inferred_maximum_global_set_cardinality/2, unfixed_deferred_set/1]).
1170
1171 % preference values shown as properties of the root node
1172 b_preference_par('MAXINT','=',MaxInt) :- get_preference(maxint,MaxInt).
1173 b_preference_par('MININT','=',MinInt) :- get_preference(minint,MinInt).
1174 b_preference_par(card(GlobalSet),Op,Card) :-
1175 b_global_set(GlobalSet),
1176 b_fd_card(GlobalSet,RCard),
1177 % provide feedback if unfixed_deferred_set
1178 (inferred_maximum_global_set_cardinality(GlobalSet,MaxCard),
1179 (inferred_minimum_global_set_cardinality(GlobalSet,MinCard) -> true ; MinCard=1),
1180 MinCard \= MaxCard,
1181 Op = ':', Card = '..'(MinCard,MaxCard)
1182
1183 ;
1184
1185 inferred_minimum_global_set_cardinality(GlobalSet,MinCard),
1186 \+ (inferred_maximum_global_set_cardinality(GlobalSet,_)),
1187 Op='>=',Card=MinCard % show minimum cardinality info
1188
1189 ;
1190
1191 Op= '=', (unfixed_deferred_set(GlobalSet)
1192 -> ajoin([RCard,' (assumed for deferred set)'],Card) ; Card=RCard)
1193 ).
1194
1195 /* --------------------------------------------------------- */
1196
1197
1198 elementary_prop(DB,Prop) :-
1199 member(bind(Var,Val),DB), %print(var_prop(Var)),nl_time,
1200 elementary_prop3(Val,Var,Prop).
1201
1202 :- use_module(custom_explicit_sets,[is_avl_relation/1,is_avl_partial_function/1,get_first_avl_elements/4, avl_approximate_size/3]).
1203 elementary_prop3(Val,Var,Prop) :- var(Val),!, Prop = '='(Var,'_VAR_').
1204 elementary_prop3(avl_set(Avl),Var,Prop) :- !,
1205 (show_avl_set(Avl) % \+ custom_explicit_sets:is_avl_sequence(Avl))
1206 -> elementary_fun_prop(avl_set(Avl),Var,Prop)
1207 ; elementary_var_prop(avl_set(Avl),Var,Prop)).
1208 elementary_prop3([H|T],Var,Prop) :- H=(_,_),
1209 get_preference(show_function_tuples_in_property,true),!,
1210 elementary_fun_prop([H|T],Var,Prop).
1211 elementary_prop3(Val,Var,Prop) :- elementary_var_prop(Val,Var,Prop).
1212
1213 show_avl_set(AVL) :- get_preference(show_function_tuples_in_property,true),
1214 is_avl_relation(AVL),
1215 avl_approximate_size(AVL,0,Size), Size < 257, % only the first 30 will be shown by get_relation_element anyway
1216 is_avl_partial_function(AVL).
1217
1218
1219 :- use_module(bmachine,[b_is_variable/2]).
1220 elementary_var_prop(Val,Var,Prop) :-
1221 % TODO: use translate:translate_bvalue_for_expression/3
1222 animation_minor_mode(tla),!,
1223 ( identifier_has_tla_type(Var,TlaType) ->
1224 translate_bvalue_with_tlatype(Val,TlaType,Text)
1225 ;
1226 translate_bvalue(Val,Text)),
1227 Prop = '='(Var,Text).
1228 elementary_var_prop(Val,Var,Prop) :-
1229 (var_cst_type(Var,Type)
1230 -> translate_bvalue_with_type_and_limit(Val,Type,320,Text)
1231 ; translate_bvalue_with_type_and_limit(Val,any,320,Text)), % translate_properties_with_limit uses 320 Limit
1232 Prop = '='(Var,Text).
1233
1234 identifier_has_tla_type(Id,Type) :-
1235 get_texpr_id(TId,Id),
1236 (b_get_machine_constants(Ids)
1237 ;b_get_machine_variables(Ids)),
1238 member(TId,Ids),!,
1239 get_texpr_info(TId,Infos),
1240 memberchk(tla_type(Type),Infos).
1241 % to do: use b_is_variable + translate_bvalue_with_type
1242 elementary_fun_prop(Val,Var,Prop) :-
1243 simple(Prop), % will only succeed if Prop is var or atom; translate_bexpression will raise error if Prop is compound
1244 nonvar(Val),
1245 (var_cst_type(Var,Type),dom_range_type(Type,Dom,Ran) -> true ; Type=any,Dom=any, Ran=any),
1246 get_relation_element(Val,X,Y), % Show value in form: Var(X) = Y
1247 create_texpr(identifier(Var),Type,[],Fun),
1248 create_value(X,Dom,Arg),
1249 create_texpr(function(Fun,Arg),Ran,[],Lhs),
1250 create_value(Y,Ran,Rhs),
1251 create_texpr(equal(Lhs,Rhs),pred,[],Expr),
1252 translate_bexpression_with_limit(Expr,80,Prop).
1253
1254 var_cst_type(Name,Type) :- (b_is_variable(Name,Type) ; b_is_constant(Name,Type)).
1255 dom_range_type(seq(R),integer,R).
1256 dom_range_type(set(couple(D,R)),D,R).
1257
1258 get_relation_element(avl_set(A),X,Y) :- custom_explicit_sets:is_avl_relation(A),
1259 !, %preferences:preference(expand_avl_upto,Limit),
1260 Limit=30, % reduced limit, as now we can inspect with evaluation view
1261 get_first_avl_elements(A,Limit,Els,CutOff),
1262 (member((X,Y),Els), X\=term(_)
1263 ;
1264 CutOff=not_all, X='...', Y='...').
1265 get_relation_element(Value,X,Y) :-
1266 member((X,Y),Value). /* otherwise we have a record structure */
1267
1268 create_value('...',_,R) :- !, R = b(string('...'),string,[]).
1269 create_value(X,T,R) :-
1270 create_texpr(value(X),T,[],R).
1271
1272
1273 :- use_module(bmachine,[b_show_machine_representation/4]).
1274 :- use_module(probcspsrc(haskell_csp_analyzer),[get_internal_csp_representation/1]).
1275 :- use_module(seqproversrc(sequent_prover_exports),[pretty_print_pos/1]).
1276 get_internal_representation(X) :- get_internal_representation(X,true,false,none).
1277 get_internal_representation(X,AdditionalInfo,UnsetMinorMode,Typing) :- animation_mode(AM),
1278 get_internal_aux(AM,X,AdditionalInfo,UnsetMinorMode,Typing).
1279 get_internal_aux(b,X,AdditionalInfo,UnsetMinorMode,Typing) :- !,
1280 b_show_machine_representation(X,AdditionalInfo,UnsetMinorMode,Typing).
1281 % UnsetMinorMode=true means we try and generate classical B syntax
1282 get_internal_aux(cspm,X,_,_,_) :- !, get_internal_csp_representation(X).
1283 get_internal_aux(csp_and_b,Res,AdditionalInfo,UnsetMinorMode,Typing) :- !,
1284 append("CSP||B - B Part only",X,Res),
1285 b_show_machine_representation(X,AdditionalInfo,UnsetMinorMode,Typing).
1286 get_internal_aux(xtl,Res,_,_,_) :- animation_minor_mode(sequent_prover), !,
1287 pretty_print_pos(Res).
1288 get_internal_aux(_,X,_,_,_) :- X="Internal representation only available in B or CSP mode".
1289
1290
1291 :- use_module(probcspsrc(haskell_csp),[channel/2]).
1292 :- use_module(bmachine,[b_top_level_operation/1, b_top_level_feasible_operation/1]).
1293 get_possible_event(OpName) :- b_or_z_mode,
1294 %b_is_operation_name(OpName).
1295 ? b_top_level_operation(OpName).
1296 get_possible_event(Channel) :- csp_mode,
1297 channel(Channel,_).
1298 % what about csp_and_b mode ?
1299
1300 get_feasible_event(OpName) :- b_or_z_mode,
1301 %b_is_operation_name(OpName).
1302 ? b_top_level_feasible_operation(OpName).
1303 get_feasible_event(Channel) :- csp_mode,
1304 channel(Channel,_).
1305
1306
1307 get_possible_language_specific_top_level_event(OpName,ResultNames,ParameterNames) :-
1308 b_or_z_mode,!,
1309 get_possible_b_top_level_event(OpName,ResultNames,ParameterNames).
1310 get_possible_language_specific_top_level_event(TransName,[],ParameterNames) :- xtl_mode, !,
1311 xtl_transition_parameters(TransName,ParameterNames).
1312 get_possible_language_specific_top_level_event(Channel,unknown,unknown) :- csp_mode,
1313 channel(Channel,_).
1314
1315 get_possible_b_top_level_event('$setup_constants',[],Ids) :-
1316 b_machine_has_constants_or_properties,
1317 b_get_machine_constants(TIds), maplist(get_texpr_id,TIds,Ids).
1318 get_possible_b_top_level_event('$initialise_machine',[],Ids) :-
1319 b_get_machine_variables(TIds), maplist(get_texpr_id,TIds,Ids).
1320 get_possible_b_top_level_event(OpName,ResultNames,ParameterNames) :-
1321 b_top_level_operation(OpName),
1322 %b_get_machine_operation(OpName,Results,RealParameters,_RealBody,_OType,_OpPos),
1323 b_get_machine_operation_for_animation(OpName,Results,AllParameters,_RealBody,_OType,_TL),
1324 maplist(get_texpr_id,Results,ResultNames),
1325 maplist(get_texpr_id,AllParameters,ParameterNames).
1326
1327 % obtain a textual description of specification category names
1328 get_specification_description(Category,Name) :-
1329 animation_mode(Mode),
1330 (animation_minor_mode(Minor) -> true ; Minor = none), % there could be several minor modes !
1331 (get_specific_description(Mode,Minor,Category,N) -> Name=N
1332 ; get_default_description(Category,N) -> Name = N
1333 ; add_error(get_specification_description,'Unknown category: ',Category),
1334 Name=Category).
1335
1336 get_specific_description(cspm,_,C,N) :- get_csp_description(C,N).
1337 get_specific_description(b,eventb,C,N) :- get_eventb_description(C,N).
1338 get_specific_description(b,tla,C,N) :- get_tla_description(C,N).
1339 get_specific_description(b,alloy,C,N) :- get_alloy_description(C,N).
1340
1341 get_csp_description(operations,'CHANNELS').
1342 get_csp_description(operation,'CHANNEL').
1343 get_csp_description(assertions,'CSP_ASSERTIONS').
1344 get_csp_description(machine,'CSP_SPECIFICATION').
1345 get_csp_description(operations_lc,'events').
1346
1347 get_tla_description(properties,'ASSUME').
1348 get_tla_description(machine,'MODULE').
1349 get_tla_description(operation,'ACTION'). % no key word
1350 get_tla_description(operations,'ACTIONS'). % no key word
1351 get_tla_description(operations_lc,'actions').
1352
1353 get_eventb_description(properties,'AXIOMS').
1354 get_eventb_description(assertions,'THEOREMS').
1355 get_eventb_description(operations,'EVENTS').
1356 get_eventb_description(operation,'EVENT').
1357 get_eventb_description(operations_lc,'events').
1358 get_eventb_description(machine,'MODEL').
1359
1360 get_alloy_description(machine,'MODULE').
1361 get_alloy_description(sets,'SIGNATURES'). % abstract
1362 get_alloy_description(operations,'RUNS').
1363 get_alloy_description(operation,'RUN').
1364
1365 get_default_description(constraints,'CONSTRAINTS').
1366 get_default_description(properties,'PROPERTIES').
1367 get_default_description(assertions,'ASSERTIONS').
1368 get_default_description(invariant,'INVARIANT').
1369 get_default_description(invariants,'INVARIANTS').
1370 get_default_description(operations,'OPERATIONS').
1371 get_default_description(operation,'OPERATION').
1372 get_default_description(operations_lc,'operations').
1373 get_default_description(machine,'MACHINE').
1374 get_default_description(constants,'CONSTANTS').
1375 get_default_description(variables,'VARIABLES').
1376 get_default_description(initialisation,'INITIALISATION').
1377 get_default_description(sets,'SETS').
1378 get_default_description(goal,'GOAL').
1379 get_default_description(definition,'DEFINITION').
1380 get_default_description(definitions,'DEFINITIONS').
1381 get_default_description(variants,'VARIANTS').