|
From: Vitor S. C. <vs...@us...> - 2008-07-22 23:34:44
|
Update of /cvsroot/yap/library In directory sc8-pr-cvs10.sourceforge.net:/tmp/cvs-serv25978/library Modified Files: lists.yap swi.yap Log Message: SWI and module fixes Index: lists.yap =================================================================== RCS file: /cvsroot/yap/library/lists.yap,v retrieving revision 1.17 retrieving revision 1.18 diff -u -r1.17 -r1.18 --- lists.yap 11 Jul 2008 17:02:09 -0000 1.17 +++ lists.yap 22 Jul 2008 23:34:49 -0000 1.18 @@ -17,12 +17,15 @@ nth/4, nth0/3, nth0/4, + nth1/3, + nth1/4, permutation/2, prefix/2, remove_duplicates/2, reverse/2, same_length/2, select/3, + selectchk/3, sublist/2, substitute/4, sum_list/2, @@ -31,9 +34,12 @@ list_concat/2, flatten/2, max_list/2, - min_list/2 + min_list/2, + numlist/3 ]). +:- ensure_loaded(library(error)). + % append(Prefix, Suffix, Combined) % is true when all three arguments are lists, and the members of Combined @@ -125,6 +131,14 @@ find_nth0(M, Tail, Elem). +nth1(V, In, Element) :- var(V), !, + generate_nth(1, V, In, Element). +nth1(1, [Head|_], Head) :- !. +nth1(N, [_|Tail], Elem) :- + nonvar(N), !, + M is N-1, % should be succ(M, N) + find_nth(M, Tail, Elem). + nth(V, In, Element) :- var(V), !, generate_nth(1, V, In, Element). nth(1, [Head|_], Head) :- !. @@ -168,6 +182,13 @@ +nth1(V, In, Element, Tail) :- var(V), !, + generate_nth(1, V, In, Element, Tail). +nth1(1, [Head|Tail], Head, Tail) :- !. +nth1(N, [Head|Tail], Elem, [Head|Rest]) :- + M is N-1, + nth1(M, Tail, Elem, Rest). + nth(V, In, Element, Tail) :- var(V), !, generate_nth(1, V, In, Element, Tail). nth(1, [Head|Tail], Head, Tail) :- !. @@ -243,6 +264,15 @@ same_length([_|List1], [_|List2]) :- same_length(List1, List2). +%% selectchk(+Elem, +List, -Rest) is semidet. +% +% Semi-deterministic removal of first element in List that unifies +% Elem. + +selectchk(Elem, List, Rest) :- + select(Elem, List, Rest0), !, + Rest = Rest0. + % select(?Element, ?Set, ?Residue) % is true when Set is a list, Element occurs in Set, and Residue is @@ -315,6 +345,7 @@ % flatten(X,Y) :- flatten_list(X,Y,[]). +flatten_list(V) --> {var(V)}, !. flatten_list([]) --> !. flatten_list([H|T]) --> !, flatten_list(H),flatten_list(T). flatten_list(H) --> [H]. @@ -345,3 +376,22 @@ min_list(L, Max0, Max) ). +%% numlist(+Low, +High, -List) is semidet. +% +% List is a list [Low, Low+1, ... High]. Fails if High < Low.% +% +% @error type_error(integer, Low) +% @error type_error(integer, High) + +numlist(L, U, Ns) :- + must_be(integer, L), + must_be(integer, U), + L =< U, + numlist_(L, U, Ns). + +numlist_(U, U, [U]) :- !. +numlist_(L, U, [L|Ns]) :- + succ(L, L2), + numlist_(L2, U, Ns). + + Index: swi.yap =================================================================== RCS file: /cvsroot/yap/library/swi.yap,v retrieving revision 1.28 retrieving revision 1.29 diff -u -r1.28 -r1.29 --- swi.yap 16 Jul 2008 10:45:47 -0000 1.28 +++ swi.yap 22 Jul 2008 23:34:50 -0000 1.29 @@ -19,9 +19,9 @@ append/3, delete/3, member/2, - memberchk/2, min_list/2, - nth/3]). + nth1/3, + nth0/3]). :- use_module(library(system), [datime/1, @@ -51,34 +51,40 @@ ; true. -:- use_module(library(maplist)). - :- multifile swi_predicate_table/4. -swi_predicate_table(_,maplist(X,Y),maplist,maplist(X,Y)). -swi_predicate_table(_,maplist(X,Y,Z),maplist,maplist(X,Y,Z)). -swi_predicate_table(_,maplist(X,Y,Z,W),maplist,maplist(X,Y,Z,W)). swi_predicate_table(_,append(X,Y),lists,append(X,Y)). swi_predicate_table(_,append(X,Y,Z),lists,append(X,Y,Z)). swi_predicate_table(_,member(X,Y),lists,member(X,Y)). swi_predicate_table(_,nextto(X,Y,Z),lists,nextto(X,Y,Z)). -swi_predicate_table(_,is_list(X),lists,is_list(X)). -swi_predicate_table(_,min_list(X,Y),lists,min_list(X,Y)). -swi_predicate_table(_,nth(X,Y,Z),lists,nth(X,Y,Z)). swi_predicate_table(_,delete(X,Y,Z),lists,delete(X,Y,Z)). -swi_predicate_table(_,nth1(X,Y,Z),lists,nth(X,Y,Z)). -swi_predicate_table(_,memberchk(X,Y),lists,memberchk(X,Y)). -swi_predicate_table(_,flatten(X,Y),lists,flatten(X,Y)). swi_predicate_table(_,select(X,Y,Z),lists,select(X,Y,Z)). +swi_predicate_table(_,selectchk(X,Y,Z),lists,selectchk(X,Y,Z)). +swi_predicate_table(_,nth0(X,Y,Z),lists,nth0(X,Y,Z)). +swi_predicate_table(_,nth1(X,Y,Z),lists,nth1(X,Y,Z)). +swi_predicate_table(_,last(X,Y),lists,last(X,Y)). +swi_predicate_table(_,reverse(X,Y),lists,reverse(X,Y)). +swi_predicate_table(_,permutation(X,Y),lists,permutation(X,Y)). +swi_predicate_table(_,flatten(X,Y),lists,flatten(X,Y)). +swi_predicate_table(_,sumlist(X,Y),lists,sumlist(X,Y)). +swi_predicate_table(_,min_list(X,Y),lists,min_list(X,Y)). +swi_predicate_table(_,max_list(X,Y),lists,max_list(X,Y)). +swi_predicate_table(_,memberchk(X,Y),lists,memberchk(X,Y)). swi_predicate_table(_,hash_term(X,Y),terms,term_hash(X,Y)). swi_predicate_table(_,term_hash(X,Y),terms,term_hash(X,Y)). -swi_predicate_table(_,term_variables(X,Y),terms,term_variables(X,Y)). -swi_predicate_table(_,term_variables(X,Y,Z),terms,term_variables(X,Y,Z)). swi_predicate_table(_,subsumes(X,Y),terms,subsumes(X,Y)). swi_predicate_table(_,unifiable(X,Y,Z),terms,unifiable(X,Y,Z)). swi_predicate_table(_,genarg(X,Y,Z),arg,genarg(X,Y,Z)). swi_predicate_table(_,tmp_file(X,Y),system,tmp_file(X,Y)). +swi_isl(X) :- lists:is_list(X). + +prolog:is_list(X) :- swi_isl(X). + +swi_mchk(X,Y) :- lists:memberchk(X,Y). + +prolog:memberchk(X,Y) :- swi_mchk(X,Y). + :- dynamic prolog:message/3. @@ -100,6 +106,15 @@ :- meta_predicate prolog:predsort(:,+,-). +switv(X,Y) :- term_variables(X, Y). +switv(X,Y,Z) :- term_variables(X, Y, Z). + +prolog:term_variables(X, Y) :- + switv(X, Y). + +prolog:term_variables(X, Y, Z) :- + switv(X, Y, Z). + prolog:plus(X, Y, Z) :- integer(X), integer(Y), !, @@ -318,3 +333,92 @@ prolog:(Term1 =@= Term2) :- variant(Term1, Term2), !. +% copied from SWI's boot/apply library +:- module_transparent + prolog:maplist/2, + maplist2/2, + prolog:maplist/3, + maplist2/3, + prolog:maplist/4, + maplist2/4, + prolog:maplist/5, + maplist2/5. + +% maplist(:Goal, +List) +% +% True if Goal can succesfully be applied on all elements of List. +% Arguments are reordered to gain performance as well as to make +% the predicate deterministic under normal circumstances. + +prolog:maplist(Goal, List) :- + maplist2(List, Goal). + +maplist2([], _). +maplist2([Elem|Tail], Goal) :- + call(Goal, Elem), + maplist2(Tail, Goal). + +% maplist(:Goal, ?List1, ?List2) +% +% True if Goal can succesfully be applied to all succesive pairs +% of elements of List1 and List2. + +prolog:maplist(Goal, List1, List2) :- + maplist2(List1, List2, Goal). + +maplist2([], [], _). +maplist2([Elem1|Tail1], [Elem2|Tail2], Goal) :- + call(Goal, Elem1, Elem2), + maplist2(Tail1, Tail2, Goal). + +% maplist(:Goal, ?List1, ?List2, ?List3) +% +% True if Goal can succesfully be applied to all succesive triples +% of elements of List1..List3. + +prolog:maplist(Goal, List1, List2, List3) :- + maplist2(List1, List2, List3, Goal). + +maplist2([], [], [], _). +maplist2([Elem1|Tail1], [Elem2|Tail2], [Elem3|Tail3], Goal) :- + call(Goal, Elem1, Elem2, Elem3), + maplist2(Tail1, Tail2, Tail3, Goal). + +% maplist(:Goal, ?List1, ?List2, ?List3, List4) +% +% True if Goal can succesfully be applied to all succesive +% quadruples of elements of List1..List4 + +prolog:maplist(Goal, List1, List2, List3, List4) :- + maplist2(List1, List2, List3, List4, Goal). + +maplist2([], [], [], [], _). +maplist2([Elem1|Tail1], [Elem2|Tail2], [Elem3|Tail3], [Elem4|Tail4], Goal) :- + call(Goal, Elem1, Elem2, Elem3, Elem4), + maplist2(Tail1, Tail2, Tail3, Tail4, Goal). + +prolog:compile_aux_clauses([]). +prolog:compile_aux_clauses([(:- G)|Cls]) :- + prolog_load_context(module, M), + once(M:G), + prolog:compile_aux_clauses(Cls). +prolog:compile_aux_clauses([Cl|Cls]) :- + prolog_load_context(module, M), + assert_static(M:Cl), + prolog:compile_aux_clauses(Cls). + +% +% convert from SWI's goal expansion to YAP/SICStus old style goal +% expansion. +% +user:term_expansion(goal_expansion(A,B),O) :- + prolog_load_context(module, user), !, + O = goal_expansion(A,user,B). +user:term_expansion(user:goal_expansion(A,B),O) :- !, + O = user:goal_expansion(A,_,B). +user:term_expansion((goal_expansion(A,B) :- G), O) :- + prolog_load_context(module, user), !, + O = (goal_expansion(A,user,B) :- G). +user:term_expansion((user:goal_expansion(A,B) :- G),O) :- + O = (user:goal_expansion(A,_,B) :- G). + |