2004-09-07 18:10:43 +01:00
|
|
|
|
2005-10-28 18:38:50 +01:00
|
|
|
:- source.
|
|
|
|
|
|
|
|
:- style_check(all).
|
|
|
|
|
|
|
|
:- yap_flag(unknown,error).
|
|
|
|
|
2005-10-18 18:04:43 +01:00
|
|
|
% redefines stuff in prolog module.
|
2005-03-13 06:26:13 +00:00
|
|
|
|
2005-10-18 18:04:43 +01:00
|
|
|
:- module(swi, []).
|
|
|
|
|
|
|
|
:- ensure_loaded(library(atts)).
|
2005-03-13 06:26:13 +00:00
|
|
|
|
2005-03-15 18:29:25 +00:00
|
|
|
:- use_module(library(charsio),[write_to_chars/2,read_from_chars/2]).
|
|
|
|
|
|
|
|
:- use_module(library(lists),[nth/3]).
|
|
|
|
|
2005-10-21 17:09:03 +01:00
|
|
|
:- use_module(library(system),[datime/1,
|
|
|
|
mktime/2]).
|
|
|
|
|
2005-10-18 18:04:43 +01:00
|
|
|
:- use_module(library(terms),[term_variables/2,
|
2005-10-28 18:38:50 +01:00
|
|
|
term_variables/3,
|
2007-06-02 16:37:50 +01:00
|
|
|
term_hash/2,
|
|
|
|
variant/2]).
|
2005-10-18 18:04:43 +01:00
|
|
|
|
|
|
|
:- multifile
|
|
|
|
prolog:message/3.
|
|
|
|
|
2006-02-01 13:28:57 +00:00
|
|
|
:- dynamic
|
|
|
|
prolog:message/3.
|
|
|
|
|
2005-10-18 18:04:43 +01:00
|
|
|
:- multifile
|
|
|
|
user:file_search_path/2.
|
2005-03-13 06:26:13 +00:00
|
|
|
|
2005-10-18 18:04:43 +01:00
|
|
|
:- dynamic
|
|
|
|
user:file_search_path/2.
|
2005-03-13 06:26:13 +00:00
|
|
|
|
2007-10-17 00:17:04 +01:00
|
|
|
prolog:is_list(L) :- var(L), !, fail.
|
|
|
|
prolog:is_list([]).
|
|
|
|
prolog:is_list([_|List]) :- prolog:is_list(List).
|
|
|
|
|
2005-03-13 06:26:13 +00:00
|
|
|
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)).
|
|
|
|
|
2007-10-10 10:44:28 +01:00
|
|
|
:- meta_predicate prolog:predsort(:,+,-).
|
|
|
|
|
2007-10-17 00:17:04 +01:00
|
|
|
prolog:plus(X, Y, Z) :-
|
|
|
|
integer(X),
|
|
|
|
integer(Y), !,
|
|
|
|
Z is X + Y.
|
|
|
|
prolog:plus(X, Y, Z) :-
|
|
|
|
integer(X),
|
|
|
|
integer(Z), !,
|
|
|
|
Y is Z - X.
|
|
|
|
prolog:plus(X, Y, Z) :-
|
|
|
|
integer(Y),
|
|
|
|
integer(Z), !,
|
|
|
|
X is Z - Y.
|
|
|
|
|
2007-10-10 10:44:28 +01:00
|
|
|
%% predsort(:Compare, +List, -Sorted) is det.
|
|
|
|
%
|
|
|
|
% Sorts similar to sort/2, but determines the order of two terms
|
|
|
|
% by calling Compare(-Delta, +E1, +E2). This call must unify
|
|
|
|
% Delta with one of <, > or =. If built-in predicate compare/3 is
|
|
|
|
% used, the result is the same as sort/2. See also keysort/2.
|
|
|
|
|
|
|
|
prolog:predsort(P, L, R) :-
|
|
|
|
length(L, N),
|
|
|
|
predsort(P, N, L, _, R1), !,
|
|
|
|
R = R1.
|
|
|
|
|
|
|
|
predsort(P, 2, [X1, X2|L], L, R) :- !,
|
|
|
|
call(P, Delta, X1, X2),
|
|
|
|
sort2(Delta, X1, X2, R).
|
|
|
|
predsort(_, 1, [X|L], L, [X]) :- !.
|
|
|
|
predsort(_, 0, L, L, []) :- !.
|
|
|
|
predsort(P, N, L1, L3, R) :-
|
|
|
|
N1 is N // 2,
|
|
|
|
plus(N1, N2, N),
|
|
|
|
predsort(P, N1, L1, L2, R1),
|
|
|
|
predsort(P, N2, L2, L3, R2),
|
|
|
|
predmerge(P, R1, R2, R).
|
|
|
|
|
|
|
|
sort2(<, X1, X2, [X1, X2]).
|
|
|
|
sort2(=, X1, _, [X1]).
|
|
|
|
sort2(>, X1, X2, [X2, X1]).
|
|
|
|
|
|
|
|
predmerge(_, [], R, R) :- !.
|
|
|
|
predmerge(_, R, [], R) :- !.
|
|
|
|
predmerge(P, [H1|T1], [H2|T2], Result) :-
|
|
|
|
call(P, Delta, H1, H2),
|
|
|
|
predmerge(Delta, P, H1, H2, T1, T2, Result).
|
|
|
|
|
|
|
|
predmerge(>, P, H1, H2, T1, T2, [H2|R]) :-
|
|
|
|
predmerge(P, [H1|T1], T2, R).
|
|
|
|
predmerge(=, P, H1, _, T1, T2, [H1|R]) :-
|
|
|
|
predmerge(P, T1, T2, R).
|
|
|
|
predmerge(<, P, H1, H2, T1, T2, [H1|R]) :-
|
|
|
|
predmerge(P, T1, [H2|T2], R).
|
|
|
|
|
|
|
|
|
2005-03-13 06:26:13 +00:00
|
|
|
%
|
|
|
|
% maybe a good idea to eventually support this in YAP.
|
|
|
|
% but for now just ignore it.
|
|
|
|
%
|
2005-10-18 18:04:43 +01:00
|
|
|
:- meta_predicate prolog:volatile(:).
|
2005-03-13 06:26:13 +00:00
|
|
|
|
|
|
|
:- op(1150, fx, 'volatile').
|
|
|
|
|
2005-10-18 18:04:43 +01:00
|
|
|
prolog:volatile(P) :- var(P),
|
2005-03-13 06:26:13 +00:00
|
|
|
throw(error(instantiation_error,volatile(P))).
|
2005-10-18 18:04:43 +01:00
|
|
|
prolog:volatile(M:P) :-
|
2005-03-13 06:26:13 +00:00
|
|
|
do_volatile(P,M).
|
2005-10-18 18:04:43 +01:00
|
|
|
prolog:volatile((G1,G2)) :-
|
|
|
|
prolog:volatile(G1),
|
|
|
|
prolog:volatile(G2).
|
|
|
|
prolog:volatile(P) :-
|
2005-03-13 06:26:13 +00:00
|
|
|
do_volatile(P,_).
|
|
|
|
|
2007-09-27 16:25:34 +01:00
|
|
|
prolog:load_foreign_library(P,Command) :-
|
|
|
|
absolute_file_name(P,[file_type(executable),solutions(first),file_errors(fail)],Lib),
|
|
|
|
load_foreign_files([Lib],[],Command).
|
|
|
|
|
|
|
|
prolog:load_foreign_library(P) :-
|
|
|
|
prolog:load_foreign_library(P,install).
|
|
|
|
|
2005-03-13 06:26:13 +00:00
|
|
|
do_volatile(_,_).
|
2004-09-07 18:10:43 +01:00
|
|
|
|
2007-10-10 10:44:28 +01:00
|
|
|
:- meta_predicate prolog:forall(:,:).
|
2004-09-07 18:10:43 +01:00
|
|
|
|
|
|
|
:- use_module(library(lists)).
|
|
|
|
|
2005-10-18 18:04:43 +01:00
|
|
|
prolog:term_to_atom(Term,Atom) :-
|
2005-03-15 18:29:25 +00:00
|
|
|
nonvar(Atom), !,
|
|
|
|
atom_codes(Atom,S),
|
|
|
|
read_from_chars(S,Term).
|
2005-10-18 18:04:43 +01:00
|
|
|
prolog:term_to_atom(Term,Atom) :-
|
2005-03-15 18:29:25 +00:00
|
|
|
write_to_chars(Term,S),
|
|
|
|
atom_codes(Atom,S).
|
|
|
|
|
2007-09-27 16:25:34 +01:00
|
|
|
prolog:concat_atom([A|List], Separator, New) :- var(List), !,
|
|
|
|
atom_codes(Separator,[C]),
|
|
|
|
atom_codes(New, NewChars),
|
|
|
|
split_atom_by_chars(NewChars,C,L,L,A,List).
|
2005-10-18 18:04:43 +01:00
|
|
|
prolog:concat_atom(List, Separator, New) :-
|
2004-09-07 18:10:43 +01:00
|
|
|
add_separator_to_list(List, Separator, NewList),
|
|
|
|
atomic_concat(NewList, New).
|
|
|
|
|
2005-10-18 18:04:43 +01:00
|
|
|
prolog:concat_atom(List, New) :-
|
2004-09-07 18:10:43 +01:00
|
|
|
atomic_concat(List, New).
|
|
|
|
|
2007-10-10 10:44:28 +01:00
|
|
|
|
2007-09-27 16:25:34 +01:00
|
|
|
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).
|
|
|
|
|
2004-09-07 18:10:43 +01:00
|
|
|
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).
|
|
|
|
|
|
|
|
|
2005-10-18 18:04:43 +01:00
|
|
|
prolog:setenv(X,Y) :- unix(putenv(X,Y)).
|
2004-09-07 18:10:43 +01:00
|
|
|
|
2005-10-18 18:04:43 +01:00
|
|
|
prolog:nth1(I,L,A) :- nth(I,L,A).
|
2004-09-07 18:10:43 +01:00
|
|
|
|
2007-09-27 16:25:34 +01:00
|
|
|
prolog:prolog_to_os_filename(X,X).
|
|
|
|
|
2005-10-18 18:04:43 +01:00
|
|
|
prolog:forall(X,Y) :-
|
2004-09-07 18:10:43 +01:00
|
|
|
catch(do_forall(X,Y), fail_forall, fail).
|
|
|
|
|
|
|
|
do_forall(X,Y) :-
|
|
|
|
call(X),
|
|
|
|
do_for_forall(Y).
|
|
|
|
do_forall(_,_).
|
|
|
|
|
|
|
|
do_for_forall(Y) :- call(Y), !, fail.
|
2005-04-10 05:01:15 +01:00
|
|
|
do_for_forall(_) :- throw(fail_forall).
|
2004-09-07 18:10:43 +01:00
|
|
|
|
2005-10-18 18:04:43 +01:00
|
|
|
prolog:between(I,_,I).
|
|
|
|
prolog:between(I0,I,J) :- I0 < I,
|
2004-09-07 18:10:43 +01:00
|
|
|
I1 is I0+1,
|
2005-10-18 18:04:43 +01:00
|
|
|
prolog:between(I1,I,J).
|
2004-09-07 18:10:43 +01:00
|
|
|
|
2005-10-18 18:04:43 +01:00
|
|
|
% SWI has a dynamic attribute scheme
|
|
|
|
|
|
|
|
prolog:get_attr(Var, Mod, Att) :-
|
|
|
|
AttTerm =.. [Mod,_,Att],
|
|
|
|
attributes:get_module_atts(Var, AttTerm).
|
|
|
|
|
|
|
|
prolog:put_attr(Var, Mod, Att) :-
|
|
|
|
AttTerm =.. [Mod,_,Att],
|
|
|
|
attributes:put_module_atts(Var, AttTerm).
|
|
|
|
|
|
|
|
prolog:del_attr(Var, Mod) :-
|
|
|
|
AttTerm =.. [Mod,_,_],
|
|
|
|
attributes:del_all_module_atts(Var, AttTerm).
|
|
|
|
|
2005-10-28 18:38:50 +01:00
|
|
|
prolog:get_attrs(AttVar, SWIAtts) :-
|
2005-10-18 18:04:43 +01:00
|
|
|
get_all_swi_atts(AttVar,SWIAtts).
|
|
|
|
|
|
|
|
prolog:put_attrs(_, []).
|
2005-10-31 18:12:51 +00:00
|
|
|
prolog:put_attrs(V, Atts) :-
|
|
|
|
cvt_to_swi_atts(Atts, YapAtts),
|
|
|
|
attributes:put_att_term(V, YapAtts).
|
|
|
|
|
|
|
|
cvt_to_swi_atts([], _).
|
|
|
|
cvt_to_swi_atts(att(Mod,Attribute,Atts), ModAttribute) :-
|
|
|
|
ModAttribute =.. [Mod, YapAtts, Attribute],
|
|
|
|
cvt_to_swi_atts(Atts, YapAtts).
|
2005-10-18 18:04:43 +01:00
|
|
|
|
|
|
|
bindings_message(V) -->
|
|
|
|
{ cvt_bindings(V, Bindings) },
|
2006-12-13 16:10:26 +00:00
|
|
|
prolog:message(query(_YesNo,Bindings)), !.
|
2005-10-18 18:04:43 +01:00
|
|
|
|
|
|
|
cvt_bindings([],[]).
|
|
|
|
cvt_bindings([[Name|Value]|L],[AName=Value|Bindings]) :-
|
|
|
|
atom_codes(AName, Name),
|
|
|
|
cvt_bindings(L,Bindings).
|
|
|
|
|
|
|
|
'$messages':prolog_message(_,L,L).
|
|
|
|
|
|
|
|
prolog:append([],L,L).
|
|
|
|
prolog:append([X|L0],L,[X|Lf]) :-
|
|
|
|
prolog:append(L0,L,Lf).
|
|
|
|
|
2005-10-28 18:38:50 +01:00
|
|
|
prolog:member(X,[X|_]).
|
2005-10-21 17:09:03 +01:00
|
|
|
prolog:member(X,[_|L0]) :-
|
|
|
|
prolog:member(X,L0).
|
|
|
|
|
2006-05-24 03:35:39 +01:00
|
|
|
prolog:select(Element, [Element|Rest], Rest).
|
|
|
|
prolog:select(Element, [Head|Tail], [Head|Rest]) :-
|
|
|
|
prolog:select(Element, Tail, Rest).
|
|
|
|
|
2005-10-18 18:04:43 +01:00
|
|
|
tv(Term,List) :- term_variables(Term,List).
|
|
|
|
|
|
|
|
prolog:term_variables(Term,List) :- tv(Term,List).
|
|
|
|
|
|
|
|
tv(Term,List,Tail) :- term_variables(Term,List,Tail).
|
|
|
|
|
|
|
|
prolog:term_variables(Term,List,Tail) :- tv(Term,List,Tail).
|
|
|
|
|
2005-10-19 11:12:50 +01:00
|
|
|
prolog:working_directory(OCWD,NCWD) :-
|
|
|
|
getcwd(OCWD),
|
|
|
|
(var(NCWD) -> true ; cd(NCWD)).
|
|
|
|
|
|
|
|
prolog:chdir(X) :- cd(X).
|
|
|
|
|
2005-10-21 17:09:03 +01:00
|
|
|
% Time is given as int, not as float.
|
|
|
|
prolog:get_time(Secs) :- datime(Datime), mktime(Datime, Secs).
|
|
|
|
|
|
|
|
% Time is received as int, and converted to "..."
|
|
|
|
prolog:convert_time(X,Y) :- swi:ctime(X,Y).
|
|
|
|
|
2005-10-28 18:38:50 +01:00
|
|
|
:- hide(atom_concat).
|
|
|
|
|
|
|
|
prolog:atom_concat(A,B) :- atomic_concat(A,B).
|
|
|
|
|
|
|
|
prolog:atom_concat(A,B,C) :- atomic_concat(A,B,C).
|
|
|
|
|
|
|
|
:- hide(create_mutable).
|
|
|
|
|
|
|
|
:- hide(get_mutable).
|
|
|
|
|
|
|
|
:- hide(update_mutable).
|
|
|
|
|
|
|
|
prolog:hash_term(X,Y) :- term_hash(X,Y).
|
|
|
|
|
|
|
|
:- meta_predicate prolog:maplist(:,?), prolog:maplist(:,?,?), prolog:maplist(:,?,?).
|
|
|
|
|
|
|
|
|
|
|
|
prolog:maplist(_, []).
|
|
|
|
prolog:maplist(G, [H|L]) :-
|
|
|
|
call(G,H),
|
|
|
|
prolog:maplist(G, L).
|
|
|
|
|
|
|
|
prolog:maplist(_, [], []).
|
|
|
|
prolog:maplist(G, [H1|L1], [H2|L2]) :-
|
|
|
|
call(G,H1,H2),
|
|
|
|
prolog:maplist(G, L1, L2).
|
|
|
|
|
|
|
|
prolog:maplist(_, [], [], []).
|
|
|
|
prolog:maplist(G, [H1|L1], [H2|L2], [H3|L3]) :-
|
|
|
|
call(G,H1,H2,H3),
|
|
|
|
prolog:maplist(G, L1, L2, L3).
|
|
|
|
|
|
|
|
prolog:make.
|
|
|
|
|
|
|
|
prolog:source_location(File,Line) :-
|
|
|
|
prolog_load_context(file, File),
|
|
|
|
prolog_load_context(term_position, '$stream_position'(_,Line,_)).
|
|
|
|
|
|
|
|
prolog:memberchk(Element, [Element|_]) :- !.
|
|
|
|
prolog:memberchk(Element, [_|Rest]) :-
|
|
|
|
prolog:memberchk(Element, Rest).
|
2005-10-21 17:09:03 +01:00
|
|
|
|
2006-05-24 03:35:39 +01:00
|
|
|
% copied from SWI lists library.
|
|
|
|
prolog:intersection([], _, []) :- !.
|
|
|
|
prolog:intersection([X|T], L, Intersect) :-
|
|
|
|
memberchk(X, L), !,
|
|
|
|
Intersect = [X|R],
|
|
|
|
prolog:intersection(T, L, R).
|
|
|
|
prolog:intersection([_|T], L, R) :-
|
|
|
|
prolog:intersection(T, L, R).
|
2005-10-21 17:09:03 +01:00
|
|
|
|
|
|
|
|
2007-06-02 16:37:50 +01:00
|
|
|
:- op(700, xfx, '=@=').
|
|
|
|
|
|
|
|
prolog:(Term1 =@= Term2) :-
|
|
|
|
variant(Term1, Term2), !.
|
2007-10-10 10:44:28 +01:00
|
|
|
|
|
|
|
%% flatten(+List1, ?List2) is det.
|
|
|
|
%
|
|
|
|
% Is true it List2 is a non nested version of List1.
|
|
|
|
%
|
|
|
|
% @deprecated Ending up needing flatten/3 often indicates,
|
|
|
|
% like append/3 for appending two lists, a bad
|
|
|
|
% design. Efficient code that generates lists
|
|
|
|
% from generated small lists must use difference
|
|
|
|
% lists, often possible through grammar rules for
|
|
|
|
% optimal readability.
|
|
|
|
|
|
|
|
prolog:flatten(List, FlatList) :-
|
|
|
|
flatten(List, [], FlatList0), !,
|
|
|
|
FlatList = FlatList0.
|
|
|
|
|
|
|
|
flatten(Var, Tl, [Var|Tl]) :-
|
|
|
|
var(Var), !.
|
|
|
|
flatten([], Tl, Tl) :- !.
|
|
|
|
flatten([Hd|Tl], Tail, List) :- !,
|
|
|
|
flatten(Hd, FlatHeadTail, List),
|
|
|
|
flatten(Tl, Tail, FlatHeadTail).
|
|
|
|
flatten(NonList, Tl, [NonList|Tl]).
|
|
|
|
|