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