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