1 % (c) 2009-2026 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 :- module(tools_lists,
6 [count_occurences/2,
7 ord_member_nonvar_chk/2,
8 ord_select_nonvar_chk/3,
9 ord_update_nonvar/4,
10 delete_first/3,
11 length_greater/2,
12 length_less/2,
13 member_nonvar_list/2,
14 exclude_count/4,
15 include_maplist/3,
16 optimized_nth1/3,
17 remove_dups_keep_order/2,
18 is_list_simple/1,
19 get_member_option/3,
20 convlist_max/4,
21 common_prefix_atom/2,
22 take_at_most/3
23 ]).
24
25 :- meta_predicate exclude_count(1,-,-,-).
26 :- meta_predicate exclude_count_aux(-,1,-,-,-).
27 :- meta_predicate convlist_max(2,-,-,-).
28 :- meta_predicate conv_aux(-,2,-,-).
29 :- meta_predicate include_maplist(2,-,-).
30 :- meta_predicate include_maplist3(-,2,-).
31
32 :- use_module(module_information).
33
34 :- module_info(group,infrastructure).
35 :- module_info(description,'A few utilities on lists seperated out from tools.pl to avoid cyclic module dependencies.').
36
37 :- use_module(library(avl), [avl_fetch/2, avl_fetch/3, avl_to_list/2, avl_store/4, empty_avl/1]).
38 :- use_module(library(lists), [nth1/3]).
39 :- use_module(library(samsort), [samsort/2]).
40
41 % self-check in tools to avoid dependency on self_check module
42 %:- assert_must_succeed((tools:count_occurences([a,b,a,a,b],R),R == [a-3,b-2])).
43
44 % count number of occurences inside a list
45 count_occurences(L,R) :- samsort(L,SL), count1(SL,R).
46
47 count1([],[]).
48 count1([H|T],R) :- count2(T,H,1,R).
49
50 count2([],H,Cnt,[H-Cnt]).
51 count2([H|T],Acc,Cnt,Res) :-
52 (H==Acc -> C1 is Cnt+1, count2(T,Acc,C1,Res)
53 ; Res = [Acc-Cnt|RT],
54 count2(T,H,1,RT)).
55
56 % like ord_member but also allows nonvar lookup terms
57 % note that this test fails for ord_member:
58 %:- assert_must_succeed((tools_lists:ord_member_nonvar_chk(p(b,X),[p(a,c),p(b,d)]),R == d)).
59 ord_member_nonvar_chk(X, [H|T]) :-
60 (X=H -> true
61 ; X @>H -> ord_member_nonvar_chk(X,T)).
62
63 :- use_module(library(lists), [select/3]).
64
65 % in contrast to delete/3 we delete only first occurence
66 ?delete_first(List,X,NewList) :- select(X,List,Del),!,NewList=Del.
67 delete_first(L,_,L).
68
69
70
71 %:- assert_must_succeed((tools_lists:ord_select_nonvar_chk(b,[a,b,c,d],R),R == [a,c,d])).
72 ord_select_nonvar_chk(X, [H|T], Res) :-
73 (X=H -> Res=T
74 ; X @>H -> Res=[H|TR], ord_select_nonvar_chk(X,T,TR)).
75
76 %:- assert_must_succeed((tools_lists:ord_update_nonvar(b(_),[a(x),b(x),d(x)],b(y),R),R == [a(x),b(y),d(x))).
77 %:- assert_must_succeed((tools_lists:ord_update_nonvar(b(_),[a(x),d(x)],b(y),R),R == [a(x),b(y),d(x))).
78 ord_update_nonvar(_, [], Y, Res) :- !, Res = [Y].
79 ord_update_nonvar(X, [H|T], Y, Res) :-
80 (X=H -> Res=[Y|T]
81 ; X @>H -> Res=[H|TR], ord_update_nonvar(X,T, Y, TR)
82 ; Res = [Y,H|T]).
83
84
85 % length_greater([1,2,3],1)
86 length_greater(_,X) :- X<0,!.
87 length_greater(L,X) :- nonvar(L), L=[_|T], % do not extend a partial list
88 X1 is X-1, length_greater(T,X1).
89
90
91 % length_less([1,2,3],4)
92 length_less([],Nr) :- Nr>0.
93 length_less([_|T],X) :- X>1, X1 is X-1, length_less(T,X1).
94
95
96 % a version of member that stops at a variable list tail
97 member_nonvar_list(X,List) :- nonvar(List), List=[H|T],
98 (X=H ; member_nonvar_list(X,T)).
99
100 % a version of exclude with counts the number of excluded items
101 exclude_count(Pred,List,ResList,Excluded) :-
102 exclude_count_aux(List,Pred,ResList,0,Excluded).
103 exclude_count_aux([],_,[],Acc,Acc).
104 exclude_count_aux([H|T],Pred,Res,Acc,Excluded) :-
105 (call(Pred,H)
106 -> Res = RT, Acc1 is Acc+1
107 ; Res = [H|RT], Acc1 = Acc
108 ), exclude_count_aux(T,Pred,RT,Acc1,Excluded).
109
110 % a combination of include and maplist
111 include_maplist(Pred,List,ResList) :- include_maplist3(List,Pred,ResList).
112
113 include_maplist3([],_,[]).
114 include_maplist3([H|T],Pred,Res) :-
115 (call(Pred,H,MH)
116 -> Res=[MH|MT],
117 include_maplist3(T,Pred,MT)
118 ; include_maplist3(T,Pred,Res)).
119
120 % a version of nth1 which has no choice point for singleton lists
121 optimized_nth1(Nr,[Single|T],Res) :- T==[], !, Nr=1,Res=Single. % avoid a pending choice point on the stack
122 ?optimized_nth1(Nr,Whens,Res) :- nth1(Nr,Whens,Res).
123
124
125 % remove_dups version which keeps order of original elements
126 remove_dups_keep_order([],[]).
127 remove_dups_keep_order([H|T],[H|Res]) :- empty_avl(E), avl_store(H,E,true,A1),
128 rem_dups(T,A1,Res).
129
130 rem_dups([],_,[]).
131 rem_dups([H|T],AVL,Res) :-
132 (avl_fetch(H,AVL) -> rem_dups(T,AVL,Res)
133 ; Res=[H|TRes],
134 avl_store(H,AVL,true,AVL1),
135 rem_dups(T,AVL1,TRes)).
136
137 /* Checks if the argument is a list, but unlike is_list/1 it just
138 checks the head and does not iterate through the list */
139 is_list_simple([]).
140 is_list_simple([_|_]).
141
142
143 % useful to extract an equality from an unsorted list of options:
144 get_member_option(Opt,List,Value) :- member(Equality,List), binding(Equality,Opt,Value).
145 binding('='(Opt,Val),Opt,Val).
146 binding('/'(Opt,Val),Opt,Val).
147
148
149
150 % a version of convlist that returns at most Max solutions
151 convlist_max(Pred,Max,List,Res) :-
152 (Max>0 -> conv_aux(List,Pred,Max,Res) ; Res=[]).
153 conv_aux([],_,_,[]).
154 conv_aux([H|T],Pred,Max,Res) :-
155 (call(Pred,H,HX)
156 -> Res=[HX|TX],
157 (Max>1 -> M1 is Max-1, conv_aux(T,Pred,M1,TX)
158 ; TX=[])
159 ; conv_aux(T,Pred,Max,Res)).
160
161
162 % common prefix of a list of atoms
163 % common_prefix_atom([ab,ac],R) -> R=a
164 common_prefix_atom([H],Res) :- !, Res=[H]. % single completion
165 common_prefix_atom([H|T],Res) :- atom_codes(H,Hs), com_all(T,Hs,ResC), atom_codes(Res,ResC).
166
167 com_all([],H,H).
168 com_all([H|T],PrevPrefix,Res) :- atom_codes(H,Hs), common_prefix(Hs,PrevPrefix,NewPrefix),
169 com_all(T,NewPrefix,Res).
170
171 common_prefix([H|T1],[H|T2],[H|Res]) :- !, common_prefix(T1,T2,Res).
172 common_prefix(_,_,[]).
173
174
175 % the first N elements of a list, or the whole list if it has at most N elements
176 % take_at_most(2,[a,b,c],R) -> R=[a,b]
177 take_at_most(N,L,Res) :- N>0, nonvar(L), L=[H|T], !, % do not extend a partial list
178 Res=[H|R], N1 is N-1, take_at_most(N1,T,R).
179 take_at_most(_,_,[]).