35
36:- module(apply,
37 [ include/3, 38 exclude/3, 39 partition/4, 40 partition/5, 41 maplist/2, 42 maplist/3, 43 maplist/4, 44 maplist/5, 45 foldl/4, 46 foldl/5, 47 foldl/6, 48 foldl/7, 49 50 scanl/4, 51 scanl/5, 52 scanl/6, 53 scanl/7 54 55 ]).
56:- use_module(library(error)).
57
68
69:- meta_predicate
70 include(1, +, -),
71 exclude(1, +, -),
72 partition(1, +, -, -),
73 partition(2, +, -, -, -),
74 maplist(1, ?),
75 maplist(2, ?, ?),
76 maplist(3, ?, ?, ?),
77 maplist(4, ?, ?, ?, ?),
78 foldl(3, +, +, -),
79 foldl(4, +, +, +, -),
80 foldl(5, +, +, +, +, -),
81 foldl(6, +, +, +, +, +, -),
82 scanl(3, +, +, -),
83 scanl(4, +, +, +, -),
84 scanl(5, +, +, +, +, -),
85 scanl(6, +, +, +, +, +, -).
86
87
95
96include(Goal, List, Included) :-
97 include_(List, Goal, Included).
98
99include_([], _, []).
100include_([X1|Xs1], P, Included) :-
101 ( call(P, X1)
102 -> Included = [X1|Included1]
103 ; Included = Included1
104 ),
105 include_(Xs1, P, Included1).
106
107
112
113exclude(Goal, List, Included) :-
114 exclude_(List, Goal, Included).
115
116exclude_([], _, []).
117exclude_([X1|Xs1], P, Included) :-
118 ( call(P, X1)
119 -> Included = Included1
120 ; Included = [X1|Included1]
121 ),
122 exclude_(Xs1, P, Included1).
123
124
130
131partition(Pred, List, Included, Excluded) :-
132 partition_(List, Pred, Included, Excluded).
133
134partition_([], _, [], []).
135partition_([H|T], Pred, Incl, Excl) :-
136 ( call(Pred, H)
137 -> Incl = [H|I],
138 partition_(T, Pred, I, Excl)
139 ; Excl = [H|E],
140 partition_(T, Pred, Incl, E)
141 ).
142
143
150
151partition(Pred, List, Less, Equal, Greater) :-
152 partition_(List, Pred, Less, Equal, Greater).
153
154partition_([], _, [], [], []).
155partition_([H|T], Pred, L, E, G) :-
156 call(Pred, H, Diff),
157 partition_(Diff, H, Pred, T, L, E, G).
158
159partition_(<, H, Pred, T, [H|L], E, G) :-
160 !,
161 partition_(T, Pred, L, E, G).
162partition_(=, H, Pred, T, L, [H|E], G) :-
163 !,
164 partition_(T, Pred, L, E, G).
165partition_(>, H, Pred, T, L, E, [H|G]) :-
166 !,
167 partition_(T, Pred, L, E, G).
168partition_(Diff, _, _, _, _, _, _) :-
169 must_be(oneof([<.=,>]), Diff).
170
171
172 175
181
182maplist(Goal, List) :-
183 maplist_(List, Goal).
184
185maplist_([], _).
186maplist_([Elem|Tail], Goal) :-
187 call(Goal, Elem),
188 maplist_(Tail, Goal).
189
193
194maplist(Goal, List1, List2) :-
195 maplist_(List1, List2, Goal).
196
197maplist_([], [], _).
198maplist_([Elem1|Tail1], [Elem2|Tail2], Goal) :-
199 call(Goal, Elem1, Elem2),
200 maplist_(Tail1, Tail2, Goal).
201
205
206maplist(Goal, List1, List2, List3) :-
207 maplist_(List1, List2, List3, Goal).
208
209maplist_([], [], [], _).
210maplist_([Elem1|Tail1], [Elem2|Tail2], [Elem3|Tail3], Goal) :-
211 call(Goal, Elem1, Elem2, Elem3),
212 maplist_(Tail1, Tail2, Tail3, Goal).
213
214
219
220maplist(Goal, List1, List2, List3, List4) :-
221 maplist_(List1, List2, List3, List4, Goal).
222
223maplist_([], [], [], [], _).
224maplist_([Elem1|Tail1], [Elem2|Tail2], [Elem3|Tail3], [Elem4|Tail4], Goal) :-
225 call(Goal, Elem1, Elem2, Elem3, Elem4),
226 maplist_(Tail1, Tail2, Tail3, Tail4, Goal).
227
228
229 232
247
248foldl(Goal, List, V0, V) :-
249 foldl_(List, Goal, V0, V).
250
251foldl_([], _, V, V).
252foldl_([H|T], Goal, V0, V) :-
253 call(Goal, H, V0, V1),
254 foldl_(T, Goal, V1, V).
255
256
257foldl(Goal, List1, List2, V0, V) :-
258 foldl_(List1, List2, Goal, V0, V).
259
260foldl_([], [], _, V, V).
261foldl_([H1|T1], [H2|T2], Goal, V0, V) :-
262 call(Goal, H1, H2, V0, V1),
263 foldl_(T1, T2, Goal, V1, V).
264
265
266foldl(Goal, List1, List2, List3, V0, V) :-
267 foldl_(List1, List2, List3, Goal, V0, V).
268
269foldl_([], [], [], _, V, V).
270foldl_([H1|T1], [H2|T2], [H3|T3], Goal, V0, V) :-
271 call(Goal, H1, H2, H3, V0, V1),
272 foldl_(T1, T2, T3, Goal, V1, V).
273
274
275foldl(Goal, List1, List2, List3, List4, V0, V) :-
276 foldl_(List1, List2, List3, List4, Goal, V0, V).
277
278foldl_([], [], [], [], _, V, V).
279foldl_([H1|T1], [H2|T2], [H3|T3], [H4|T4], Goal, V0, V) :-
280 call(Goal, H1, H2, H3, H4, V0, V1),
281 foldl_(T1, T2, T3, T4, Goal, V1, V).
282
283
284 287
303
304scanl(Goal, List, V0, [V0|Values]) :-
305 scanl_(List, Goal, V0, Values).
306
307scanl_([], _, _, []).
308scanl_([H|T], Goal, V, [VH|VT]) :-
309 call(Goal, H, V, VH),
310 scanl_(T, Goal, VH, VT).
311
312
313scanl(Goal, List1, List2, V0, [V0|Values]) :-
314 scanl_(List1, List2, Goal, V0, Values).
315
316scanl_([], [], _, _, []).
317scanl_([H1|T1], [H2|T2], Goal, V, [VH|VT]) :-
318 call(Goal, H1, H2, V, VH),
319 scanl_(T1, T2, Goal, VH, VT).
320
321
322scanl(Goal, List1, List2, List3, V0, [V0|Values]) :-
323 scanl_(List1, List2, List3, Goal, V0, Values).
324
325scanl_([], [], [], _, _, []).
326scanl_([H1|T1], [H2|T2], [H3|T3], Goal, V, [VH|VT]) :-
327 call(Goal, H1, H2, H3, V, VH),
328 scanl_(T1, T2, T3, Goal, VH, VT).
329
330
331scanl(Goal, List1, List2, List3, List4, V0, [V0|Values]) :-
332 scanl_(List1, List2, List3, List4, Goal, V0, Values).
333
334scanl_([], [], [], [], _, _, []).
335scanl_([H1|T1], [H2|T2], [H3|T3], [H4|T4], Goal, V, [VH|VT]) :-
336 call(Goal, H1, H2, H3, H4, V, VH),
337 scanl_(T1, T2, T3, T4, Goal, VH, VT).
338
339
340 343
344:- multifile
345 sandbox:safe_meta_predicate/1.
346
347safe_api(Name/Arity, sandbox:safe_meta_predicate(apply:Name/Arity)).
348
349term_expansion(safe_api, Clauses) :-
350 module_property(apply, exports(API)),
351 maplist(safe_api, API, Clauses).
352
353safe_api.