2009-07-21 04:56:54 +01:00
|
|
|
|
2010-04-22 12:16:37 +01:00
|
|
|
% SWI emulation.
|
|
|
|
% written in an on-demand basis.
|
|
|
|
|
|
|
|
|
|
|
|
:- module(system, [term_to_atom/2,
|
2010-04-22 16:27:32 +01:00
|
|
|
concat_atom/2,
|
|
|
|
concat_atom/3,
|
|
|
|
setenv/2,
|
|
|
|
prolog_to_os_filename/2,
|
|
|
|
is_absolute_file_name/1,
|
|
|
|
read_clause/1,
|
|
|
|
string/1,
|
|
|
|
working_directory/2,
|
|
|
|
chdir/1,
|
|
|
|
compile_aux_clauses/1,
|
|
|
|
convert_time/2,
|
|
|
|
'$set_source_module'/2,
|
|
|
|
'$declare_module'/5,
|
|
|
|
'$set_predicate_attribute'/3,
|
|
|
|
load_foreign_library/1,
|
|
|
|
load_foreign_library/2,
|
|
|
|
time_file/2,
|
|
|
|
flag/3,
|
|
|
|
current_flag/1
|
2010-04-22 12:16:37 +01:00
|
|
|
]).
|
|
|
|
|
|
|
|
:- reexport(library(charsio),[
|
|
|
|
write_to_chars/2,
|
|
|
|
read_from_chars/2
|
2009-12-03 16:33:10 +00:00
|
|
|
]).
|
|
|
|
|
2010-04-22 12:16:37 +01:00
|
|
|
:- reexport(library(lists),[append/2,
|
|
|
|
append/3,
|
|
|
|
delete/3,
|
|
|
|
member/2,
|
|
|
|
flatten/2,
|
|
|
|
intersection/3,
|
|
|
|
last/2,
|
|
|
|
memberchk/2,
|
|
|
|
max_list/2,
|
|
|
|
min_list/2,
|
|
|
|
nextto/3,
|
|
|
|
permutation/2,
|
|
|
|
reverse/2,
|
|
|
|
select/3,
|
|
|
|
selectchk/3,
|
|
|
|
sublist/2,
|
|
|
|
sumlist/2,
|
|
|
|
nth1/3,
|
|
|
|
nth0/3]).
|
|
|
|
|
|
|
|
:- reexport(library(apply),[maplist/2,
|
|
|
|
maplist/3,
|
|
|
|
maplist/4,
|
|
|
|
maplist/5,
|
|
|
|
include/3,
|
|
|
|
exclude/3,
|
|
|
|
partition/4,
|
|
|
|
partition/5
|
|
|
|
]).
|
|
|
|
|
|
|
|
:- reexport(library(system),
|
2009-03-06 10:59:02 +00:00
|
|
|
[datime/1,
|
|
|
|
mktime/2,
|
2010-02-28 22:19:52 +00:00
|
|
|
file_property/2,
|
2009-03-06 10:59:02 +00:00
|
|
|
sleep/1]).
|
|
|
|
|
2010-04-22 12:16:37 +01:00
|
|
|
:- reexport(library(arg),
|
2009-03-06 10:59:02 +00:00
|
|
|
[genarg/3]).
|
|
|
|
|
2010-04-22 12:16:37 +01:00
|
|
|
:- reexport(library(apply_macros),
|
2009-04-24 22:43:08 +01:00
|
|
|
[]).
|
|
|
|
|
2010-04-22 12:16:37 +01:00
|
|
|
:- reexport(library(terms),
|
2009-03-06 10:59:02 +00:00
|
|
|
[subsumes/2,
|
2010-02-28 10:07:36 +00:00
|
|
|
subsumes_chk/2,
|
2009-03-06 10:59:02 +00:00
|
|
|
term_hash/2,
|
|
|
|
unifiable/3,
|
2010-04-22 12:16:37 +01:00
|
|
|
cyclic_term/1,
|
2009-03-06 10:59:02 +00:00
|
|
|
variant/2]).
|
|
|
|
|
2010-04-22 12:16:37 +01:00
|
|
|
:- source.
|
|
|
|
|
|
|
|
:- style_check(all).
|
|
|
|
|
|
|
|
:- yap_flag(unknown,error).
|
|
|
|
|
|
|
|
:- yap_flag(open_expands_filename,false).
|
|
|
|
|
|
|
|
:- yap_flag(autoload,true).
|
|
|
|
|
|
|
|
|
|
|
|
:- set_prolog_flag(user_flags,silent).
|
|
|
|
|
2010-05-04 15:15:46 +01:00
|
|
|
:- load_foreign_files([plstream], [], initIO).
|
|
|
|
|
|
|
|
% Time is given as a float in SWI-Prolog.
|
|
|
|
swi_get_time(FSecs) :- datime(Datime), mktime(Datime, Secs), FSecs is Secs*1.0.
|
|
|
|
|
2010-04-22 12:16:37 +01:00
|
|
|
goal_expansion(atom_concat(A,B),atomic_concat(A,B)).
|
|
|
|
goal_expansion(atom_concat(A,B,C),atomic_concat(A,B,C)).
|
2010-05-05 19:37:56 +01:00
|
|
|
%goal_expansion(arg(A,_,_),_) :- nonvar(A), !, fail.
|
2010-04-22 12:16:37 +01:00
|
|
|
goal_expansion(arg(A,B,C),genarg(A,B,C)).
|
|
|
|
goal_expansion(time_file(A,B),system:swi_time_file(A,B)).
|
2010-05-04 15:15:46 +01:00
|
|
|
|
2010-04-22 12:16:37 +01:00
|
|
|
goal_expansion(get_time(A),system:swi_get_time(A)).
|
2010-05-04 15:15:46 +01:00
|
|
|
goal_expansion(time_file(A,B),system:swi_time_file(A,B)).
|
|
|
|
goal_expansion(expand_file_name(A,B),system:swi_expand_file_name(A,B)).
|
|
|
|
goal_expansion(wildcard_match(A,B),system:swi_wilcard_match(A,B)).
|
|
|
|
goal_expansion(directory_files(A,B),system:swi_directory_files(A,B)).
|
|
|
|
goal_expansion(file_name_extension(A,B,C), system:swi_file_name_extension(A,B,C)).
|
|
|
|
goal_expansion(exists_file(A), system:swi_exists_file(A)).
|
|
|
|
goal_expansion(exists_directory(A), system:swi_exists_directory(A)).
|
|
|
|
|
|
|
|
:- dynamic swi_io/0.
|
|
|
|
|
|
|
|
:- if(swi_io).
|
|
|
|
goal_expansion(open(A,B,C,D),system:swi_open(A,B,C,D)).
|
|
|
|
goal_expansion(open(A,B,C), system:swi_open(A,B,C)).
|
|
|
|
goal_expansion(close(A), system:swi_close(A)).
|
|
|
|
goal_expansion(close(A,B), system:swi_close(A,B)).
|
|
|
|
goal_expansion(set_input(A), system:swi_set_input(A)).
|
|
|
|
goal_expansion(set_output(A), system:swi_set_output(A)).
|
|
|
|
goal_expansion(current_input(A), system:swi_current_input(A)).
|
|
|
|
goal_expansion(current_output(A), system:swi_current_output(A)).
|
|
|
|
goal_expansion(get_code(A,B),system:swi_get_code(A,B)).
|
|
|
|
goal_expansion(get_code(A), system:swi_get_code(A)).
|
|
|
|
goal_expansion(get_char(A,B),system:swi_get_char(A,B)).
|
|
|
|
goal_expansion(get_char(A), system:swi_get_char(A)).
|
|
|
|
goal_expansion(get_byte(A,B),system:swi_get_byte(A,B)).
|
|
|
|
goal_expansion(get_byte(A), system:swi_get_byte(A)).
|
|
|
|
goal_expansion(peek_code(A,B),system:swi_peek_code(A,B)).
|
|
|
|
goal_expansion(peek_code(A), system:swi_peek_code(A)).
|
|
|
|
goal_expansion(peek_char(A,B),system:swi_peek_char(A,B)).
|
|
|
|
goal_expansion(peek_char(A), system:swi_peek_char(A)).
|
|
|
|
goal_expansion(peek_byte(A,B),system:swi_peek_byte(A,B)).
|
|
|
|
goal_expansion(peek_byte(A), system:swi_peek_byte(A)).
|
|
|
|
goal_expansion(put_byte(A,B),system:swi_put_byte(A,B)).
|
|
|
|
goal_expansion(put_byte(A), system:swi_put_byte(A)).
|
|
|
|
goal_expansion(put_code(A,B),system:swi_put_code(A,B)).
|
|
|
|
goal_expansion(put_code(A), system:swi_put_code(A)).
|
|
|
|
goal_expansion(put_char(A,B),system:swi_put_char(A,B)).
|
|
|
|
goal_expansion(put_char(A), system:swi_put_char(A)).
|
|
|
|
goal_expansion(flush_output, system:swi_flush_output).
|
|
|
|
goal_expansion(flush_output(A), system:swi_flush_output(A)).
|
|
|
|
goal_expansion(at_end_of_stream(A), system:swi_at_end_of_stream(A)).
|
|
|
|
goal_expansion(at_end_of_stream, system:swi_at_end_of_stream).
|
|
|
|
goal_expansion(stream_property(A,B),system:swi_stream_property(A,B)).
|
|
|
|
goal_expansion(set_stream_position(A,B),system:swi_set_stream_position(A,B)).
|
|
|
|
|
|
|
|
/* edinburgh IO */
|
|
|
|
goal_expansion(see(A), system:swi_see(A)).
|
|
|
|
goal_expansion(seen, system:swi_seen).
|
|
|
|
goal_expansion(seeing(A), system:swi_seeing(A)).
|
|
|
|
goal_expansion(tell(A), system:swi_tell(A)).
|
|
|
|
goal_expansion(append(A), system:swi_append(A)).
|
|
|
|
goal_expansion(told, system:swi_told).
|
|
|
|
goal_expansion(telling(A), system:swi_telling(A)).
|
|
|
|
goal_expansion(put(A,B),system:swi_put(A,B)).
|
|
|
|
goal_expansion(put(A), system:swi_put(A)).
|
|
|
|
goal_expansion(skip(A), system:swi_skip(A)).
|
|
|
|
goal_expansion(skip(A,B),system:swi_skip(A,B)).
|
|
|
|
goal_expansion(get(A), system:swi_get(A)).
|
|
|
|
goal_expansion(get(A,B),system:swi_get(A,B)).
|
|
|
|
goal_expansion(get0(A,B),system:swi_get0(A,B)).
|
|
|
|
goal_expansion(get0(A), system:swi_get0(A)).
|
|
|
|
goal_expansion(ttyflush, system:swi_ttyflush).
|
|
|
|
goal_expansion(prompt(A,B),system:swi_prompt(A,B)).
|
|
|
|
goal_expansion(tab(A,B),system:swi_tab(A,B)).
|
|
|
|
goal_expansion(tab(A), system:swi_tab(A)).
|
|
|
|
/* Quintus IO */
|
|
|
|
goal_expansion(byte_count(A,B),system:swi_byte_count(A,B)).
|
|
|
|
goal_expansion(character_count(A,B),system:swi_character_count(A,B)).
|
|
|
|
goal_expansion(line_count(A,B),system:swi_line_count(A,B)).
|
|
|
|
goal_expansion(line_position(A,B),system:swi_line_position(A,B)).
|
|
|
|
goal_expansion(open_null_stream(A), system:swi_open_null_stream(A)).
|
|
|
|
|
|
|
|
/* SWI specific */
|
|
|
|
goal_expansion(is_stream(A), system:swi_is_stream(A)).
|
|
|
|
goal_expansion(set_stream(A,B),system:swi_set_stream(A,B)).
|
|
|
|
goal_expansion(with_output_to(A,B),system:swi_with_output_to(A,B)).
|
|
|
|
goal_expansion(set_prolog_IO(A,B,C), system:swi_set_prolog_IO(A,B,C)).
|
|
|
|
goal_expansion(protocol(A), system:swi_protocol(A)).
|
|
|
|
goal_expansion(protocola(A), system:swi_protocola(A)).
|
|
|
|
goal_expansion(noprotocol, noprotocol).
|
|
|
|
goal_expansion(protocolling(A), system:swi_protocolling(A)).
|
|
|
|
goal_expansion(prompt1(A), system:swi_prompt1(A)).
|
|
|
|
goal_expansion(seek(A,B,C,D),system:swi_seek(A,B,C,D)).
|
|
|
|
goal_expansion(wait_for_input(A,B,C), system:swi_wait_for_input(A,B,C)).
|
|
|
|
goal_expansion(get_single_char(A), system:swi_get_single_char(A)).
|
|
|
|
goal_expansion(read_pending_input(A,B,C), system:swi_read_pending_input(A,B,C)).
|
|
|
|
goal_expansion(source_location(A,B),system:swi_source_location(A,B)).
|
|
|
|
goal_expansion(copy_stream_data(A,B,C), system:swi_copy_stream_data(A,B,C)).
|
|
|
|
goal_expansion(copy_stream_data(A,B),system:swi_copy_stream_data(A,B)).
|
|
|
|
|
|
|
|
/* SWI internal */
|
|
|
|
goal_expansion('$push_input_context', system:'swi_$push_input_context').
|
|
|
|
goal_expansion('$pop_input_context', system:'swi_$pop_input_context').
|
|
|
|
goal_expansion('$size_stream'(A,B),system:'swi_$size_stream'(A,B)).
|
|
|
|
|
|
|
|
goal_expansion(working_directory(A,B),system:swi_working_directory(A,B)).
|
|
|
|
goal_expansion(access_file(A,B),system:swi_access_file(A,B)).
|
|
|
|
goal_expansion(size_file(A,B),system:swi_size_file(A,B)).
|
|
|
|
goal_expansion(read_link(A,B,C), system:swi_read_link(A,B,C)).
|
|
|
|
goal_expansion(tmp_file(A,B),system:swi_tmp_file(A,B)).
|
|
|
|
goal_expansion(tmp_file_stream(A,B,C), system:swi_tmp_file_stream(A,B,C)).
|
|
|
|
goal_expansion(delete_file(A), delete_file(A)).
|
|
|
|
goal_expansion(delete_directory(A), delete_directory(A)).
|
|
|
|
goal_expansion(make_directory(A), make_directory(A)).
|
|
|
|
goal_expansion(same_file(A,B),system:swi_same_file(A,B)).
|
|
|
|
goal_expansion(rename_file(A,B),system:swi_rename_file(A,B)).
|
|
|
|
goal_expansion(is_absolute_file_name(A), is_absolute_file_name(A)).
|
|
|
|
goal_expansion(file_base_name(A,B),system:swi_file_base_name(A,B)).
|
|
|
|
goal_expansion(file_directory_name(A,B),system:swi_file_directory_name(A,B)).
|
|
|
|
goal_expansion(prolog_to_os_filename(A,B),system:swi_prolog_to_os_filename(A,B)).
|
|
|
|
goal_expansion('$mark_executable'(A), system:'swi_is_absolute_file_name'(A)).
|
|
|
|
goal_expansion('$absolute_file_name'(A,B),system:'swi_$absolute_file_name'(A,B)).
|
|
|
|
:- endif.
|
2010-04-22 12:16:37 +01:00
|
|
|
|
2009-03-06 10:59:02 +00:00
|
|
|
|
|
|
|
% make sure we also use
|
|
|
|
:- user:library_directory(X),
|
|
|
|
atom(X),
|
2010-04-22 12:16:37 +01:00
|
|
|
atom_concat([X,'/dialect/swi'],SwiDir),
|
2009-03-06 10:59:02 +00:00
|
|
|
\+ user:library_directory(SwiDir),
|
|
|
|
asserta(user:library_directory(SwiDir)),
|
|
|
|
fail
|
|
|
|
;
|
|
|
|
true.
|
|
|
|
|
|
|
|
:- multifile
|
|
|
|
user:file_search_path/2.
|
|
|
|
|
|
|
|
:- dynamic
|
|
|
|
user:file_search_path/2.
|
|
|
|
|
|
|
|
user:file_search_path(swi, Home) :-
|
|
|
|
current_prolog_flag(home, Home).
|
|
|
|
user:file_search_path(foreign, swi(ArchLib)) :-
|
|
|
|
current_prolog_flag(arch, Arch),
|
|
|
|
atom_concat('lib/', Arch, ArchLib).
|
|
|
|
user:file_search_path(foreign, swi(lib)).
|
|
|
|
|
|
|
|
|
|
|
|
%
|
|
|
|
% maybe a good idea to eventually support this in YAP.
|
|
|
|
% but for now just ignore it.
|
|
|
|
%
|
2010-04-22 12:16:37 +01:00
|
|
|
load_foreign_library(P,Command) :-
|
2009-03-06 10:59:02 +00:00
|
|
|
absolute_file_name(P,[file_type(executable),solutions(first),file_errors(fail)],Lib),
|
|
|
|
load_foreign_files([Lib],[],Command).
|
|
|
|
|
2010-04-22 12:16:37 +01:00
|
|
|
load_foreign_library(P) :-
|
2009-03-06 10:59:02 +00:00
|
|
|
prolog:load_foreign_library(P,install).
|
|
|
|
|
2010-04-22 12:16:37 +01:00
|
|
|
term_to_atom(Term,Atom) :-
|
2009-03-06 10:59:02 +00:00
|
|
|
nonvar(Atom), !,
|
|
|
|
atom_codes(Atom,S),
|
|
|
|
read_from_chars(S,Term).
|
2010-04-22 12:16:37 +01:00
|
|
|
term_to_atom(Term,Atom) :-
|
2009-03-06 10:59:02 +00:00
|
|
|
write_to_chars(Term,S),
|
|
|
|
atom_codes(Atom,S).
|
|
|
|
|
2010-04-22 12:16:37 +01:00
|
|
|
concat_atom([A|List], Separator, New) :- var(List), !,
|
2009-03-06 10:59:02 +00:00
|
|
|
atom_codes(Separator,[C]),
|
|
|
|
atom_codes(New, NewChars),
|
|
|
|
split_atom_by_chars(NewChars,C,L,L,A,List).
|
2010-04-22 12:16:37 +01:00
|
|
|
concat_atom(List, Separator, New) :-
|
2009-03-06 10:59:02 +00:00
|
|
|
add_separator_to_list(List, Separator, NewList),
|
|
|
|
atomic_concat(NewList, New).
|
|
|
|
|
|
|
|
|
|
|
|
split_atom_by_chars([],_,[],L,A,[]):-
|
|
|
|
atom_codes(A,L).
|
|
|
|
split_atom_by_chars([C|NewChars],C,[],L,A,[NA|Atoms]) :- !,
|
|
|
|
atom_codes(A,L),
|
|
|
|
split_atom_by_chars(NewChars,C,NL,NL,NA,Atoms).
|
|
|
|
split_atom_by_chars([C1|NewChars],C,[C1|LF],LAtom,Atom,Atoms) :-
|
|
|
|
split_atom_by_chars(NewChars,C,LF,LAtom,Atom,Atoms).
|
|
|
|
|
|
|
|
add_separator_to_list([], _, []).
|
|
|
|
add_separator_to_list([T], _, [T]) :- !.
|
|
|
|
add_separator_to_list([H|T], Separator, [H,Separator|NT]) :-
|
|
|
|
add_separator_to_list(T, Separator, NT).
|
|
|
|
|
2010-04-22 12:16:37 +01:00
|
|
|
concat_atom(List, New) :-
|
|
|
|
atomic_concat(List, New).
|
|
|
|
|
2009-03-06 10:59:02 +00:00
|
|
|
|
2010-04-22 12:16:37 +01:00
|
|
|
setenv(X,Y) :- unix(putenv(X,Y)).
|
2009-03-06 10:59:02 +00:00
|
|
|
|
2010-04-22 12:16:37 +01:00
|
|
|
prolog_to_os_filename(X,X).
|
2009-03-06 10:59:02 +00:00
|
|
|
|
2010-04-22 12:16:37 +01:00
|
|
|
is_absolute_file_name(X) :-
|
2009-03-06 10:59:02 +00:00
|
|
|
absolute_file_name(X,X).
|
|
|
|
|
2010-04-22 12:16:37 +01:00
|
|
|
read_clause(X,Y) :-
|
2009-03-06 10:59:02 +00:00
|
|
|
read_term(X,Y,[singetons(warning)]).
|
|
|
|
|
2010-04-22 12:16:37 +01:00
|
|
|
string(_) :- fail.
|
2009-03-06 10:59:02 +00:00
|
|
|
|
|
|
|
bindings_message(V) -->
|
|
|
|
{ cvt_bindings(V, Bindings) },
|
|
|
|
prolog:message(query(_YesNo,Bindings)), !.
|
|
|
|
|
|
|
|
cvt_bindings([],[]).
|
|
|
|
cvt_bindings([[Name|Value]|L],[AName=Value|Bindings]) :-
|
|
|
|
atom_codes(AName, Name),
|
|
|
|
cvt_bindings(L,Bindings).
|
|
|
|
|
2010-04-22 12:16:37 +01:00
|
|
|
working_directory(OCWD,NCWD) :-
|
2009-03-06 10:59:02 +00:00
|
|
|
getcwd(OCWD),
|
|
|
|
(var(NCWD) -> true ; cd(NCWD)).
|
|
|
|
|
2010-04-22 12:16:37 +01:00
|
|
|
chdir(X) :- cd(X).
|
2009-03-06 10:59:02 +00:00
|
|
|
|
|
|
|
% Time is received as int, and converted to "..."
|
2010-04-22 12:16:37 +01:00
|
|
|
% ctime is a built-in.
|
|
|
|
convert_time(X,Y) :- swi:ctime(X,Y).
|
2009-03-06 10:59:02 +00:00
|
|
|
|
2010-04-22 12:16:37 +01:00
|
|
|
compile_aux_clauses([]).
|
|
|
|
compile_aux_clauses([(:- G)|Cls]) :-
|
2009-03-06 10:59:02 +00:00
|
|
|
prolog_load_context(module, M),
|
|
|
|
once(M:G),
|
2010-04-22 12:16:37 +01:00
|
|
|
compile_aux_clauses(Cls).
|
|
|
|
compile_aux_clauses([Cl|Cls]) :-
|
2009-03-06 10:59:02 +00:00
|
|
|
prolog_load_context(module, M),
|
|
|
|
assert_static(M:Cl),
|
2010-04-22 12:16:37 +01:00
|
|
|
compile_aux_clauses(Cls).
|
2009-11-23 10:12:10 +00:00
|
|
|
|
2010-04-22 12:16:37 +01:00
|
|
|
'$set_source_module'(Source0, SourceF) :-
|
|
|
|
prolog_load_context(module, Source0), !,
|
2009-11-23 10:12:10 +00:00
|
|
|
module(SourceF).
|
2010-04-22 12:16:37 +01:00
|
|
|
'$set_source_module'(Source0, SourceF) :-
|
2010-02-28 22:19:52 +00:00
|
|
|
current_module(Source0, SourceF).
|
|
|
|
|
2010-04-22 12:16:37 +01:00
|
|
|
'$declare_module'(Name, Context, _, _, _) :-
|
2009-12-04 11:00:13 +00:00
|
|
|
add_import_module(Name, Context, start).
|
2009-11-23 10:12:10 +00:00
|
|
|
|
2010-04-22 12:16:37 +01:00
|
|
|
'$set_predicate_attribute'(_, _, _).
|
2010-02-28 22:19:52 +00:00
|
|
|
|
2010-04-22 12:16:37 +01:00
|
|
|
flag(Key, Old, New) :-
|
2010-02-28 22:19:52 +00:00
|
|
|
recorded(Key, Old, R), !,
|
|
|
|
(
|
|
|
|
Old \== New
|
|
|
|
->
|
|
|
|
erase(R),
|
|
|
|
recorda(Key, New, _)
|
|
|
|
;
|
|
|
|
true
|
|
|
|
).
|
2010-04-22 12:16:37 +01:00
|
|
|
flag(Key, 0, New) :-
|
2010-02-28 22:19:52 +00:00
|
|
|
functor(Key, N, Ar),
|
|
|
|
functor(K, N, Ar),
|
2010-04-22 12:16:37 +01:00
|
|
|
assert(flag(K)),
|
2010-02-28 22:19:52 +00:00
|
|
|
recorda(K, New, _).
|
|
|
|
|
2010-04-22 12:16:37 +01:00
|
|
|
current_flag(Key) :-
|
2010-02-28 22:19:52 +00:00
|
|
|
swi:flag(Key).
|