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