1 | | % (c) 2009-2022 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(predicate_evaluator,[tcltk_analyse_goal/1, tcltk_analyse_goal/2, |
6 | | tcltk_analyse_invariant/2, |
7 | | tcltk_analyse_assertions/2, tcltk_analyse_assertions/3, |
8 | | tcltk_analyse_properties/2, check_summary_all_true/1, |
9 | | tcltk_analyse_deadlock/2, |
10 | | %get_analyse_error_span_linecol/4, |
11 | | |
12 | | % a way to customize what is done with false/unknown conjuncts |
13 | | reset_conjunct_error_hook/0, register_conjunct_error_hook/1, |
14 | | register_conjunct_register_error_span/0 % conjuncts which are not true are registered as error spans |
15 | | ]). |
16 | | |
17 | | % a simple textual analysis of the conjuncts of a predicate |
18 | | |
19 | | :- meta_predicate register_conjunct_error_hook(5). |
20 | | :- meta_predicate analyse_quick_time_out(0). |
21 | | :- meta_predicate call_residue_check(0). |
22 | | :- meta_predicate time_out_and_catch_errors(0,*,*). |
23 | | |
24 | | :- use_module(tools). |
25 | | |
26 | | :- use_module(module_information,[module_info/2]). |
27 | | :- module_info(group,misc). |
28 | | :- module_info(description,'Various tools to analyse conjunctions of predicates textually (along with statistics).'). |
29 | | |
30 | | :- use_module(bmachine). |
31 | | :- use_module(error_manager). |
32 | | :- use_module(translate). |
33 | | :- use_module(debug). |
34 | | :- use_module(preferences,[get_computed_preference/2,get_preference/2,get_time_out_preference_with_factor/2]). |
35 | | :- use_module(state_space,[current_state_corresponds_to_setup_constants_b_machine/0, |
36 | | current_state_corresponds_to_fully_setup_b_machine/0]). |
37 | | |
38 | | |
39 | | |
40 | | current_b_state_with_all_constants_valued :- |
41 | | (current_state_corresponds_to_setup_constants_b_machine -> true |
42 | | ; \+ b_machine_has_constants). % also allow analysis if we have no constants (but possibly properties) |
43 | | |
44 | | tcltk_analyse_goal(List) :- tcltk_analyse_goal(List,_). |
45 | | tcltk_analyse_goal(list(List),Summary) :- |
46 | | current_b_state_with_all_constants_valued, |
47 | | b_get_machine_goal(Goal),!, |
48 | | b_analyse_boolean_expression(Goal,'GOAL',false,List,Summary). |
49 | | tcltk_analyse_goal(list(['Cannot evaluate GOAL yet.\nSetup constants first.\n\n'|List]),Summary) :- |
50 | | b_get_machine_goal(Goal),!, |
51 | | get_conjuncts(Goal,List,undefined), unknown_summary(1,Summary). |
52 | | tcltk_analyse_goal(list(['Cannot find GOAL DEFINITION (must be a predicate).\n']),Summary) :- |
53 | | unknown_summary(1,Summary). |
54 | | |
55 | | tcltk_analyse_invariant(list(List),Summary) :- |
56 | | current_state_corresponds_to_fully_setup_b_machine,!, |
57 | | b_get_invariant_from_machine(Invariant), |
58 | | get_preference(partition_predicates,Partition), |
59 | | b_analyse_boolean_expression(Invariant,'INVARIANT',Partition,List,Summary). |
60 | | tcltk_analyse_invariant(list(['Cannot evaluate invariant yet.\nInitialise machine first.\n\n'|List]),Summary) :- |
61 | | b_get_invariant_from_machine(Invariant), |
62 | | get_conjuncts(Invariant,List,undefined), |
63 | | length(List,TL), unknown_summary(TL,Summary). |
64 | | |
65 | | :- use_module(specfile,[animation_mode/1, csp_with_bz_mode/0]). |
66 | | tcltk_analyse_assertions(List,Summary) :- tcltk_analyse_assertions(all,List,Summary). |
67 | | tcltk_analyse_assertions(_ALL,list(['CSP Assertions'|CA]),TSummary) :- (animation_mode(cspm) ; csp_with_bz_mode),!, |
68 | | statistics(walltime,[CurTime,_]), /* get current time in ms */ |
69 | | tcltk_interface:tcltk_check_csp_assertions(CA,Summary), |
70 | | statistics(walltime,[CurTime2,_]), /* get current time in ms */ |
71 | | Delta is CurTime2-CurTime, |
72 | | TSummary = [runtime/Delta|Summary], |
73 | | format('% CSP Assertion Checks Completed: ~w~n',[CA]). |
74 | | tcltk_analyse_assertions(all,list(['This Machine has no ASSERTIONS.']),Summ) :- |
75 | | b_get_dynamic_assertions_from_machine([]),b_get_static_assertions_from_machine([]),!,empty_summary(Summ). |
76 | | tcltk_analyse_assertions(main,list(['The Main Machine has no ASSERTIONS.']),Summ) :- |
77 | | b_main_machine_has_no_assertions, |
78 | | !,empty_summary(Summ). |
79 | | tcltk_analyse_assertions(ALL,list(List),Summary) :- |
80 | | current_state_corresponds_to_fully_setup_b_machine,!, |
81 | | %conjunct_predicates(Ass,BExpr) |
82 | ? | b_get_assertions(ALL,static,SAss), |
83 | | println_silent('Checking Static Assertions'), |
84 | | % translate:nested_print_bexpr(SAss), |
85 | | b_analyse_boolean_expression(SAss,'ASSERTIONS (static)',false,SList,SSummary), |
86 | | b_get_assertions(ALL,dynamic,DAss), |
87 | | println_silent('Checking Dynamic Assertions'), |
88 | | b_analyse_boolean_expression(DAss,'ASSERTIONS (dynamic)',false,DList,DSummary), |
89 | | combine_summary(SSummary,DSummary,Summary), |
90 | ? | combine_static_dynamic_result(SList,DList,List). |
91 | | tcltk_analyse_assertions(ALL,list(ListRes),Summary) :- |
92 | | current_b_state_with_all_constants_valued, |
93 | ? | b_get_assertions(ALL,static,Ass),Ass\=[],!, |
94 | | b_analyse_boolean_expression(Ass,'ASSERTIONS',false,SList,SSummary), |
95 | | b_get_assertions(ALL,dynamic,DynAss), |
96 | | (DynAss=[] -> ListRes = List, Summary = SSummary |
97 | | ; ListRes = ['Only evaluating STATIC ASSERTIONS.\nInitialise machine to evaluate other ASSERTIONS.\n\n'|List], |
98 | | (length(DynAss,Len) -> true ; Len=1), |
99 | | unknown_summary(Len,US),combine_summary(US,SSummary,Summary) |
100 | | ), |
101 | | get_assertions_as_undefined(ALL,dynamic,DList), |
102 | ? | combine_static_dynamic_result(SList,DList,List). |
103 | | tcltk_analyse_assertions(ALL,list(['Cannot evaluate assertions yet.\nInitialise machine first.\n\n'|List]),Summary) :- |
104 | | print('*** Cannot evaluate ASSERTIONS\nSPECIFICATION not initialised.'),nl, |
105 | | get_assertions_as_undefined(ALL,static,SList), |
106 | | get_assertions_as_undefined(ALL,dynamic,DList), |
107 | | length(SList,SL), length(DList,DL), TL is SL+DL, unknown_summary(TL,Summary), |
108 | | combine_static_dynamic_result(SList,DList,List). |
109 | | |
110 | | :- use_module(library(lists),[maplist/3]). |
111 | | get_assertions_as_undefined(ALL,SD,List) :- |
112 | | b_get_assertions(ALL,SD,Ass), |
113 | | flatten_conjunction(Ass,FAss,[]), |
114 | | maplist(get_as_undefined,FAss,List). |
115 | | % (Ass=[] -> List=[] ; conjunct_predicates(Ass,BExpr),get_conjuncts(BExpr,List,undefined)). |
116 | | |
117 | | get_as_undefined(Conjunct,Translation) :- translate_conj_status(Conjunct,undefined,false,Translation). |
118 | | |
119 | | combine_static_dynamic_result([],L,L). |
120 | | combine_static_dynamic_result(L,[],L). |
121 | | combine_static_dynamic_result(S,D,List) :- |
122 | | List = ['STATIC ASSERTIONS:\n'|List0], |
123 | | append(S,['DYNAMIC ASSERTIONS:\n'|D],List0). |
124 | | |
125 | | unknown_summary(Nr,[total/Nr,unknown/Nr,unknown_after_expansion/Nr]). |
126 | | combine_summary([],S,S). |
127 | | combine_summary([K/V|T],S,R) :- update(S,K,V,S2), combine_summary(T,S2,R). |
128 | | update([],K,V,[K/V]). |
129 | | update([K1/V1|T],K1,V2,[K1/NV|T]) :- !,NV is V1+V2. |
130 | | update([K1/V1|T],K,V,[K1/V1|NT]) :- update(T,K,V,NT). |
131 | | |
132 | | :- use_module(b_state_model_check,[get_unsorted_all_guards_false_pred/1]). |
133 | | tcltk_analyse_deadlock(list(List),Summary) :- current_state_corresponds_to_fully_setup_b_machine,!, |
134 | | get_unsorted_all_guards_false_pred(BExpr), |
135 | | b_analyse_boolean_expression(BExpr,'DEADLOCK (guard disjunction)',false,List,Summary). |
136 | | tcltk_analyse_deadlock(list(['Cannot evaluate deadlock yet.\nInitialise machine first.\n\n'|List]),[]) :- |
137 | | get_unsorted_all_guards_false_pred(BExpr), |
138 | | get_conjuncts(BExpr,List,undefined). |
139 | | |
140 | | tcltk_analyse_properties(list([Res]),[]) :- |
141 | | b_get_properties_from_machine(b(truth,_,_)),!, |
142 | | (b_machine_has_constants |
143 | | -> Res = 'This Machine has only typing information in the PROPERTIES.' |
144 | | ; Res = 'This Machine has no PROPERTIES clause.' |
145 | | ). |
146 | | tcltk_analyse_properties(list(List),Summary) :- |
147 | | current_b_state_with_all_constants_valued, |
148 | | !, |
149 | | b_get_properties_from_machine(Prop), |
150 | | get_preference(partition_predicates,Partition), |
151 | ? | b_analyse_boolean_expression(Prop,'PROPERTIES',Partition,List,Summary). |
152 | | tcltk_analyse_properties(list(['Cannot evaluate properties yet.\nSet up constants first.\n\n'|List]),Summary) :- |
153 | | b_get_properties_from_machine(Prop), |
154 | | (get_preference(partition_predicates,false) |
155 | | -> get_conjuncts(Prop,List,undefined), length(List,NrConjuncts) |
156 | | ; bsyntaxtree:predicate_components(Prop,Components), |
157 | | findall(CList, |
158 | | (member(component(CP,_CompVars),Components), |
159 | | get_conjuncts(CP,CList,undefined)), CCList), |
160 | | flatten_comp(CCList,List,NrConjuncts) |
161 | | ), |
162 | | unknown_summary(NrConjuncts,Summary). |
163 | | |
164 | | flatten_comp([],[],0). |
165 | | flatten_comp([H|T],['COMPONENT:\n'|Res],NrConjuncts) :- |
166 | | flatten_comp(T,TR,TNr), |
167 | | length(H,LH), NrConjuncts is LH+TNr, |
168 | | append(H,TR,Res). |
169 | | |
170 | | |
171 | | %:- dynamic conj_nr/1. |
172 | | %conj_nr(1). |
173 | | %get_and_inc_conj_nr(X) :- retract(conj_nr(X)), X1 is X+1, assertz(conj_nr(X1)). |
174 | | %reset_conj_nr :- retractall(conj_nr(_)), assertz(conj_nr(1)). |
175 | | |
176 | | |
177 | | % check if a summary report is all true |
178 | | check_summary_all_true(L) :- |
179 | ? | member(total_after_expansion/Nr,L), member(true_after_expansion/Nr,L), |
180 | ? | member(false/0,L), member(unknown/0,L), member(timeout/0,L). |
181 | | |
182 | | empty_summary([total/0, total_after_expansion/0, |
183 | | true/0, true_after_expansion/0, false/0, false_after_expansion/0, |
184 | | unknown/0, unknown_after_expansion/0, timeout/0, runtime/0, enum_warning/0]). |
185 | | :- use_module(specfile, [current_b_expression/1]). |
186 | | :- use_module(state_space, [current_state_id/1]). |
187 | | get_current_b_expression(S) :- |
188 | | (current_b_expression(S) -> true |
189 | | ; current_state_id(root), \+ b_machine_has_constants -> S = [] |
190 | | ; add_internal_error('Could not get B state for analysis',get_current_b_expression(S)),S=[] |
191 | | ). |
192 | | b_analyse_boolean_expression(VAR,Kind,P,L,S) :- var(VAR),!, |
193 | | add_internal_error('Illegal call: ',b_analyse_boolean_expression(VAR,Kind,P,L,S)), |
194 | | S=[unknown/1]. |
195 | | b_analyse_boolean_expression(BExpr,Kind,Partition,List, |
196 | | [total/NrTotal, total_after_expansion/NrTotalExpanded, |
197 | | true/NrTrue, true_after_expansion/NrTrueExpanded, |
198 | | false/NrFalse, false_after_expansion/NrFalseExpanded, |
199 | | unknown/NrUnknown, unknown_after_expansion/NrUnknownExpanded, |
200 | | timeout/NrTimeout, runtime/TotalTime, enum_warning/NrEnumWarning]) :- |
201 | | get_current_b_expression(RealCurState), |
202 | | %reset_conj_nr, |
203 | | statistics(walltime,[CurTime,_]), /* get current time in ms */ |
204 | | b_analyze_all_sub_conjuncts(Kind,Partition,BExpr, RealCurState, CList), |
205 | | statistics(walltime,[CurTime2,_]), /* get current time in ms */ |
206 | | formatsilent('~N=======~NSUMMARY (~w)~N=======~N',[Kind]), |
207 | | new_counter(Counter0), |
208 | ? | l_translate_conjunct_analysis_result(CList,List,none/true,'$_no_model'(none),Counter0,ResCounter), |
209 | | %print_counter(ResCounter), |
210 | | get_counter(total,ResCounter,NrTotal), get_counter(total_after_expansion,ResCounter,NrTotalExpanded), |
211 | | get_counter(true,ResCounter,NrTrue), get_counter(true_after_expansion,ResCounter,NrTrueExpanded), |
212 | | get_counter(false,ResCounter,NrFalse), get_counter(false_after_expansion,ResCounter,NrFalseExpanded), |
213 | | get_counter(unknown,ResCounter,NrUnknown), get_counter(unknown_after_expansion,ResCounter,NrUnknownExpanded), |
214 | | get_counter(timeout,ResCounter,NrTimeout), get_counter(enum_warning,ResCounter,NrEnumWarning), |
215 | | (silent_mode(on),NrFalse==0,NrUnknown==0 -> true |
216 | | ; NrTotal=NrTotalExpanded |
217 | | -> format('~p predicates: ~p TRUE, ~p FALSE, ~p UNKNOWN (~p timeouts)~N',[NrTotal,NrTrue,NrFalse,NrUnknown,NrTimeout]) |
218 | | ; format('~p predicates (~p after expansion): ~p TRUE (~p after expansion), ~p FALSE (~p after expansion), ~p UNKNOWN (~p after expansion) (~p timeouts)~N',[NrTotal,NrTotalExpanded,NrTrue,NrTrueExpanded,NrFalse,NrFalseExpanded,NrUnknown,NrUnknownExpanded,NrTimeout])), |
219 | | TotalTime is CurTime2-CurTime, |
220 | | formatsilent('~w ms to analyse ~w~n',[TotalTime,Kind]). |
221 | | |
222 | | :- use_module(bsyntaxtree). |
223 | | |
224 | | b_analyze_all_sub_conjuncts(Kind,true,BExpr, RealCurState, CList) :- !, |
225 | | bsyntaxtree:predicate_components(BExpr,Components), % Note: does not support list of conjunctions (as generated by b_get_assertions_from_machine) |
226 | | findall(comp_res(CCList,CompVars), |
227 | | (member(component(CompBExpr,CompVars),Components), |
228 | | analyze_all_conjuncts2(Kind,CompBExpr, RealCurState, CCList)),CompRes), |
229 | | flatten_comp_res(CompRes,CList). |
230 | | b_analyze_all_sub_conjuncts(Kind,_Partition,BExpr, RealCurState, CList) :- |
231 | | analyze_all_conjuncts2(Kind,BExpr, RealCurState, CList). |
232 | | |
233 | | flatten_comp_res([],[]). |
234 | | flatten_comp_res([comp_res(CCList,Vars)|T],Res) :- |
235 | | flatten_comp_res(T,FT), |
236 | | append([vars(Vars)|CCList],FT,Res). |
237 | | |
238 | | analyze_all_conjuncts2(Kind,BExpr, RealCurState, CList) :- |
239 | | findall(conj(Nr,Conjunct,Status,EnumWarning,IsExpanded), |
240 | | (%get_and_inc_conj_nr(Nr), print(analysing_conjunct(Nr)),nl, |
241 | | b_analyse_conjunction(Kind,BExpr,[],RealCurState,Nr,Conjunct,Status,EnumWarning,IsExpanded) |
242 | | ), |
243 | | CList). |
244 | | |
245 | | %:- use_module(library(codesio)). |
246 | | :- use_module(tools_meta,[translate_term_into_atom/2]). |
247 | | l_translate_conjunct_analysis_result([],[],CurConjNr/CurStatus,_LastModel,Acc,ResAcc) :- |
248 | | flush_cur_status(CurConjNr,CurStatus,Acc,ResAcc). |
249 | | l_translate_conjunct_analysis_result([vars(Vars)|T],Res,CurConjNr/CurStatus,_LastModel,Acc,CRes) :- |
250 | | flush_cur_status(CurConjNr,CurStatus,Acc,Acc1), |
251 | | (Vars=[] -> Res = ['** ISOLATED CONJUNCT **\n'|TT] |
252 | | ; Res = [CS,CV,' **\n'|TT], CS = '** CONJUNCTS OVER IDENTIFIERS:'), |
253 | | translate_term_into_atom(Vars,CV), |
254 | ? | l_translate_conjunct_analysis_result(T,TT,none/true,'$_no_model'(none),Acc1,CRes). |
255 | | l_translate_conjunct_analysis_result([conj(NextNr,Conjunct,Status,EnumWarning,IsExpanded)|T],TranslationResult,CurConjNr/CurStatus,LastModel,Acc,CRes) :- |
256 | | translate_conjunct(Conjunct,Status,EnumWarning,CS), |
257 | | inc_counter(total_after_expansion,Acc,AA), |
258 | | (EnumWarning==true -> inc_counter(enum_warning,AA,A1) ; AA=A1), |
259 | | ( Status = 'TRUE' -> inc_counter(true_after_expansion,A1,NewAcc) |
260 | | ; Status = timeout_true -> inc_counter(true_after_expansion,A1,A2),inc_counter(timeout,A2,NewAcc) |
261 | | ; Status = false -> inc_counter(false_after_expansion,A1,NewAcc) |
262 | | ; Status = timeout_false -> inc_counter(false_after_expansion,A1,A2),inc_counter(timeout,A2,NewAcc) |
263 | | ; Status = both_true_false -> inc_counter(unknown_after_expansion,A1,NewAcc) |
264 | | ; Status = wd_error_true -> inc_counter(unknown_after_expansion,A1,NewAcc) |
265 | | ; Status = wd_error_false -> inc_counter(unknown_after_expansion,A1,NewAcc) |
266 | | ; inc_counter(unknown_after_expansion,A1,A2),inc_counter(timeout,A2,NewAcc) |
267 | | ), |
268 | | (call_conjunct_error_hook(Conjunct,Status,EnumWarning,IsExpanded, CS) -> true |
269 | | ; add_internal_error('Conjunct error hook failed: ', |
270 | | call_conjunct_error_hook(Conjunct,Status,EnumWarning,IsExpanded, CS))), |
271 | | (CurConjNr==NextNr |
272 | | -> combine_status(CurStatus,Status,NewStatus), NewAcc=NewAcc2 % still the same conjunct; combine status |
273 | | ; flush_cur_status(CurConjNr,CurStatus,NewAcc,NewAcc2), |
274 | | convert_status(Status,NewStatus)), |
275 | | ((get_rodin_model_name(Conjunct,NewModel), NewModel \= LastModel) |
276 | | -> TranslationResult = [' * MODEL: ',NewModel,'\n',CS|TT] % show from which model the conjuncts come from |
277 | | ; NewModel = LastModel, TranslationResult = [CS|TT]), |
278 | ? | l_translate_conjunct_analysis_result(T,TT,NextNr/NewStatus,NewModel,NewAcc2,CRes). |
279 | | |
280 | | flush_cur_status(CurConjNr,CurStatus,Acc,NewAcc2) :- |
281 | | (CurConjNr==none -> Acc=NewAcc2 |
282 | | ; inc_counter(total,Acc,NewAcc1), |
283 | | (CurStatus = true -> inc_counter(true,NewAcc1,NewAcc2) |
284 | | ; CurStatus = false -> inc_counter(false,NewAcc1,NewAcc2) |
285 | | ; inc_counter(unknown,NewAcc1,NewAcc2)) ). |
286 | | |
287 | | combine_status(true,X,R) :- convert_status(X,R). |
288 | | combine_status(false,_,false). |
289 | | combine_status(unknown,false,R) :- !,R=false. |
290 | | combine_status(unknown,_,unknown). |
291 | | |
292 | | convert_status('TRUE',true) :- !. |
293 | | convert_status(timeout_true,true) :- !. |
294 | | convert_status(false,false) :- !. |
295 | | convert_status(timeout_false,false) :- !. |
296 | | convert_status(_,unknown). |
297 | | |
298 | | :- dynamic conjunct_error_hook_pred/1. |
299 | | conjunct_error_hook_pred(default_hook). |
300 | | call_conjunct_error_hook(Conjunct,Status,EnumWarning,IsExpanded, TranslatedConjunct) :- |
301 | | convert_status(Status,ConvStatus), |
302 | | conjunct_error_hook_pred(Pred), |
303 | | call(Pred,Conjunct,ConvStatus,EnumWarning,IsExpanded, TranslatedConjunct). |
304 | | :- public default_hook/5. |
305 | | default_hook(_,_Status,_,_, _CS). |
306 | | %((Status='TRUE' ; Status = timeout_true) -> print(CS),nl ; true). |
307 | | |
308 | | :- use_module(error_manager,[register_error_span/2]). |
309 | | register_span_hook(Conjunct,ConvStatus,_EnumWarning,_IsExpanded, _TranslatedConjunct) :- |
310 | | %print(status(ConvStatus)),nl, |
311 | | (ConvStatus=true -> true ; register_error_span(predicate_evaluator,Conjunct)). % store positions for Tcl/Tk |
312 | | |
313 | | reset_conjunct_error_hook :- retractall(conjunct_error_hook_pred(_)), assertz(conjunct_error_hook_pred(default_hook)). |
314 | | |
315 | | register_conjunct_error_hook(Pred) :- retractall(conjunct_error_hook_pred(_)), assertz(conjunct_error_hook_pred(Pred)). |
316 | | |
317 | | register_conjunct_register_error_span :- register_conjunct_error_hook(register_span_hook). |
318 | | |
319 | | |
320 | | % some utilities to count occurences of certain things: |
321 | | :- use_module(library(assoc)). |
322 | | new_counter(C) :- empty_assoc(C). |
323 | | %reset_counter(Key,C,NC) :- put_assoc(Key,C,0,NC). |
324 | | inc_counter(Key,C,NC) :- (get_assoc(Key,C,Val) -> NewVal is Val+1 ; NewVal =1), |
325 | | put_assoc(Key,C,NewVal,NC). |
326 | | %cond_inc_counter(true,Key,C,NC) :- !, inc_counter(Key,C,NC). |
327 | | %cond_inc_counter(_,_,C,C). |
328 | | get_counter(Key,C,Val) :- (get_assoc(Key,C,Val) -> true ; Val =0). |
329 | | %print_counter(C) :- portray_assoc(C),nl. |
330 | | %get_counter_list(C,L) :- assoc_to_list(C,L). |
331 | | |
332 | | :- use_module(b_interpreter). |
333 | | :- use_module(bsyntaxtree,[is_a_conjunct/3]). |
334 | | |
335 | | % conjunct_ |
336 | | %b_analyse_conjunction(C, LocalState,State,Conjunct,Status,EnumWarning) :- tools_printing:print_term_summary(C),nl,fail. |
337 | | |
338 | | :- use_module(library(lists),[nth1/3]). |
339 | | b_analyse_conjunction(Kind,Conj, LocalState,State,Nr,EvConjunct,Status,EnumWarning,IsExpanded) :- |
340 | | flatten_conjunction(Conj,ConjList,[]), |
341 | ? | nth1(Nr, ConjList, Conjunct), |
342 | ? | b_analyse_conjunction_nr(Kind,Nr,Conjunct,LocalState,State,EvConjunct,Status,EnumWarning,IsExpanded). |
343 | | |
344 | | flatten_conjunction([]) --> !, []. |
345 | | flatten_conjunction([H|T]) --> !, flatten_conjunction(H),flatten_conjunction(T). |
346 | | flatten_conjunction(C) --> {is_a_conjunct(C,LHS,RHS)}, |
347 | ? | {\+ do_not_decompose(C)}, % otherwise the user wants to group it |
348 | | !, |
349 | | flatten_conjunction(LHS), flatten_conjunction(RHS). |
350 | | flatten_conjunction(C) --> [C]. |
351 | | |
352 | | % maybe provide option to always decompose for precision/performance/forall expansion |
353 | ? | do_not_decompose(C) :- bsyntaxtree:get_texpr_labels(C,_),!. % otherwise the user wants to group it |
354 | | do_not_decompose(C) :- get_texpr_info(C,Infos), |
355 | ? | member(generated_conjunct,Infos). % the conjunct has been generated, e.g., by b_ast_cleanup |
356 | | |
357 | | :- use_module(tools_meta,[safe_time_out/3]). |
358 | | analyse_quick_time_out(Call) :- |
359 | | get_time_out_preference_with_factor(0.05,DTimeOut), |
360 | | (DTimeOut > 400 -> TO = 400 ; TO = DTimeOut), |
361 | ? | on_enumeration_warning( |
362 | | safe_time_out(Call,TO,Res), |
363 | | fail), % in case of enumeration warnings: we could not compute all elements of domain |
364 | | \+ is_time_out_result(Res). |
365 | | %analyse_time_out(Call) :- |
366 | | % preferences:get_computed_preference(debug_time_out,DTimeOut), |
367 | | % time_out_with_enum_warning_one_solution(Call,DTimeOut,Res), |
368 | | % \+ is_time_out_result(Res). |
369 | | |
370 | | |
371 | | :- use_module(b_interpreter,[b_generate_for_all_list_domain_nolwf/8]). |
372 | | :- use_module(kernel_waitflags,[ground_wait_flags/1,init_wait_flags/2]). |
373 | | |
374 | | b_analyse_conjunction_nr(Kind,Nr,b(forall(Parameters,LHS,RHS),pred,Info),LocalState,State,EvConjunct,Status,EnumWarning,IsExpanded) :- |
375 | | IsExpanded=true, |
376 | | get_preference(expand_forall_upto,MaxLenForEpxansion), |
377 | | MaxLenForEpxansion>0, |
378 | | % print(generating_for_all_domain(Parameters)),nl, |
379 | ? | analyse_quick_time_out(( |
380 | | kernel_waitflags:init_wait_flags_with_call_stack(WF,[prob_command_context(analyse_quick_time_out,Info)]), |
381 | | b_interpreter:b_generate_for_all_list_domain_nolwf(Parameters,LHS,RHS,LocalState,State,AllSols,NewRHS,WF), |
382 | | kernel_waitflags:ground_wait_flags(WF))), |
383 | | length(AllSols,Len), Len>0, Len=<MaxLenForEpxansion,!, |
384 | | (silent_mode(on) -> true ; print('FORALL domain computed: '), translate:print_bexpr(LHS),nl), |
385 | ? | member(ParResult,AllSols), |
386 | | set_up_equality(Parameters,ParResult,EqList), |
387 | | conjunct_predicates(EqList,EqPred), |
388 | | EvConjunct = b(forall(Parameters,EqPred,RHS),pred,Info), |
389 | | (debug_mode(on) -> print('Testing: '), print_bexpr(EvConjunct),nl ; true), |
390 | | set_up_typed_localstate(Parameters,ParResult,_,LocalState,NewLocalState,positive), |
391 | | b_analyse_individual_conjunct(Kind,Nr,NewRHS,NewLocalState,State,_,Status,EnumWarning). |
392 | | b_analyse_conjunction_nr(Kind,Nr,Conjunct,LocalState,State,EvConjunct,Status,EnumWarning,IsExpanded) :- |
393 | | %debug:debug_println(9,analysing_conjunct_in_component(Nr)), |
394 | | IsExpanded=false, |
395 | | b_analyse_individual_conjunct(Kind,Nr,Conjunct,LocalState,State,EvConjunct,Status,EnumWarning). |
396 | | |
397 | | set_up_equality([],[],[]). |
398 | | set_up_equality([Identifier|IR],[Val|VR],[b(equal(Identifier,b(value(Val),IDType,[])),pred,[])|TE]) :- |
399 | | get_texpr_type(Identifier,IDType), |
400 | | set_up_equality(IR,VR,TE). |
401 | | |
402 | | |
403 | | |
404 | | :- use_module(tools_printing,[print_goal/1]). |
405 | | :- use_module(tools_meta,[safe_time_out/3,call_residue/2]). |
406 | | |
407 | | call_residue_check(Call) :- |
408 | ? | call_residue(Call,Residue), |
409 | | (Residue = [] -> true |
410 | | ; add_internal_error('Call had residue: ',residue(Residue)/call(Call)), |
411 | | print_goal(Residue),nl |
412 | | ). |
413 | | |
414 | | :- use_module(clpfd_interface,[catch_clpfd_overflow_call1/1]). |
415 | | % very similar to time_out_with_enum_warning, adds catch_clpfd_overflow_call1 |
416 | | time_out_and_catch_errors(Call,TimeOut,Result) :- |
417 | ? | catch( |
418 | | safe_time_out(catch_clpfd_overflow_call1(call_residue_check(Call)),TimeOut,Result), |
419 | | enumeration_warning(_,_,_,_,_), |
420 | | (print('Treating enumeration warning as TIMEOUT !'),nl, |
421 | | Result=time_out)). |
422 | | |
423 | | :- use_module(tools_printing,[print_red/1, print_green/1]). |
424 | | b_analyse_individual_conjunct(Kind,Nr,Conjunct,LocalState,State,Conjunct,Status,EnumWarning) :- |
425 | | |
426 | | get_computed_preference(debug_time_out,DTimeOut), |
427 | | |
428 | | (silent_mode(on) -> true |
429 | | ; nl,print('==> '),translate:print_bexpr(Conjunct),nl), |
430 | | %translate:print_bexpr_with_limit(Conjunct,250),nl, |
431 | | statistics(walltime,[T1,_]), |
432 | | enter_new_error_scope(ScopeID,b_analyse_individual_conjunct), |
433 | ? | (time_out_and_catch_errors(b_test_boolean_expression_cs(Conjunct,LocalState,State,Kind,Nr), |
434 | | DTimeOut,TimeOutRes) -> |
435 | | /* PREDICATE TRUE */ |
436 | | statistics(walltime,[T2,_]),check_enum_warning(positive,EnumWarning), |
437 | | exit_error_scope(ScopeID,ErrOcc,b_analyse_individual_conjunct_true), % Warning: if strict_raise_warnings is set to true we also get the warnings as errors here !! |
438 | | (TimeOutRes=time_out |
439 | | -> print_red('TIMEOUT'),nl,Status=timeout |
440 | | ; ErrOcc=true -> (get_preference(strict_raise_warnings,true) -> print('POSSIBLY ') ; true), |
441 | | print_red('NOT-WELL-DEFINED (TRUE)'),nl, Status = wd_error_true |
442 | | ; print_true, |
443 | | enter_new_error_scope(NegScopeID,b_analyse_double_evaluation_true), |
444 | | ((get_preference(double_evaluation_when_analysing,true), |
445 | | time_out_and_catch_errors(b_not_test_boolean_expression_cs(Conjunct,LocalState,State,Kind,Nr), |
446 | | DTimeOut,TimeOutRes2)) |
447 | | -> (TimeOutRes2==time_out -> Status = timeout_true |
448 | | ; Status = both_true_false, |
449 | | add_error(predicate_evaluator,'Conjunct is both true and false: ',Conjunct,Conjunct) |
450 | | ) |
451 | | ; Status = 'TRUE' ), |
452 | | check_enum_warning(_,_NegEnumWarning), |
453 | | exit_error_scope(NegScopeID,_NegTErrorOccurred,b_analyse_double_evaluation_true) % Should we also report those errors ? + NegEnumWarning ? |
454 | | ) |
455 | | ; /* PREDICATE FALSE */ |
456 | | statistics(walltime,[T2,_]), check_enum_warning(negative,EnumWarning), |
457 | | exit_error_scope(ScopeID,ErrOcc,b_analyse_individual_conjunct_false), |
458 | | (ErrOcc=true |
459 | | -> print_red('NOT-WELL-DEFINED (FALSE)'),nl, Status = wd_error_false |
460 | | ; (EnumWarning=true -> print_red('UNKNOWN'),nl |
461 | | ; print_false), |
462 | | enter_new_error_scope(NegScopeID,b_analyse_double_evaluation_false), |
463 | | ((get_preference(double_evaluation_when_analysing,false) ; |
464 | ? | time_out_and_catch_errors( |
465 | | b_not_test_boolean_expression_cs(Conjunct,LocalState,State,Kind,Nr), |
466 | | DTimeOut,TimeOutRes2)) |
467 | | -> (TimeOutRes2==time_out -> Status = timeout_false ; Status = false) |
468 | | ; Status = unknown), |
469 | | exit_error_scope(NegScopeID,_NegFErrorOccurred,b_analyse_double_evaluation_false) % Should we also report those errors ? + NegEnumWarning ? |
470 | | ) |
471 | | ), |
472 | | statistics(walltime,[T3,_]), |
473 | | Time1 is T2-T1, Time2 is T3-T2, Time is T3-T1, |
474 | | (silent_mode(on) -> true |
475 | | ; format('~w (~w ms + ~w ms = ~w ms)',[Status,Time1,Time2,Time]), |
476 | | (Status = 'TRUE' -> true |
477 | | ; translate:translate_span(Conjunct,SpanS), SpanS \= '' -> print(' '),print(SpanS) |
478 | | ; true),nl |
479 | | ). |
480 | | |
481 | | print_true :- silent_mode(on),!. |
482 | | print_true :- print_green('TRUE'),nl. |
483 | | print_false :- silent_mode(on),!. |
484 | | print_false :- print_red('FALSE'),nl. |
485 | | |
486 | | |
487 | | check_enum_warning(positive,EnumWarning) :- !, |
488 | | (enumeration_warning_occured_in_error_scope |
489 | | -> print('### Enumeration warnings not critical (solution found)'),nl %, error_manager:trace_if_user_wants_it %% |
490 | | ; true), EnumWarning = false. |
491 | | check_enum_warning(_,EnumWarning) :- |
492 | | \+ critical_enumeration_warning_occured_in_error_scope,!, |
493 | | EnumWarning = false. |
494 | | check_enum_warning(_,EnumWarning) :- |
495 | | (event_occurred_in_error_scope(enumeration_warning(enumerating(_),_,_,_,_)) |
496 | | -> print('### Warning: infinite types were enumerated'),nl, E1=true |
497 | | ; true), |
498 | | (event_occurred_in_error_scope(enumeration_warning(checking_finite_closure,_,_,_,_)) |
499 | | -> print('### Warning: finiteness of comprehension sets could not be determined'),nl, E2=true |
500 | | ; true), |
501 | | (var(E1),var(E2) |
502 | | -> print('### Unknown enumeration warning occurred !') |
503 | | ; true), |
504 | | EnumWarning = true. |
505 | | |
506 | | translate_conjunct([],_Status,_EnumWarning,'EmptyInvariant') :- !. |
507 | | /* translate_conjunct(Conjunct,true,true) :- !. */ /* do not show true conjuncts */ |
508 | | translate_conjunct(Conjunct,Status,EnumWarning,Translation) :- |
509 | | translate_conj_status(Conjunct,Status,EnumWarning,Translation). |
510 | | |
511 | | |
512 | | get_conjuncts(BoolExpr,Conjuncts,Status) :- findall(C,get_conjunct(BoolExpr,C,Status),Conjuncts). |
513 | | get_conjunct(C,Conjunct,Status) :- is_a_conjunct(C,LHS,RHS),!, |
514 | | (get_conjuncts(LHS,Conjunct,Status) ; get_conjuncts(RHS,Conjunct,Status)). |
515 | | get_conjunct(Conjunct,Translation,Status) :- |
516 | | translate_conj_status(Conjunct,Status,false,Translation). |
517 | | |
518 | | translate_conj_status(Conjunct,Status,EnumWarning,Translation) :- |
519 | | translate_bexpression_with_limit(Conjunct,500,TC), |
520 | | (EnumWarning=true -> |
521 | | ajoin([TC,'\n == ',Status,' (#### infinite set expanded ####)\n\n'],Translation) |
522 | | ; ajoin([TC,'\n == ',Status,'\n\n'],Translation)). |
523 | | |