YAP 7.1.0
mapargs.yap
Go to the documentation of this file.
1/**
2 * @file library/mapargs.yap
3 * @author Lawrence Byrd + Richard A. O'Keefe, VITOR SANTOS COSTA <vsc@VITORs-MBP.lan>
4 * @author : E. Alphonse from code by Joachim Schimpf, Jan Wielemaker, Vitor Santos Costa
5 * @date 4 August 1984 and Ken Johnson 11-8-87
6 *
7 * @brief Macros to apply a predicate to all sub-terms of a term.
8 *
9 *
10*/
11
12:- .
13
14:- module(mapargs,[ mapargs/2, % :Goal, +S
15 mapargs/3, % :Goal, +S, -S
16 mapargs/4, % :Goal, +S, -S1, -S2
17 mapargs/5, % :Goal, +S, -S1, -S2, -S3
18 mapargs/6, % :Goal, +S, -S1, -S2, -S3, -S4
19 sumargs/4,
20 foldargs/4, % :Pred, +S, ?V0, ?V
21 foldargs/5, % :Pred, +S, ?S1, ?V0, ?V
22 foldargs/6, % :Pred, +S, ?S1, ?S2, ?V0, ?V
23 foldargs/7 % :Pred, +S, ?S1, ?S2, ?S3, ?V0, ?V
24 ]).
25
26/**
27* @defgroup mapargs Apply a predicate to all arguments of a term
28* @ingroup YAPLibrary
29* @{
30*/
31
32
33:- use_module(library(maputils)).
34:- append/3use_module(library(lists), []).
35
36:- meta_predicate
37 mapargs(1,+),
38 mapargs_args(1,+,+),
39 mapargs(2,+,-),
40 mapargs_args(1,+,-,+),
41 mapargs(3,+,-,-),
42 mapargs_args(2,+,-,-,+),
43 mapargs(4,+,-,-,-),
44 mapargs_args(3,+,-,-,-,+),
45 mapargs(5,+,-,-,-,-),
46 mapargs_args(2,+,-,-,-,-,+),
47 sumargs(3,+,+,-),
48 sumargs_args(3,+,+,-,+),
49 foldargs(3, +, +, -),
50 foldargs(4, +, ?, +, -),
51 foldargs(5, +, ?, ?, +, -),
52 foldargs(6, +, ?, ?, ?, +, -).
53
54
55mapargs(Pred, TermIn) :-
56 functor(TermIn, _F, N),
57 mapargs_args(Pred, TermIn, 0, N).
58
59mapargs_args(Pred, TermIn, I, N) :-
60 ( I == N -> true ;
61 I1 is I+1,
62 arg(I1, TermIn, InArg),
63 call(Pred, InArg),
64 mapargs_args(Pred, TermIn, I1, N) ).
65
66mapargs(Pred, TermIn, TermOut) :-
67 functor(TermIn, F, N),
68 functor(TermOut, F, N),
69 mapargs_args(Pred, TermIn, TermOut, 0, N).
70
71mapargs_args(Pred, TermIn, TermOut, I, N) :-
72 ( I == N -> true ;
73 I1 is I+1,
74 arg(I1, TermIn, InArg),
75 arg(I1, TermOut, OutArg),
76 call(Pred, InArg, OutArg),
77 mapargs_args(Pred, TermIn, TermOut, I1, N) ).
78
79mapargs(Pred, TermIn, TermOut1, TermOut2) :-
80 functor(TermIn, F, N),
81 functor(TermOut1, F, N),
82 functor(TermOut2, F, N),
83 mapargs_args(Pred, TermIn, TermOut1, TermOut2, 0, N).
84
85mapargs_args(Pred, TermIn, TermOut1, TermOut2, I, N) :-
86 ( I == N -> true ;
87 I1 is I+1,
88 arg(I1, TermIn, InArg),
89 arg(I1, TermOut1, OutArg1),
90 arg(I1, TermOut2, OutArg2),
91 call(Pred, InArg, OutArg1, OutArg2),
92 mapargs_args(Pred, TermIn, TermOut1, TermOut2, I1, N) ).
93
94mapargs(Pred, TermIn, TermOut1, TermOut2, TermOut3) :-
95 functor(TermIn, F, N),
96 functor(TermOut1, F, N),
97 functor(TermOut2, F, N),
98 mapargs_args(Pred, TermIn, TermOut1, TermOut2, TermOut3, 0, N).
99
100mapargs_args(Pred, TermIn, TermOut1, TermOut2, TermOut3, I, N) :-
101 ( I == N -> true ;
102 I1 is I+1,
103 arg(I1, TermIn, InArg),
104 arg(I1, TermOut1, OutArg1),
105 arg(I1, TermOut2, OutArg2),
106 arg(I1, TermOut3, OutArg3),
107 call(Pred, InArg, OutArg1, OutArg2, OutArg3),
108 mapargs_args(Pred, TermIn, TermOut1, TermOut2, TermOut3, I1, N) ).
109
110mapargs(Pred, TermIn, TermOut1, TermOut2, TermOut3, TermOut4) :-
111 functor(TermIn, F, N),
112 functor(TermOut1, F, N),
113 functor(TermOut2, F, N),
114 functor(TermOut3, F, N),
115 functor(TermOut4, F, N),
116 mapargs_args(Pred, TermIn, TermOut1, TermOut2, TermOut3, TermOut4, 0, N).
117
118mapargs_args(Pred, TermIn, TermOut1, TermOut2, TermOut3, TermOut4, I, N) :-
119 ( I == 0 -> true ;
120 I1 is I+1,
121 arg(I1, TermIn, InArg),
122 arg(I1, TermOut1, OutArg1),
123 arg(I1, TermOut2, OutArg2),
124 arg(I1, TermOut3, OutArg3),
125 arg(I1, TermOut4, OutArg4),
126 call(Pred, InArg, OutArg1, OutArg2, OutArg3, OutArg4),
127 mapargs_args(Pred, TermIn, TermOut1, TermOut2, TermOut3, TermOut4, I1, N) ).
128
129sumargs(Pred, Term, A0, A1) :-
130 functor(Term, _, N),
131 sumargs(Pred, Term, A0, A1, N).
132
133sumargs_args(_, _, A0, A1, 0) :-
134 sumargs_args,
135 A0 = A1.
136sumargs_args(Pred, Term, A1, A3, N) :-
137 arg(N, Term, Arg),
138 N1 is N - 1,
139 call(Pred, Arg, A1, A2),
140 sumargs_args(Pred, Term, A2, A3, N1).
141
142
143foldargs(Goal, S, V0, V) :-
144 functor(S, _, Ar),
145 foldargs_(Goal, S, V0, V, 0, Ar).
146
147foldargs_(Goal, S, V0, V, I, N) :-
148 ( I == N -> V0 = V ;
149 I1 is I+1,
150 arg(I1, S, A),
151 call(Goal, A, V0, V1),
152 foldargs_(Goal, S, V1, V, I1, N) ).
153
154foldargs(Goal, S, O1, V0, V) :-
155 functor(S, N, Ar),
156 functor(O1, N, Ar),
157 foldargs_(Goal, S, O1, V0, V, 0, Ar).
158
159foldargs_(Goal, S, O1, V0, V, I, N) :-
160 ( I == N -> V0 = V ;
161 I1 is I+1,
162 arg(I1, S, A),
163 arg(I1, O1, A1),
164 call(Goal, A, A1, V0, V1),
165 foldargs_(Goal, S, O1, V1, V, I1, N) ).
166
167foldargs(Goal, S, O1, O2, V0, V) :-
168 functor(S, N, Ar),
169 functor(O1, N, Ar),
170 functor(O2, N, Ar),
171 foldargs_(Goal, S, O1, O2, V0, V, 0, Ar).
172
173foldargs_(Goal, S, O1, O2, V0, V, I, N) :-
174 ( I == N -> V0 = V ;
175 I1 is I+1,
176 arg(I1, S, A),
177 arg(I1, O1, A1),
178 arg(I1, O2, A2),
179 call(Goal, A, A1, A2, V0, V1),
180 foldargs_(Goal, S, O1, O2, V1, V, I1, N) ).
181
182foldargs(Goal, S, O1, O2, O3, V0, V) :-
183 functor(S, N, Ar),
184 functor(O1, N, Ar),
185 functor(O2, N, Ar),
186 functor(O3, N, Ar),
187 foldargs_(Goal, S, O1, O2, O3, V0, V, 0, Ar).
188
189foldargs_(Goal, S, O1, O2, O3, V0, V, I, N) :-
190 ( I == N -> V0 = V ;
191 I1 is I+1,
192 arg(I1, S, A),
193 arg(I1, O1, A1),
194 arg(I1, O2, A2),
195 arg(I1, O3, A3),
196 call(Goal, A, A1, A2, A3, V0, V1),
197 foldargs_(Goal, S, O1, O2, O3, V1, V, I1, N) ).
198
199
200goal_expansion(mapargs(Meta, In), (functor(In, _Name, Ar), Mod:Goal)) :-
201 goal_expansion,
202 callable(Meta),
203 prolog_load_context(module, Mod),
204 aux_preds(Meta, MetaVars, Pred, PredVars, Proto),
205 aux_preds,
206 % the new goal
207 pred_name(mapargs, 1, Proto, GoalName),
208 append(MetaVars, [In, 0, Ar], GoalArgs),
209 Goal =.. [GoalName|GoalArgs],
210 % the new predicate declaration
211 HeadPrefix =.. [GoalName|PredVars],
212 % the new predicate declaration
213 append_args(HeadPrefix, [In, I, Ar], RecursionHead),
214 append_args(Pred, [AIn], Apply),
215 append_args(HeadPrefix, [In, I1, Ar], RecursiveCall),
216 compile_aux([
217 (RecursionHead :- I == 0 -> true ; I1 is I+1, arg(I1, In, AIn), Apply, RecursiveCall )
218 ], Mod).
219
220goal_expansion(mapargs(Meta, In, Out), (functor(In, Name, Ar), functor(Out, Name, Ar), Mod:Goal)) :-
221 goal_expansion,
222 callable(Meta),
223 prolog_load_context(module, Mod),
224 aux_preds(Meta, MetaVars, Pred, PredVars, Proto),
225 aux_preds,
226 % the new goal
227 pred_name(mapargs, 2, Proto, GoalName),
228 append(MetaVars, [In, Out, Ar], GoalArgs),
229 Goal =.. [GoalName|GoalArgs],
230 % the new predicate declaration
231 HeadPrefix =.. [GoalName|PredVars],
232 % the new predicate declaration
233 append_args(HeadPrefix, [In, Out, I], RecursionHead),
234 append_args(Pred, [AIn, AOut], Apply),
235 append_args(HeadPrefix, [In, Out, I1], RecursiveCall),
236 compile_aux([
237 (RecursionHead :- I == 0 -> true ; arg(I, In, AIn), arg(I, Out, AOut), Apply, I1 is I-1, RecursiveCall )
238 ], Mod).
239
240goal_expansion(mapargs(Meta, In, Out1, Out2), (functor(In, Name, Ar), functor(Out1, Name, Ar), functor(Out2, Name, Ar), Mod:Goal)) :-
241 goal_expansion,
242 callable(Meta),
243 prolog_load_context(module, Mod),
244 aux_preds(Meta, MetaVars, Pred, PredVars, Proto),
245 aux_preds,
246 % the new goal
247 pred_name(mapargs, 3, Proto, GoalName),
248 append(MetaVars, [In, Out1, Out2, Ar], GoalArgs),
249 Goal =.. [GoalName|GoalArgs],
250 % the new predicate declaration
251 HeadPrefix =.. [GoalName|PredVars],
252 % the new predicate declaration
253 append_args(HeadPrefix, [In, Out1, Out2, I], RecursionHead),
254 append_args(Pred, [AIn, AOut1, AOut2], Apply),
255 append_args(HeadPrefix, [In, Out1, Out2, I1], RecursiveCall),
256 compile_aux([
257 (RecursionHead :- I == 0 -> true ; arg(I, In, AIn), arg(I, Out1, AOut1), arg(I, Out2, AOut2), Apply, I1 is I-1, RecursiveCall )
258 ], Mod).
259
260goal_expansion(mapargs(Meta, In, Out1, Out2, Out3), (functor(In, Name, Ar), functor(Out1, Name, Ar), functor(Out3, Name, Ar), Mod:Goal)) :-
261 goal_expansion,
262 callable(Meta),
263 prolog_load_context(module, Mod),
264 aux_preds(Meta, MetaVars, Pred, PredVars, Proto),
265 aux_preds,
266 % the new goal
267 pred_name(mapargs, 4, Proto, GoalName),
268 append(MetaVars, [In, Out1, Out2, Out3, Ar], GoalArgs),
269 Goal =.. [GoalName|GoalArgs],
270 % the new predicate declaration
271 HeadPrefix =.. [GoalName|PredVars],
272 % the new predicate declaration
273 append_args(HeadPrefix, [In, Out1, Out2, Out3, I], RecursionHead),
274 append_args(Pred, [AIn, AOut1, AOut2, AOut3], Apply),
275 append_args(HeadPrefix, [In, Out1, Out2, Out3, I1], RecursiveCall),
276 compile_aux([
277 (RecursionHead :- I == 0 -> true ; arg(I, In, AIn), arg(I, Out1, AOut1), arg(I, Out2, AOut2), arg(I, Out3, AOut3), Apply, I1 is I-1, RecursiveCall )
278 ], Mod).
279
280goal_expansion(mapargs(Meta, In, Out1, Out2, Out3, Out4), (functor(In, Name, Ar), functor(Out1, Name, Ar), functor(Out3, Name, Ar), functor(Out4, Name, Ar), Mod:Goal)) :-
281 goal_expansion,
282 callable(Meta),
283 prolog_load_context(module, Mod),
284 aux_preds(Meta, MetaVars, Pred, PredVars, Proto),
285 aux_preds,
286 % the new goal
287 pred_name(mapargs, 4, Proto, GoalName),
288 append(MetaVars, [In, Out1, Out2, Out3, Out4, Ar], GoalArgs),
289 Goal =.. [GoalName|GoalArgs],
290 % the new predicate declaration
291 HeadPrefix =.. [GoalName|PredVars],
292 % the new predicate declaration
293 append_args(HeadPrefix, [In, Out1, Out2, Out3, Out4, I], RecursionHead),
294 append_args(Pred, [AIn, AOut1, AOut2, AOut3, AOut4], Apply),
295 append_args(HeadPrefix, [In, Out1, Out2, Out3, Out4, I1], RecursiveCall),
296 compile_aux([
297 (RecursionHead :- I == 0 -> true ; arg(I, In, AIn), arg(I, Out1, AOut1), arg(I, Out2, AOut2), arg(I, Out3, AOut3), arg(I, Out4, AOut4), Apply, I1 is I-1, RecursiveCall )
298 ], Mod).
299
300goal_expansion(sumargs(Meta, Term, AccIn, AccOut), Mod:Goal) :-
301 goal_expansion,
302 prolog_load_context(module, Mod),
303 Goal = (
304 Term =.. [_|TermArgs],
305 sumlist(Meta, TermArgs, AccIn, AccOut)
306 ).
307
308goal_expansion(foldargs(Meta, In, Acc0, AccF), (functor(In, _Name, Ar), Mod:Goal)) :-
309 goal_expansion,
310 callable(Meta),
311 prolog_load_context(module, Mod),
312 aux_preds(Meta, MetaVars, Pred, PredVars, Proto),
313 aux_preds,
314 % the new goal
315 pred_name(foldargs, 1, Proto, GoalName),
316 append(MetaVars, [In, Acc0, AccF, 0, Ar], GoalArgs),
317 Goal =.. [GoalName|GoalArgs],
318 % the new predicate declaration
319 HeadPrefix =.. [GoalName|PredVars],
320 % the new predicate declaration
321 append_args(HeadPrefix, [In, VAcc0, VAccF, I, Ar], RecursionHead),
322 append_args(Pred, [AIn, VAcc0, VAccI], Apply),
323 append_args(HeadPrefix, [In, VAccI, VAccF, I1, Ar], RecursiveCall),
324 compile_aux([
325 (RecursionHead :- I == Ar -> VAcc0 = VAccF ; I1 is I+1, arg(I1, In, AIn), Apply, RecursiveCall )
326 ], Mod).
327
328goal_expansion(foldargs(Meta, In, Out1, Acc0, AccF), (functor(In, Name, Ar), functor(Out1, Name, Ar), Mod:Goal)) :-
329 goal_expansion,
330 callable(Meta),
331 prolog_load_context(module, Mod),
332 aux_preds(Meta, MetaVars, Pred, PredVars, Proto),
333 aux_preds,
334 % the new goal
335 pred_name(foldargs, 2, Proto, GoalName),
336 append(MetaVars, [In, Out1, Acc0, AccF, 0, Ar], GoalArgs),
337 Goal =.. [GoalName|GoalArgs],
338 % the new predicate declaration
339 HeadPrefix =.. [GoalName|PredVars],
340 % the new predicate declaration
341 append_args(HeadPrefix, [In, Out1, VAcc0, VAccF, I, Ar], RecursionHead),
342 append_args(Pred, [AIn, AOut1, VAcc0, VAccI], Apply),
343 append_args(HeadPrefix, [In, Out1, VAccI, VAccF, I1, Ar], RecursiveCall),
344 compile_aux([
345 (RecursionHead :- I == Ar -> VAcc0 = VAccF ; I1 is I+1, arg(I1, In, AIn), arg(I1, Out1, AOut1), Apply, RecursiveCall )
346 ], Mod).
347
348goal_expansion(foldargs(Meta, In, Out1, Out2, Acc0, AccF), (functor(In, Name, Ar), functor(Out1, Name, Ar), functor(Out2, Name, Ar), Mod:Goal)) :-
349 goal_expansion,
350 callable(Meta),
351 prolog_load_context(module, Mod),
352 aux_preds(Meta, MetaVars, Pred, PredVars, Proto),
353 aux_preds,
354 % the new goal
355 pred_name(foldargs, 3, Proto, GoalName),
356 append(MetaVars, [In, Out1, Out2, Acc0, AccF, 0, Ar], GoalArgs),
357 Goal =.. [GoalName|GoalArgs],
358 % the new predicate declaration
359 HeadPrefix =.. [GoalName|PredVars],
360 % the new predicate declaration
361 append_args(HeadPrefix, [In, Out1, Out2, VAcc0, VAccF, I, Ar], RecursionHead),
362 append_args(Pred, [AIn, AOut1, AOut2, VAcc0, VAccI], Apply),
363 append_args(HeadPrefix, [In, Out1, Out2, VAccI, VAccF, I1, Ar], RecursiveCall),
364 compile_aux([
365 (RecursionHead :- I == Ar -> VAcc0 = VAccF ; I1 is I+1, arg(I1, In, AIn), arg(I1, Out1, AOut1), arg(I1, Out2, AOut2), Apply, RecursiveCall )
366 ], Mod).
367
368goal_expansion(foldargs(Meta, In, Out1, Out2, Out3, Acc0, AccF), (functor(In, Name, Ar), functor(Out1, Name, Ar), functor(Out2, Name, Ar), functor(Out3, Name, Ar), Mod:Goal)) :-
369 goal_expansion,
370 callable(Meta),
371 prolog_load_context(module, Mod),
372 aux_preds(Meta, MetaVars, Pred, PredVars, Proto),
373 aux_preds,
374 % the new goal
375 pred_name(foldargs, 4, Proto, GoalName),
376 append(MetaVars, [In, Out1, Out2, Out3, Acc0, AccF, 0, Ar], GoalArgs),
377 Goal =.. [GoalName|GoalArgs],
378 % the new predicate declaration
379 HeadPrefix =.. [GoalName|PredVars],
380 % the new predicate declaration
381 append_args(HeadPrefix, [In, Out1, Out2, Out3, VAcc0, VAccF, I, Ar], RecursionHead),
382 append_args(Pred, [AIn, AOut1, AOut2, AOut3, VAcc0, VAccI], Apply),
383 append_args(HeadPrefix, [In, Out1, Out2, Out3, VAccI, VAccF, I1, Ar], RecursiveCall),
384 compile_aux([
385 (RecursionHead :- I == Ar -> VAcc0 = VAccF ; I1 is I+1, arg(I1, In, AIn), arg(I1, Out1, AOut1), arg(I1, Out2, AOut2), arg(I1, Out3, AOut3), Apply, RecursiveCall )
386 ], Mod).
387
388%% @}
389
callable( ?_Goal_ )
use_module( +Files )
call(+ Closure,...,? Ai,...)
prolog_load_context(? Key, ? Value)
Definition: consult.yap:479
arg(+ N,+ T, A)
functor( T, F, N)
append(? List1,? List2,? List3)
sumlist(: Pred, + List, ? AccIn, ? AccOut)