1 % (c) 2009-2024 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(store, [ %store_values_for_existing_ids/4,
6 store_value_for_existing_id/4,
7 variable_is_defined/2,
8 store_value/4,
9 store_updates_and_normalise/3,
10 store_intermediate_updates_wf/5,
11 save_updates_in_existing_store/2,
12 %lazy_store_updates_and_normalise/3,
13 merge_updates/3,
14 get_id_value/3,
15 lookup_value/3,
16 lookup_value/4,lookup_value_with_span_wf/6,
17 lookup_value_without_check/4,
18 lookup_value_for_existing_id/3, lookup_value_for_existing_id/4,
19 lookup_value_for_existing_id_wf/4, lookup_value_for_existing_id_wf/5,
20 lookup_and_delete_value/7,
21 no_value_for_variable/2,
22 empty_state/1,
23 add_var_to_localstate/4,
24 set_up_localstate/4,
25 set_up_undefined_localstate/3, set_up_undefined_localstate_with_predefined/4,
26 delete_variables_from_state/3,
27
28 normalise_value_for_var/3, normalise_value_for_var/4,
29 normalise_value/2,
30 l_normalise_values/2,
31 l_expand_and_normalise_values/2, l_expand_and_normalise_values/3,
32 normalise_store/2,
33 normalise_state/2, normalise_states/2,
34 copy_variable_store/4,
35 check_valid_store/2
36 ]).
37
38 :- use_module(library(lists)).
39 :- use_module(self_check).
40 :- use_module(debug).
41
42 :- use_module(typechecker).
43 :- use_module(preferences).
44 :- use_module(tools).
45
46 :- use_module(bsyntaxtree).
47
48 :- use_module(error_manager).
49
50 :- use_module(bmachine, [b_is_constant/1, constant_variable_marked_as_expand/1]).
51
52 :- type variable_id +--> (lambda_res(integer) ; atomic).
53 :- type binding +--> bind(variable_id,any).
54 :- type store +--> list(binding).
55
56 :- use_module(tools).
57 :- use_module(module_information).
58 :- module_info(group,interpreter).
59 :- module_info(description,'This module takes care of storing B values in an environment and normalising values.').
60
61 /* --------------------------------------------------------- */
62
63 /* store and lookup of values */
64
65
66
67 variable_is_defined(Var,Store) :- safe_member(bind(Var,_),Store,variable_is_defined).
68
69 store_value(Var,Value,Old,New) :- store_value2(Old,Var,Value,New).
70 store_value2([],Var,Value,[bind(Var,Value)]).
71 store_value2([bind(V,VV)|T],Var,Value,[bind(V,NValue)|UT]) :-
72 ( Var=V ->
73 NValue=Value, UT=T
74 ;
75 NValue=VV, store_value2(T,Var,Value,UT)).
76
77
78 store_value_for_existing_id(L,ID,V,L2) :- var(ID),!,
79 add_internal_error('VarID is var !!',store_value_for_existing_id(ID,V,L,L2)),fail.
80 store_value_for_existing_id(L,ID,V,L2) :- store_value_for_existing_id_aux(L,ID,V,L2).
81 store_value_for_existing_id_aux([],Var,Value,[bind(Var,Value)]) :-
82 add_error(store_value_for_existing_id,'Variable does not exist in store: ',Var).
83 store_value_for_existing_id_aux([bind(V,VV)|T],Var,Value,[bind(V,VV2)|UT]) :-
84 %we could use: arg(1,BIND,V), arg(2,BIND,VV),
85 (Var=V -> VV2=Value, UT=T
86 ; VV2=VV,store_value_for_existing_id_aux(T,Var,Value,UT)).
87
88
89
90
91
92 /* store_intermediate_updates_wf(InState,Updates,OutStates,WF) */
93 /* rewrote it so that it copies state skeleton even if updates are not yet known */
94
95 :- assert_must_succeed((store:store_intermediate_updates_wf([bind(y,1),bind(x,2),bind(a,22)],[bind(x,33)],R,unit_test1,no_wf_available),R==[bind(y,1),bind(x,33),bind(a,22)])).
96 :- assert_must_succeed((store:store_intermediate_updates_wf([bind(Y,1),bind(X,2),bind(A,22)],[bind(x,33)],R,unit_test2,no_wf_available),X=x,A=a,Y=y,R==[bind(y,1),bind(x,33),bind(a,22)])).
97
98
99
100 store_intermediate_updates_wf(InStore,Update,OutStore,PP,WF) :- Update==[], !,
101 copy_store(InStore,OutStore,PP,WF).
102 store_intermediate_updates_wf(InStore,Update,OutStore,PP,WF) :-
103 store_intermediate_updates2(InStore,Update,OutStore,ctxt(PP,InStore,Update),WF).
104
105 :- block store_intermediate_updates2(-,?,?,?,?), store_intermediate_updates2(?,-,?,?,?).
106 store_intermediate_updates2([],Updates,[],PP,_WF) :-
107 store_intermediate_updates3(Updates,PP).
108 store_intermediate_updates2([bind(Var,OldVal)|InT],Updates,[bind(Var,NewVal)|OutT],PP,WF) :-
109 %we could do: arg(1,BIND,Var), arg(2,BIND,OldVal),
110 % TO DO: maybe it would be better to copy Update variables as they come in rather than forcing the order as in the store
111 ? search_and_delete_value(Var,OldVal,NewVal,Updates,NUpdates),
112 (NUpdates == []
113 -> copy_store(InT,OutT,PP,WF)
114 ? ; store_intermediate_updates2(InT,NUpdates,OutT,PP,WF)
115 ).
116
117 :- use_module(kernel_objects,[equal_object_wf/4]).
118 copy_store(In,Out,_,_) :- var(Out),!, Out=In.
119 copy_store(In,Out,PP,_) :- var(In),!, add_warning(store,'In store skeleton not set up:',In,PP), Out=In.
120 copy_store([],[],_PP,_) :- !.
121 copy_store([bind(Var1,InVal)|T1],[bind(Var2,OutVal)|T2],PP,WF) :- !,
122 (Var1=Var2
123 ? -> equal_object_wf(InVal,OutVal,copy_store,WF)
124 % usually InVal==OutVal, but in test 1043 became false after WF0 was no longer grounded inside SELECT/PRE
125 ; add_error(copy_store,'Variable mismatch:',Var1:Var2,PP), fail),
126 copy_store(T1,T2,PP,WF).
127 copy_store(S1,S2,PP,_) :-
128 check_valid_store(S1,PP), check_valid_store(S2,PP),
129 add_error(copy_store,'Illegal stores:',S1:S2,PP),fail.
130
131 :- use_module(library(ordsets),[ord_intersection/3]).
132 :- block store_intermediate_updates3(-,?).
133 store_intermediate_updates3(Updates,PP) :-
134 ( Updates = [] -> true
135 %; Updates = [bind(abort__flag__info,_)] -> true
136 ; PP = ctxt(SpanInfo,InStore,FullUpdate),
137 % TO DO: determine if illegal assignments, which do not occur in InStore
138 get_ids_from_store(InStore,_,LegalIds),
139 get_ids_from_store(FullUpdate,WrIDs,_WrittenIds),
140 get_ids_from_store(Updates,_,IllegalIds),
141 ord_intersection(IllegalIds,LegalIds,DoubleIds),
142 (DoubleIds \= [] % at least some of the assignments are legal, hence must be double
143 -> add_error(store_intermediate_updates,'Double assignments:',
144 DoubleIds:(illegal_updates(Updates),written_ids(WrIDs)),SpanInfo)
145 % for: get_preference(try_operation_reuse,true/full) we now add term(undefined) for all variables written but not read
146 ; add_error(store_intermediate_updates,'Illegal assignments:',
147 IllegalIds:(illegal_updates(Updates),legal_ids(LegalIds),written_ids(WrIDs)),SpanInfo)
148 )
149 ).
150
151 get_ids_from_store(Store,Ids,SIds) :- findall(Id,member(bind(Id,_),Store),Ids), sort(Ids,SIds).
152
153 :- assert_pre(store:store_updates_and_normalise(A,B,_C),(type_check(A,store),type_check(B,store))).
154 :- assert_post(store:store_updates_and_normalise(_A,_B,C),(type_check(C,store))).
155
156 % TO DO: optimize; probably better to traverse InState once, rather than for each update binding
157 %:- block store_updates_and_normalise(-,?,?). % block normally not necessary
158 store_updates_and_normalise(Updates,InState,OutState) :- var(Updates), !,
159 add_internal_error('Variable update binding list:',store_updates_and_normalise(Updates,InState,OutState)),
160 when(ground(Updates), store_updates_and_normalise(Updates,InState,OutState)).
161 store_updates_and_normalise(Updates,InState,OutState) :- %print(store_updates_and_normalise),nl,
162 preferences:get_computed_preference(convert_closures_into_explicit_form_for_store,EXPAND),
163 sort(Updates,SUpdates), % only necessary for checking for multiple variables; we could remove this call
164 store_updates_and_normalise1(SUpdates,EXPAND,'$$NONE$$',InState,OutState).
165
166
167
168
169 store_updates_and_normalise1([],_EXPAND,_Last,InState,InState).
170 store_updates_and_normalise1([bind(Var,NewVal)|RestUpdates],EXPAND,LastVar,InState,OutState) :-
171 %NormalisedNewVal=NewVal, % TO DO: do not normalise for operation_cache here
172 normalise_value_for_var(Var,EXPAND,NewVal,NormalisedNewVal),
173 (Var==LastVar % test is in principle no longer necessary
174 -> add_error(store_updates_and_normalise,'Variable has been assigned twice in parallel: ',Var:NewVal)
175 ; var(Var)
176 -> add_internal_error('Prolog variable for Var: ',store_updates_and_normalise1([bind(Var,NewVal)|RestUpdates]))
177 ; true),
178 store_updates_and_normalise2(Var,EXPAND,NormalisedNewVal,RestUpdates,InState,OutState).
179
180 % no longer necessary; bmachine checks this: (b_is_constant(Var) -> add_error(store_updates_and_normalise,'Assigned value to constant: ',Var) ; true)
181
182 %:- block store_updates_and_normalise2(-,?,?,?,?).
183 store_updates_and_normalise2(Var,EXPAND,NewVal,RestUpdates,[H|T],OutState) :- !,
184 (H=bind(Var,_)
185 -> OutState = [bind(Var,NewVal)|TOut],
186 store_updates_and_normalise1(RestUpdates,EXPAND,Var,T,TOut)
187 ; OutState = [H|TOut],
188 store_updates_and_normalise2(Var,EXPAND,NewVal,RestUpdates,T,TOut)).
189 store_updates_and_normalise2(Var,EXPAND,NewVal,RestUpdates,InState,[bind(Var,NewVal)|TOut]) :-
190 add_error(store,'Variable not found in store:',Var),
191 store_updates_and_normalise1(RestUpdates,EXPAND,Var,InState,TOut).
192
193
194
195
196 /* use to merge updates, e.g. for sequential composition LHS ; RHS */
197 :- assert_pre(store:merge_updates(_A,_B,_C),true). % (type_check(_A,store),type_check(_B,store))).
198 :- assert_post(store:merge_updates(_A,_B,_C),true). %(type_check(_C,store))).
199
200 :- assert_must_succeed((store:merge_updates([bind(x,1)],[bind(x,2)],R),R==[bind(x,2)])).
201 :- assert_must_succeed((store:merge_updates([bind(x,1)],[bind(X,2),bind(y,3)],R),X=x,R==[bind(x,2),bind(y,3)])).
202 :- assert_must_succeed((store:merge_updates([bind(x,1)],[bind(X,2),bind(y,3)],R),X=z,R==[bind(z,2),bind(y,3),bind(x,1)])).
203 :- assert_must_succeed((store:merge_updates([bind(y,1),bind(x,1)],[bind(x,2)],R),R==[bind(x,2),bind(y,1)])).
204
205 :- block merge_updates(-,?,?).
206 merge_updates([],RHSUpdates,RHSUpdates).
207 merge_updates([Bind|LHT],RHSUpdates,Rest) :-
208 ? merge_updates2( Bind,LHT,RHSUpdates,Rest).
209 :- block merge_updates2(?,?,-,?).
210 merge_updates2(bind(Var,NewVal),LHT,RHSUpdates,Rest) :-
211 merge_remove(RHSUpdates,Var,NewVal,RHSUpdates2),
212 ? merge_updates(LHT,RHSUpdates2,Rest).
213
214 :- block merge_remove(-,?,?,?).
215 merge_remove([],Var,NewVal,[bind(Var,NewVal)]).
216 merge_remove([bind(Var2,Val2)|T2],Var1,Val1,[bind(Var2,Val2)|NewT2]) :-
217 merge_remove_aux(Var2,T2,Var1,Val1,NewT2).
218
219 :- block merge_remove_aux(-,?,?,?,?).
220 merge_remove_aux(Var2,T2,Var1,Val1,NewT2) :-
221 (Var1=Var2
222 -> NewT2=T2 /* binding in second updates overrides first update */
223 ; merge_remove(T2,Var1,Val1,NewT2)
224 ).
225 /* ----------------------- */
226
227 :- block save_updates_in_existing_store(-,?).
228 save_updates_in_existing_store([],_Store).
229 save_updates_in_existing_store([bind(Var,Value)|Urest],Store) :-
230 save_updates_in_existing_store2(Var,Value,Store),
231 save_updates_in_existing_store(Urest,Store).
232 :- block save_updates_in_existing_store2(-,?,?).
233 save_updates_in_existing_store2(Var,Value,Store) :-
234 lookup_value_for_existing_id(Var,Store,Value).
235
236
237 :- assert_must_succeed(store:l_normalise_values([int(1)],[int(1)])).
238
239 l_normalise_values([],[]).
240 l_normalise_values([H|T],[NH|NT]) :- normalise_value(H,NH), l_normalise_values(T,NT).
241
242 :- block l_expand_and_normalise_values(-,?).
243 l_expand_and_normalise_values([],[]).
244 l_expand_and_normalise_values([H|T],[NH|NT]) :-
245 normalise_value_for_var('$unkown',true,H,NH),
246 l_expand_and_normalise_values(T,NT).
247 :- block l_expand_and_normalise_values(-,?,?).
248 l_expand_and_normalise_values([],[],_).
249 l_expand_and_normalise_values([H|T],[NH|NT],[VarName|VT]) :-
250 %% print(normalising(VarName)),nl, %%
251 normalise_value_for_var(VarName,true,H,NH),
252 l_expand_and_normalise_values(T,NT,VT).
253
254
255
256 :- assert_must_succeed(store:normalise_value(int(1),int(1))).
257 :- assert_must_succeed((store:normalise_value([int(1)],R), R==avl_set(node(int(1),true,0,empty,empty)))).
258 :- assert_must_succeed((store:normalise_value([int(1),int(2)],R1),
259 store:normalise_value([int(2),int(1)],R2),R1==R2)).
260 %:- assert_must_succeed((store:normalise_value(X,[int(1),int(2)]),X=[int(2),int(1)])).
261 :- assert_must_succeed((store:normalise_value(X,_R),X=[Y,Z],
262 Y=[int(2),int(1)],Z=[int(1)])).
263 :- assert_must_succeed(store:normalise_value(_X,[int(1),int(3)])).
264 :- assert_must_succeed((store:normalise_value(X,[int(3),int(1)]),
265 kernel_objects:equal_object(X,[int(1),int(3)]))).
266 :- assert_must_fail(store:normalise_value(int(2),int(1))).
267
268
269 normalise_value_for_var(Var,Val,NormVal) :-
270 preferences:get_computed_preference(convert_closures_into_explicit_form_for_store,EXPAND),
271 normalise_value_for_var(Var,EXPAND,Val,NormVal).
272
273 % EXPAND: can be false, limit(Limit), true, force
274 :- block normalise_value_for_var(?,?,-,-).
275 normalise_value_for_var(Var,EXPAND,Val,NormVal) :-
276 % print(check_var(Var)),nl, b_interpreter:check_value(Val,Var), print(ok(Var)),nl,
277 expand_closure_value_for_normalisation(Val,EXPAND,Var,ENewVal), % also normalises values inside closure if not expanded
278 normalise_value(ENewVal,NormVal). % , tools_printing:print_term_summary(normalised(ENewVal,NormVal)).
279
280 :- block normalise_value(-,-).
281 normalise_value(V,Res) :-
282 (nonvar(V),var(Res) -> normalise2(V,ResR),Res=ResR ; normalise3(V,Res)).
283 % (nonvar(V),var(Res) -> normalise2(V,Res) ; normalise3(V,Res)).
284
285
286
287 :- use_module(b_global_sets,[b_integer_or_real_or_string_set/1,all_elements_of_type/2]).
288 :- use_module(library(avl)).
289
290 %:- use_module(debug,[time/1]).
291
292 % a normalise which does not need to call equal_object on the result argument:
293 normalise2([H|T],Res) :- !,
294 normalise_list([H|T],Res).
295 normalise2((A,B),(NA,NB)) :- !, normalise_value(A,NA), normalise_value(B,NB).
296 normalise2(global_set(GS),N) :- !,
297 ? (b_integer_or_real_or_string_set(GS)
298 -> N = global_set(GS) /* can be very large or infinite, so don't expand */
299 ; all_elements_of_type(GS,Res),
300 normalise2(Res,N)).
301 normalise2(avl_set(A),R) :- !,
302 (%empty_avl(A)
303 A=empty
304 -> R=[] ;
305 %A = node(_,_,_,empty,_) -> R = avl_set(A) ; % avoid normalising certain one and two element sets; does not seem to bring any improvement
306 avl_to_list(A,L), ord_list_to_avl(L,B), R=avl_set(B)).
307 normalise2(string(S),R) :- !, R=string(S).
308 normalise2(int(S),R) :- !, R=int(S).
309 normalise2(fd(N,T),R) :- !, R=fd(N,T).
310 normalise2(rec(Fields),R) :- !, R=rec(NFields), normalise_fields(Fields,NFields).
311 normalise2(V,V).
312
313 :- block normalise_fields(-,-).
314 normalise_fields([],[]).
315 normalise_fields([field(Name,Val)|T], [field(Name,NVal)|NT]) :- normalise_value(Val,NVal),
316 normalise_fields(T,NT).
317
318 /* version to be called if for some reason second argument already instantiated;
319 e.g. when operation arguments and results are already instantiated */
320 %%normalise3(X,R) :- var(R),var(X),!,R=X. /* just in case the when is unfrozen by hypercall */
321 :- use_module(kernel_objects,[equal_object/3]).
322 %normalise3(R,[H|T]) :- !,kernel_objects:equal_object(R,[H|T]).
323 /* don't insist on normalising if a set is already partially given*/
324 normalise3((A,B),(NA,NB)) :- !, normalise_value(A,NA), normalise_value(B,NB).
325 normalise3(V,Res) :- equal_object(V,Res,normalise3).
326
327
328 :- use_module(custom_explicit_sets,[construct_avl_from_lists/2]).
329 :- use_module(closures,[is_symbolic_closure/1]).
330 :- use_module(kernel_tools,[ground_value_check/2]).
331 normalise_list(List,Res) :-
332 ground_value_check(List,ListGround), % this is slightly slower than ground() for long lists not containing avl_set values
333 normalise_list_aux(ListGround,List,Res).
334 :- block normalise_list_aux(-,?,?).
335 normalise_list_aux(_,[H|T],Res) :- % used to check T==[]; for test 2420 T ist not [] and we get a time-out
336 cannot_be_normalised_in_avl(H),!,
337 % do not expand this; would lead to enumeration warning; cf test 2157,
338 sort([H|T],Sorted),
339 kernel_objects:equal_object(Res,Sorted,normalise_list).
340 normalise_list_aux(_,List,Res) :-
341 % catch enumeration warnings in case infinite set is later in list
342 on_enumeration_warning(
343 construct_avl_from_lists(List,Avl),
344 Avl=List),
345 kernel_objects:equal_object(Res,Avl,normalise_list).
346
347 % check for some values that cannot be normalised within a list
348 cannot_be_normalised_in_avl((A,B)) :- !,
349 ? (cannot_be_normalised_in_avl(A) -> true ; cannot_be_normalised_in_avl(B) -> true).
350 cannot_be_normalised_in_avl([H|_]) :- cannot_be_normalised_in_avl(H).
351 ?cannot_be_normalised_in_avl(C) :- is_symbolic_closure(C).
352 ?cannot_be_normalised_in_avl(rec(Fields)) :- !, member(field(_,A),Fields), cannot_be_normalised_in_avl(A).
353 %cannot_be_normalised_in_avl(C) :- is_infinite_explicit_set(C). % do we need this; we mark them as symbolic before?!
354
355
356
357 expand_closure_value_for_normalisation(Var,_,_,R) :- var(Var),!,R=Var.
358 expand_closure_value_for_normalisation(closure(P,T,B),EXPAND,VarName,ExpandedValue) :- !, %print(expand_closure_value_for_normalisation(P,T,B)),nl,
359 expand_closure_value2(P,T,B,EXPAND,VarName,ExpandedValue).
360 %expand_closure_value_for_normalisation(closure_x(P,T,B,Exp),EXPAND,VarName,ExpandedValue) :- !,
361 % (ground(Exp) -> ExpandedValue=Exp ; expand_closure_value_for_normalisation(closure(P,T,B),EXPAND,VarName,ExpandedValue)).
362 expand_closure_value_for_normalisation(Val,_Expand,_VarName,Val).
363
364 :- use_module(custom_explicit_sets,[dont_expand_this_explicit_set/1,dont_expand_this_explicit_set/2,
365 is_infinite_explicit_set/1, is_infinite_or_very_large_explicit_set/1]).
366 :- use_module(clpfd_interface,[catch_clpfd_overflow_call2/2]).
367 :- use_module(tools_timeout, [time_out_with_factor_call/3]).
368
369 :- block expand_closure_value2(-,?,?,?,?,?), expand_closure_value2(?,-,?,?,?,?), expand_closure_value2(?,?,-,?,?,?).
370 expand_closure_value2(P,T,B,EXPAND,VarName,ExpandedValue) :- !,
371 % print(expand_closure_value2(VarName,EXPAND,P)),debug:nl_time,
372 ? (dont_expand_for_norm(EXPAND,closure(P,T,B),VarName,B)
373 -> construct_normalised_closure(P,T,B,ExpandedValue) %, print(not_expanding(closure(P,T,B))),nl
374 ; %format('Expanding closure value of ~w for normalisation (expand:~w)~n',[VarName,EXPAND]),
375 % translate:print_bvalue(closure(P,T,B)),nl,
376 ? (EXPAND=force, is_symbolic_closure(closure(P,T,B))
377 -> Factor = 0.1 % more likely that expansion is not possible
378 ; Factor = 0.3),
379 catch_clpfd_overflow_call2(
380 time_out_with_factor_call(
381 call_with_enumeration_warning( /* ensure we generate enumeration_warning exceptions */
382 store:expand_closure_value3(P,T,B,VarName,ExpandedValue)
383 ),
384 Factor,
385 store:time_out_expand_backup(VarName,P,T,B,ExpandedValue))
386 , (format('% CLP(FD) overflow during closure expansion: ~w~n',[VarName]),
387 construct_normalised_closure(P,T,B,ExpandedValue)))
388 %, tools_printing:print_term_summary(expanded(VarName,ExpandedValue)),debug:nl_time
389 ).
390
391 :- use_module(kernel_waitflags, [init_wait_flags_with_call_stack/2,ground_wait_flags/1]).
392 expand_closure_value3(P,T,B,VarName,ExpandedValue) :-
393 init_wait_flags_with_call_stack(WF,[prob_command_context(expanding_value_for_normalisation(VarName),unknown)]),
394 custom_explicit_sets:expand_closure_to_avl_or_list(P,T,B,ExpandedValue,no_check,WF),
395 ground_wait_flags(WF).
396
397 :- use_module(closures,[is_recursive_closure/1]).
398 dont_expand_for_norm(false,_,_,_) :- !.
399 dont_expand_for_norm(limit(Limit),C,_,_) :- !,
400 (dont_normalise_this_explicit_set(C) ;
401 dont_expand_this_explicit_set(C,Limit)).
402 dont_expand_for_norm(_,C,VarName,Span) :-
403 constant_variable_marked_as_expand(VarName),!, % we have a user annotation, e.g., /*@desc expand */
404 % see also b_add_constant_definition for constants in b_interpreter
405 (is_recursive_closure(C)
406 -> add_warning(store,'Cannot expand recursive constant or variable: ',VarName,Span)
407 ; is_infinite_explicit_set(C)
408 -> add_warning(store,'Cannot expand infinite constant or variable: ',VarName,Span)
409 ).
410 dont_expand_for_norm(true,C,_,_) :-
411 ? dont_normalise_this_explicit_set(C) ;
412 ? dont_expand_this_explicit_set(C). % detects e.g. symbolic closures or large intervals
413 dont_expand_for_norm(force,C,_,_) :-
414 (is_recursive_closure(C) -> true ; is_infinite_or_very_large_explicit_set(C)).
415
416 :- use_module(memoization,[is_memoization_closure/4]).
417 % we usually do not want to expand memoized closures when storing in the state !
418 ?dont_normalise_this_explicit_set(closure(P,T,B)) :- is_memoization_closure(P,T,B,_).
419
420 /*
421 :- use_module(custom_explicit_sets,[dont_expand_this_explicit_set/1, is_interval_closure/5]).
422 % maybe systematically expand small intervals and global_set's for normalization purposes ??
423 % (alternatively, we could re-convert certain avl-sets to symbolic form here)
424 dont_expand_closure_value(_,_,_) :-
425 preferences:get_computed_preference(convert_closures_into_explicit_form_for_store,false),!.
426 %dont_expand_closure_value(P,T,B) :-
427 % is_interval_closure(P,T,B,Low,Up), number(Low), number(Up),!,
428 % % interval closures are quite efficient for certain manipulations
429 % Size is Up+1-Low, Size>100. % another magic constant ; which value to choose ??
430 %dont_expand_closure_value(_,_,_) :-
431 % preferences:get_preference(convert_comprehension_sets_into_closures,false),!.
432 % % in the case above: we have already decided not to expand the closure earlier, dont repeat check !? TO DO: check that this is indeed ok; that no closures are constructed otherwise; maybe we should just check for small interval closures ?
433 dont_expand_closure_value(P,T,B) :- dont_expand_this_explicit_set(closure(P,T,B)).
434 */
435
436 time_out_expand_backup(VarName,P,T,B,ExpandedValue) :-
437 add_message(expand_closure_value,'Time-out while trying to expand value of: ',VarName,B),
438 construct_normalised_closure(P,T,B,ExpandedValue).
439
440 construct_normalised_closure(P,T,B1,closure(P,T,NB)) :-
441 %print(normalising(P)),nl,
442 %b_compile(B1,P,[],[],B2,no_wf_available), % probably much more expensive than simple code with can_be_computed below
443 normalise_closure_body(B1,NB).
444 % print(norm),nl,print(B),nl,print(NB),nl,nl.
445
446 % normalise values that are compiled inside closures
447 normalise_closure_body(b(OP,T,I),b(NOP,T,FI)) :-
448 (var(I) -> print(b(OP,T,I)),nl , when(nonvar(OP),(print(b(OP,T,I)),nl)) ; true),
449 filter_info(OP,I,FI),
450 ncb(OP,OP2),
451 (can_be_computed(OP2) % when compiling not all information was available for computation, now it is
452 -> b_interpreter:b_compute_expression_nowf(b(OP,T,I),[],[],Val,'normalising closure body',0),
453 %print(comp(Val)),nl,
454 ncb(value(Val),NOP) % TO DO: better apply the can_be_computed check after ncb, if performance ok
455 ; NOP=OP2).
456
457 % should we throw away all infos for certain operators, e.g., for truth (cartesian product closures) or for certain values and member closures?
458 filter_info(truth,_,Res) :- !, % we have a maximal set
459 Res=[]. % we could check preference(provide_trace_information,false)
460 filter_info(falsity,_,Res) :- !, Res=[].
461 filter_info(_,V,R) :- filter_info2(V,R).
462
463 filter_info2(V,R) :- var(V),!,R=V.
464 filter_info2([],[]).
465 filter_info2([H|T],Res) :-
466 (important_info(H) -> Res=[H|TR] ; Res=TR), filter_info2(T,TR).
467
468 important_info(contains_wd_condition).
469 important_info(prob_annotation(_)).
470 important_info(used_ids(_)).
471 important_info(reads(_)). % operation_call_in_expr, modifies(.), non_det_modifies(.) only appear in subst
472 important_info(was(_)).
473 important_info(nodeid(_)).
474 important_info(allow_to_lift_exists).
475
476 :- use_module(kernel_mappings).
477 % ncb stands for normalise closure body
478 ncb(V,R) :- var(V),!, R=V.
479 ncb(bool_set,R) :- !, R=bool_set.
480 ncb(truth,R) :- !, R=truth.
481 ncb(event_b_identity,R) :- !, R=event_b_identity.
482 ncb(falsity,R) :- !, R=falsity.
483 ncb(max_int,R) :- !, R=max_int.
484 ncb(min_int,R) :- !, R=min_int.
485 ncb(value(V),value(NV)) :- !,normalise_value_for_var('$closure_body',V,NV).
486 ncb(integer(X),integer(NX)) :- !, X=NX.
487 ncb(identifier(X),identifier(NX)) :- !, X=NX.
488 ncb(float_set,R) :- !, R=float_set.
489 ncb(real(X),real(NX)) :- !, X=NX.
490 ncb(real_set,R) :- !, R=real_set.
491 ncb(string(X),string(NX)) :- !, X=NX.
492 ncb(string_set,R) :- !, R=string_set.
493 ncb(integer_set(X),integer_set(NX)) :- !, X=NX.
494 ncb(set_extension(X),set_extension(NX)) :- !, X=NX.
495 ncb(partition(S,L),partition(NS,NL)) :- !, normalise_closure_body(S,NS),
496 maplist(normalise_closure_body,L,NL).
497 ncb(general_sum(Ids,Condition,Expression),general_sum(Ids,NC,NE)) :- !,
498 normalise_closure_body(Condition,NC),normalise_closure_body(Expression,NE).
499 ncb(general_product(Ids,Condition,Expression),general_product(Ids,NC,NE)) :- !,
500 normalise_closure_body(Condition,NC),normalise_closure_body(Expression,NE).
501 ncb(sequence_extension(X),sequence_extension(NX)) :- !, X=NX.
502 ncb(external_function_call(F,L),external_function_call(F,LL)) :- !, L=LL.
503 ncb(external_pred_call(F,L),external_pred_call(F,LL)) :- !, L=LL.
504 ncb(equal(A,B),equal(NA,NB)) :- !, normalise_closure_body(A,NA), normalise_closure_body(B,NB).
505 ncb(couple(A,B),couple(NA,NB)) :- !, normalise_closure_body(A,NA), normalise_closure_body(B,NB).
506 ncb(interval(A,B),interval(NA,NB)) :- !, normalise_closure_body(A,NA), normalise_closure_body(B,NB).
507 ncb(function(A,B),function(NA,NB)) :- !, normalise_closure_body(A,NA), normalise_closure_body(B,NB).
508 ncb(operation_call_in_expr(A,B),operation_call_in_expr(NA,B)) :- !,
509 normalise_closure_body(A,NA).
510 ncb(relations(A,B),relations(NA,NB)) :- !, normalise_closure_body(A,NA), normalise_closure_body(B,NB).
511 ncb(member(A,B),member(NA,NB)) :- !, normalise_closure_body(A,NA), normalise_closure_body(B,NB).
512 ncb(not_member(A,B),not_member(NA,NB)) :- !, normalise_closure_body(A,NA), normalise_closure_body(B,NB).
513 ncb(negation(B),negation(NB)) :- !, normalise_closure_body(B,NB).
514 ncb(conjunct(A,B),conjunct(NA,NB)) :- !, normalise_closure_body(A,NA), normalise_closure_body(B,NB).
515 ncb(disjunct(A,B),disjunct(NA,NB)) :- !, normalise_closure_body(A,NA), normalise_closure_body(B,NB).
516 ncb(equivalence(A,B),equivalence(NA,NB)) :- !, normalise_closure_body(A,NA), normalise_closure_body(B,NB).
517 ncb(if_then_else(A,B,C),if_then_else(NA,NB,NC)) :- !,
518 normalise_closure_body(A,NA), normalise_closure_body(B,NB), normalise_closure_body(C,NC).
519 ncb(implication(A,B),implication(NA,NB)) :- !, normalise_closure_body(A,NA), normalise_closure_body(B,NB).
520 ncb(comprehension_set(V,B),comprehension_set(V,NB)) :- !, normalise_closure_body(B,NB).
521 ncb(convert_bool(B),convert_bool(NB)) :- !, normalise_closure_body(B,NB).
522 ncb(convert_real(B),convert_real(NB)) :- !, normalise_closure_body(B,NB).
523 ncb(convert_int_floor(B),convert_int_floor(NB)) :- !, normalise_closure_body(B,NB).
524 ncb(convert_int_ceiling(B),convert_int_ceiling(NB)) :- !, normalise_closure_body(B,NB).
525 ncb(exists(IDs,B),exists(IDs,NB)) :- !, normalise_closure_body(B,NB).
526 ncb(forall(IDs,A,B),forall(IDs,NA,NB)) :- !, normalise_closure_body(A,NA), normalise_closure_body(B,NB).
527 ncb(assertion_expression(Cond,ErrMsg,Expr),assertion_expression(NA,ErrMsg,NB)) :- !,
528 normalise_closure_body(Cond,NA), normalise_closure_body(Expr,NB).
529 ncb(freetype_case(FT,IsCase,Expr),freetype_case(FT,IsCase,NB)) :- !,
530 normalise_closure_body(Expr,NB).
531 ncb(freetype_constructor(Id,Case,Expr),freetype_constructor(Id,Case,NB)) :- !,
532 normalise_closure_body(Expr,NB).
533 ncb(freetype_destructor(Id,Case,Expr),freetype_destructor(Id,Case,NB)) :- !,
534 normalise_closure_body(Expr,NB).
535 ncb(record_field(R,Name),record_field(NR,Name)) :- !,
536 normalise_closure_body(R,NR).
537 ncb(rec(Fields),rec(NFields)) :- !,
538 ncb_fields(Fields,NFields).
539 ncb(recursive_let(ID,R),recursive_let(ID,NR)) :- !,
540 normalise_closure_body(R,NR).
541 ncb(kodkod(Id,Ids),R) :- !, R=kodkod(Id,Ids).
542 ncb(lazy_let_expr(Id,E,Expr),lazy_let_expr(Id,NE,NExpr)) :- !, normalise_closure_body(E,NE),
543 normalise_closure_body(Expr,NExpr).
544 ncb(lazy_let_pred(Id,E,Pred),lazy_let_pred(Id,NE,NPred)) :- !, normalise_closure_body(E,NE),
545 normalise_closure_body(Pred,NPred).
546 % lazy_let_subst should not occur in closure bodies
547 ncb(lazy_lookup_expr(X),lazy_lookup_expr(NX)) :- !, X=NX.
548 ncb(lazy_lookup_pred(X),lazy_lookup_pred(NX)) :- !, X=NX.
549 ncb(let_predicate(Ids,AssignmentExprs,Pred),let_predicate(Ids,AssignmentExprs,NPred)) :- !,
550 normalise_closure_body(Pred,NPred).
551 ncb(let_expression(Ids,AssignmentExprs,Expr),let_expression(Ids,AssignmentExprs,NExpr)) :- !,
552 normalise_closure_body(Expr,NExpr).
553 ncb(let_expression_global(Ids,AssignmentExprs,Expr),let_expression_global(Ids,AssignmentExprs,NExpr)) :- !,
554 normalise_closure_body(Expr,NExpr).
555 ncb(F,NF) :- functor(F,Op,1),
556 (unary_function(Op,_,_) ; unary_in_boolean_type(Op,_)), !,
557 arg(1,F,Arg1), functor(NF,Op,1), arg(1,NF,NormArg1),
558 normalise_closure_body(Arg1,NormArg1).
559 ncb(F,NF) :- functor(F,Op,2),
560 (binary_function(Op,_,_) ; kernel_mappings:binary_boolean_operator(Op,_,_) ; binary_in_boolean_type(Op,_)), !,
561 arg(1,F,Arg1),arg(2,F,Arg2), functor(NF,Op,2), arg(1,NF,NormArg1), arg(2,NF,NormArg2),
562 normalise_closure_body(Arg1,NormArg1),
563 normalise_closure_body(Arg2,NormArg2).
564 ncb(X,X) :- print(ncb(X)),nl.
565
566
567 can_be_computed(domain(X)) :- is_known_set_or_int_value(X).
568 can_be_computed(range(X)) :- is_known_set_or_int_value(X).
569 can_be_computed(plus(X,Y)) :- is_known_set_or_int_value(X),is_known_set_or_int_value(Y).
570 can_be_computed(minus(X,Y)) :- is_known_set_or_int_value(X),is_known_set_or_int_value(Y).
571 can_be_computed(times(X,Y)) :- is_known_set_or_int_value(X),is_known_set_or_int_value(Y).
572 % check for more possible partial evaluations; check for common code with b_compiler, CSE or other parts of ProB
573
574 is_known_set_or_int_value(b(value(V),_,_)) :- is_known_aux(V).
575 is_known_aux(X) :- var(X),!,fail.
576 is_known_aux([]).
577 is_known_aux(avl_set(_)).
578 is_known_aux(int(Nr)) :- number(Nr).
579
580 ncb_fields([],[]).
581 ncb_fields([field(Name,Expr)|T], [field(Name,NExpr)|NT]) :-
582 normalise_closure_body(Expr,NExpr),
583 ncb_fields(T,NT).
584
585 %ncb(set_extension([b(couple(b(add(b(identifier(idx),integer,[nodeid(pos(53,1,11,91,11,93))]),b(integer(1),integer,[nodeid(pos(54,1,11,95,11,95))])),integer,[nodeid(pos(52,1,11,91,11,95))]),b(identifier(bit),integer,[nodeid(pos(55,1,11,101,11,103))])),couple(integer,integer),[nodeid(pos(51,1,11,91,11,103))])]))
586
587 normalise_store(T,NT) :-
588 preferences:get_computed_preference(convert_closures_into_explicit_form_for_store,EXPAND),
589 normalise_store1(T,EXPAND,NT).
590 :- block normalise_store1(-,?,?).
591 normalise_store1([],_Expand,Res) :- !, Res=[].
592 normalise_store1([bind(Var,Val)|T],EXPAND,Res) :- !, Res = [bind(Var,NVal)|NT],
593 %print(normalise(Val)),nl,print(Var),nl,
594 normalise_value_for_var(Var,EXPAND,Val,NVal),
595 normalise_store1(T,EXPAND,NT).
596 normalise_store1(Store,E,Res) :-
597 add_internal_error('Illegal store: ',normalise_store1(Store,E,Res)),
598 Res=Store.
599
600 /* ----------------------- */
601
602 % a utility to also accept const_and_vars states
603 normalise_states([],[]).
604 normalise_states([I|Irest],[O|Orest]) :-
605 normalise_state(I,O),
606 normalise_states(Irest,Orest).
607 normalise_state(I,O) :-
608 extract_vars(I,VI,VO,O),
609 normalise_store(VI,VO).
610 extract_vars(const_and_vars(ConstID,IV),IV,OV,const_and_vars(ConstID,OV)) :- !.
611 extract_vars(IV,IV,OV,OV).
612
613
614 /* ----------------------- */
615
616 :- assert_pre(store:lookup_value_for_existing_id(Id,State,_Val),
617 (type_check(Id,variable_id),list_skeleton(State),type_check(State,store))).
618 :- assert_post(store:lookup_value_for_existing_id(_Id,_State,_Val), true).
619
620 lookup_value_for_existing_id(Id,State,Res) :-
621 (member_bind(Id,Val,State) % safe_member(bind(Id,Val),State,lookup_value_for_existing_id)
622 -> check_val(Val,Id,State),Res=Val
623 ; add_internal_error('Call Failed: ',lookup_value_for_existing_id(Id,State,Res)),
624 Res=term(undefined)
625 ).
626
627 :- assert_pre(store:lookup_value_for_existing_id(Id,LS,State,_Val),
628 (type_check(Id,variable_id),type_check(LS,store),type_check(State,store),list_skeleton(LS),list_skeleton(State))).
629 :- assert_post(store:lookup_value_for_existing_id(_Id,_LS,_State,_Val), true).
630
631 lookup_value_for_existing_id(Id,LocalState,State,Val) :-
632 (member_bind(Id,Val,LocalState) %safe_member(bind(Id,Val),LocalState,lookup_value_for_existing_id__4)
633 -> check_val(Val,Id,LocalState)
634 ; lookup_value_for_existing_id(Id,State,Val)).
635
636 % pass WF for call_stack info:
637 lookup_value_for_existing_id_wf(Id,LocalState,State,Val,WF) :-
638 (member_bind(Id,Val,LocalState) %safe_member(bind(Id,Val),LocalState,lookup_value_for_existing_id__4)
639 -> check_val_with_span_wf(Val,Id,LocalState,unknown,WF)
640 ; lookup_value_for_existing_id_wf(Id,State,Val,WF)).
641
642 lookup_value_for_existing_id_wf(Id,State,Val,WF) :-
643 (member_bind(Id,Val,State) %safe_member(bind(Id,Val),LocalState,lookup_value_for_existing_id__4)
644 -> check_val_with_span_wf(Val,Id,State,unknown,WF)
645 ; add_state_error_wf(lookup_value_for_existing_id_wf,'Cannot find identifier: ',Id,unknown,WF),
646 fail
647 ).
648
649 lookup_value(Id,State,Val) :-
650 (member_bind(Id,Val,State) %safe_member(bind(Id,Val),State,lookup_value)
651 -> check_val(Val,Id,State)
652 ).
653
654 lookup_value(Id,LocalState,State,Val) :-
655 (member_bind(Id,Val,LocalState) %safe_member(bind(Id,Val),LocalState,lookup_value__4)
656 -> check_val(Val,Id,LocalState)
657 ; member_bind(Id,Val,State) -> check_val(Val,Id,State)).
658
659 lookup_value_with_span_wf(Id,LocalState,State,Val,Span,WF) :-
660 (member_bind(Id,Val,LocalState) %safe_member(bind(Id,Val),LocalState,lookup_value__4)
661 -> check_val_with_span_wf(Val,Id,LocalState,Span,WF)
662 ; member_bind(Id,Val,State) -> check_val_with_span_wf(Val,Id,State,Span,WF)).
663
664 lookup_value_without_check(Id,LocalState,State,Val) :- % does not check if value is undefined
665 (member_bind(Id,Val,LocalState) %safe_member(bind(Id,Val),LocalState,lookup_value__4)
666 -> true
667 ; member_bind(Id,Val,State) -> true).
668
669 % a faster version of member(bind(Id,Val),List) with only one solution
670 member_bind(Id,Val,[H|T]) :-
671 arg(1,H,Id) -> arg(2,H,Val) ; member_bind(Id,Val,T).
672
673 get_id_value(Id,State,Val) :- safe_member(bind(Id,Val),State,get_id_value). /* can be backtracked over */
674
675 %safe_member(X,T) :- safe_member(X,T,safe_member).
676 safe_member(_,[],_) :- !,fail.
677 safe_member(X,[H|T],Loc) :- !, (X=H ; safe_member(X,T,Loc)).
678 safe_member(X,T,Loc) :- add_internal_error('Argument not a list: ',safe_member(X,T,Loc)),fail.
679
680 :- use_module(kernel_waitflags,[add_state_error_wf/5]).
681 check_val_with_span_wf(X,V,_State,Span,WF) :-
682 (X==term(undefined) ->
683 add_state_error_wf(reading_undefined_variable,'Trying to read undefined variable: ',V,Span,WF) % do not fail; otherwise we will look elsewhere and generate another error message, see test 1660
684 ; true).
685
686 check_val(X,V,_State) :-
687 (X==term(undefined) ->
688 add_state_error_wf(reading_undefined_variable,'Trying to read undefined variable: ',V,unknown,no_wf_available)
689 % do not fail
690 ; true).
691
692 :- assert_must_succeed(( store:lookup_and_delete_value(x,X,[bind(y,2),bind(x,3)],R,true,_WF,F),
693 X==3, R==[bind(y,2)],F==true )).
694 :- assert_must_succeed(( store:lookup_and_delete_value(x,X,[bind(y,2)|T],R,true,_WF,F),
695 T = [bind(x,3)],
696 X==3, R==[bind(y,2)],F==true )).
697
698 lookup_and_delete_value(Var,Value,T,UT,ReportError,WF,Finished) :-
699 lookup_and_delete_value1(T,Var,Value,UT,ReportError,WF,Finished).
700 :- use_module(kernel_waitflags,[add_abort_error_span/5]).
701 :- block lookup_and_delete_value1(-,?,?,?,?,?,?).
702 lookup_and_delete_value1([],Var,Value,[],ReportError,WF,Finished) :-
703 (ReportError=report_error(WF)
704 -> add_abort_error_span(missing_assignment_error,'Statement did not assign to variable: ',Var,unknown,WF)
705 /* lookup_and_delete is called only from get_results */
706 ; true
707 ), no_value_for_variable(Value,Var),
708 Finished=true.
709 lookup_and_delete_value1([bind(Var2,Value2)|T],Var,Value,Res,ReportError,WF,Finished) :-
710 lookup_and_delete_value2(Var2,Value2,T,Var,Value,Res,ReportError,WF,Finished).
711 :- block lookup_and_delete_value2(-,?,?,?,?,?,?,?,?).
712 lookup_and_delete_value2(Var2,Value2,T,Var,Value,Res,ReportError,WF,Finished) :-
713 (Var=Var2
714 -> Value=Value2, T=Res, Finished=true
715 ; Res=[bind(Var2,Value2)|UT],
716 lookup_and_delete_value1(T,Var,Value,UT,ReportError,WF,Finished)
717 ).
718
719 no_value_for_variable(term(no_value_for(Var)),Var).
720
721 :- assert_must_succeed(( store:search_and_delete_value(x,22,X,[bind(y,2),bind(x,3)],R),
722 X==3, R==[bind(y,2)] )).
723 :- assert_must_succeed(( store:search_and_delete_value(x,22,X,[bind(y,2)],R),
724 X==22, R==[bind(y,2)] )).
725 :- assert_must_succeed(( store:search_and_delete_value(x,22,X,[bind(y,2)|T],R),
726 T = [bind(x,3)],
727 X==3, R==[bind(y,2)] )).
728
729 :- block search_and_delete_value(-,?,?,?,?).
730 search_and_delete_value(Var,OldVal,Value,InStore,OutStore) :-
731 ? search_and_delete_value2(InStore,Var,OldVal,Value,OutStore).
732
733 :- block search_and_delete_value2(-,?,?,?,?).
734 ?search_and_delete_value2([],_Var,OldVal,Value,[]) :- eq_obj(Value,OldVal).
735 search_and_delete_value2([BIND|T],Var,OldVal,Value,Res) :- % BIND=bind(Var2,Value2)
736 arg(1,BIND,Var2), arg(2,BIND,Value2),
737 ? search_and_delete_value3(Var2,Value2,T,Var,OldVal,Value,Res).
738
739 :- block search_and_delete_value3(-,?,?,?,?,?,?).
740 search_and_delete_value3(Var2,Value2,T,Var,OldVal,Value,Res) :-
741 (Var=Var2
742 ? -> eq_obj(Value,Value2), T=Res
743 ; Res=[bind(Var2,Value2)|UT],
744 ? search_and_delete_value2(T,Var,OldVal,Value,UT)
745 ).
746
747 eq_obj(Val1,Val2) :- (var(Val1);var(Val2)),!,Val1=Val2.
748 ?eq_obj(Val1,Val2) :- equal_object(Val1,Val2,search_and_delete_value).
749
750 empty_state([]).
751 add_var_to_localstate(Var,Val,InState,[bind(Var,Val)|InState]).
752
753 :- assert_pre(store:set_up_localstate(Vars,Vals,In,_),
754 (type_check(Vars,list(variable_id)),type_check(Vals,vlist(any)),type_check(In,store))).
755 :- assert_post(store:set_up_localstate(_,_,_,Out), type_check(Out,store)).
756
757 set_up_localstate([],[],S,S) :- !.
758 set_up_localstate([Var|T],[Val|FT],InS,[bind(VarID,Val)|OutS]) :- !,
759 /* add Var,Val to Local state at the beginning to properly handle nested
760 blocks with same variable names */
761 (Var=b(identifier(Id),_,_) -> %print(typed_identifier_for_set_up_localstate(Var)),nl,
762 VarID = Id
763 ; VarID = Var),
764 set_up_localstate(T,FT,InS,OutS).
765 set_up_localstate(Vars,Vals,In,Out) :-
766 add_internal_error('Illegal call: ',set_up_localstate(Vars,Vals,In,Out)), fail.
767
768 set_up_undefined_localstate([],S,S).
769 set_up_undefined_localstate([Var|T],InS,[bind(Id,term(undefined))|OutS]) :-
770 def_get_texpr_id(Var,Id),
771 /* add Var,Val to Local state at the beginning to properly handle nested
772 blocks with same variable names */
773 set_up_undefined_localstate(T,InS,OutS).
774
775 :- use_module(tools_lists,[delete_first/3]).
776 % sets up undefined values for variables, except for a list of predefined variables (all of which must be used)
777 set_up_undefined_localstate_with_predefined([],PredefinedValues,S,S) :-
778 (PredefinedValues=[] -> true
779 ; add_internal_error('Unknown predefined values: ',set_up_undefined_localstate_with_predefined([],PredefinedValues,S,S))).
780 set_up_undefined_localstate_with_predefined([Var|T],PredefinedValues,InS,[bind(Id,Val)|OutS]) :-
781 def_get_texpr_id(Var,Id),
782 delete_first(InS,bind(Id,_),InS2), % in case of name clash: remove the old binding from the state
783 ? (select(bind(Id,Val),PredefinedValues,Rest)
784 -> set_up_undefined_localstate_with_predefined(T,Rest,InS2,OutS)
785 ; Val=term(undefined),
786 set_up_undefined_localstate_with_predefined(T,PredefinedValues,InS2,OutS)).
787
788
789 :- assert_pre(store:delete_variables_from_state(Vars,In,_),
790 (type_check(Vars,list(variable_id)),type_check(In,store))).
791 :- assert_post(store:delete_variables_from_state(_,_,Out), type_check(Out,store)).
792
793 delete_variables_from_state([],S,R) :- !, R=S.
794 delete_variables_from_state([Var|T],InState,OutState) :- !,
795 delete_variable(InState, Var, S2),
796 delete_variables_from_state(T,S2,OutState).
797 delete_variables_from_state(Vs,I,O) :-
798 add_internal_error('Illegal call to:',delete_variables_from_state(Vs,I,O)),fail.
799
800 delete_variable([],_,[]).
801 delete_variable([bind(V,Val)|T],Var,Res) :-
802 (Var==V
803 -> Res = T
804 ; Res = [bind(V,Val)|DT], delete_variable(T,Var,DT)
805 ).
806 /* --------------------------------- */
807
808 % unused at the moment:
809 %:- assert_pre(store:copy_store_template(In,_),
810 % (type_check(In,store))).
811 %:- assert_post(store:copy_store_template(_,Out), type_check(Out,store)).
812 %copy_store_template([],[]).
813 %copy_store_template([bind(Var,_)|T],[bind(Var,_)|CT]) :- copy_store_template(T,CT).
814
815 :- use_module(library(ordsets)).
816 % copy_store_template copies a store and its values except some changing variables
817 % Two new stores are created: constants are not copied into the first new update state, but
818 % to the second new full state
819 copy_variable_store(InState,SortedChanges,Updates,NewFullState) :-
820 (SortedChanges=[] -> Updates = [],NewFullState=InState % Query Operation/Event ,print(no_upd),nl
821 ; copy_variable_store2(InState,SortedChanges,Updates,NewFullState)).
822
823 copy_variable_store2([],_,[],[]).
824 copy_variable_store2([bind(Var,Old)|T],SortedChanges,Updates,NewFullState) :-
825 ( b_is_constant(Var) ->
826 Updates = CT, NewFullState = [bind(Var,Old)|FT]
827 %; safe_member(Var,SortedChanges,copy_variable_store) ->
828 ; ord_member(Var,SortedChanges) ->
829 %; ord_delete(SortedChanges,Var,SortedChanges2) -> % in principle better than ord_member; but seems slightly slower on examples/EventBPrologPackages/Advance_WP2/v6_Sep2014/ex_mch.eventb
830 Updates = [bind(Var,New)|CT], NewFullState = [bind(Var,New)|FT]
831 ;
832 Updates = CT, NewFullState = [bind(Var,Old)|FT]
833 ), copy_variable_store2(T,SortedChanges,CT,FT).
834
835 %ord_delete([H|T],El,Res) :- El @>= H,
836 % (H==El -> Res=T ; Res = [H|RR], ord_delete(T,El,RR)).
837
838
839 % -------------------------------
840
841 :- assert_must_succeed(store:check_valid_store([bind(x,int(1)), bind(y,int(2))],unit_test)).
842
843 :- block check_valid_store(-,?).
844 check_valid_store([],_PP).
845 check_valid_store([bind(Var,Value)|T],PP) :- member(bind(Var,Val2),T),
846 add_internal_error('Multiple occurences of variable: ',bind(Var,Value,Val2,PP)),
847 fail.
848 check_valid_store([_|T],PP) :- check_valid_store(T,PP).
849
850