1 % (c) 2009-2025 Lehrstuhl fuer Softwaretechnik und Programmiersprachen,
2 % Heinrich Heine Universitaet Duesseldorf
3 % This software is licenced under EPL 1.0 (http://www.eclipse.org/org/documents/epl-v10.html)
4
5
6 :- module(tools, [exact_member/2,
7 exact_member_lookup/4, exact_member_replace/5,
8 remove/3, remove_all/3, insert/3,
9 remove_variables/3,
10 list_intersection/3, list_difference/3,
11 disjoint_list_union/3, lists_are_disjoint/2,
12 string_concatenate/3, is_upper_case_name/1,
13 %write_to_file/2, write_to_utf8_file/2, put_codes/2, % now in tools_files
14 %print_error/1,
15 print_message/1, print_message_with_max_depth/2,
16 print_short_msg/1,
17 print_bt_message/1, print_bt_trace_message/1, bt_trace/1,
18 format_bt/2,
19 print_wtime/1,
20 print_mb/1, print_gb/1,
21 prints/1,
22 is_absolute_path/1,
23 split_common_path/4,
24 gen_relative_path/3, gen_relative_path_to_cur_dir/2,
25 get_parent_directory/2, get_parent_directory_name/2,
26 get_parent_directory_of_directory/2,
27 get_tail_filename/2, get_modulename_filename/2,
28 get_filename_extension/2,
29 get_option_from_list/4, get_options_from_list/2,
30 get_options/4,get_options/5,
31 arg_is_number/2, arg_is_number_or_wildcard/2,
32 check_filename_arg/2,
33 arg_is_integer/2,
34 split_filename/3,
35 safe_absolute_file_name/2, safe_absolute_file_name/3,
36 host_file_name_case_insensitive/0,
37 same_file_name/2,
38
39 filter/4,
40 flatten/2,
41 % count_occurences/2, % now in tools_lists
42 split_last/4,
43 split_atom/3, split_chars/3, split_complex_sep/3,
44 latex_escape_atom/2,
45 b_escape_string_atom/2, b_string_escape_codes/2,
46 string_escape/2, print_escaped/1, % can also be used for dot
47 simple_dot_string_escape/2, % for dot, just quoting "
48 html_escape/2, html_escape_codes/2,
49 xml_attribute_escape/2,
50 read_term_from_file/2,
51 safe_read_term_from_atom/2, safe_read_term_from_codes/2,
52 read_string_from_file/2, read_string_from_file/3,
53 safe_read_string_from_file/3,
54 read_atom_from_file/3,
55 % write_lines_to_file/2, % now in tools_files
56 open_temp_file/3,
57 open_temp_file/4,
58 detect_xml_encoding/3,
59
60 ajoin/2,
61 ajoin_with_limit/3,
62 ajoin_with_sep/3,
63 substitute/4,
64 % call_residue/2, % now in tools_meta
65
66 catch_call/1,
67 observe_user_interrupt_signal/3,
68 %safe_on_exception/3, safe_on_exception_silent/3, % now in tools_meta
69 %reraise_important_exception/1, % now in tools_meta
70 % catch_matching/3, % now in tools_meta
71
72 convert_list_into_pairs/2, convert_pairs_into_list/3,
73
74 safe_univ/2, safe_univ_no_cutoff/2,
75 safe_sort/3,
76 safe_set_sort/3,
77 safe_functor/4,
78
79 safe_atom_codes/2, safe_atom_chars/3,
80 atom_codes_with_limit/2, atom_codes_with_limit/3,
81 truncate_atom/3, wrap_and_truncate_atom/4,
82 safe_number_codes/2,
83 atom_or_number_codes/2,
84 ensure_atom/2,
85 number_suffix/3,
86
87 print_size_of_table/1,
88 print_runtime/0, start_ms_timer/1, stop_ms_timer/2, stop_ms_timer/1,
89 stop_ms_timer_with_msg/2,
90 stop_ms_timer_with_silent_msg/2,
91 stop_ms_timer_with_debug_msg/2,
92 stop_ms_walltimer_with_msg/2,
93 bt_start_ms_timer/1, bt_stop_ms_timer/1,
94 get_elapsed_walltime/2,
95 get_elapsed_timer/2, combiner_timer/3,
96 get_elapsed_runtime/2,
97 cputime/1, walltime/1,
98 convert_ms_time_to_string/2,
99
100 retract_with_statistics/2,
101 statistics_memory_used/1,
102 get_memory_used/1, print_memory_used_difference/2, print_memory_used/1,
103 print_memory_used_wo_gc/0, print_memory_used_wo_gc/1,
104
105 split_list/4,
106 split_list_idx/5, re_split_list_idx/4,
107
108 minimize_lasso/4,
109
110 map_split_list/4,
111 foldl/4,foldl/5,foldl/6,
112 maplist5/5,
113 average/2,
114
115 map_optlist/2,
116 optlist_to_list/2,
117
118 (space_call)/1,
119 assert_once/1,
120
121 % safe_time_out/3, % now in tools_meta
122 % time_out_call/2, time_out_call/1, time_out_with_factor_call/3, % now in tools_timeout
123
124 unique_id/2,
125
126 get_PROBPATH/1,
127 atom_to_number/2,
128 get_set_optional_prolog_flag/3,
129
130 bb_safe_get/2,
131 bb_inc/1, bb_inc_by/2,
132
133 top_sort/3
134 ]).
135
136 :- meta_predicate foldl(3,?,?,?).
137 :- meta_predicate foldl2(?,3,?,?).
138 :- meta_predicate foldl(4,?,?,?,?).
139 :- meta_predicate foldl2(?,4,?,?,?).
140 :- meta_predicate foldl(5,?,?,?,?,?).
141 :- meta_predicate foldl2(?,5,?,?,?,?).
142 :- meta_predicate maplist5(4,-,-,-,-).
143 :- meta_predicate catch_call(0).
144 :- meta_predicate assert_once(0).
145 :- meta_predicate split_list_idx(1,?,?,?,?).
146 :- meta_predicate split_list_idx2(?,1,?,?,?).
147 :- meta_predicate map_split_list(2,?,?,?).
148 :- meta_predicate map_split_list2(?,2,?,?).
149 :- meta_predicate split_list(1,?,?,?).
150 :- meta_predicate split_list2(?,1,?,?).
151 :- meta_predicate observe_user_interrupt_signal(-,-,0).
152 :- meta_predicate filter(1,*,*,*).
153 :- meta_predicate get_options(+,4,-,-).
154 :- meta_predicate get_options(+,4,-,-,0).
155 :- meta_predicate retract_all_count(0,-,-).
156 :- meta_predicate space_call(0).
157 :- meta_predicate map_optlist(1,-).
158 :- meta_predicate map_optlist_aux(-,1).
159 :- meta_predicate call_optional(1,-).
160 :- meta_predicate call_optional_aux(-,1,-).
161
162 :- meta_predicate get_calls_for_table(:,-).
163 :- meta_predicate print_size_of_table(:).
164
165 :- meta_predicate string_escape(2,-,-).
166
167 :- meta_predicate bb_safe_get(0,-).
168 :- meta_predicate bb_inc(0).
169 :- meta_predicate bb_inc_by(0,-).
170
171 :- use_module(module_information).
172
173 :- module_info(group,infrastructure).
174 :- module_info(description,'This module contains many general helper predicates.').
175
176 :- use_module(pathes,[runtime_application_path/1]). % we just import it to set-up pathes, we don't need any predicates
177
178 :- use_module(library(lists)).
179 :- use_module(library(system)).
180 %%:- use_module(library(file_systems)). %% not required ?
181 %% :- use_module(library(codesio)).
182
183
184 :- use_module(tools_meta,[reraise_important_exception/1, safe_on_exception/3]).
185
186 :- set_prolog_flag(double_quotes, codes).
187
188
189 catch_call(Call) :-
190 catch(call(Call), Exception, (
191 add_error(catch_call,'Call raised an exception: ',(Call:Exception)),
192 /* read(_), */
193 reraise_important_exception(Exception),
194 fail
195 )).
196
197 :- use_module(error_manager,[add_warning/4]).
198 % catch CTRL-C and print a message if it happens and re-throw the interrupt
199 observe_user_interrupt_signal(Context,Span,Call) :-
200 catch(Call, user_interrupt_signal, (
201 add_warning(user_interrupt_signal,'CTRL-C occurred in context: ',Context,Span),
202 %(pending_abort_error(WF,Msg,_ErrTerm,Span)
203 % % Unfortunately any abort errors induced by Call itself are already removed by the exception
204 % -> add_message(user,'Pending WD-Error could cause long runtimes: ',Msg,Span) ; true),
205 throw(user_interrupt_signal)
206 )).
207
208
209
210 :- use_module(self_check). % put after search paths have been set
211
212 % These predicates from tools_platform are tested here to avoid module load order issues.
213
214 :- use_module(tools_platform, [map_host_platform/2, map_host_processor/2, host_platform/1]).
215 :- assert_must_succeed(map_host_platform("x86-win32-nt-4",windows)).
216 :- assert_must_succeed(map_host_platform("x86_64-win32-nt-4",windows)).
217 :- assert_must_succeed(map_host_platform("x86_64-darwin-10.6.0",darwin)).
218 :- assert_must_succeed(map_host_platform("x86_64-darwin-18.7.0",darwin)).
219 :- assert_must_succeed(map_host_platform("arm64-darwin-20.1.0",darwin)).
220 :- assert_must_succeed(map_host_platform("x86-linux-glibc2.7",linux)).
221 :- assert_must_succeed(map_host_platform("x86_64-linux-glibc2.17",linux)).
222 :- assert_must_succeed(map_host_platform("aarch64-linux-glibc2.28",linux)).
223 :- assert_must_succeed(map_host_platform("nonsense-potatos-4.2",unknown)).
224 % On SWI, the arch/host_type flag doesn't include an OS version number.
225 :- assert_must_succeed(map_host_platform("i386-win32",windows)).
226 :- assert_must_succeed(map_host_platform("x64-win64",windows)).
227 :- assert_must_succeed(map_host_platform("x86_64-darwin",darwin)).
228 :- assert_must_succeed(map_host_platform("arm64-darwin",darwin)).
229 :- assert_must_succeed(map_host_platform("aarch64-linux",linux)).
230 :- assert_must_succeed(map_host_platform("nonsense-potatos",unknown)).
231
232 :- assert_must_succeed((host_platform(Platform), Platform \= unknown)).
233
234
235 :- assert_must_succeed(map_host_processor("x86-win32-nt-4",x86)).
236 :- assert_must_succeed(map_host_processor("x86_64-win32-nt-4",x86_64)).
237 :- assert_must_succeed(map_host_processor("x86_64-darwin-10.6.0",x86_64)).
238 :- assert_must_succeed(map_host_processor("x86_64-darwin-18.7.0",x86_64)).
239 :- assert_must_succeed(map_host_processor("arm64-darwin-20.1.0",aarch64)).
240 :- assert_must_succeed(map_host_processor("x86-linux-glibc2.7",x86)).
241 :- assert_must_succeed(map_host_processor("x86_64-linux-glibc2.17",x86_64)).
242 :- assert_must_succeed(map_host_processor("aarch64-linux-glibc2.28",aarch64)).
243 :- assert_must_succeed(map_host_processor("nonsense-potatos-4.2",unknown)).
244 % On SWI, the arch/host_type flag doesn't include an OS version number.
245 :- assert_must_succeed(map_host_processor("i386-win32",x86)).
246 :- assert_must_succeed(map_host_processor("x64-win64",x86_64)).
247 :- assert_must_succeed(map_host_processor("x86_64-darwin",x86_64)).
248 :- assert_must_succeed(map_host_processor("arm64-darwin",aarch64)).
249 :- assert_must_succeed(map_host_processor("aarch64-linux",aarch64)).
250 :- assert_must_succeed(map_host_processor("nonsense-potatos",unknown)).
251
252 % --------------------------------------
253
254 cputime(T) :-
255 statistics(runtime,[T,_]).
256
257 walltime(WT) :-
258 statistics(walltime,[WT,_]).
259 % --------------------------------------
260
261
262 :- use_module(error_manager,[add_error/3, add_internal_error/2]).
263 print_message(Msg) :- print_message_with_max_depth(Msg,20).
264 print_message_with_max_depth(Msg,MaxDepth) :-
265 safe_on_exception(E,print_message2(Msg,MaxDepth),
266 add_error(tools,'Exception in print_message: ',E)). % added because sometimes in Windows/Vista we get an exception here
267 print_message2(Msg,MaxDepth) :-
268 (var(Msg) -> print_message(informational,'_')
269 ; write(user_output,'% '),write_term(user_output,Msg,[max_depth(MaxDepth)]),nl(user_output) ).
270 print_short_msg(Msg) :-
271 write(user_output,Msg).
272
273 format_bt(Msg,P) :- format(Msg,P).
274 format_bt(Msg,P) :- print(' * BACKTRACK: '),format(Msg,P),
275 fail.
276
277 print_bt_message(Msg) :- print_message(Msg).
278 print_bt_message(Msg) :- print_message(backtrack(Msg)),
279 %(Msg = found_enumeration_of_constants(_,_) -> trace ; true),
280 fail.
281
282 % like print_bt_message but trace upon backtrack
283 print_bt_trace_message(Msg) :- print_message(Msg).
284 print_bt_trace_message(Msg) :- trace,
285 print_message(backtrack(Msg)),
286 fail.
287
288 % trace upon backtrack:
289 bt_trace(_) :- true.
290 bt_trace(PP) :- print(' * BACKTRACK: '), print(PP),nl, trace,fail.
291
292 print_wtime(PP) :- statistics(walltime,[WT,_]),
293 format('~w : ~w ms ~n', [PP,WT]).
294
295 % a print that will automatically stop after 25 prints and give the user the option to inspect the printed messages
296 :- dynamic prints_count/1.
297 prints_count(25).
298 prints(L) :- print_bt_message(L),
299 (retract(prints_count(X)) -> true ; X=25),
300 (X<1
301 -> print('*** Stopped printing >'),
302 read(RT),
303 (number(RT) -> X1=RT ; X1=25)
304 ; X1 is X-1
305 ), assertz(prints_count(X1)).
306
307 :- assert_pre(tools:exact_member(_Var,Vs),
308 (list_skeleton(Vs))).
309 :- assert_post(tools:exact_member(_Var,_Vs), true).
310 :- assert_must_succeed(tools:exact_member(V,[V])).
311 :- assert_must_succeed(tools:exact_member(V,[_X,_Z,V])).
312 :- assert_must_fail(tools:exact_member(_W,[_X,_Z,_V])).
313 :- assert_must_fail(tools:exact_member(_W,[])).
314
315 exact_member(X,[Y|T]) :-
316 (X==Y -> true ; exact_member(X,T)).
317
318
319 :- assert_pre(tools:exact_member_lookup(_Var,_ValRes,Vs,Vals),
320 (list_skeleton(Vs),list_skeleton(Vals))).
321 :- assert_post(tools:exact_member_lookup(_Var,_ValRes,_Vs,_Vals), true).
322 :- assert_must_succeed(tools:exact_member_lookup(V,2,[V],[2])).
323 :- assert_must_succeed(tools:exact_member_lookup(V,2,[_X,_Z,V],[1,3,2])).
324 :- assert_must_fail(tools:exact_member_lookup(V,3,[_X,_Z,V],[1,3,2])).
325 :- assert_must_fail(tools:exact_member_lookup(_W,3,[_X,_Z,_V],[1,3,2])).
326 :- assert_must_fail(tools:exact_member_lookup(_W,3,[],[1,3,2])).
327
328 exact_member_lookup(Var,ValRes,[V|TV],[Val|TVal]) :-
329 (Var==V -> ValRes=Val ; exact_member_lookup(Var,ValRes,TV,TVal)).
330
331
332 :- assert_pre(tools:exact_member_replace(_Var,_ValRes,Vs,Vals,_),
333 (list_skeleton(Vs),list_skeleton(Vals))).
334 :- assert_post(tools:exact_member_replace(_Var,_ValRes,_Vs,_Vals,NewVals),
335 list_skeleton(NewVals)).
336 :- assert_must_succeed(tools:exact_member_replace(V,44,[_X,_Z,V],[1,3,2],[1,3,44])).
337 :- assert_must_fail(tools:exact_member_replace(_V,44,[_X,_Z,_VV],[1,3,2],[1,3,44])).
338
339 exact_member_replace(Var,NewVal,[V|TV],[Val|TVal],[NV|TN]) :-
340 ((Var==V) -> (NV=NewVal,TN=TVal)
341 ; (NV=Val,exact_member_replace(Var,NewVal,TV,TVal,TN))).
342
343
344
345 remove([X|T],X,T).
346 ?remove([Y|T],X,[Y|DT]) :- \+(X=Y), remove(T,X,DT).
347
348 :- assert_must_succeed(tools:flatten([],[])).
349 :- assert_must_succeed(tools:flatten([[1,2,3]],[1,2,3])).
350 :- assert_must_succeed(tools:flatten([[1],[[99]],[2,3],[4,5]],[1,99,2,3,4,5])).
351 :- assert_must_succeed(tools:flatten([[]],[])).
352
353 flatten(List,FlatList) :- flatten1(List,[],FlatList).
354 flatten1([],L,L) :- !.
355 flatten1([H|T],Tail,List) :- !, flatten1(H,FlatList,List), flatten1(T,Tail,FlatList).
356 flatten1(NonList,Tail,[NonList|Tail]).
357
358
359 :- use_module(tools_lists,[count_occurences/2]).
360 :- assert_must_succeed((tools_lists:count_occurences([a,b,a,a,b],R),R == [a-3,b-2])).
361
362
363
364 :- assert_must_succeed((tools:filter(var,[1,2,X,3,Y],R,Out),R == [X,Y],Out == [1,2,3])).
365 :- assert_must_succeed((tools:filter(nonvar,[1,2,X,3,Y],R,Out),R == [1,2,3],Out == [X,Y])).
366
367 filter(_Pred,[],[],[]).
368 filter(Pred,[H|T],True,False) :-
369 ? (call(Pred,H) -> True = [H|TT], filter(Pred,T,TT,False)
370 ? ; False = [H|FF], filter(Pred,T,True,FF)).
371
372 :- public is_a_comment/3. % used for testing filter
373 :- assert_must_succeed((tools: filter(tools:is_a_comment('/*','*/'),['This comment ','/* comment */','will be ignored!'],R,Out),
374 R==['/* comment */'],Out==['This comment ','will be ignored!'])).
375 % checking whether an atom is a comment /* ... */
376 is_a_comment(Begin,End,Comment) :-
377 maplist(atom_codes,[Begin,End,Comment],[BL,EL,CL]),
378 ? prefix(CL,BL),suffix(CL,EL).
379
380 :- assert_must_succeed((tools:remove_variables([X,Y,Z],[Y],R),R==[X,Z])).
381 remove_variables(List,Vars,Remaining) :-
382 exclude(exact_member_rev(Vars),List,Remaining).
383 % a version of exact member with reversed parameters - usefull with
384 % higher-order functions
385 exact_member_rev(List,Member) :- exact_member(Member,List).
386
387 :- assert_must_succeed(tools:insert([],a,[a])).
388 :- assert_must_succeed(tools:insert([a,b,c,d],a,[a,b,c,d])).
389 :- assert_must_succeed(tools:insert([a,b,c,d],b,[a,b,c,d])).
390 :- assert_must_succeed(tools:insert([a,b,c,d],d,[a,b,c,d])).
391 :- assert_must_succeed(tools:insert([a,b,c,d],x,[a,b,c,d,x])).
392 insert([],X,[X]).
393 insert([H|T],X,R) :- (H=X -> R=[H|T] ; R=[H|R2],insert(T,X,R2)).
394
395
396 :- assert_must_succeed(tools:list_intersection([a,b,c,d],[d,f,b],[b,d])).
397
398 list_intersection([],_L,[]).
399 list_intersection([H|T],L,Res) :-
400 ? (remove(L,H,NL) -> (Res=[H|RR]) ; (Res=RR,NL=L)),
401 list_intersection(T,NL,RR).
402
403 insert_new([],X,[X]).
404 insert_new([H|T],X,R) :- (H=X -> fail ; R=[H|R2],insert_new(T,X,R2)).
405
406 :- assert_must_succeed(tools:disjoint_list_union([a,b,c,d],[e,f,g],[e,f,g,a,b,c,d])).
407 :- assert_must_fail(tools:disjoint_list_union([a,b,c,d],[d,f,b],_)).
408 :- assert_must_fail(tools:disjoint_list_union([a,b,c,d],[d,f,b],[d,f,b,a,c])).
409 disjoint_list_union([],L,L).
410 disjoint_list_union([H|T],L,Res) :- insert_new(L,H,L2), disjoint_list_union(T,L2,Res).
411
412 :- assert_must_succeed(tools:lists_are_disjoint([a,b,c,d],[e,f,g])).
413 :- assert_must_succeed(tools:lists_are_disjoint([a,b,c,d],[])).
414 :- assert_must_succeed(tools:lists_are_disjoint([],[e,f,g])).
415 :- assert_must_fail(tools:lists_are_disjoint([a,b,c,d],[e,f,g,d])).
416 lists_are_disjoint([],_).
417 lists_are_disjoint([H|T],List2) :- \+ member(H,List2), lists_are_disjoint(T,List2).
418
419 :- assert_must_succeed(tools:list_difference([a,b,c,d],[b,f,d],[a,c])).
420
421 list_difference([],_L,[]).
422 list_difference([H|T],L,Res) :-
423 ? (remove(L,H,NL) -> (Res=RR) ; (Res=[H|RR],NL=L)),
424 list_difference(T,NL,RR).
425
426 :- use_module(tools_strings,[string_concatenate/3]).
427 :- assert_must_succeed(( tools_strings:string_concatenate('5','.10',R), R=='5.10' )).
428
429
430
431
432 :- assert_must_succeed(tools:is_upper_case_name('GOODS')).
433 :- assert_must_succeed(tools:is_upper_case_name('ZZAA')).
434 :- assert_must_fail(tools:is_upper_case_name('capacity')).
435 :- assert_must_fail(tools:is_upper_case_name('PARAs')).
436
437 :- use_module(tools_strings,[safe_name/2]).
438 is_upper_case_name(Name) :-
439 safe_name(Name,AsciiList),
440 upper_case_list(AsciiList).
441
442 upper_case_list([]).
443 upper_case_list([H|T]) :- 0'A =< H, H =< 0'Z, upper_case_list(T).
444
445
446 :- assert_must_succeed(tools:split_common_path('/aaaa/bbb/cc/d.app','/aaaa/bbb/cc/','/aaaa/bbb/cc','d.app')).
447 :- assert_must_succeed(tools:split_common_path('/aaaa/bbb/cc/d.app','/aaaa/bbb/cc/e','/aaaa/bbb/cc','d.app')).
448 :- assert_must_succeed(tools:split_common_path('/aaaa/bbb/cc/d.app','/aaaa/bbb/ce/e','/aaaa/bbb','cc/d.app')).
449 :- assert_must_succeed(tools:split_common_path('/aaaa/bbb/cc/d.app','/aaaa/e','/aaaa','bbb/cc/d.app')).
450 split_common_path(Path1,Path2,CommonPrefix,Suffix1) :-
451 split_atom_with_empty(Path1,['/','\\'],PC1),
452 split_atom_with_empty(Path2,['/','\\'],PC2),
453 split_com_aux(PC1,PC2,Com,Suff),
454 ajoin_with_sep(Com,'/',CommonPrefix),
455 ajoin_with_sep(Suff,'/',Suffix1).
456
457 split_com_aux([],_,[],Suffix) :- !, Suffix=[].
458 split_com_aux([H|T1],[H|T2],[H|CT],Suffix) :- !,
459 split_com_aux(T1,T2,CT,Suffix).
460 split_com_aux(P1,_,[],P1).
461
462
463 :- assert_must_succeed(tools:gen_relative_path('/aaaa/bbb/cc/d.app','/aaaa/e.app','bbb/cc/d.app')).
464 :- assert_must_succeed(tools:gen_relative_path('/aaaa/bbb/cc/d.app','/aaaa/bbb/e.app','cc/d.app')).
465 :- assert_must_succeed(tools:gen_relative_path('/aaaa/bbb/cc/d.app','/aaaa/bbb/cc/e.app','d.app')).
466 :- assert_must_succeed(tools:gen_relative_path('/aaaa/bbb/cc/d.app','/aaaa/xx/e.app','../bbb/cc/d.app')).
467 :- assert_must_succeed(tools:gen_relative_path('/aaaa/bbb/cc/d.app','/aaaa/xx/yy/e.app','../../bbb/cc/d.app')).
468 :- assert_must_succeed(tools:gen_relative_path('/aaaa/bbb/cc/d.app','/aaaa/xx/yy/','../../bbb/cc/d.app')).
469 :- assert_must_succeed(tools:gen_relative_path('d.app','/aaaa/xx/yy/','d.app')).
470 :- assert_must_succeed(tools:gen_relative_path('d/e.app','/aaaa/xx/yy/','d/e.app')).
471 % can be used for already expanded full paths;
472 % the relative_to option absolute_file_name does not seem to work on already expanded full paths
473 gen_relative_path(Path,ReferencePath,RelPath) :-
474 split_atom_with_empty(Path,['/','\\'],PC1),
475 (PC1 = [File|_], File \= ''
476 -> % this is a relative path, not an absolute one; this is not the intended use of this predicate
477 RelPath = Path % but can happen, e.g., in error_manager
478 ; split_atom_with_empty(ReferencePath,['/','\\'],PC2),
479 split_com_aux(PC1,PC2,_,Suff1),
480 split_com_aux(PC2,PC1,_,Suff2),
481 (Suff2=[_|D2] -> true ; D2=Suff2), % remove last entry
482 maplist(replace_by_dotdot,D2,DotDot), % replace directory names by ..
483 append(DotDot,Suff1,Rel1),
484 ajoin_with_sep(Rel1,'/',RelPath)
485 ).
486
487 replace_by_dotdot(_,'..').
488
489 :- use_module(library(file_systems),[current_directory/1]).
490 gen_relative_path_to_cur_dir(Path,RelPath) :-
491 current_directory(CurDir),
492 gen_relative_path(Path,CurDir,RelPath).
493
494
495 :- assert_must_succeed(tools:is_absolute_path('/aaaa/bbb/cc/d.app')).
496 :- assert_must_fail(tools:is_absolute_path('cc/d.app')).
497 :- assert_must_fail(tools:is_absolute_path('./cc/')).
498 is_absolute_path(Path) :-
499 atom_chars(Path,PathAscii),
500 PathAscii = ['/'|_]. % TO DO: add other rules for windows C: ...
501
502 :- assert_must_succeed(tools:get_parent_directory('/aaaa/bbb/cc/d.app','/aaaa/bbb/cc/')).
503 :- assert_must_succeed(tools:get_parent_directory('/aaaa/bbb/cc/','/aaaa/bbb/cc/')).
504 :- assert_must_succeed(tools:get_parent_directory('d.app','')).
505 :- assert_must_succeed(tools:get_parent_directory('/a/b/cc/(machine from Jupyter cell).mch','/a/b/cc/')).
506 :- assert_must_succeed(tools:get_parent_directory('D:\\Users\\OneDrive - hhu\\[Master] PA\\models\\r\\RF.mch',
507 'D:\\Users\\OneDrive - hhu\\[Master] PA\\models\\r\\')).
508
509 get_parent_directory(Path,NewPath) :-
510 atom_chars(Path,PathAscii),
511 strip_last(PathAscii,[],[],New),
512 atom_chars(NewPath,New).
513
514 :- assert_must_succeed(tools:get_parent_directory_of_directory('/aaaa/bbb/cc/d.app','/aaaa/bbb/cc/')).
515 :- assert_must_succeed(tools:get_parent_directory_of_directory('/aaaa/bbb/cc/','/aaaa/bbb/')).
516 :- assert_must_succeed(tools:get_parent_directory_of_directory('\\aaaa\\bb b\\cc\\','\\aaaa\\bb b\\')).
517 % just like get_parent_directory, except when last character is a slash
518 get_parent_directory_of_directory(Path,NewPath) :-
519 atom_chars(Path,PathAscii0),
520 (append(PathAscii,[Last],PathAscii0), is_path_slash(Last)
521 -> true ; PathAscii=PathAscii0),
522 strip_last(PathAscii,[],[],New),
523 atom_chars(NewPath,New).
524
525 is_path_slash('/').
526 is_path_slash('\\'). % Windows
527
528 :- assert_must_succeed(tools:get_parent_directory_name('/aaaa/bbb/cc/d.app','cc')).
529 :- assert_must_succeed(tools:get_parent_directory_name('/aaaa/bbb/cc/','cc')).
530 :- assert_must_succeed(tools:get_parent_directory_name('\\aaaa\\bbb\\cc\\','cc')).
531 :- assert_must_succeed(tools:get_parent_directory_name('\\aaaa\\bbb b\\cc c\\','cc c')).
532 :- assert_must_fail(tools:get_parent_directory_name('d.app',_)).
533 :- assert_must_succeed(tools:get_parent_directory_name('/a/b/cd/(machine from Jupyter cell).mch','cd')).
534
535 get_parent_directory_name(Path,DirName) :-
536 atom_chars(Path,P1),
537 split_last2_lst(P1, ['/','\\'], [],[], P2, _),
538 split_last2_lst(P2, ['/','\\'], [],[], _, DChars),
539 atom_chars(DirName,DChars).
540
541 :- use_module(library(lists)).
542
543 % strip part after last path slash
544 strip_last([],ResSoFar,_,Res) :- reverse(ResSoFar,Res).
545 strip_last([Slash|Tail],ResSoFar,StripSoFar,Res) :- is_path_slash(Slash), !,
546 append([Slash|StripSoFar],ResSoFar,NewRes),
547 strip_last(Tail,NewRes,[],Res).
548 strip_last([A|Tail],ResSoFar,StripSoFar,Res) :-
549 strip_last(Tail,ResSoFar,[A|StripSoFar],Res).
550
551 :- assert_must_succeed((tools:split_atom('ef,g',[','],R), R==[ef,g])).
552 :- assert_must_succeed((tools:split_atom('ef, g',[',',' '],R), R==[ef,g])).
553 :- assert_must_succeed((tools:split_atom('ab,cd,ef,g',[','],R), R==['ab','cd',ef,g])).
554 :- assert_must_succeed((tools:split_atom('ab,cd,ef;g',[',',';'],R), R==['ab','cd',ef,g])).
555 :- assert_must_succeed((tools:split_atom('/ef/g/',['/'],R), R==[ef,g])).
556
557 split_atom(Atom,SepList,SplitList) :-
558 atom_chars(Atom,ListAscii),
559 split2(ListAscii,SepList,SplitList).
560
561 split2([],_,R) :- !,R=[].
562 split2(List,Sep,Res) :- get_next_word(List,Sep,Word,Tail),!,
563 (Word=[]
564 -> split2(Tail,Sep,Res) % skip empty atom
565 ; Res=[Atom|TA], atom_chars(Atom,Word),
566 split2(Tail,Sep,TA)).
567
568 get_next_word([],_Sep,[],[]).
569 get_next_word([H|T],Sep,Word,Tail) :-
570 ? member(H,Sep) -> Word=[],Tail=T
571 ; Word=[H|TR], get_next_word(T,Sep,TR,Tail).
572
573
574 :- assert_must_succeed((tools:split_atom_with_empty('/ef/g/',['/'],R), R==['',ef,g,''])).
575 :- assert_must_succeed((tools:split_atom_with_empty('/ef/g',['/'],R), R==['',ef,g])).
576 :- assert_must_succeed((tools:split_atom_with_empty('ef/g',['/'],R), R==[ef,g])).
577 :- assert_must_succeed((tools:split_atom_with_empty('efg',['/'],R), R==[efg])).
578
579 % a version of split atom that also returns empty sub atoms
580 split_atom_with_empty(Atom,SepList,SplitList) :-
581 atom_chars(Atom,ListAscii),
582 split_wempty2(ListAscii,SepList,SplitList).
583
584 split_wempty2(List,Sep,Res) :-
585 get_next_match(List,Sep,Word,Tail),!,
586 Res=[Atom|TA], atom_chars(Atom,Word),
587 split_wempty2(Tail,Sep,TA).
588 split_wempty2(List,_,[Atom]) :- atom_chars(Atom,List).
589
590 get_next_match([H|T],Sep,Word,Tail) :-
591 ? member(H,Sep) -> Word=[],Tail=T
592 ; Word=[H|TR], get_next_match(T,Sep,TR,Tail).
593
594
595 :- assert_must_succeed((tools:split_complex_sep("ef,,g,h",",,",R), R==["ef","g,h"])).
596 % a version of split that allows longer seperators, % TO DO: make more efficient
597 split_complex_sep(L,Sep,[First|Rest]) :- append(Sep,LRest,SepR),
598 append(First,SepR,L),
599 !,
600 split_complex_sep(LRest,Sep,Rest).
601 split_complex_sep(H,_,[H]).
602
603
604 :- assert_must_succeed((tools:split_chars("ef,g",",",R), R==["ef","g"])).
605 :- assert_must_succeed((tools:split_chars("10",".",R), R==["10"])).
606 :- assert_must_succeed((tools:split_chars("",".",R), R==[""])).
607 :- assert_must_succeed((tools:split_chars("1.0",".",R), R==["1","0"])).
608 :- assert_must_succeed((tools:split_chars("1.",".",R), R==["1",""])).
609 :- assert_must_succeed((tools:split_chars(".1",".",R), R==["","1"])).
610
611 split_chars(List,Sep,Res) :- get_next_word_until_sep(List,Sep,Word,Tail),!,
612 Res=[Word|TA],
613 split_chars(Tail,Sep,TA).
614 split_chars(List,_,[List]).
615
616 get_next_word_until_sep([H|T],Sep,Word,Tail) :-
617 member(H,Sep) -> Word=[],Tail=T
618 ; Word=[H|TR], get_next_word_until_sep(T,Sep,TR,Tail).
619
620 :- assert_must_succeed(tools:split_last('/aaaa/bbb/cc/d.app','/','/aaaa/bbb/cc','d.app')).
621 :- assert_must_succeed(tools:split_last('/aaaa/bbb/cc/d.app','.','/aaaa/bbb/cc/d','app')).
622 split_last(Atom, Sep, Head, Tail) :- \+ atom(Sep),!,
623 add_internal_error('Separator not an atom: ', split_last(Atom, Sep, Head, Tail)),fail.
624 split_last(Atom, Sep, Head, Tail) :- atom_chars(Sep,SepACodes),
625 split_last_lst(Atom,SepACodes,Head,Tail).
626
627
628 :- assert_must_succeed(tools:split_last_lst('/aaaa/bbb;cc/d.app',['/',';'],'/aaaa/bbb;cc','d.app')).
629 :- assert_must_succeed(tools:split_last_lst('/aaaa/bbb/cc/d.app',['.'],'/aaaa/bbb/cc/d','app')).
630 % a list version of split_last: obtains a list of sperator chars
631 split_last_lst(Atom, Seps, Head, Tail) :- \+ atom(Atom),!,
632 add_internal_error('First arg not an atom: ', split_last_lst(Atom, Seps, Head, Tail)),fail.
633 split_last_lst(Atom, Seps, Head, Tail) :-
634 atom_chars(Atom,ListAscii),
635 split_last2_lst(ListAscii,Seps,[],[],HeadA, TailA),
636 atom_chars(Head,HeadA), atom_chars(Tail,TailA).
637
638 split_last2_lst([],_,CurSplit,[_|Head],ResH,ResT) :-
639 reverse(CurSplit,ResT),
640 reverse(Head,ResH).
641 ?split_last2_lst([Sep|Tail],Seps,CurSplit,Head,ResH,ResT) :- member(Sep,Seps), % TO DO: use ord_member ?
642 !,
643 append([Sep|CurSplit],Head,NewHead),
644 split_last2_lst(Tail,Seps,[],NewHead,ResH,ResT).
645 split_last2_lst([H|Tail],Seps,CurSplit,Head,ResH,ResT) :-
646 split_last2_lst(Tail,Seps,[H|CurSplit],Head,ResH,ResT).
647
648
649
650 :- assert_must_succeed(tools:split_filename('/aaaa/bbb/cc/d.app','/aaaa/bbb/cc/d','app')).
651 :- assert_must_succeed((Z='/aaaa/bbb/cc/d',tools:split_filename(Z,R,X),X=='',R==Z)).
652
653 split_filename(Filename,Base,Ext) :-
654 (split_last(Filename,'.',Base,Ext) -> true ; Base=Filename,Ext='').
655
656
657 :- assert_must_succeed(tools:get_tail_filename('/aaaa/bbb/cc/d.app','d.app')).
658 :- assert_must_succeed(tools:get_tail_filename('\\aaaa\\bbb\\c\\d.app','d.app')).
659 :- assert_must_succeed(tools:get_tail_filename('d.app','d.app')).
660 :- assert_must_succeed(tools:get_tail_filename('/aaaa/bbb/cc/','')).
661 get_tail_filename(Path,Tail) :- compound(Path),!,
662 add_internal_error('Not a filename: ',get_tail_filename(Path,Tail)),
663 Tail=Path.
664 get_tail_filename(Path,Tail) :- (split_last_lst(Path, ['/','\\'], _, T) -> Tail=T ; Tail=Path).
665
666 :- assert_must_succeed(tools:get_modulename_filename('/aaaa/bbb/cc/d.app','d')).
667 :- assert_must_succeed(tools:get_modulename_filename('d.app','d')).
668 get_modulename_filename(Path,Module) :-
669 get_tail_filename(Path,Tail),
670 (split_last(Tail, '.', M, _) -> Module=M ; Module=Tail).
671
672
673 :- assert_must_succeed(get_filename_extension('/aaaa/bbb/cc/d.app','app')).
674 get_filename_extension(Path,Ext) :- split_filename(Path,_,Ext).
675
676 % also works if numbers passed (can happen by accident in test_runner ...)
677 % absolute_file_name('$a',X) generates a permission error
678 safe_absolute_file_name(F,AF,Options) :-
679 ensure_atom(F,A),
680 catch(
681 absolute_file_name(A,AF,Options),
682 error(permission_error(_,_,_),ERR),
683 (format('*** Permission Error for absolute_file_name: ~w~n',[ERR]),AF=F)).
684
685 safe_absolute_file_name(F,AF) :- safe_absolute_file_name(F,AF,[]).
686
687 % host_platform specific check if file names match
688 same_file_name(F,F) :- !.
689 same_file_name(File1,File2) :- host_file_name_case_insensitive,
690 atom_codes(File1,FC1),
691 atom_codes(File2,FC2),
692 maplist(case_insensitive_match,FC1,FC2).
693
694 case_insensitive_match(Code,Code) :- !.
695 case_insensitive_match(C1,C2) :-
696 simple_lowcase(C1,L1),
697 simple_lowcase(C2,L1).
698
699 % TODO: are there other changes to be made in Windows filenames?
700 simple_lowcase(H,R) :- H >="A", H=<"Z", !, R is H+"a"-"A".
701 simple_lowcase(92,R) :- !, R=47. % convert Windows path divider \ to Unix one /
702 simple_lowcase(Code,Code).
703
704 host_file_name_case_insensitive :-
705 host_platform(Platform),
706 host_file_name_case_insensitive(Platform).
707 host_file_name_case_insensitive(windows).
708 host_file_name_case_insensitive(darwin). % by default case insensitive, but up/lower case stored
709 % TODO: can we check if main filesystem is case sensitive??
710
711 %*******************************************************************************
712 % remove_all(A,B,Result): Result is the list of elements of A which do not occur in B
713 remove_all([],_,[]).
714 remove_all([H|T],Remove,Result) :-
715 ? (member(H,Remove) -> !,Result = Rest ; Result = [H|Rest]),
716 remove_all(T,Remove,Rest).
717
718
719
720 %*******************************************************************************
721 % get options from Prolog list passed as parameter
722
723
724 :- assert_must_succeed((get_option_from_list(b,2,[a/false,b/true],B),B==true)).
725 get_option_from_list(Option,Default, OptionList,Value) :-
726 (memberchk(Option/V,OptionList) -> Value=V ; Value=Default).
727
728
729 :- assert_must_succeed(get_options_from_list([option(a,true,false)],[a/false,b/true])).
730 :- assert_must_succeed((get_options_from_list([option(a,true,A)],[a/false,b/true]),A==false)).
731 :- assert_must_succeed((get_options_from_list([option(c,3,C),option(b,1,B),option(a,2,A)],[a/false,b/true]),
732 A==false,B==true,C==3)).
733
734 :- use_module(library(lists), [select/3]).
735
736 get_options_from_list([],_OptionList). % TO DO: provide optional check that no other options left
737 get_options_from_list([option(OptionName,Default,Value)|T],OptionList) :-
738 ? (select(OptionName/V,OptionList,Rest) -> Value=V
739 ; Value=Default, Rest=OptionList),
740 get_options_from_list(T,Rest).
741
742
743 %*******************************************************************************
744 % get_options/4 for parsing command line arguments
745
746
747 get_options(List,Pred,Options,Rest) :-
748 get_options(List,Pred,Options,Rest,halt).
749 get_options([],_,[],[],_).
750 get_options([X|T],Recognised,Options,Args,HALTCMD) :-
751 ? ( call(Recognised,X,Opt,Values,Action)
752 ->
753 ( append(Values, Rest, T) -> true
754 ;
755 length(Values,Len),
756 length(T,TLen),
757 (TLen < Len
758 -> format('Command ~w expects ~w argument(s); ~w provided.~n',[X,Len,TLen])
759 ; format('~nInvalid argument(s) for option: ~w.~n',[X]) % will never happen ??
760 ),
761 HALTCMD),
762 ( call(Action) -> true
763 ;
764 format('~nInvalid argument(s) for option ~w : ~w.~n',[X,Values]),
765 HALTCMD),
766 RT = Rest,
767 Options = [Opt|OT], Args = AT
768 ; % option not recognised, keep in Args list (for probcli these are assumed to be files)
769 Options = OT, Args = [X|AT],
770 RT = T
771 ),
772 get_options(RT,Recognised,OT,AT,HALTCMD).
773
774 arg_is_number(Arg,Nr) :- number(Arg),!,Nr=Arg.
775 arg_is_number(Arg,Nr) :- atom(Arg),atom_codes(Arg,Str),safe_number_codes(Nr,Str),number(Nr).
776 arg_is_integer(Arg,Nr) :-
777 ( append("-",SPos,Arg) -> % negative number
778 arg_is_number(SPos,Pos),
779 Nr is -Pos
780 ;
781 arg_is_number(Arg,Nr)).
782
783 % utilities for command-line arguments
784 % allow Number, _, >Number, <Number
785 arg_is_number_or_wildcard('_',R) :- !, R=_.
786 arg_is_number_or_wildcard('*',R) :- !, R=_. % * on command-line is expanded to files unless quoted
787 arg_is_number_or_wildcard(Arg,R) :- arg_is_number(Arg,N),!,R=N.
788 arg_is_number_or_wildcard(Comparator,R) :- atom(Comparator), atom_codes(Comparator,AC),
789 comparator(AC,CompOp,NC), safe_number_codes(Nr,NC), R=comparison_operator(CompOp,Nr).
790 comparator([0'>|T],>,T). % Ascii code 62
791 comparator([0'<|T],<,T).
792 comparator([0'>,0'=|T],'>=',T).
793 comparator([0'=,0'<|T],'=<',T).
794
795
796 % TO DO: add comparator
797
798 :- use_module(error_manager,[add_warning/3]).
799 check_filename_arg(File,Command) :- tools:arg_is_number(File,_),!,
800 ajoin(['File argument to -',Command,' is a number: '],Msg),
801 add_warning(Command,Msg,File).
802 check_filename_arg(File,Command) :- atom(File),
803 sub_atom(File,0,1,_,'-'), % atom_concat('-',_,File),
804 !,
805 ajoin(['File argument to -',Command,' starts with a hypen: '],Msg),
806 add_warning(Command,Msg,File).
807 check_filename_arg(_,_).
808
809
810 :- assert_must_succeed( tools:convert_list_into_pairs([a],a)).
811 :- assert_must_succeed( (tools:convert_list_into_pairs([a,b,c],R), R = ((a,b),c) )).
812
813 convert_list_into_pairs([X|T],Res) :- !,conv2(T,X,Res).
814 convert_list_into_pairs([],Res) :- !, Res=[].
815 convert_list_into_pairs(X,R) :- add_internal_error('Not a list: ',convert_list_into_pairs(X,R)),R=X.
816 conv2([],X,X).
817 conv2([X|T],Acc,Res) :- conv2(T,(Acc,X),Res).
818
819 :- assert_must_succeed(( tools:convert_pairs_into_list([x],a,R), R==[a] )).
820 %:- assert_must_succeed(( b_interpreter:convert_pairs_into_list([x,y,z],((a,b),c),R), R == [a,b,c] )).
821 :- assert_must_succeed(( tools:convert_pairs_into_list([x,y,z],((a,b),c),R), R == [a,b,c] )).
822 :- assert_must_succeed(( tools:convert_pairs_into_list([x,y,z],A,B),nonvar(A),nonvar(B),A=((_,_),_),B=[_,_,_] )).
823
824 % the first argument just indicates the identifiers, the second is a constructed pair and the third the list of values
825 convert_pairs_into_list([_],X,R) :- !,R=[X].
826 convert_pairs_into_list([_|Guide],Pair,R) :- !, R = [A|TValList],
827 conv4(Guide,A,Pair,TValList).
828 convert_pairs_into_list(List,Pairs,Result) :-
829 add_internal_error('Illegal arguments:',convert_pairs_into_list(List,Pairs,Result)),
830 fail.
831
832 conv4([],X,X,[]).
833 conv4([_|T],Acc,Res,[X|TValList]) :- conv4(T,(Acc,X),Res,TValList).
834
835
836 /* ex: substitute(1, [1,2,3,4], 5, X). */
837 :- assert_must_succeed(( tools:substitute(1, [1,2,3,4], 5, X), X==[5,2,3,4])).
838 substitute(X,L,Y,Res) :- sub(L,X,Y,Res).
839 sub([],_,_,[]).
840 sub([H|T],X,Y,[SH|ST]) :-
841 (H=X -> SH=Y ; SH=H),
842 sub(T,X,Y,ST).
843
844
845
846 :- use_module(tools_strings,[ajoin/2, ajoin_with_sep/3, ajoin_with_limit/3]).
847 % tests are stored here to avoid cyclic module dependencies
848 :- assert_must_succeed((tools_strings: ajoin_with_sep([link,a,xa],'.',Text), Text == 'link.a.xa')).
849 :- assert_must_succeed((tools_strings: ajoin_with_sep([link],'.',Text), Text == 'link')).
850 :- assert_must_succeed((tools_strings: ajoin_with_sep(['',a,''],'.',Text), Text == '.a.')).
851
852 :- assert_must_succeed((tools_strings: ajoin_with_limit(['A','B','C','D'],100,Text), Text == 'ABCD')).
853 :- assert_must_succeed((tools_strings: ajoin_with_limit(['A','B','C','D'],2,Text), Text == 'AB...')).
854
855
856 :- use_module(error_manager,[add_error_and_fail/3]).
857 safe_univ(Term,List) :- nonvar(Term),!,Term=..List.
858 safe_univ(Term,List) :- var(List), !,add_error_and_fail(tools,'Arguments to safe_univ (=..) both var:', safe_univ(Term,List)).
859 safe_univ(Term,List) :- %Term is a variable
860 current_prolog_flag(max_arity,MA),
861 (MA = unbounded -> CL = List ; cut_off_list(List,MA,CL)),
862 !, % avoid pending choice points
863 Term =.. CL.
864
865 cut_off_list([],_,[]).
866 cut_off_list([H|T],MA,R) :- (MA<2 -> R=['...'] ; R=[H|TR],MA1 is MA-1, cut_off_list(T,MA1,TR)).
867
868
869 % a version of safe_univ which does not remove args; just puts the extra arguments into the last arg
870
871 safe_univ_no_cutoff(Term,List) :- nonvar(Term),!,Term=..List.
872 safe_univ_no_cutoff(Term,List) :- var(List), !,
873 add_error_and_fail(tools,'Arguments to safe_univ (=..) both var:', safe_univ_no_cutoff(Term,List)).
874 safe_univ_no_cutoff(Term,List) :- %Term is a variable
875 current_prolog_flag(max_arity,MA),
876 (MA = unbounded -> CL = List ; squash_list(List,MA,CL)),
877 !, % avoid pending choice points
878 Term =.. CL.
879 squash_list([],_,[]).
880 squash_list([H|T],MA,R) :- (MA<3,T\=[] -> R=[H,T] ; R=[H|TR],MA1 is MA-1, squash_list(T,MA1,TR)).
881
882 safe_atom_chars(A,B,Loc) :-
883 catch(atom_chars(A,B), error(E1,E2), (
884 add_internal_error('atom_chars error: ',Loc:E1),
885 throw(error(E1,E2))
886 )).
887
888 :- use_module(tools_strings,[atom_codes_with_limit/2, atom_codes_with_limit/3]).
889
890 safe_atom_codes(V,C) :- var(V),var(C),!,
891 add_internal_error('Variables in call: ',safe_atom_codes(V,C)), C='$VARIABLE$'.
892 safe_atom_codes(A,C) :-
893 catch(atom_codes(A,C), error(representation_error(max_atom_length),_), (
894 print(exception(max_atom_length)),nl,
895 atom_codes_with_limit(A,1000,C)
896 )).
897
898 safe_number_codes(V,C) :- var(V),var(C),!,
899 add_internal_error('Variables in call: ',safe_number_codes(V,C)), C='$VARIABLE$'.
900 safe_number_codes(A,C) :-
901 catch(number_codes(A,C), error(syntax_error(_N),_), (
902 %print(9,syntax_error_in_number_codes(_N)),nl,
903 % in this case safe_number_codes fails ; we cannot convert the codes into a number
904 fail
905 )).
906
907 % for an identifier "x" and a number N, create a new identifier "x$N"
908 number_suffix(Id,N,FullId) :-
909 safe_atom_chars(Id,IdChars,number_suffix1),number_chars(N,NChars),
910 append(IdChars,['$'|NChars],FullIdChars),
911 safe_atom_chars(FullId,FullIdChars,number_suffix2).
912
913
914 :- assert_must_succeed(ensure_atom(19,'19')).
915 % ensure that numbers get converted to atoms:
916 ensure_atom(Var,A) :- var(Var),!, A='_'.
917 ensure_atom(N,Res) :- number(N),!,number_codes(N,C), atom_codes(A,C), Res=A.
918 ensure_atom(A,A).
919
920
921 :- assert_must_succeed(atom_or_number_codes(19,"19")).
922 :- assert_must_succeed(atom_or_number_codes(aa,"aa")).
923 atom_or_number_codes(N,Res) :- number(N),!,number_codes(N,Res).
924 atom_or_number_codes(A,Res) :- atom(A),!,atom_codes(A,Res).
925 atom_or_number_codes(Other,Res) :-
926 add_internal_error('Not atom or number: ',atom_or_number_codes(Other,Res)),
927 Res = [].
928
929
930 :- use_module(tools_strings,[truncate_atom/3]).
931 :- assert_must_succeed((tools_strings:truncate_atom(abcd,100,Text), Text == 'abcd')).
932 :- assert_must_succeed((tools_strings:truncate_atom(abcd,2,Text), Text == 'ab...')).
933 :- assert_must_succeed((tools_strings:truncate_atom(abcd,0,Text), Text == '...')).
934
935
936 wrap_and_truncate_atom(Atom,LineLength,Limit,NewAtom) :- \+ atom(Atom),!,
937 add_internal_error('Argument to wrap_and_truncate_atom not atom: ',wrap_and_truncate_atom(Atom,LineLength,Limit,NewAtom)),
938 NewAtom=Atom.
939 wrap_and_truncate_atom(Atom,LineLength,Limit,NewAtom) :-
940 atom_codes(Atom,Codes),
941 wrap_and_truncate_codes(Codes,LineLength,LineLength,Limit,NewCodes,Chng),
942 (Chng=true -> atom_codes(NewAtom,NewCodes) ; NewAtom=Atom).
943
944 wrap_and_truncate_codes([],_,_,_,[],false).
945 wrap_and_truncate_codes([H|T],CharsOnLineCount,MaxLineLength,TotCount,Res,Chng) :-
946 (TotCount<1 -> Res = [46,46,46],Chng=true /* '...' */
947 ; (CharsOnLineCount<1 -> Res=[92,110,H|TT], /* add newline \n */
948 TR=T, L1 is MaxLineLength, Chng=true
949 ; H=92,T=[H2|T2] -> Res = [H,H2|TT], /* do not split escaped char */
950 TR=T2, L1 is CharsOnLineCount-1, Chng2=Chng
951 ; Res = [H|TT], TR=T, L1 is CharsOnLineCount-1, Chng2=Chng),
952 TC1 is TotCount-1, wrap_and_truncate_codes(TR,L1,MaxLineLength,TC1,TT,Chng2)
953 ).
954
955
956 % print_size_of_table counts the number of succeeded calls of the
957 % given predicate
958 get_calls_for_table(Module:P/N,Call) :- !,
959 functor(Call,P,N),
960 call(Module:Call).
961 get_calls_for_table(P/N,Call) :-
962 functor(Call,P,N),
963 call(Call).
964
965 :- volatile count/1.
966 :- dynamic count/1.
967 print_size_of_table(Pred) :- retractall(count(_)),
968 assertz(count(0)),
969 get_calls_for_table(Pred,_),inc_size_of_table,fail.
970 print_size_of_table(Pred) :-
971 print('% size of table for '), print(Pred), print(': '), count(X), print(X),nl.
972 inc_size_of_table :- retract(count(X)),X1 is X+1, assertz(count(X1)).
973
974
975 % ----------------- Sorting ---------------
976
977 safe_sort(Orig,A,B) :- var(A),!, add_internal_error('Illegal call: ',safe_sort(Orig,A,B)),A=B.
978 safe_sort(_,A,B) :- sort(A,B).
979
980 :- use_module(library(samsort)).
981 % a sorting function which checks that there were no multiples in the original list
982 safe_set_sort(Orig,S,Res) :- var(S),!, add_internal_error('Illegal call: ',safe_set_sort(Orig,S,Res)),S=Res.
983 %safe_set_sort(Orig,S,Res) :- !,sort(S,Res).
984 safe_set_sort(Orig,S,Res) :- samsort(S,SS),
985 (SS=[H|T] -> (check_for_multiples(T,H,Orig) -> Res=SS ; sort(SS,Res)) ; Res=SS).
986 check_for_multiples([],_,_).
987 check_for_multiples([H|T],Prev,Origin) :-
988 (H=Prev
989 -> add_error(Origin,'Multiple occurrences in set list of: ',H),fail
990 ; check_for_multiples(T,H,Origin)).
991
992 % ----------------------
993
994 safe_functor(Src,F,A,Term) :- var(F),var(A),var(Term),!,
995 add_internal_error('Illegal functor call: ',safe_functor(Src,F,A,Term)),
996 fail.
997 safe_functor(_,F,A,T) :- functor(F,A,T).
998
999
1000 print_runtime :- statistics(runtime,[Tot,SinceLast]), print(' Total runtime: '), print(Tot),
1001 print(' ms, since last: '), print(SinceLast), print(' ms'),nl.
1002
1003 :- use_module(debug,[debug_mode/1, silent_mode/1]).
1004 start_ms_timer(timer(R,T,W)) :- statistics(runtime,[R,_]),
1005 statistics(total_runtime,[T,_]),
1006 statistics(walltime,[W,_]).
1007 stop_ms_timer(T) :- stop_ms_timer(T,[runtime/RT,total_runtime/RTT,walltime/WT]),
1008 format('% Runtime: ~w ms (with gc: ~w ms, walltime: ~w ms)~n',[RT,RTT,WT]).
1009 stop_ms_timer_with_debug_msg(T,Msg) :-
1010 (debug_mode(on) -> stop_ms_timer_with_msg(T,Msg) ; true).
1011 stop_ms_timer_with_silent_msg(T,Msg) :-
1012 (silent_mode(on) -> true ; stop_ms_timer_with_msg(T,Msg)).
1013 stop_ms_timer_with_msg(T,Msg) :- stop_ms_timer(T,[runtime/RT,total_runtime/RTT,walltime/WT]),
1014 statistics(walltime,[WE,_]),
1015 convert_ms_time_to_string(WE,WEStr),
1016 format('% Runtime for ~w: ~w ms (with gc: ~w ms, walltime: ~w ms); since start: ~w~n',[Msg,RT,RTT,WT,WEStr]).
1017 stop_ms_walltimer_with_msg(T,Msg) :- stop_ms_timer(T,[runtime/_RT,total_runtime/_RTT,walltime/WT]),
1018 format('% Walltime for ~w: ~w ms~n',[Msg,WT]).
1019 stop_ms_timer(timer(R,T,W),[runtime/RT,total_runtime/RTT,walltime/WT]) :-!,
1020 statistics(runtime,[RE,_]),
1021 % These refer to CPU time used while executing, excluding time spent in memory management tasks or or in system calls.
1022 statistics(total_runtime,[TE,_]),
1023 % These refer to total CPU time used while executing, including memory management tasks such as garbage collection but excluding system calls.
1024 statistics(walltime,[WE,_]),
1025 % These refer to absolute time elapsed.
1026 RT is RE-R, RTT is TE-T, WT is WE-W.
1027 stop_ms_timer(X,Y) :-
1028 add_internal_error('Illegal call: ', stop_ms_timer(X,Y)),
1029 Y = [].
1030 get_elapsed_walltime(timer(_R,_T,W),WTot) :-
1031 statistics(walltime,[W2,_]), WTot is W2-W.
1032
1033 % convert ms time to a user readable time string
1034 convert_ms_time_to_string(TimeMS,Str) :-
1035 convert_ms_time_to_hms(TimeMS,H,M,S,MS),
1036 (H > 0 -> ajoin([H,' h ', M, ' min ', S, ' sec ', MS, ' ms'],Str)
1037 ; M > 0 -> ajoin([M, ' min ', S, ' sec ', MS, ' ms'],Str)
1038 ; ajoin([S, ' sec ', MS, ' ms'],Str)).
1039
1040 convert_ms_time_to_hms(TimeMS,Hours,MinsMod,SecsMod,MilSecs) :-
1041 MilSecs is TimeMS mod 1000,
1042 Secs is TimeMS // 1000, SecsMod is Secs mod 60,
1043 Mins is Secs // 60, MinsMod is Mins mod 60,
1044 Hours is Mins // 60.
1045
1046 % get delta timer between old timer1 and current time
1047 get_elapsed_timer(timer(R1,T1,W1),timer(R,T,W)) :-
1048 statistics(runtime,[R2,_]),
1049 statistics(total_runtime,[T2,_]),
1050 statistics(walltime,[W2,_]),
1051 R is R2-R1, T is T2-T1, W is W2-W1.
1052 get_elapsed_runtime(timer(R1,_,_),Delta) :-
1053 statistics(runtime,[R2,_]),
1054 Delta is R2-R1.
1055
1056 % combine (add) two timers
1057 combiner_timer(0,T2,R) :- !, R=T2.
1058 combiner_timer(timer(R1,T1,W1),timer(R2,T2,W2),timer(R,T,W)) :-
1059 R is R1+R2, T is T1+T2, W is W1+W2.
1060
1061 % a timer that measures backtracking times
1062 :- dynamic last_bt_timer/2.
1063 bt_start_ms_timer(Msg) :- retractall(last_bt_timer(Msg,_)),
1064 start_ms_timer(T), assertz(last_bt_timer(Msg,T)).
1065 bt_start_ms_timer(Msg) :- retract(last_bt_timer(Msg,Timer)),
1066 stop_ms_timer(Timer,[runtime/RT,total_runtime/_RTT,walltime/WT]),
1067 format('% Runtime to FINALISE ~w: ~w ms (walltime: ~w ms)~n',[Msg,RT,WT]),
1068 fail.
1069
1070
1071 bt_stop_ms_timer(Msg) :-
1072 retract(last_bt_timer(Msg,Last)),
1073 stop_ms_timer(Last,[runtime/RT,total_runtime/_RTT,walltime/WT]),
1074 format('% Runtime for SOLUTION for ~w: ~w ms (walltime: ~w ms)~n',[Msg,RT,WT]).
1075 bt_stop_ms_timer(Msg) :- start_ms_timer(BT_Timer),
1076 assertz(last_bt_timer(Msg,BT_Timer)),
1077 fail.
1078 % ---------------------------
1079
1080
1081 retract_with_statistics(Module,ListOfFacts) :-
1082 nl,print('Retracting Facts'),nl,
1083 get_memory_used(M),
1084 print(' Memory usage: '),print_memory_used(M),nl,
1085 retract_with_statistics(Module,ListOfFacts,M).
1086
1087
1088 retract_with_statistics(_Module,[],_) :- nl.
1089 retract_with_statistics(Module,[Fact|T],Mem) :-
1090 format('~w : ',[Fact]),
1091 %retractall(Module:Fact),
1092 retract_all_count(Module:Fact,0,Nr), format(' ~w facts : ',[Nr]),
1093 get_memory_used(NewMem),
1094 print_memory_used(NewMem),
1095 print_memory_used_difference(Mem,NewMem),
1096 retract_with_statistics(Module,T,NewMem).
1097
1098 retract_all_count(Fact,Acc,Res) :- \+ (\+ (retract(Fact))),!, A1 is Acc+1, retract_all_count(Fact,A1,Res).
1099 retract_all_count(_,R,R).
1100
1101 :- if(current_prolog_flag(dialect, swi)).
1102 % this could possibly be moved to SWI compatibility code
1103 statistics_memory_used(M) :-
1104 statistics(stack,GL), statistics(trail,T), statistics(heapused,H), M is GL+T+H.
1105 % not sure this computes all of memory used; other keys: globalused, localused, codes
1106 :- else.
1107 statistics_memory_used(M) :- statistics(memory_used,M).
1108 :- endif.
1109
1110 get_memory_used([M,PU]) :- garbage_collect,garbage_collect_atoms, get_memory_used_wo_gc([M,PU]).
1111 get_memory_used_wo_gc([M,PU]) :- statistics(program,[PU,_]),statistics_memory_used(M).
1112 print_memory_used_wo_gc :- print_memory_used_wo_gc(user_output).
1113 print_memory_used_wo_gc(Stream) :- get_memory_used_wo_gc(M), print_memory_used(Stream,M).
1114
1115 print_memory_used(M) :- print_memory_used(user_output,M).
1116 print_memory_used(Stream,[M,PU]) :- print_mb(Stream,M),
1117 write(Stream,' ('), print_mb(Stream,PU), write(Stream,' program) ').
1118
1119 print_memory_used_difference([M1,_PU1],[NewM2,_PU2]) :- Diff is (M1)-(NewM2),
1120 (Diff >= 0 -> print(' freed: '), print_bytes(Diff)
1121 ; print(' allocated: '), D2 is -(Diff), print_bytes(D2)),nl.
1122 %print(' / '),Diff2 is (PU1)-(PU2), print_mb(Diff2).
1123 print_bytes(X) :- (X<50000 -> print_kb(X) ; print_mb(X)).
1124 print_gb(X) :- XGB is X / 1000000000, % used instead of deprecated 1048576
1125 format(' ~3f GB',[XGB]).
1126 print_mb(X) :- print_mb(user_output,X).
1127 print_mb(Stream,X) :- XMB is X / 1000000, % used instead of deprecated 1048576
1128 format(Stream,' ~3f MB',[XMB]).
1129 print_kb(X) :- XKB is X / 1000, % used instead of deprecated 1024
1130 format(' ~3f KB',[XKB]).
1131
1132 space_call(Call) :- get_memory_used(M1),
1133 call(Call),
1134 get_memory_used(M2),
1135 print_memory_used(M2),
1136 print_memory_used_difference(M1,M2).
1137
1138 % ---------------------------
1139
1140 :- use_module(library(fastrw),[fast_read/2]).
1141
1142 read_term_from_file(Filename,Term) :-
1143 absolute_file_name(Filename,AbsFilename,[]),
1144 open(AbsFilename,read,Stream),
1145 peek_code(Stream,Code),
1146 read_term_from_file2(Code,Stream,AbsFilename,Term).
1147
1148 read_term_from_file2(Code,Stream,File,Term) :-
1149 fastrw_start_code(Code), !,
1150 close(Stream),
1151 open(File,read,NewStream,[type(binary)]),
1152 fast_read(NewStream,Term),
1153 close(NewStream).
1154 read_term_from_file2(_,Stream,_,Term) :-
1155 read_term(Stream,Term,[]),
1156 close(Stream).
1157
1158 fastrw_start_code(0'D). % like for .prob in parsercall
1159
1160 :- use_module(library(codesio), [read_from_codes/2]).
1161 safe_read_term_from_atom(Atom,Term) :-
1162 safe_atom_codes(Atom,Codes),
1163 safe_read_term_from_codes(Codes,Term).
1164 safe_read_term_from_codes(Codes,Term) :-
1165 (append(_,[46],Codes) % add dot: 46 = '.' if required
1166 -> Codes1 = Codes
1167 ; append(Codes,[46],Codes1)
1168 ),
1169 catch(read_from_codes(Codes1,Term),
1170 error(syntax_error(StxMsg),_),
1171 (atom_codes(AT,Codes),
1172 ajoin(['Prolog syntax error: ',StxMsg,' in'],Msg),
1173 add_error(read_term_from_codes,Msg,AT), fail)
1174 ).
1175
1176
1177 read_string_from_file(Filename,String) :-
1178 absolute_file_name(Filename,AbsFilename,[]),
1179 open(AbsFilename,read,S), % utf
1180 read_string(S,String),
1181 close(S).
1182
1183 % Encoding can be any value of text_encoding preference category: auto, 'ISO-8859-1', 'UTF-8', ...
1184 read_string_from_file(Filename,auto,String) :- !, read_string_from_file(Filename,String).
1185 read_string_from_file(Filename,Encoding,String) :-
1186 absolute_file_name(Filename,AbsFilename,[]),
1187 open(AbsFilename,read,S,[encoding(Encoding)]),
1188 read_string(S,String),
1189 close(S).
1190
1191 read_string(S,String) :-
1192 get_code(S,C),!,
1193 (C= -1
1194 -> String = []
1195 ; String = [C|Rest], read_string(S,Rest)).
1196
1197 read_atom_from_file(Filename,Encoding,Atom) :-
1198 read_string_from_file(Filename,Encoding,String),
1199 atom_codes(Atom,String).
1200
1201 % version which catches exceptions
1202 safe_read_string_from_file(Filename,Encoding,String) :-
1203 catch(read_string_from_file(Filename,Encoding,String), E, (
1204 ajoin(['Could not read string from file ',Filename,':'],Msg),
1205 add_error(read_string_from_file,Msg,E),
1206 fail
1207 )).
1208
1209 :- use_module(debug,[debug_format/3]).
1210 % open a file using auto and try and read XML header
1211 % <?xml version="1.0" encoding="UTF-8"?>
1212 % useful to first open file and detect encoding and then re-open file using the found encoding
1213 detect_xml_encoding(Filename,Version,Encoding) :-
1214 absolute_file_name(Filename,AbsFilename,[]),
1215 open(AbsFilename,read,Stream),
1216 call_cleanup(
1217 (
1218 match(" <?xml_version = \"",Stream),
1219 read_quoted_xml_string_contents(Stream,Version),
1220 debug_format(19,'XML version ~s detected~n',[Version]),
1221 (match(" encoding = \"",Stream)
1222 -> read_quoted_xml_string_contents(Stream,EncodingCodes),
1223 debug_format(19,'XML encoding ~s detected~n',[EncodingCodes]),
1224 (EncodingCodes = "windows 1252" -> UEnc=Encoding % only encoding using lower case
1225 ; maplist(simple_upcase,EncodingCodes,UEnc) % convert utf-8 to UTF-8;
1226 ),
1227 atom_codes(Encoding,UEnc)
1228 ; debug_format(19,'No XML encoding detected in header, using auto ~w~n',[Filename]),
1229 Encoding = auto
1230 )
1231 ),
1232 close(Stream)).
1233
1234
1235 simple_upcase(H,R) :- H >="a", H=<"z", !, R is H+"A"-"a".
1236 simple_upcase(H,H).
1237
1238 % simple read contents of a string until end quote; no escaping detected (yet)
1239 read_quoted_xml_string_contents(Stream,String) :-
1240 get_code(Stream,C),!,
1241 (C= -1
1242 -> String = []
1243 ; C = 34 -> String = [] % string finished
1244 ; String=[C|Rest],read_quoted_xml_string_contents(Stream,Rest)).
1245
1246 match([],_).
1247 match([H|T],Stream) :- !, get_code(Stream,C),
1248 match2(H,T,C,Stream).
1249
1250 match2(32,T,C,Stream) :- !, % treat a space character as optional whitespace
1251 (is_ws(C) -> match([32|T],Stream)
1252 ; T = [H1|T1],
1253 match2(H1,T1,C,Stream)
1254 ).
1255 match2(95,T,C,Stream) :- !, % treat underscore as mandatory whitespace
1256 (is_ws(C) -> match([32|T],Stream)
1257 ; debug_format(19,'Not a valid xml header ("~s" instead of whitespace)~n',[[C]]),fail
1258 ).
1259 match2(H,T,C,Stream) :-
1260 (C=H -> match(T,Stream)
1261 ; format('Not a valid xml header ("~s" instead of "~s")~n',[[C],[H]]),fail
1262 ).
1263
1264 is_ws(32).
1265 is_ws(9). % tab
1266 is_ws(10).
1267 is_ws(13).
1268
1269 %! open_temp_file(+BaseName, -Path, -Stream, +Options) is det.
1270 %
1271 % Compatible way to create and open a temporary file.
1272 %
1273 % ReqName will be used as the file name if possible,
1274 % but the actual file name may be different
1275 % (e. g. it may have a unique suffix added).
1276 % Path is the temporary file's full path,
1277 % which is automatically opened for writing as Stream.
1278 % Supported Options are type(binary) and encoding(Encoding).
1279 % see also tmpdir preference, default /tmp/ on Unix
1280
1281 :- if(predicate_property(tmp_file_stream(_,_,_), _)). % SWI
1282
1283 :- use_module(library(lists), [select/4]).
1284 open_temp_file(ReqName, Path, Stream, Options) :-
1285 select(type(binary), Options, encoding(octet), OptionsMod),
1286 !,
1287 open_temp_file(ReqName, Path, Stream, OptionsMod).
1288 open_temp_file(ReqName, Path, Stream, Options) :-
1289 % tmp_file_stream only allows controlling the extension, not the name before it.
1290 get_filename_extension(ReqName, Extension),
1291 tmp_file_stream(Path, Stream, [extension(Extension)|Options]).
1292
1293 :- else. % SICStus
1294
1295 open_temp_file(ReqName, Path, Stream, Options) :-
1296 open(temp(ReqName), write, Stream, [if_exists(generate_unique_name)|Options]),
1297 stream_property(Stream, file_name(Path)).
1298
1299 :- endif.
1300
1301 open_temp_file(ReqName, Path, Stream) :- open_temp_file(ReqName, Path, Stream, []).
1302
1303 % ------------------------------
1304
1305 % encoding atoms for Latex:
1306
1307 latex_escape_atom(Atom,EscAtom) :- \+ atom(Atom),!,
1308 add_internal_error('Cannot escape: ',latex_escape_atom(Atom,EscAtom)),
1309 EscAtom=Atom.
1310 latex_escape_atom(Atom,EscAtom) :-
1311 atom_codes(Atom,Codes), latex_escape_codes(Codes,ECodes), atom_codes(EscAtom,ECodes).
1312
1313 latex_escape_codes([],[]).
1314 %latex_escape_codes([92,C|T],[92,C|ET]) :- !, % already escaped
1315 % latex_escape_codes(T,ET).
1316 latex_escape_codes([C|T],[92,C|ET]) :- latex_escape_code(C),!, % 95 = _ underscore, 92 = \ backslash
1317 latex_escape_codes(T,ET).
1318 latex_escape_codes([Code|T],ET) :- translate_code(Code,String),
1319 !,
1320 append(String,ET2,ET),
1321 latex_escape_codes(T,ET2).
1322 latex_escape_codes([H|T],[H|ET]) :- latex_escape_codes(T,ET).
1323
1324 latex_escape_code(35). % #
1325 latex_escape_code(36). % $
1326 latex_escape_code(37). % %
1327 latex_escape_code(38). % &
1328 latex_escape_code(95). % 95 = _ underscore
1329 latex_escape_code(123). % {
1330 latex_escape_code(125). % }
1331
1332 translate_code(92,"\\textbackslash{}"). % \ % \textbackslash seems to work in both math and normal mode
1333 translate_code(94,"\\textasciicircum{}"). % ^
1334 translate_code(126,"\\textasciitilde{}"). % ~
1335 %translate_code(126,"\\~{}"). % 126 = ~ tilde -> \~{} (\sim would be alternative in math mode)
1336
1337 % ------------------------------
1338
1339 % encoding atoms for B Strings:
1340 % escape special characters so that we can output the string between quotes "..." and obtain a valid value
1341
1342 :- assert_must_succeed(tools:b_escape_string_atom('{"a"}','{\\"a\\"}')).
1343 :- assert_must_succeed(tools:b_escape_string_atom('{"a \\/ b"}','{\\"a \\/ b\\"}')).
1344
1345 b_escape_string_atom(Atom,EscAtom) :- \+ atom(Atom),!,
1346 add_internal_error('Cannot escape: ',b_escape_string_atom(Atom,EscAtom)),
1347 EscAtom=Atom.
1348 b_escape_string_atom(Atom,EscAtom) :-
1349 atom_codes(Atom,Codes), b_string_escape_codes(Codes,ECodes), atom_codes(EscAtom,ECodes).
1350
1351 b_string_escape_codes([],R) :- !, R=[].
1352 b_string_escape_codes([C|T],Res) :-
1353 (C=92, T=[C2|_], % 92 = \ backslash
1354 \+ valid_backslash_escape(C2) % we do not need to escape the \ in \x for example, but we need to escape in \n
1355 -> Res = [92|ET]
1356 ; b_escape_code(C,EC) -> Res = [92,EC|ET]
1357 ; Res = [C|ET]
1358 ),
1359 b_string_escape_codes(T,ET).
1360
1361 b_escape_code(9,116). % tab, 116 = t
1362 b_escape_code(10,110). % newline, 110 = n
1363 b_escape_code(13,114). % return 114 = r
1364 b_escape_code(34,34). % "
1365 %b_escape_code(39,39). % ' % not necessary for "..." literals
1366 b_escape_code(92,92). % \
1367
1368 % these are the escape codes the parser / ProB currently supports:
1369 valid_backslash_escape(34). % "
1370 valid_backslash_escape(39). % '
1371 valid_backslash_escape(92). % \
1372 valid_backslash_escape(110). %n
1373 valid_backslash_escape(114). %r
1374 valid_backslash_escape(116). %t
1375
1376 % ------------------------------
1377
1378
1379 %%
1380 % Escape is needed for &, \/, /\, ", ', etc.
1381 % used mainly for dot output
1382 % It seems dotty does not understand escapes, neither C style nor HTML style.
1383 % Dot does render '&' correctly, no matter of escape.
1384 %
1385
1386 print_escaped(Atom) :- string_escape(Atom,E), write(E).
1387
1388 :- use_module(library(lists), [
1389 maplist/3,
1390 scanlist/4]).
1391
1392 string_escape(Atom, EscapedAtom):-
1393 string_escape(dot_string_escape_map, Atom, EscapedAtom).
1394
1395
1396 string_escape(_,Number, EscapedAtom):-
1397 number(Number),!, number_chars(Number,C), atom_chars(EscapedAtom,C). % to detect when label=value
1398 string_escape(Map, Atom, EscapedAtom):-
1399 atom(Atom),
1400 atom_chars(Atom, Chars),
1401 maplist(Map, Chars, EscapedChars),
1402 scanlist(x_atom_concat_rev, EscapedChars, '', EscapedAtom),!.
1403 string_escape(Map,Term, EscapedTerm):-
1404 Term =.. [Fkt|Args],
1405 string_escape(Map, Fkt, EscapedFkt),
1406 maplist(string_escape(Map), Args, EscapedArgs),!,
1407 EscapedTerm =.. [EscapedFkt|EscapedArgs].
1408 string_escape(_,X,X).
1409
1410
1411 x_atom_concat_rev(A,B,BA):-
1412 atom_concat(B,A,BA).
1413
1414
1415 % simple escape, just ensuring no syntax errors and not disturbing records already constructed as B string,...
1416 % however, not that fields of records are already escaped
1417 simple_dot_string_escape(Number, EscapedAtom) :- number(Number),!,
1418 number_chars(Number,C), atom_chars(EscapedAtom,C).
1419 simple_dot_string_escape(Atom, EscapedAtom) :- atom(Atom),!,
1420 atom_chars(Atom,Chars),
1421 simple_dot_esc( Chars, EC), atom_chars(EscapedAtom,EC).
1422 simple_dot_string_escape(Atom,Res) :-
1423 add_internal_error('Not atomic: ',simple_dot_string_escape(Atom,Res)),
1424 Res=Atom.
1425
1426 simple_dot_esc([],[]).
1427 simple_dot_esc(['\\','"' | T],['\\','"' |TR]) :- !, % already escaped
1428 simple_dot_esc(T,TR).
1429 simple_dot_esc(['\\','\\' | T],['\\','\\' |TR]) :- !, % already escaped
1430 simple_dot_esc(T,TR).
1431 simple_dot_esc(['"' | T],['\\','"' |TR]) :- !,
1432 simple_dot_esc(T,TR).
1433 simple_dot_esc([H | T],[H|TR]) :-simple_dot_esc(T,TR).
1434
1435
1436 % ----------------
1437
1438
1439 % escape for XML attribute values for use in double quotes
1440 %https://stackoverflow.com/questions/19766669/which-characters-are-permitted-in-xml-attributes
1441 % AttValue ::= '"' ([^<&"] | Reference)* '"'
1442 % | "'" ([^<&'] | Reference)* "'"
1443
1444 xml_attribute_escape(Codes,EscapedCodes) :- xml_attr_escape(Codes,EscapedCodes,[]).
1445 xml_attr_escape([]) --> "".
1446 xml_attr_escape([0'&|T]) --> !, "&", xml_attr_escape(T).
1447 xml_attr_escape([0'<|T]) --> !, "<", xml_attr_escape(T).
1448 xml_attr_escape([0'>|T]) --> !, ">", xml_attr_escape(T). % strictly speaking not necessary for attribute values
1449 xml_attr_escape([10|T]) --> !, " ", xml_attr_escape(T). % newline
1450 xml_attr_escape([9|T]) --> !, " ", xml_attr_escape(T). % tab
1451 xml_attr_escape([0'"|T]) --> !, """, xml_attr_escape(T).
1452 xml_attr_escape([H|T]) --> [H], xml_attr_escape(T).
1453
1454 % ----------------
1455
1456 html_escape(Number, EscapedAtom) :-
1457 number(Number),!, number_chars(Number,C), atom_chars(EscapedAtom,C). % to detect when label=value
1458 html_escape(Atom, EscapedAtom) :-
1459 atom_codes(Atom,Codes),
1460 html_escape_codes(Codes,ECodes),
1461 atom_codes(EscapedAtom,ECodes).
1462 % atom_chars(Atom, Chars),
1463 % maplist(html_string_escape_map, Chars, EscapedChars),!,
1464 % scanlist(x_atom_concat_rev, EscapedChars, '', EscapedAtom). % very inefficient for large atoms
1465
1466
1467 html_escape_codes([],[]).
1468 html_escape_codes([H|T],Res) :-
1469 \+ no_escape_necessary(H), % avoid calling atom_codes
1470 atom_codes(Atom,[H]),
1471 html_string_escape_map(Atom,NewAtom), % TODO: rewrite html_escape map to work with codes
1472 !,
1473 atom_codes(NewAtom,NewCodes),
1474 append(NewCodes,TR,Res),
1475 html_escape_codes(T,TR).
1476 html_escape_codes([H|T],[H|TR]) :-
1477 html_escape_codes(T,TR).
1478
1479 no_escape_necessary(32).
1480 no_escape_necessary(40). % (
1481 no_escape_necessary(41). % )
1482 no_escape_necessary(44). % ,
1483 no_escape_necessary(45). % -
1484 no_escape_necessary(46). % .
1485 no_escape_necessary(61). % =
1486 no_escape_necessary(95).
1487 no_escape_necessary(X) :- (X>=97,X=<122) ; (X>=65, X=<90) ; (X>=48, X=<57). % alphadigit
1488
1489 dot_string_escape_map('\n', '\\n').
1490 dot_string_escape_map('\\', '\\\\').
1491 dot_string_escape_map('"', '\\"').
1492 dot_string_escape_map('\'', '\\\'').
1493 dot_string_escape_map('{', '\\{'). % important if used inside dot records
1494 dot_string_escape_map('}', '\\}'). % important if used inside dot records
1495 dot_string_escape_map('|', '\\|'). % important if used inside dot records
1496 dot_string_escape_map('>', '\\>'). % important if used inside dot records
1497 dot_string_escape_map('<', '\\<'). % important if used inside dot records
1498 dot_string_escape_map(A,B) :- x_string_escape_map(A,B).
1499
1500 html_string_escape_map('&', '&') :- !. % michael: habe diese 3 Zeilen auskommentiert
1501 html_string_escape_map('<', '<') :- !.
1502 html_string_escape_map('>', '>') :- !.
1503 html_string_escape_map('"', '"') :- !.
1504 html_string_escape_map(A,B) :- x_string_escape_map(A,B),!.
1505
1506
1507 % this is the SICSTus encoding for Unicode either \octal_number\ or \xHexNumber\
1508 % See Section 4.1.7.6 Escape Sequences in SICStus Manual (page 60)
1509 x_string_escape_map('\344\', 'ä'). % a mit Umlaut
1510 x_string_escape_map('\366\', 'ö'). % o mit Umlaut
1511 x_string_escape_map('\374\', 'ü'). % u mit Umlaut
1512 x_string_escape_map('\304\', 'Ä'). % A mit Umlaut
1513 x_string_escape_map('\326\', 'Ö'). % O mit Umlaut
1514 x_string_escape_map('\334\', 'Ü'). % U mit Umlaut
1515
1516 x_string_escape_map('\353\', 'ë'). % e mit Umlaut
1517 x_string_escape_map('\313\', 'Ë'). % E mit Umlaut
1518
1519 x_string_escape_map('\350\', 'è'). % e mit Accent
1520 x_string_escape_map('\351\', 'é').
1521 x_string_escape_map('\352\', 'ê').
1522 x_string_escape_map('\310\', 'È'). % E mit Accent
1523 x_string_escape_map('\311\', 'É').
1524 x_string_escape_map('\312\', 'Ê').
1525
1526 x_string_escape_map('\340\', 'à'). % a mit Accent
1527 x_string_escape_map('\341\', 'á').
1528 x_string_escape_map('\342\', 'â').
1529 x_string_escape_map('\300\', 'À'). % A mit Accent
1530 x_string_escape_map('\301\', 'Á').
1531 x_string_escape_map('\302\', 'Â').
1532
1533 x_string_escape_map('\354\', 'ì'). % i mit Accent
1534 x_string_escape_map('\355\', 'í').
1535 x_string_escape_map('\356\', 'î').
1536 x_string_escape_map('\314\', 'Ì'). % I mit Accent
1537 x_string_escape_map('\315\', 'Í').
1538 x_string_escape_map('\316\', 'Î').
1539
1540 x_string_escape_map('\362\', 'ò'). % o mit Accent
1541 x_string_escape_map('\363\', 'ó').
1542 x_string_escape_map('\364\', 'ô').
1543 x_string_escape_map('\322\', 'Ò'). % O mit Accent
1544 x_string_escape_map('\323\', 'Ó').
1545 x_string_escape_map('\324\', 'Ô').
1546
1547 x_string_escape_map('\347\', 'ç'). % cedille
1548 x_string_escape_map('\307\', 'Ç'). % Cedille
1549
1550 x_string_escape_map('\337\', 'ß'). % scharfes S (sz)
1551 x_string_escape_map('\361\', 'ñ'). % n with tilde
1552
1553 x_string_escape_map('\1661\', 'α'). % Greek
1554 x_string_escape_map('\1662\', 'β').
1555 x_string_escape_map('\1663\', 'γ').
1556 x_string_escape_map('\1664\', 'δ').
1557 x_string_escape_map('\1665\', 'ε').
1558 x_string_escape_map('\1666\', 'ζ').
1559 x_string_escape_map('\1667\', 'η').
1560 x_string_escape_map('\1670\', 'θ').
1561 x_string_escape_map('\1671\', 'ι').
1562 x_string_escape_map('\1672\', 'κ').
1563 x_string_escape_map('\1673\', 'λ').
1564 x_string_escape_map('\1674\', 'μ').
1565 x_string_escape_map('\1675\', 'ν').
1566 x_string_escape_map('\1676\', 'ξ').
1567 x_string_escape_map('\1677\', 'ο').
1568 x_string_escape_map('\1700\', 'π').
1569 x_string_escape_map('\1701\', 'ρ').
1570 x_string_escape_map('\1702\', 'ς').
1571 x_string_escape_map('\1703\', 'σ').
1572 x_string_escape_map('\1704\', 'τ').
1573 x_string_escape_map('\1705\', 'υ').
1574 x_string_escape_map('\1706\', 'φ').
1575 x_string_escape_map('\1707\', 'χ').
1576 x_string_escape_map('\1710\', 'ψ').
1577 x_string_escape_map('\1711\', 'ω').
1578
1579 % to do: add missing upper-case Greek letters:
1580 x_string_escape_map('\1624\', 'Δ').
1581 x_string_escape_map('\1630\', 'Θ').
1582 x_string_escape_map('\1633\', 'Λ').
1583 x_string_escape_map('\1636\', 'Ξ').
1584 x_string_escape_map('\1645\', 'Υ').
1585 x_string_escape_map('\1647\', 'Χ').
1586 x_string_escape_map('\1650\', 'Ψ').
1587 x_string_escape_map('\1651\', 'Ω').
1588
1589 % other symbols (converted using http://www.online-toolz.com/tools/unicode-html-entities-convertor.php )
1590 x_string_escape_map('\21242\','⊢'). %vdash turnstyle decimal: 8866
1591 x_string_escape_map('\21250\','⊨'). %models turnstyle decimal: 8872
1592 x_string_escape_map('\x21D4\','⇔'). % equivalence
1593 x_string_escape_map('\x21D2\','⇒'). % implication
1594 x_string_escape_map('\x2203\','∃'). % exists
1595 x_string_escape_map('\x2200\','∀'). % forall
1596 x_string_escape_map('ยท','·'). % dot used for quantifiers, ASCII 183
1597 x_string_escape_map('\x2227\','∧'). % conjunct
1598 x_string_escape_map('\x2228\','∨'). % disjunct
1599 x_string_escape_map('\xAC\','¬'). % negation
1600 x_string_escape_map('\x21A6\','↦'). % maplet |->
1601 x_string_escape_map('\x2286\','⊆'). % <: subseteq
1602 x_string_escape_map('\x222A\','∪'). % union
1603 x_string_escape_map('\x2229\','∩'). % intersection
1604 x_string_escape_map('\x2205\','∅'). % empty set
1605 x_string_escape_map('\x2260\','≠'). % not equal
1606 x_string_escape_map('\x2264\','≤'). % less equal
1607 x_string_escape_map('\x2265\','≥'). % greater equal
1608 x_string_escape_map('\x2124\','ℤ'). % Z (INTEGER)
1609 x_string_escape_map('\x2115\','ℕ'). % NATURAL
1610
1611 % Numbers without HTML translation: .. 8229
1612
1613 % translate unknown unicode chars to lozenge
1614 x_string_escape_map(Unicode, Result) :- atom(Unicode),
1615 atom_codes(Unicode,[Nr]), Nr>127,
1616 !,
1617 ajoin(['&#',Nr,';'],Result).
1618 %x_string_escape_map(Unicode, '◊') :-
1619 % atom(Unicode), atom_codes(Unicode,[Code]), Code>127.
1620 % format(user_output,'Uni: ~w ~n',[Unicode]).
1621 x_string_escape_map(X, X).
1622
1623 %
1624 % split_list(Pred,List,ListA,ListB):
1625 % List contains exactly the same elements as ListA and ListB
1626 % An element E is member of ListA iff Pred(E) is true and E is element of List.
1627 % An element E is member of ListB iff Pred(E) is false and E is element of List.
1628 % combination of include/exclude from library(lists) (include_exclude)
1629 :- assert_must_succeed(( split_list(number,[1,a,2,b],S,F), S==[1,2], F==[a,b] )).
1630 split_list(Pred,List,A,B) :-
1631 split_list2(List,Pred,A,B).
1632 split_list2([],_Pred,[],[]).
1633 split_list2([Elem|Rest],Pred,A,B) :-
1634 ? ( call(Pred,Elem) -> A=[Elem|AR], B=BR
1635 ; A=AR, B=[Elem|BR]),
1636 split_list2(Rest,Pred,AR,BR).
1637
1638
1639 :- assert_must_succeed(( map_split_list(just_for_unit_test_add(0),[1,2,3],S,F), S==[10,20,30], F==[] )).
1640 % like map_list but put unsuccessful elements in a second list
1641 map_split_list(Pred,List,A,B) :-
1642 map_split_list2(List,Pred,A,B).
1643 map_split_list2([],_Pred,[],[]).
1644 map_split_list2([Elem|Rest],Pred,A,B) :-
1645 ( call(Pred,Elem,Res) -> A=[Res|AR], B=BR
1646 ; A=AR, B=[Elem|BR]),
1647 map_split_list2(Rest,Pred,AR,BR).
1648
1649 % a variation of split_list which also returns a list of predicate results
1650 % with re_split_list_idx(L,PredResult,A,B) : we can split another list using the same pattern
1651 split_list_idx(Pred,List,PredResult,A,B) :-
1652 split_list_idx2(List,Pred,PredResult,A,B).
1653 split_list_idx2([],_Pred,[],[],[]).
1654 split_list_idx2([Elem|Rest],Pred,[PredTrue|PT],A,B) :-
1655 (call(Pred,Elem) -> PredTrue=true, A=[Elem|AR], B=BR
1656 ; PredTrue=false, A=AR, B=[Elem|BR]),
1657 split_list_idx2(Rest,Pred,PT,AR,BR).
1658
1659
1660 re_split_list_idx([],[],[],[]).
1661 re_split_list_idx([Elem|Rest],[PredTrue|PT],A,B) :-
1662 (PredTrue=true -> A=[Elem|AR], B=BR
1663 ; A=AR, B=[Elem|BR]),
1664 re_split_list_idx(Rest,PT,AR,BR).
1665
1666
1667
1668 % try and avoid going through same transitions twice
1669 % works with simple transition ids, or with atom/3 entries from ltl model_checker
1670 :- assert_must_succeed((minimize_lasso([0,1,2,4,8,14,7,12],[15,4,8,14,7,12],P,L),
1671 P==[0,1,2], L==[4,8,14,7,12,15])).
1672 minimize_lasso(Prefix,Loop,NewPrefix,NewLoop) :- reverse(Prefix,RP), reverse(Loop,RL),
1673 common_prefix(RP,RL,Common,RestP,RestLoop),
1674 append(RestLoop,Common,NewLR), reverse(NewLR,NewLoop),
1675 reverse(RestP,NewPrefix).
1676 common_prefix([H|T1],[H|T2],[H|Res],Rest1,Rest2) :- !, common_prefix(T1,T2,Res,Rest1,Rest2).
1677 common_prefix(T1,T2,[],T1,T2).
1678
1679
1680
1681 just_for_unit_test_add(A,B,C) :- C is A+(10*B).
1682 :- assert_must_succeed(( foldl(just_for_unit_test_add,[],0,R), R==0 )).
1683 :- assert_must_succeed(( foldl(just_for_unit_test_add,[3,6,7,2,6],0,R), R==36726 )).
1684
1685 foldl(MPred,List,Start,Result) :-
1686 foldl2(List,MPred,Start,Result).
1687 foldl2([],_Pred,Value,Value).
1688 foldl2([Elem|Rest],MPred,OldValue,NewValue) :-
1689 call(MPred,Elem,OldValue,Value),
1690 foldl2(Rest,MPred,Value,NewValue).
1691
1692
1693 just_for_unit_test_add2(A,B,C,D) :- D is 100*C+10*A+B.
1694 :- assert_must_succeed(( foldl(just_for_unit_test_add2,[],[],0,R), R==0 )).
1695 :- assert_must_succeed(( foldl(just_for_unit_test_add2,[3,6,7,2,6],[5,2,9,0,8],0,R), R==3562792068 )).
1696
1697 foldl(MPred,List,List1,Start,Result) :-
1698 foldl2(List,MPred,List1,Start,Result).
1699 foldl2([],_Pred,[],Value,Value).
1700 foldl2([Elem|Rest],MPred,[H1|R1],OldValue,NewValue) :-
1701 call(MPred,Elem,H1,OldValue,Value),
1702 foldl2(Rest,MPred,R1,Value,NewValue).
1703
1704
1705 just_for_unit_test_add3(A,B,C,D,E) :- E is D*1000+100*A+10*B+C.
1706 :- assert_must_succeed(( foldl(just_for_unit_test_add3,[],[],[],0,R), R==0 )).
1707 :- assert_must_succeed(( foldl(just_for_unit_test_add3,[3,6,7],[5,2,9],[4,1,0],0,R),
1708 R==354621790 )).
1709
1710 foldl(MPred,A,B,C,Start,Result) :-
1711 foldl2(A,MPred,B,C,Start,Result).
1712 foldl2([],_Pred,[],[],Value,Value).
1713 foldl2([Elem|Rest],MPred,[H1|R1],[H2|R2],OldValue,NewValue) :-
1714 call(MPred,Elem,H1,H2,OldValue,Value),
1715 foldl2(Rest,MPred,R1,R2,Value,NewValue).
1716
1717
1718 :- assert_must_succeed(( maplist5(just_for_unit_test_add2,[],[],[],R), R==[] )).
1719 :- assert_must_succeed(( maplist5(just_for_unit_test_add2,[1],[2],[3],R), R==[312] )).
1720 :- assert_must_succeed(( maplist5(just_for_unit_test_add2,[1,1],[2,0],[3,0],R), R==[312,10] )).
1721
1722 maplist5(_P,[],[],[],[]).
1723 maplist5(P,[H1|T1],[H2|T2],[H3|T3],[H4|T4]) :-
1724 if(call(P,H1,H2,H3,H4),
1725 ? maplist5(P,T1,T2,T3,T4),
1726 (add_internal_error('Call fails: ',maplist5(P,H1,H2,H3,H4)),
1727 fail)).
1728
1729
1730 :- assert_must_succeed(( average([2,4,8,10],Avg), D is abs(Avg-6),D<0.001 )).
1731 :- assert_must_succeed(( average([2],Avg), D is abs(Avg-2), D<0.001 )).
1732 average(List,Avg) :-
1733 length(List,N),sumlist(List,Sum),Avg is Sum/N.
1734
1735
1736 % assert_once works like assertz, but checks if the fact has already been stored before and
1737 % will not store it a second time
1738 assert_once(MPredicate) :-
1739 (call(MPredicate) -> true ; assertz(MPredicate)).
1740
1741
1742
1743 :- dynamic id_counter/1.
1744 unique_id(Prefix,Id) :-
1745 (id_counter(V) -> retractall(id_counter(_)) ; V is 0),
1746 N is V+1,
1747 assertz( id_counter(N) ),
1748 number_codes(N,NCodes),
1749 append(Prefix,NCodes,ICodes),
1750 atom_codes(Id,ICodes).
1751
1752 % PROBPATH is the runtime search path for defintion files not found relative to the original machine.
1753 % By default this is the stdlib directory relative to the prob base dir.
1754 % The user can provide a list of : separated directories in the PROBPATH
1755 % environment variables which are prepended to the default path.
1756 get_PROBPATH(PROBPATH) :-
1757 environ('PROBPATH', CustomPATH),
1758 atom_length(CustomPATH, L),
1759 L > 0, !,
1760 get_path_separator(PS),
1761 get_stdlib_path(STDLIB), ajoin([CustomPATH, PS, STDLIB], PROBPATH).
1762
1763 get_PROBPATH(PROBPATH) :- get_stdlib_path(PROBPATH).
1764
1765 get_stdlib_path(STDLIB) :- runtime_application_path(Base), atom_concat(Base, '/stdlib', STDLIB).
1766
1767 % Platform specific path separator char
1768 map_path_separator(windows, ';').
1769 map_path_separator(_, ':').
1770
1771 get_path_separator(PS) :- host_platform(Platform), map_path_separator(Platform, PS), !.
1772
1773 %% atom_to_number(?Atom, ?Number).
1774 atom_to_number(Atom, Number) :-
1775 atom(Atom),
1776 catch((
1777 atom_codes(Atom, Codes),
1778 number_codes(Number, Codes)
1779 ), _, fail).
1780 atom_to_number(Atom, Number) :-
1781 number(Number),
1782 number_codes(Number, Codes),
1783 atom_codes(Atom, Codes).
1784
1785 % ----------
1786
1787 % useful for e.g. SWI Prolog where redefine_warnings is not defined
1788 get_set_optional_prolog_flag(Flag,Old,New) :-
1789 (catch(current_prolog_flag(Flag, Old), error(domain_error(_, _), _), fail) ->
1790 set_prolog_flag(Flag, New)
1791 ;
1792 current_prolog_flag(dialect, Prolog),
1793 format('Prolog flag ~w not supported on ~w~n', [Flag, Prolog])
1794 ).
1795
1796
1797 % ----------
1798
1799 % predicates for lists with optional items represented using optional_value(V,pred_true/pred_false)
1800 % we assume that optional values constructors (optional_value/2) are instantiated *before* calling maplist_optional
1801
1802 ?map_optlist(Pred,List) :- map_optlist_aux(List,Pred).
1803
1804 :- block map_optlist_aux(-,?).
1805 map_optlist_aux([],_) :- !.
1806 map_optlist_aux([H|T],Pred) :- !,
1807 ? call_optional(Pred,H),
1808 ? map_optlist_aux(T,Pred).
1809 map_optlist_aux(Other,Pred) :-
1810 add_internal_error('First argument not a list:',map_optlist_aux(Other,Pred)).
1811
1812 call_optional(Pred,OptVal) :-
1813 is_optional_value(OptVal,Val,Use),
1814 !,
1815 call_optional_aux(Use,Pred,Val).
1816 ?call_optional(Pred,Val) :- call(Pred,Val).
1817
1818 :- block call_optional_aux(-,?,?).
1819 call_optional_aux(pred_true,Pred,Val) :- !, call(Pred,Val).
1820 call_optional_aux(pred_false,_,_) :- !. % ignore optional item
1821 call_optional_aux(Other,Pred,_) :-
1822 add_internal_error('First argument not a BOOL:',call_optional_aux(Other,Pred)).
1823
1824 :- block optlist_to_list(-,?).
1825 optlist_to_list([],R) :- !, R=[].
1826 optlist_to_list([OptVal|T],R) :- !,
1827 (is_optional_value(OptVal,Val,Use)
1828 -> optlist_to_list_aux(Use,Val,T,R)
1829 ; R = [OptVal|RT], optlist_to_list(T,RT)
1830 ).
1831 optlist_to_list(Other,R) :-
1832 add_internal_error('First argument not a list:',optlist_to_list(Other,R)),
1833 R=Other.
1834
1835 :- block optlist_to_list_aux(-,?,?,?).
1836 optlist_to_list_aux(pred_true,Val,T,[Val|TR]) :- !, optlist_to_list(T,TR).
1837 optlist_to_list_aux(pred_false,_,T,R) :- !, optlist_to_list(T,R).
1838 optlist_to_list_aux(Other,V,T,R) :-
1839 add_internal_error('First argument not a BOOL:',optlist_to_list_aux(Other,V,T,R)),
1840 fail.
1841
1842 is_optional_value(OptVal,Val,Use) :- nonvar(OptVal), OptVal = optional_value(Val,Use).
1843
1844 % ---------
1845
1846 % blackboard utilities
1847
1848
1849 bb_safe_get(Counter,R) :- (bb_get(Counter,X) -> R=X ; R=0).
1850
1851 bb_inc(Counter) :- bb_inc_by(Counter,1).
1852
1853 bb_inc_by(Counter,Inc) :-
1854 bb_safe_get(Counter,R),
1855 R1 is R+Inc, bb_put(Counter,R1).
1856
1857
1858 % ---------
1859
1860
1861 :- use_module(library(avl),[ord_list_to_avl/2]).
1862
1863 % a slight variation of top_sort from library(ugraphs) which allows the sorting to be not completed
1864 % top_sort(G,S,0) corresponds to top_sort in ugraphs
1865 top_sort(Graph, Sorted,UnsortedNr) :-
1866 ugraphs:fanin_counts(Graph, Counts),
1867 ugraphs:get_top_elements(Counts, Top, 0, I),
1868 ord_list_to_avl(Counts, Map),
1869 top_sort_aux(Top, I, Map, Sorted,UnsortedNr).
1870
1871 top_sort_aux([], I, _, [], UnsortedNr) :- UnsortedNr = I.
1872 top_sort_aux([V-VN|Top0], I, Map0, [V|Sorted],UnsortedNr) :-
1873 ugraphs:dec_counts(VN, I, J, Map0, Map, Top0, Top),
1874 top_sort_aux(Top, J, Map, Sorted,UnsortedNr).